From 3fc690e763667e079b459cc60d0cba57164d2a18 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 22 Jan 2022 17:03:48 -0800 Subject: [PATCH] Calculate and translate unread status counts in view --- bookwyrm/static/js/bookwyrm.js | 33 +------------------------------ bookwyrm/templates/feed/feed.html | 9 ++++++--- bookwyrm/urls.py | 2 +- bookwyrm/views/__init__.py | 2 +- bookwyrm/views/feed.py | 1 - bookwyrm/views/updates.py | 26 +++++++++++++++++++----- 6 files changed, 30 insertions(+), 43 deletions(-) diff --git a/bookwyrm/static/js/bookwyrm.js b/bookwyrm/static/js/bookwyrm.js index 94163787..eccd4c55 100644 --- a/bookwyrm/static/js/bookwyrm.js +++ b/bookwyrm/static/js/bookwyrm.js @@ -122,39 +122,8 @@ let BookWyrm = new (class { */ updateCountElement(counter, data) { let count = data.count; - const count_by_type = data.count_by_type; const currentCount = counter.innerText; const hasMentions = data.has_mentions; - const allowedStatusTypesEl = document.getElementById("unread-notifications-wrapper"); - - // If we're on the right counter element - if (counter.closest("[data-poll-wrapper]").contains(allowedStatusTypesEl)) { - const allowedStatusTypes = JSON.parse(allowedStatusTypesEl.textContent); - - // For keys in common between allowedStatusTypes and count_by_type - // This concerns 'review', 'quotation', 'comment' - count = allowedStatusTypes.reduce(function (prev, currentKey) { - const currentValue = count_by_type[currentKey] | 0; - - return prev + currentValue; - }, 0); - - // Add all the "other" in count_by_type if 'everything' is allowed - if (allowedStatusTypes.includes("everything")) { - // Clone count_by_type with 0 for reviews/quotations/comments - const count_by_everything_else = Object.assign({}, count_by_type, { - review: 0, - quotation: 0, - comment: 0, - }); - - count = Object.keys(count_by_everything_else).reduce(function (prev, currentKey) { - const currentValue = count_by_everything_else[currentKey] | 0; - - return prev + currentValue; - }, count); - } - } if (count != currentCount) { this.addRemoveClass(counter.closest("[data-poll-wrapper]"), "is-hidden", count < 1); @@ -517,7 +486,7 @@ let BookWyrm = new (class { duplicateInput(event) { const trigger = event.currentTarget; - const input_id = trigger.dataset["duplicate"]; + const input_id = trigger.dataset.duplicate; const orig = document.getElementById(input_id); const parent = orig.parentNode; const new_count = parent.querySelectorAll("input").length + 1; diff --git a/bookwyrm/templates/feed/feed.html b/bookwyrm/templates/feed/feed.html index dbbc650f..9e625313 100644 --- a/bookwyrm/templates/feed/feed.html +++ b/bookwyrm/templates/feed/feed.html @@ -24,9 +24,12 @@ {# announcements and system messages #} {% if not activities.number > 1 %} - {% if request.user.show_goal and not goal and tab.key == 'home' %} diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index 7cdfd92a..1d894eee 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -47,7 +47,7 @@ urlpatterns = [ re_path(r"^ostatus_subscribe/?$", views.ostatus_follow_request), # polling updates re_path("^api/updates/notifications/?$", views.get_notification_count), - re_path("^api/updates/stream/(?P[a-z]+)/?$", views.get_unread_status_count), + re_path("^api/updates/stream/(?P[a-z]+)/?$", views.get_unread_status_string), # authentication re_path(r"^login/?$", views.Login.as_view(), name="login"), re_path(r"^login/(?Pconfirmed)/?$", views.Login.as_view(), name="login"), diff --git a/bookwyrm/views/__init__.py b/bookwyrm/views/__init__.py index 3f57c274..ec7af64d 100644 --- a/bookwyrm/views/__init__.py +++ b/bookwyrm/views/__init__.py @@ -101,7 +101,7 @@ from .rss_feed import RssFeed from .search import Search from .status import CreateStatus, EditStatus, DeleteStatus, update_progress from .status import edit_readthrough -from .updates import get_notification_count, get_unread_status_count +from .updates import get_notification_count, get_unread_status_string from .user import User, Followers, Following, hide_suggestions, user_redirect from .wellknown import * from .annual_summary import ( diff --git a/bookwyrm/views/feed.py b/bookwyrm/views/feed.py index cb16371b..a8d8edd5 100644 --- a/bookwyrm/views/feed.py +++ b/bookwyrm/views/feed.py @@ -62,7 +62,6 @@ class Feed(View): "streams": STREAMS, "goal_form": forms.GoalForm(), "feed_status_types_options": FeedFilterChoices, - "allowed_status_types": request.user.feed_status_types, "filters_applied": filters_applied, "path": f"/{tab['key']}", "annual_summary_year": get_annual_summary_year(), diff --git a/bookwyrm/views/updates.py b/bookwyrm/views/updates.py index 765865ef..7c2574f3 100644 --- a/bookwyrm/views/updates.py +++ b/bookwyrm/views/updates.py @@ -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)} )