2021-06-28 07:33:58 +00:00
|
|
|
import inspect
|
2022-12-07 07:09:01 +00:00
|
|
|
from importlib import import_module
|
2021-06-11 09:11:29 +00:00
|
|
|
|
2022-12-07 07:09:01 +00:00
|
|
|
from django.apps import AppConfig
|
2021-06-28 07:33:58 +00:00
|
|
|
from django.db.models.signals import post_migrate
|
2022-12-07 07:09:01 +00:00
|
|
|
from django.db.models.signals import post_save
|
2021-06-11 09:11:29 +00:00
|
|
|
from django.dispatch import receiver
|
2022-12-07 07:09:01 +00:00
|
|
|
from django.utils.functional import LazyObject
|
2021-06-11 09:11:29 +00:00
|
|
|
|
2022-12-07 07:09:01 +00:00
|
|
|
from common.decorator import on_transaction_commit
|
|
|
|
from common.utils import get_logger
|
|
|
|
from common.utils.connection import RedisPubSub
|
2021-08-24 06:20:54 +00:00
|
|
|
from notifications.backends import BACKEND
|
|
|
|
from users.models import User
|
2023-02-01 08:43:43 +00:00
|
|
|
from .models import MessageContent, SystemMsgSubscription, UserMsgSubscription
|
2021-06-28 07:33:58 +00:00
|
|
|
from .notifications import SystemMessage
|
2021-06-11 09:11:29 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class NewSiteMsgSubPub(LazyObject):
|
|
|
|
def _setup(self):
|
2022-12-07 07:09:01 +00:00
|
|
|
self._wrapped = RedisPubSub('notifications.SiteMessageCome')
|
2021-06-11 09:11:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
new_site_msg_chan = NewSiteMsgSubPub()
|
|
|
|
|
|
|
|
|
2023-02-01 08:43:43 +00:00
|
|
|
@receiver(post_save, sender=MessageContent)
|
2021-06-11 09:11:29 +00:00
|
|
|
@on_transaction_commit
|
|
|
|
def on_site_message_create(sender, instance, created, **kwargs):
|
|
|
|
if not created:
|
|
|
|
return
|
|
|
|
logger.debug('New site msg created, publish it')
|
|
|
|
user_ids = instance.users.all().values_list('id', flat=True)
|
|
|
|
user_ids = [str(i) for i in user_ids]
|
|
|
|
data = {
|
|
|
|
'id': str(instance.id),
|
|
|
|
'subject': instance.subject,
|
|
|
|
'message': instance.message,
|
|
|
|
'users': user_ids
|
|
|
|
}
|
|
|
|
new_site_msg_chan.publish(data)
|
2021-06-28 07:33:58 +00:00
|
|
|
|
|
|
|
|
2022-03-03 01:58:09 +00:00
|
|
|
@receiver(post_migrate, dispatch_uid='notifications.signal_handlers.create_system_messages')
|
2021-06-28 07:33:58 +00:00
|
|
|
def create_system_messages(app_config: AppConfig, **kwargs):
|
|
|
|
try:
|
|
|
|
notifications_module = import_module('.notifications', app_config.module.__package__)
|
|
|
|
|
|
|
|
for name, obj in notifications_module.__dict__.items():
|
|
|
|
if name.startswith('_'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not inspect.isclass(obj):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not issubclass(obj, SystemMessage):
|
|
|
|
continue
|
|
|
|
|
|
|
|
attrs = obj.__dict__
|
|
|
|
if 'message_type_label' not in attrs:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if 'category' not in attrs:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if 'category_label' not in attrs:
|
|
|
|
continue
|
|
|
|
|
|
|
|
message_type = obj.get_message_type()
|
|
|
|
sub, created = SystemMsgSubscription.objects.get_or_create(message_type=message_type)
|
|
|
|
if created:
|
|
|
|
obj.post_insert_to_db(sub)
|
2022-12-07 07:09:01 +00:00
|
|
|
logger.info(
|
|
|
|
f'Create SystemMsgSubscription: package={app_config.module.__package__} type={message_type}')
|
2021-06-28 07:33:58 +00:00
|
|
|
except ModuleNotFoundError:
|
|
|
|
pass
|
2021-08-24 06:20:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=User)
|
|
|
|
def on_user_post_save(sender, instance, created, **kwargs):
|
2022-02-18 06:28:28 +00:00
|
|
|
if not created:
|
|
|
|
return
|
|
|
|
receive_backends = []
|
|
|
|
# Todo: IDE 识别不了 get_account
|
|
|
|
for backend in BACKEND:
|
|
|
|
if backend.get_account(instance):
|
|
|
|
receive_backends.append(backend)
|
|
|
|
UserMsgSubscription.objects.create(user=instance, receive_backends=receive_backends)
|