1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-10 19:32:25 +00:00

add update all notification API

This commit is contained in:
孙永强 2024-11-27 15:06:30 +08:00
parent f4558a0f46
commit ef77d90280
3 changed files with 50 additions and 15 deletions

View File

@ -22,7 +22,7 @@ class Notification extends React.Component {
componentDidMount() { componentDidMount() {
seafileAPI.listAllNotifications().then(res => { seafileAPI.listAllNotifications().then(res => {
let unseen_count = res.data.general_notification.unseen_count + res.data.discussion_notification.unseen_count; let unseen_count = res.data.general.unseen_count + res.data.discussion.unseen_count;
this.setState({ unseenCount: unseen_count }); this.setState({ unseenCount: unseen_count });
}); });
} }
@ -54,8 +54,8 @@ class Notification extends React.Component {
let page = 1; let page = 1;
let perPage = 5; let perPage = 5;
seafileAPI.listAllNotifications(page, perPage).then(res => { seafileAPI.listAllNotifications(page, perPage).then(res => {
let generalNoticeList = res.data.general_notification.notification_list; let generalNoticeList = res.data.general.notification_list;
let discussionNoticeList = res.data.discussion_notification.notification_list; let discussionNoticeList = res.data.discussion.notification_list;
this.setState({ this.setState({
generalNoticeList: generalNoticeList, generalNoticeList: generalNoticeList,
discussionNoticeList: discussionNoticeList discussionNoticeList: discussionNoticeList
@ -111,7 +111,7 @@ class Notification extends React.Component {
}; };
onMarkAllNotifications = () => { onMarkAllNotifications = () => {
seafileAPI.updateNotifications().then(() => { seafileAPI.updateAllNotifications().then(() => {
this.setState({ this.setState({
unseenCount: 0, unseenCount: 0,
}); });

View File

@ -1460,6 +1460,11 @@ class SeafileAPI {
return this.req.get(url, { params: params }); return this.req.get(url, { params: params });
} }
updateAllNotifications() {
const url = this.server + '/api/v2.1/all-notifications/';
return this.req.put(url);
}
listNotifications(page, perPage) { listNotifications(page, perPage) {
const url = this.server + '/api/v2.1/notifications/'; const url = this.server + '/api/v2.1/notifications/';
let params = { let params = {

View File

@ -320,8 +320,8 @@ class AllNotificationsView(APIView):
1. login user. 1. login user.
""" """
result = { result = {
'general_notification': {}, 'general': {},
'discussion_notification': {} 'discussion': {}
} }
username = request.user.username username = request.user.username
@ -378,25 +378,55 @@ class AllNotificationsView(APIView):
# for case of count value is `0` # for case of count value is `0`
if unseen_count_from_cache is not None: if unseen_count_from_cache is not None:
result['general_notification']['unseen_count'] = unseen_count_from_cache result['general']['unseen_count'] = unseen_count_from_cache
else: else:
unseen_count = UserNotification.objects.filter(to_user=username, seen=False).count() unseen_count = UserNotification.objects.filter(to_user=username, seen=False).count()
result['general_notification']['unseen_count'] = unseen_count result['general']['unseen_count'] = unseen_count
cache.set(cache_key, unseen_count) cache.set(cache_key, unseen_count)
if sdoc_unseen_count_from_cache is not None: if sdoc_unseen_count_from_cache is not None:
result['discussion_notification']['unseen_count'] = sdoc_unseen_count_from_cache result['discussion']['unseen_count'] = sdoc_unseen_count_from_cache
else: else:
sdoc_unseen_count = SeadocNotification.objects.filter(username=username, seen=False).count() sdoc_unseen_count = SeadocNotification.objects.filter(username=username, seen=False).count()
result['discussion_notification']['unseen_count'] = sdoc_unseen_count result['discussion']['unseen_count'] = sdoc_unseen_count
cache.set(sdoc_cache_key, sdoc_unseen_count) cache.set(sdoc_cache_key, sdoc_unseen_count)
total_count = UserNotification.objects.filter(to_user=username).count() total_count = UserNotification.objects.filter(to_user=username).count()
sdoc_total_count = SeadocNotification.objects.filter(username=username).count() sdoc_total_count = SeadocNotification.objects.filter(username=username).count()
result['general_notification']['notification_list'] = notification_list result['general']['notification_list'] = notification_list
result['discussion_notification']['notification_list'] = sdoc_notification_list result['discussion']['notification_list'] = sdoc_notification_list
result['general_notification']['count'] = total_count result['general']['count'] = total_count
result['discussion_notification']['count'] = sdoc_total_count result['discussion']['count'] = sdoc_total_count
return Response(result) return Response(result)
def put(self, request):
""" currently only used for mark all notifications seen
Permission checking:
1. login user.
"""
username = request.user.username
# unseen_notices = UserNotification.objects.get_user_notifications(username,
# seen=False)
# for notice in unseen_notices:
# notice.seen = True
# notice.save()
try:
UserNotification.objects.get_user_notifications(username, seen=False).update(seen=True)
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_notifications(username)
sdoc_cache_key = get_cache_key_of_unseen_sdoc_notifications(username)
cache.delete(cache_key)
cache.delete(sdoc_cache_key)
return Response({'success': True})