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

@ -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;

View file

@ -24,9 +24,12 @@
{# announcements and system messages #}
{% if not activities.number > 1 %}
<a href="{{ request.path }}" class="transition-y is-hidden notification is-primary is-block" data-poll-wrapper>
{% blocktrans with tab_key=tab.key %}load <span data-poll="stream/{{ tab_key }}">0</span> unread status(es){% endblocktrans %}
{{ allowed_status_types|json_script:"unread-notifications-wrapper" }}
<a
href="{{ request.path }}"
class="transition-y is-hidden notification is-primary is-block"
data-poll-wrapper
>
<span data-poll="stream/{{ tab.key }}"></span>
</a>
{% if request.user.show_goal and not goal and tab.key == 'home' %}

View file

@ -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<stream>[a-z]+)/?$", views.get_unread_status_count),
re_path("^api/updates/stream/(?P<stream>[a-z]+)/?$", views.get_unread_status_string),
# authentication
re_path(r"^login/?$", views.Login.as_view(), name="login"),
re_path(r"^login/(?P<confirmed>confirmed)/?$", views.Login.as_view(), name="login"),

View file

@ -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 (

View file

@ -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(),

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)}
)