moviewyrm/bookwyrm/views/updates.py

49 lines
1.5 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" endpoints for getting updates about activity """
2021-01-19 00:32:02 +00:00
from django.contrib.auth.decorators import login_required
from django.http import Http404, JsonResponse
from django.utils.translation import ngettext
2021-01-19 00:32:02 +00:00
2021-03-23 19:52:38 +00:00
from bookwyrm import activitystreams
2021-03-08 16:49:10 +00:00
2021-03-23 20:07:29 +00:00
2021-03-23 19:52:38 +00:00
@login_required
def get_notification_count(request):
2021-04-26 16:15:42 +00:00
"""any notifications waiting?"""
2021-03-23 20:07:29 +00:00
return JsonResponse(
{
"count": request.user.unread_notification_count,
"has_mentions": request.user.has_unread_mentions,
2021-03-23 20:07:29 +00:00
}
)
2021-03-23 19:52:38 +00:00
@login_required
def get_unread_status_string(request, stream="home"):
2021-04-26 16:15:42 +00:00
"""any unread statuses for this feed?"""
2021-03-23 19:52:38 +00:00
stream = activitystreams.streams.get(stream)
if not stream:
raise Http404
counts_by_type = stream.get_unread_count_by_status_type(request.user).items()
if counts_by_type == {}:
count = stream.get_unread_count(request.user)
else:
# only consider the types that are visible in the feed
allowed_status_types = request.user.feed_status_types
count = sum(c for (k, c) in counts_by_type if k in allowed_status_types)
# if "everything else" is allowed, add other types to the sum
2022-01-23 03:01:42 +00:00
count += sum(
c
for (k, c) in counts_by_type
if k not in ["review", "comment", "quotation"]
)
2022-01-28 02:23:31 +00:00
if not count:
return JsonResponse({})
translation_string = lambda c: ngettext(
2022-01-23 03:01:42 +00:00
"Load %(count)d unread status", "Load %(count)d unread statuses", c
) % {"count": c}
2022-01-28 02:23:31 +00:00
return JsonResponse({"count": translation_string(count)})