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

+3
View File
@@ -4,3 +4,6 @@ from django.apps import AppConfig
class FeedsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "apps.feeds"
def ready(self):
from . import signals # noqa: F401
+14
View File
@@ -0,0 +1,14 @@
from django.db.models.signals import post_delete, post_save
from django.dispatch import receiver
from apps.core.cache_utils import invalidate_sidebar_unread_cache
from .models import Subscription
@receiver([post_save, post_delete], sender=Subscription)
def drop_sidebar_counts_when_subscriptions_change(sender, instance, **kwargs):
"""The cached per-feed unread counts only cover feeds the user was
subscribed to when they were computed, so a new subscription would
show no count until the cache expired."""
invalidate_sidebar_unread_cache(instance.user_id)
+2
View File
@@ -10,6 +10,7 @@ from django.db.models import Q
from django.utils import timezone
from apps.articles.tasks import apply_filter_rules_for_feed
from apps.core.cache_utils import invalidate_feed_subscribers_sidebar_cache
from apps.core.models import SiteSettings
from .models import Feed
@@ -187,6 +188,7 @@ def fetch_feed(self, feed_id: int):
changed_article_ids = result.new_article_ids + result.updated_article_ids
if changed_article_ids:
invalidate_feed_subscribers_sidebar_cache(feed_id)
logger.info(
"Feed %s: %d new article(s), %d updated",
feed.feed_url,
+19
View File
@@ -121,6 +121,25 @@ def test_fetch_feed_task_ingests_and_saves_feed_state():
assert Article.objects.filter(feed=feed, guid="guid-1").exists()
@pytest.mark.django_db
@responses.activate
def test_fetch_feed_drops_subscribers_cached_sidebar_counts(django_user_model):
"""New articles must show in the sidebar at the next refresh, not after
the counts cache's TTL."""
from apps.articles.queries import unread_counts_by_feed
from apps.feeds.models import Subscription
user = django_user_model.objects.create_user("reader", password="pw-reader-123")
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
Subscription.objects.create(user=user, feed=feed)
assert unread_counts_by_feed(user) == {} # now cached
responses.add(responses.GET, feed.feed_url, body=GOOD_FEED_XML, status=200)
fetch_feed(feed.id)
assert unread_counts_by_feed(user) == {feed.id: 1}
@pytest.mark.django_db
def test_fetch_feed_task_skips_inactive_feed():
feed = Feed.objects.create(feed_url="https://example.com/feed.xml", is_active=False)