From 96e2340a81df68707b252e51ebf3f12f40b1d945 Mon Sep 17 00:00:00 2001 From: lian Date: Fri, 7 Jul 2017 11:05:53 +0800 Subject: [PATCH] update user notifications 1. use cache to store user unseen notifications count 1. add UNREAD_NOTIFICATIONS_REQUEST_INTERVAL --- seahub/api2/endpoints/notifications.py | 29 ++++++++++++++++++++--- seahub/notifications/models.py | 10 ++++++++ seahub/settings.py | 3 +++ seahub/templates/libraries.html | 1 + seahub/views/__init__.py | 4 +++- static/scripts/app/views/notifications.js | 2 +- 6 files changed, 44 insertions(+), 5 deletions(-) diff --git a/seahub/api2/endpoints/notifications.py b/seahub/api2/endpoints/notifications.py index 58f11e2aa3..098fba882c 100644 --- a/seahub/api2/endpoints/notifications.py +++ b/seahub/api2/endpoints/notifications.py @@ -6,10 +6,14 @@ from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from rest_framework.views import APIView +from django.core.cache import cache + from seahub.api2.authentication import TokenAuthentication from seahub.api2.throttling import UserRateThrottle from seahub.notifications.models import UserNotification +from seahub.notifications.models import get_cache_key_of_unseen_notifications + logger = logging.getLogger(__name__) json_content_type = 'application/json; charset=utf-8' @@ -26,10 +30,22 @@ class NotificationsView(APIView): 1. login user. """ - username = request.user.username - unseen_count = UserNotification.objects.count_unseen_user_notifications(username) result = {} - result['unseen_count'] = unseen_count + + username = request.user.username + cache_key = get_cache_key_of_unseen_notifications(username) + + count_from_cache = cache.get(cache_key, None) + + # for case of count value is `0` + if count_from_cache is not None: + result['unseen_count'] = count_from_cache + else: + count_from_db = UserNotification.objects.count_unseen_user_notifications(username) + result['unseen_count'] = count_from_db + + # set cache + cache.set(cache_key, count_from_db) return Response(result) @@ -47,6 +63,9 @@ class NotificationsView(APIView): notice.seen = True notice.save() + cache_key = get_cache_key_of_unseen_notifications(username) + cache.delete(cache_key) + return Response({'success': True}) class NotificationView(APIView): @@ -74,4 +93,8 @@ class NotificationView(APIView): notice.seen = True notice.save() + username = request.user.username + cache_key = get_cache_key_of_unseen_notifications(username) + cache.delete(cache_key) + return Response({'success': True}) diff --git a/seahub/notifications/models.py b/seahub/notifications/models.py index ae031ded3f..f1f6fdf569 100644 --- a/seahub/notifications/models.py +++ b/seahub/notifications/models.py @@ -9,6 +9,7 @@ from django.db import models from django.forms import ModelForm, Textarea from django.utils.html import escape from django.utils.translation import ugettext as _ +from django.core.cache import cache import seaserv from seaserv import seafile_api, ccnet_api @@ -20,6 +21,7 @@ from seahub.utils.repo import get_repo_shared_users # Get an instance of a logger logger = logging.getLogger(__name__) + ########## system notification class Notification(models.Model): message = models.CharField(max_length=512) @@ -79,6 +81,10 @@ def file_comment_msg_to_json(repo_id, file_path, author, comment): 'author': author, 'comment': comment}) +def get_cache_key_of_unseen_notifications(username): + + return "%s_unseen_notifications_count" % username + class UserNotificationManager(models.Manager): def _add_user_notification(self, to_user, msg_type, detail): @@ -92,6 +98,10 @@ class UserNotificationManager(models.Manager): n = super(UserNotificationManager, self).create( to_user=to_user, msg_type=msg_type, detail=detail) n.save() + + cache_key = get_cache_key_of_unseen_notifications(to_user) + cache.delete(cache_key) + return n def get_all_notifications(self, seen=None, time_since=None): diff --git a/seahub/settings.py b/seahub/settings.py index 50df9998a8..10c49e8544 100644 --- a/seahub/settings.py +++ b/seahub/settings.py @@ -313,6 +313,9 @@ DISABLE_SYNC_WITH_ANY_FOLDER = False ENABLE_TERMS_AND_CONDITIONS = False +# interval for request unread notifications +UNREAD_NOTIFICATIONS_REQUEST_INTERVAL = 3 * 60 # seconds + # File preview FILE_PREVIEW_MAX_SIZE = 30 * 1024 * 1024 OFFICE_PREVIEW_MAX_SIZE = 2 * 1024 * 1024 diff --git a/seahub/templates/libraries.html b/seahub/templates/libraries.html index 9838ea2b42..9a037ff656 100644 --- a/seahub/templates/libraries.html +++ b/seahub/templates/libraries.html @@ -289,6 +289,7 @@ app["pageOptions"] = { folder_perm_enabled: {% if folder_perm_enabled %} true {% else %} false {% endif %}, is_pro: {% if is_pro %} true {% else %} false {% endif %}, file_audit_enabled: {% if file_audit_enabled %} true {% else %} false {% endif %}, + unread_notifications_request_interval: {{ unread_notifications_request_interval }}, cur_note: {% if request.cur_note %} {'id': '{{ request.cur_note.id }}'} {% else %} null {% endif %} }; app.ui = { diff --git a/seahub/views/__init__.py b/seahub/views/__init__.py index bffe45996f..30436ad651 100644 --- a/seahub/views/__init__.py +++ b/seahub/views/__init__.py @@ -50,7 +50,8 @@ from seahub.views.modules import MOD_PERSONAL_WIKI, enable_mod_for_user, \ disable_mod_for_user import seahub.settings as settings from seahub.settings import AVATAR_FILE_STORAGE, \ - ENABLE_SUB_LIBRARY, ENABLE_FOLDER_PERM + ENABLE_SUB_LIBRARY, ENABLE_FOLDER_PERM, \ + UNREAD_NOTIFICATIONS_REQUEST_INTERVAL LIBRARY_TEMPLATES = getattr(settings, 'LIBRARY_TEMPLATES', {}) @@ -759,6 +760,7 @@ def libraries(request): 'file_audit_enabled': FILE_AUDIT_ENABLED, 'can_add_pub_repo': can_add_pub_repo, 'joined_groups': joined_groups, + 'unread_notifications_request_interval': UNREAD_NOTIFICATIONS_REQUEST_INTERVAL, 'library_templates': LIBRARY_TEMPLATES.keys() if \ isinstance(LIBRARY_TEMPLATES, dict) else [] }, context_instance=RequestContext(request)) diff --git a/static/scripts/app/views/notifications.js b/static/scripts/app/views/notifications.js index c8aaa3d55b..f7ebb32f5c 100644 --- a/static/scripts/app/views/notifications.js +++ b/static/scripts/app/views/notifications.js @@ -72,7 +72,7 @@ define([ }; reqUnreadNum(); // request every 30s - var reqInterval = setInterval(reqUnreadNum, 30*1000); + var reqInterval = setInterval(reqUnreadNum, app.pageOptions.unread_notifications_request_interval*1000); $('#notice-icon').click(function() { _this.toggle();