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

+102 -55
View File
@@ -188,8 +188,7 @@ def test_sidebar_shows_a_tags_total_unread_count(client, alice):
response = client.get(reverse("reader:stream"))
# The only "7" on the page — the individual feeds show 3 and 4.
content = response.content.decode()
assert '<span class="unread-count">7</span>' in content
assert sorted(_visible_counts(response.content.decode())) == ["3", "4", "7"]
@pytest.mark.django_db
@@ -226,35 +225,27 @@ def test_article_rows_carry_their_utc_instant_for_client_side_date_grouping(clie
@pytest.mark.django_db
def test_check_new_articles_reports_nothing_below_the_given_cursor(client, alice):
def test_pages_render_the_banner_placeholder_and_baseline_for_any_scope(client, alice):
"""The banner lives in the base layout with the page's baseline id
rendered in, so it works under any feed/tag selection (and on pages
that aren't the stream), not only the unscoped list."""
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
Subscription.objects.create(user=alice, feed=feed)
existing = Article.objects.create(feed=feed, guid="existing", title="Existing")
article = Article.objects.create(feed=feed, guid="g1", title="Hello")
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:check_new"), {"filter": "all", "since": existing.pk})
assert response.status_code == 200
assert response.content.decode().strip() == ""
for query in ({"filter": "all"}, {"feed": feed.pk}):
content = client.get(reverse("reader:stream"), query).content.decode()
assert 'class="new-articles-banner"' in content
assert f'data-since="{article.pk}"' in content
other_page = client.get(reverse("feeds:subscription_list")).content.decode()
assert 'class="new-articles-banner"' in other_page
@pytest.mark.django_db
def test_check_new_articles_reports_articles_created_after_the_cursor(client, alice):
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
Subscription.objects.create(user=alice, feed=feed)
existing = Article.objects.create(feed=feed, guid="existing", title="Existing")
Article.objects.create(feed=feed, guid="new-1", title="New one")
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:check_new"), {"filter": "all", "since": existing.pk})
content = response.content.decode()
assert "1 new article" in content
assert "click to show" in content
@pytest.mark.django_db
def test_check_new_articles_respects_the_active_tag_scope(client, alice):
def test_new_since_counts_new_articles_outside_the_page_s_own_feed(client, alice):
"""The banner for a page showing feed A still reports a new article in
feed B — the poll isn't scoped to the selected feed or tag."""
from apps.articles.models import SubscriptionTag, Tag
feed_a = Feed.objects.create(feed_url="https://a.example.com/feed.xml")
@@ -263,38 +254,93 @@ def test_check_new_articles_respects_the_active_tag_scope(client, alice):
Subscription.objects.create(user=alice, feed=feed_b)
tag = Tag.objects.create(user=alice, name="work")
SubscriptionTag.objects.create(user=alice, subscription=sub_a, tag=tag)
cursor = Article.objects.create(feed=feed_a, guid="cursor", title="Cursor").pk
# New in feed_b, which isn't tagged "work" — shouldn't count for this scope.
Article.objects.create(feed=feed_b, guid="untagged-new", title="Untagged new")
client.login(username="alice", password="pw-alice-123")
response = client.get(
reverse("reader:check_new"), {"filter": "all", "tag": tag.pk, "since": cursor}
)
body = client.get(reverse("reader:new_since"), {"since": cursor}).json()
assert response.content.decode().strip() == ""
# New in feed_a (tagged "work") — should count.
Article.objects.create(feed=feed_a, guid="tagged-new", title="Tagged new")
response = client.get(
reverse("reader:check_new"), {"filter": "all", "tag": tag.pk, "since": cursor}
)
assert "1 new article" in response.content.decode()
assert body["new_count"] == 1
@pytest.mark.django_db
def test_stream_exposes_the_latest_article_id_and_scope_for_the_banner(client, alice):
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
Subscription.objects.create(user=alice, feed=feed)
article = Article.objects.create(feed=feed, guid="g1", title="Hello")
def test_new_since_returns_current_sidebar_counts(client, alice):
from apps.articles.models import SubscriptionTag, Tag
feed = Feed.objects.create(feed_url="https://a.example.com/feed.xml")
sub = Subscription.objects.create(user=alice, feed=feed)
tag = Tag.objects.create(user=alice, name="work")
SubscriptionTag.objects.create(user=alice, subscription=sub, tag=tag)
for i in range(3):
Article.objects.create(feed=feed, guid=f"g{i}", title=f"T{i}")
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:stream"), {"filter": "all"})
content = response.content.decode()
counts = client.get(reverse("reader:new_since"), {"since": 0}).json()["counts"]
assert f"since={article.pk}" in content
assert "filter=all" in content
assert counts == {"feeds": {str(feed.pk): 3}, "tags": {str(tag.pk): 3}, "starred": 0}
@pytest.mark.django_db
@pytest.mark.parametrize(
"url_name, uses_article",
[("reader:toggle_read", True), ("reader:toggle_star", True), ("reader:article_inline", True)],
)
def test_actions_that_change_counts_tell_the_page_to_refresh_them(
client, alice, url_name, uses_article
):
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
Subscription.objects.create(user=alice, feed=feed)
article = Article.objects.create(feed=feed, guid="a-1", title="Hello")
client.login(username="alice", password="pw-alice-123")
url = reverse(url_name, args=[article.pk])
response = client.get(url) if url_name == "reader:article_inline" else client.post(url)
assert response["HX-Trigger"] == "countsChanged"
def _visible_counts(content):
"""Texts of the sidebar count badges that are actually shown."""
return re.findall(
r'<span class="unread-count" data-(?:feed|tag)-id="\d+">(\d+)</span>', content
)
@pytest.mark.django_db
def test_sidebar_count_is_fresh_right_after_new_articles_are_ingested(client, alice):
"""Ingestion drops the cached counts, so the next render (and the next
poll) shows new articles at once instead of after the cache TTL."""
from apps.core.cache_utils import invalidate_feed_subscribers_sidebar_cache
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
Subscription.objects.create(user=alice, feed=feed)
Article.objects.create(feed=feed, guid="a-1", title="One")
client.login(username="alice", password="pw-alice-123")
assert _visible_counts(client.get(reverse("reader:stream")).content.decode()) == ["1"]
Article.objects.create(feed=feed, guid="a-2", title="Two")
assert _visible_counts(client.get(reverse("reader:stream")).content.decode()) == ["1"]
invalidate_feed_subscribers_sidebar_cache(feed.pk)
assert _visible_counts(client.get(reverse("reader:stream")).content.decode()) == ["2"]
@pytest.mark.django_db
def test_new_subscription_shows_its_count_without_waiting_for_the_cache(client, alice):
feed_a = Feed.objects.create(feed_url="https://a.example.com/feed.xml")
feed_b = Feed.objects.create(feed_url="https://b.example.com/feed.xml")
Subscription.objects.create(user=alice, feed=feed_a)
Article.objects.create(feed=feed_a, guid="a", title="A")
Article.objects.create(feed=feed_b, guid="b", title="B")
client.login(username="alice", password="pw-alice-123")
client.get(reverse("reader:stream"))
Subscription.objects.create(user=alice, feed=feed_b)
assert sorted(_visible_counts(client.get(reverse("reader:stream")).content.decode())) == [
"1",
"1",
]
@pytest.mark.django_db
@@ -310,12 +356,12 @@ def test_sidebar_unread_count_updates_immediately_after_reading_an_article(clien
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:stream"))
assert '<span class="unread-count">1</span>' in response.content.decode()
assert _visible_counts(response.content.decode()) == ["1"]
client.get(reverse("reader:article_detail", args=[article.pk]))
response = client.get(reverse("reader:stream"))
assert "unread-count" not in response.content.decode()
assert _visible_counts(response.content.decode()) == []
@pytest.mark.django_db
@@ -327,12 +373,12 @@ def test_sidebar_unread_count_updates_immediately_after_toggle_read(client, alic
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:stream"))
assert "unread-count" not in response.content.decode()
assert _visible_counts(response.content.decode()) == []
client.post(reverse("reader:toggle_read", args=[article.pk]))
response = client.get(reverse("reader:stream"))
assert '<span class="unread-count">1</span>' in response.content.decode()
assert _visible_counts(response.content.decode()) == ["1"]
@pytest.mark.django_db
@@ -343,12 +389,12 @@ def test_sidebar_unread_count_updates_immediately_after_mark_all_read(client, al
client.login(username="alice", password="pw-alice-123")
response = client.get(reverse("reader:stream"))
assert '<span class="unread-count">1</span>' in response.content.decode()
assert _visible_counts(response.content.decode()) == ["1"]
client.post(reverse("reader:mark_all_read"))
response = client.get(reverse("reader:stream"))
assert "unread-count" not in response.content.decode()
assert _visible_counts(response.content.decode()) == []
def _stream_article_ids(client, query):
@@ -520,7 +566,8 @@ def test_new_since_without_a_cursor_returns_the_baseline_id(client, alice):
client.login(username="alice", password="pw-alice-123")
body = client.get(reverse("reader:new_since")).json()
assert body == {"latest_id": article.pk, "new_count": 0}
assert body["latest_id"] == article.pk
assert body["new_count"] == 0
@pytest.mark.django_db
@@ -546,14 +593,14 @@ def test_new_since_counts_only_new_unread_visible_articles_from_my_feeds(client,
@pytest.mark.django_db
def test_authenticated_pages_load_the_tab_badge_script_with_its_endpoint(client, alice):
def test_authenticated_pages_load_the_live_updates_script_with_its_endpoint(client, alice):
client.login(username="alice", password="pw-alice-123")
content = client.get(reverse("reader:stream")).content.decode()
assert "js/tab-badge" in content
assert "js/live-updates" in content
assert f'data-url="{reverse("reader:new_since")}"' in content
client.logout()
assert "tab-badge" not in client.get(reverse("accounts:login")).content.decode()
assert "live-updates" not in client.get(reverse("accounts:login")).content.decode()
@pytest.mark.django_db
-1
View File
@@ -6,7 +6,6 @@ app_name = "reader"
urlpatterns = [
path("", views.stream, name="stream"),
path("check-new/", views.check_new_articles, name="check_new"),
path("new-since/", views.new_since, name="new_since"),
path("mark-all-read/", views.mark_all_read, name="mark_all_read"),
path("articles/<int:article_id>/", views.article_detail, name="article_detail"),
+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}))