Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
孙永强 committed Nov 26, 2024
1 parent a8a7a20 commit 9f30fe5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
17 changes: 10 additions & 7 deletions seahub/api2/endpoints/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from seahub.notifications.models import get_cache_key_of_unseen_notifications
from seahub.notifications.utils import update_notice_detail, update_sdoc_notice_detail
from seahub.api2.utils import api_error
from seahub.seadoc.models import SeadocCommentReply, SeadocNotification
from seahub.seadoc.models import SeadocNotification
from seahub.utils.timeutils import datetime_to_isoformat_timestr

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -228,12 +228,15 @@ def get(self, request):
return Response(result)

def put(self, request):
"""mark all sdoc notifications seen"""
username = request.user.username
unseen_notices = SeadocNotification.objects.filter(username=username, seen=False)
for notice in unseen_notices:
notice.seen = True
notice.save()

try:
SeadocNotification.objects.filter(username=username, seen=False).update(seen=True)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg)

cache_key = get_cache_key_of_unseen_sdoc_notifications(username)
cache.delete(cache_key)

Expand Down Expand Up @@ -327,7 +330,7 @@ def get(self, request):
per_page = int(request.GET.get('per_page', ''))
page = int(request.GET.get('page', ''))
except ValueError:
per_page = 25
per_page = 5
page = 1

if page < 1:
Expand Down
27 changes: 17 additions & 10 deletions seahub/notifications/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,30 +399,37 @@ def update_notice_detail(request, notices):


def update_sdoc_notice_detail(request, notices):
doc_uuid_set = set()
for notice in notices:
doc_uuid_set.add(notice.doc_uuid)
doc_uuid_map = {}
uuids = FileUUIDMap.objects.get_fileuuidmap_in_uuids(doc_uuid_set)
for uuid in uuids:
if uuid not in doc_uuid_map:
origin_file_path = posixpath.join(uuid.parent_path, uuid.filename)
doc_uuid_map[str(uuid.uuid)] = (origin_file_path, uuid.filename, uuid.repo_id)

for notice in notices:
uuid = doc_uuid_map[notice.doc_uuid]
if notice.is_comment():
try:
d = json.loads(notice.detail)
uuid = FileUUIDMap.objects.get_fileuuidmap_by_uuid(notice.doc_uuid)
origin_file_path = posixpath.join(uuid.parent_path, uuid.filename)
url, _, _ = api_avatar_url(d['author'])
d['avatar_url'] = url
d['sdoc_path'] = origin_file_path
d['sdoc_name'] = uuid.filename
d['repo_id'] = uuid.repo_id
d['sdoc_path'] = uuid[0]
d['sdoc_name'] = uuid[1]
d['repo_id'] = uuid[2]
notice.detail = d
except Exception as e:
logger.error(e)
elif notice.is_reply():
try:
d = json.loads(notice.detail)
uuid = FileUUIDMap.objects.get_fileuuidmap_by_uuid(notice.doc_uuid)
origin_file_path = posixpath.join(uuid.parent_path, uuid.filename)
url, _, _ = api_avatar_url(d['author'])
d['avatar_url'] = url
d['sdoc_path'] = origin_file_path
d['sdoc_name'] = uuid.filename
d['repo_id'] = uuid.repo_id
d['sdoc_path'] = uuid[0]
d['sdoc_name'] = uuid[1]
d['repo_id'] = uuid[2]
notice.detail = d
except Exception as e:
logger.error(e)
Expand Down
18 changes: 7 additions & 11 deletions seahub/seadoc/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django.views.decorators.http import condition
from django.core.cache import cache

from seaserv import seafile_api, check_quota, get_org_id_by_repo_id, ccnet_api

Expand All @@ -53,7 +54,7 @@
from seahub.tags.models import FileUUIDMap
from seahub.utils.error_msg import file_type_error_msg
from seahub.utils.repo import parse_repo_perm, get_related_users_by_repo
from seahub.seadoc.models import SeadocHistoryName, SeadocRevision, SeadocCommentReply, SeadocNotification
from seahub.seadoc.models import SeadocHistoryName, SeadocRevision, SeadocCommentReply, SeadocNotification, get_cache_key_of_unseen_sdoc_notifications
from seahub.avatar.templatetags.avatar_tags import api_avatar_url
from seahub.base.templatetags.seahub_tags import email2nickname, \
email2contact_email
Expand Down Expand Up @@ -1000,11 +1001,6 @@ def post(self, request, file_uuid):
error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg)

try:
avatar_size = int(request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE))
except ValueError:
avatar_size = AVATAR_DEFAULT_SIZE

comment = request.data.get('comment', '')
detail = request.data.get('detail', '')
author = request.data.get('author', '')
Expand Down Expand Up @@ -1047,6 +1043,9 @@ def post(self, request, file_uuid):
))
try:
SeadocNotification.objects.bulk_create(new_notifications)
# delete sdoc notification count cache
sdoc_cache_key = get_cache_key_of_unseen_sdoc_notifications(username)
cache.delete(sdoc_cache_key)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
Expand All @@ -1070,11 +1069,6 @@ def get(self, request, file_uuid, comment_id):
error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg)

try:
avatar_size = int(request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE))
except ValueError:
avatar_size = AVATAR_DEFAULT_SIZE

# resource check
try:
file_comment = FileComment.objects.get(pk=comment_id)
Expand Down Expand Up @@ -1276,6 +1270,8 @@ def post(self, request, file_uuid, comment_id):
))
try:
SeadocNotification.objects.bulk_create(new_notifications)
sdoc_cache_key = get_cache_key_of_unseen_sdoc_notifications(username)
cache.delete(sdoc_cache_key)
except Exception as e:
logger.error(e)
error_msg = 'Internal Server Error'
Expand Down
6 changes: 6 additions & 0 deletions seahub/tags/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def get_fileuuidmap_by_uuid(self, uuid):
return super(FileUUIDMapManager, self).get(uuid=uuid)
except self.model.DoesNotExist:
return None

def get_fileuuidmap_in_uuids(self, uuids):
try:
return super(FileUUIDMapManager, self).filter(uuid__in=uuids)
except self.model.DoesNotExist:
return None

def get_or_create_fileuuidmap(self, repo_id, parent_path, filename, is_dir):
""" create filemap by repo_id、 parent_path、filename、id_dir
Expand Down

0 comments on commit 9f30fe5

Please sign in to comment.