2021-05-31 09:20:38 +00:00
|
|
|
from django.db.models import F
|
2021-06-11 09:11:29 +00:00
|
|
|
from django.db import transaction
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
from common.utils.timezone import now
|
|
|
|
from users.models import User
|
|
|
|
from .models import SiteMessage as SiteMessageModel, SiteMessageUsers
|
|
|
|
|
|
|
|
|
2021-06-11 09:11:29 +00:00
|
|
|
class SiteMessageUtil:
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
@classmethod
|
2021-06-08 03:11:27 +00:00
|
|
|
def send_msg(cls, subject, message, user_ids=(), group_ids=(),
|
|
|
|
sender=None, is_broadcast=False):
|
2021-05-31 09:20:38 +00:00
|
|
|
if not any((user_ids, group_ids, is_broadcast)):
|
|
|
|
raise ValueError('No recipient is specified')
|
|
|
|
|
2021-06-11 09:11:29 +00:00
|
|
|
with transaction.atomic():
|
|
|
|
site_msg = SiteMessageModel.objects.create(
|
|
|
|
subject=subject, message=message,
|
|
|
|
is_broadcast=is_broadcast, sender=sender,
|
|
|
|
)
|
2021-05-31 09:20:38 +00:00
|
|
|
|
2021-06-11 09:11:29 +00:00
|
|
|
if is_broadcast:
|
|
|
|
user_ids = User.objects.all().values_list('id', flat=True)
|
|
|
|
else:
|
|
|
|
if group_ids:
|
|
|
|
site_msg.groups.add(*group_ids)
|
2021-05-31 09:20:38 +00:00
|
|
|
|
2021-06-11 09:11:29 +00:00
|
|
|
user_ids_from_group = User.groups.through.objects.filter(
|
|
|
|
usergroup_id__in=group_ids
|
|
|
|
).values_list('user_id', flat=True)
|
|
|
|
user_ids = [*user_ids, *user_ids_from_group]
|
2021-05-31 09:20:38 +00:00
|
|
|
|
2021-06-11 09:11:29 +00:00
|
|
|
site_msg.users.add(*user_ids)
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_user_all_msgs(cls, user_id):
|
|
|
|
site_msgs = SiteMessageModel.objects.filter(
|
|
|
|
m2m_sitemessageusers__user_id=user_id
|
|
|
|
).distinct().annotate(
|
|
|
|
has_read=F('m2m_sitemessageusers__has_read'),
|
|
|
|
read_at=F('m2m_sitemessageusers__read_at')
|
|
|
|
).order_by('-date_created')
|
|
|
|
|
|
|
|
return site_msgs
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_user_all_msgs_count(cls, user_id):
|
|
|
|
site_msgs_count = SiteMessageModel.objects.filter(
|
|
|
|
m2m_sitemessageusers__user_id=user_id
|
|
|
|
).distinct().count()
|
|
|
|
return site_msgs_count
|
|
|
|
|
|
|
|
@classmethod
|
2021-06-09 12:51:31 +00:00
|
|
|
def filter_user_msgs(cls, user_id, has_read=False):
|
2021-05-31 09:20:38 +00:00
|
|
|
site_msgs = SiteMessageModel.objects.filter(
|
|
|
|
m2m_sitemessageusers__user_id=user_id,
|
2021-06-09 12:51:31 +00:00
|
|
|
m2m_sitemessageusers__has_read=has_read
|
2021-05-31 09:20:38 +00:00
|
|
|
).distinct().annotate(
|
|
|
|
has_read=F('m2m_sitemessageusers__has_read'),
|
|
|
|
read_at=F('m2m_sitemessageusers__read_at')
|
|
|
|
).order_by('-date_created')
|
|
|
|
|
|
|
|
return site_msgs
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_user_unread_msgs_count(cls, user_id):
|
|
|
|
site_msgs_count = SiteMessageModel.objects.filter(
|
|
|
|
m2m_sitemessageusers__user_id=user_id,
|
|
|
|
m2m_sitemessageusers__has_read=False
|
|
|
|
).distinct().count()
|
|
|
|
return site_msgs_count
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def mark_msgs_as_read(cls, user_id, msg_ids):
|
2021-06-11 09:11:29 +00:00
|
|
|
site_msg_users = SiteMessageUsers.objects.filter(
|
2021-05-31 09:20:38 +00:00
|
|
|
user_id=user_id, sitemessage_id__in=msg_ids,
|
|
|
|
has_read=False
|
|
|
|
)
|
|
|
|
|
2021-06-11 09:11:29 +00:00
|
|
|
for site_msg_user in site_msg_users:
|
|
|
|
site_msg_user.has_read = True
|
|
|
|
site_msg_user.read_at = now()
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
SiteMessageUsers.objects.bulk_update(
|
2021-06-11 09:11:29 +00:00
|
|
|
site_msg_users, fields=('has_read', 'read_at'))
|