Calculate and translate unread status counts in view

This commit is contained in:
Mouse Reeve
2022-01-22 17:03:48 -08:00
parent a74f907a61
commit 3fc690e763
6 changed files with 30 additions and 43 deletions

View File

@ -1,6 +1,7 @@
""" endpoints for getting updates about activity """
from django.contrib.auth.decorators import login_required
from django.http import Http404, JsonResponse
from django.utils.translation import ngettext
from bookwyrm import activitystreams
@ -17,14 +18,29 @@ def get_notification_count(request):
@login_required
def get_unread_status_count(request, stream="home"):
def get_unread_status_string(request, stream="home"):
"""any unread statuses for this feed?"""
stream = activitystreams.streams.get(stream)
if not stream:
raise Http404
counts_by_type = stream.get_unread_count_by_status_type(request.user).items()
print(counts_by_type)
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
count += sum(c for (k, c) in counts_by_type if k not in ["review", "comment", "quotation"])
translation_string = lambda c: ngettext(
"Load %(count)d unread status",
"Load %(count)d unread statuses",
c
) % {"count": c}
return JsonResponse(
{
"count": stream.get_unread_count(request.user),
"count_by_type": stream.get_unread_count_by_status_type(request.user),
}
{"total": translation_string(count)}
)