mirror of https://github.com/jumpserver/jumpserver
fix: 站内信未读信息计数不准
parent
891a5157a7
commit
7dddf0c3c2
|
@ -2,6 +2,7 @@ from rest_framework.response import Response
|
|||
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
|
||||
from rest_framework.decorators import action
|
||||
|
||||
from common.http import is_true
|
||||
from common.permissions import IsValidUser
|
||||
from common.const.http import GET, PATCH, POST
|
||||
from common.drf.api import JmsGenericViewSet
|
||||
|
@ -26,13 +27,18 @@ class SiteMessageViewSet(ListModelMixin, RetrieveModelMixin, JmsGenericViewSet):
|
|||
|
||||
def get_queryset(self):
|
||||
user = self.request.user
|
||||
msgs = SiteMessage.get_user_all_msgs(user.id)
|
||||
has_read = self.request.query_params.get('has_read')
|
||||
|
||||
if has_read is None:
|
||||
msgs = SiteMessage.get_user_all_msgs(user.id)
|
||||
else:
|
||||
msgs = SiteMessage.filter_user_msgs(user.id, has_read=is_true(has_read))
|
||||
return msgs
|
||||
|
||||
@action(methods=[GET], detail=False, url_path='unread-total')
|
||||
def unread_total(self, request, **kwargs):
|
||||
user = request.user
|
||||
msgs = SiteMessage.get_user_unread_msgs(user.id)
|
||||
msgs = SiteMessage.filter_user_msgs(user.id, has_read=False)
|
||||
return Response(data={'total': msgs.count()})
|
||||
|
||||
@action(methods=[PATCH], detail=False, url_path='mark-as-read')
|
||||
|
|
|
@ -1,10 +1,17 @@
|
|||
import django_filters
|
||||
|
||||
from common.drf.filters import BaseFilterSet
|
||||
from .models import SiteMessage
|
||||
|
||||
|
||||
class SiteMsgFilter(django_filters.FilterSet):
|
||||
has_read = django_filters.BooleanFilter(field_name='m2m_sitemessageusers__has_read')
|
||||
class SiteMsgFilter(BaseFilterSet):
|
||||
# 不用 Django 的关联表过滤,有个小bug,会重复关联相同表
|
||||
# SELECT DISTINCT * FROM `notifications_sitemessage`
|
||||
# INNER JOIN `notifications_sitemessageusers` ON (`notifications_sitemessage`.`id` = `notifications_sitemessageusers`.`sitemessage_id`)
|
||||
# INNER JOIN `notifications_sitemessageusers` T4 ON (`notifications_sitemessage`.`id` = T4.`sitemessage_id`)
|
||||
# WHERE (`notifications_sitemessageusers`.`user_id` = '40c8f140dfa246d4861b80f63cf4f6e3' AND NOT T4.`has_read`)
|
||||
# ORDER BY `notifications_sitemessage`.`date_created` DESC LIMIT 15;
|
||||
has_read = django_filters.BooleanFilter(method='do_nothing')
|
||||
|
||||
class Meta:
|
||||
model = SiteMessage
|
||||
|
|
|
@ -51,10 +51,10 @@ class SiteMessage:
|
|||
return site_msgs_count
|
||||
|
||||
@classmethod
|
||||
def get_user_unread_msgs(cls, user_id):
|
||||
def filter_user_msgs(cls, user_id, has_read=False):
|
||||
site_msgs = SiteMessageModel.objects.filter(
|
||||
m2m_sitemessageusers__user_id=user_id,
|
||||
m2m_sitemessageusers__has_read=False
|
||||
m2m_sitemessageusers__has_read=has_read
|
||||
).distinct().annotate(
|
||||
has_read=F('m2m_sitemessageusers__has_read'),
|
||||
read_at=F('m2m_sitemessageusers__read_at')
|
||||
|
|
Loading…
Reference in New Issue