1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-01 07:01:12 +00:00

update user notifications

1. use cache to store user unseen notifications count
1. add UNREAD_NOTIFICATIONS_REQUEST_INTERVAL
This commit is contained in:
lian
2017-07-07 11:05:53 +08:00
parent 94b7d8f9d5
commit 96e2340a81
6 changed files with 44 additions and 5 deletions

View File

@@ -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})

View File

@@ -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):

View File

@@ -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

View File

@@ -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 = {

View File

@@ -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))

View File

@@ -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();