Notify the open stream when new articles arrive

The stream had no way to learn about articles the background poll
ingested while the page was open — only a manual reload or navigation
ever picked them up.

Adds a lightweight "N new articles — click to show" banner: the stream
records the highest article id in its current scope (filter/tag/feed)
when rendered, and an htmx poll every 30s checks reader:check_new for
anything newer in that same scope. It never swaps the list out from
under you mid-read — clicking the banner is what actually reloads it.
id, not published_at, is the cursor, since a feed can backdate an
entry's published date below everything currently on screen while it's
still genuinely new to this stream.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5 committed 2026-10-31 15:04:55 -06:00
1 parent 17e7f63e61
commit 03dd37e69d
6 files changed
+139 -4

No files matched your search

+42 -4
View File
@@ -1,7 +1,8 @@
from itertools import groupby
from urllib.parse import urlencode
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.db.models import Max, Q
from django.http import Http404
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
@@ -117,8 +118,7 @@ def _group_by_date(articles):
]
@login_required
def stream(request):
def _resolve_active_filter(request):
requested_filter = request.GET.get("filter")
if requested_filter in VALID_FILTERS:
active_filter = requested_filter
@@ -128,7 +128,14 @@ def stream(request):
# selected, rather than always resetting to "unread".
active_filter = request.session.get("stream_filter", "unread")
request.session["stream_filter"] = active_filter
return active_filter
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
new articles?" check always matches exactly what the stream itself
would show."""
queryset = _base_queryset(request.user)
if active_filter == "unread":
queryset = queryset.exclude(user_states__user=request.user, user_states__is_read=True)
@@ -146,8 +153,13 @@ def stream(request):
article_tags__tag__name=SAVED_TAG_NAME,
article_tags__tag__is_system=True,
)
return _apply_scope(request, queryset)
queryset, active_tag, active_feed = _apply_scope(request, queryset)
@login_required
def stream(request):
active_filter = _resolve_active_filter(request)
queryset, active_tag, active_feed = _filtered_queryset(request, active_filter)
try:
page = max(int(request.GET.get("page", 1)), 1)
@@ -158,6 +170,12 @@ def stream(request):
total_count = queryset.count()
articles = with_user_state(queryset[start:end], request.user)
scope_params = {"filter": active_filter}
if active_tag:
scope_params["tag"] = active_tag.pk
if active_feed:
scope_params["feed"] = active_feed.pk
context = {
"date_groups": _group_by_date(articles),
"active_filter": active_filter,
@@ -168,10 +186,30 @@ def stream(request):
"page": page,
"has_next_page": end < total_count,
"has_previous_page": page > 1,
# 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),
}
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
@require_POST
def mark_all_read(request):