Make the banner and sidebar counts live on every page

One 5-minute poll (/new-since/) now drives the new-articles banner, the
tab dot and the sidebar counts, across all subscriptions rather than the
selected feed/tag. The banner lives in the base layout with a
server-rendered baseline id and reloads the current view on click; new
articles are never inserted automatically.

Sidebar counts are patched in place by the poll, and immediately after
reading or starring (HX-Trigger: countsChanged). Ingestion, filter rules
and subscription changes now drop the cached counts instead of waiting
out the TTL.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5.5 committed 2026-11-03 07:39:36 -07:00
1 parent fed5bcc49e
commit 2a3e9f632e
18 files changed
+399 -196

No files matched your search

+21 -32
View File
@@ -22,6 +22,7 @@ from apps.articles.models import (
)
from apps.articles.queries import with_user_state
from apps.core.cache_utils import invalidate_sidebar_unread_cache
from apps.core.context_processors import sidebar_counts
from apps.feeds.models import Feed
FILTER_CHOICES = [
@@ -137,7 +138,7 @@ def _resolve_active_filter(request):
def _filtered_queryset(request, active_filter):
"""The stream's fully-scoped queryset (filter tab + tag/feed scope),
unpaginated — shared by stream() and check_new_articles() so the "any
unpaginated — used by stream() so the "any
new articles?" check always matches exactly what the stream itself
would show."""
queryset = _base_queryset(request.user)
@@ -233,44 +234,25 @@ def stream(request):
"has_older_page": has_older_page and bool(articles),
"newer_qs": newer_qs,
"older_qs": older_qs,
# Lets the "new articles" banner ask, on this exact same scope,
# whether anything with a higher id has shown up since this page
# was rendered — id (not published_at) because it's monotonic
# regardless of how a feed backdates an entry's published date.
"latest_article_id": queryset.aggregate(Max("id"))["id__max"] or 0,
"new_check_qs": urlencode(scope_params),
"scope_qs": urlencode(scope_params),
}
return render(request, "reader/stream.html", context)
@login_required
def check_new_articles(request):
active_filter = _resolve_active_filter(request)
queryset, _active_tag, _active_feed = _filtered_queryset(request, active_filter)
try:
since_id = int(request.GET.get("since", 0))
except ValueError:
since_id = 0
new_count = queryset.filter(id__gt=since_id).count()
return render(request, "reader/_new_articles_banner.html", {"new_count": new_count})
@login_required
def new_since(request):
"""Feeds the browser-tab dot (static/js/tab-badge.js) on every page:
without ?since it returns the current highest article id, which the
page keeps as its baseline; with it, how many unread, non-hidden
articles from the user's subscriptions have appeared after that id.
Deliberately not scoped to the stream's current filter/tag/feed — the
tab should light up for anything new, whichever page is open."""
"""Feeds the live updates (static/js/live-updates.js) on every
authenticated page: how many unread, non-hidden articles from the
user's subscriptions have appeared after the page's baseline id
(?since), for the new-articles banner and the browser-tab dot, plus
the sidebar's current counts. Deliberately not scoped to the stream's
current filter/tag/feed — anything new, whichever page is open."""
latest_id = Article.objects.aggregate(Max("id"))["id__max"] or 0
counts = sidebar_counts(request.user)
try:
since_id = int(request.GET["since"])
except (KeyError, ValueError):
return JsonResponse({"latest_id": latest_id, "new_count": 0})
return JsonResponse({"latest_id": latest_id, "new_count": 0, "counts": counts})
new_count = (
_base_queryset(request.user)
@@ -278,7 +260,7 @@ def new_since(request):
.exclude(user_states__user=request.user, user_states__is_read=True)
.count()
)
return JsonResponse({"latest_id": latest_id, "new_count": new_count})
return JsonResponse({"latest_id": latest_id, "new_count": new_count, "counts": counts})
@login_required
@@ -318,6 +300,13 @@ def mark_all_read(request):
return redirect(f"{stream_url}?{query}" if query else stream_url)
def _counts_changed(response):
"""Tells the page's live-update script (static/js/live-updates.js) to
refresh the sidebar counts right now rather than at the next poll."""
response["HX-Trigger"] = "countsChanged"
return response
def _load_article_and_mark_read(user, article_id):
article = get_object_or_404(_base_queryset(user), pk=article_id)
@@ -353,7 +342,7 @@ def article_inline(request, article_id):
content fragment plus an out-of-band update of the row's own read/star
icons (see templates/reader/_article_inline.html)."""
article = _load_article_and_mark_read(request.user, article_id)
return render(request, "reader/_article_inline.html", {"article": article})
return _counts_changed(render(request, "reader/_article_inline.html", {"article": article}))
@login_required
@@ -369,7 +358,7 @@ def toggle_read(request, article_id):
article.is_read = state.is_read
article.is_starred, article.is_saved_for_later = _article_tag_flags(request.user, article)
return render(request, "reader/_article_actions.html", {"article": article})
return _counts_changed(render(request, "reader/_article_actions.html", {"article": article}))
@login_required
@@ -405,4 +394,4 @@ def _toggle_system_tag(request, article_id, tag_name):
article.is_read = state.is_read if state else False
article.is_starred, article.is_saved_for_later = _article_tag_flags(request.user, article)
return render(request, "reader/_article_actions.html", {"article": article})
return _counts_changed(render(request, "reader/_article_actions.html", {"article": article}))