Files
Evection/apps/core/context_processors.py
T
ksmithandClaude Sonnet 5.5 2a3e9f632e 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>
2026-11-03 07:39:36 -07:00

95 lines
3.9 KiB
Python

from django.db.models import Max
from apps.articles.models import STARRED_TAG_NAME, Article, ArticleTag, SubscriptionTag, Tag
from apps.articles.queries import unread_counts_by_feed
from apps.feeds.models import Subscription
def starred_count(user):
return ArticleTag.objects.filter(
user=user, tag__user=user, tag__name=STARRED_TAG_NAME, tag__is_system=True
).count()
def sidebar_counts(user):
"""The sidebar's numbers on their own, for the live-update poll
(static/js/live-updates.js) to patch into the rendered sidebar:
unread per feed, unread per tag (summed over the feeds it covers,
as in sidebar() below), and the starred total."""
unread_counts = unread_counts_by_feed(user)
tag_totals = {}
for tag_id, feed_id in SubscriptionTag.objects.filter(
user=user, tag__is_system=False
).values_list("tag_id", "subscription__feed_id"):
tag_totals[str(tag_id)] = tag_totals.get(str(tag_id), 0) + unread_counts.get(feed_id, 0)
return {
"feeds": {str(feed_id): count for feed_id, count in unread_counts.items()},
"tags": tag_totals,
"starred": starred_count(user),
}
def sidebar(request):
"""Tag/feed tree and starred count for the persistent sidebar rendered
by base.html on every authenticated page. Kept out of individual views
so every page gets the same sidebar for free.
Feeds are grouped by tag (a feed can appear under more than one, since
SubscriptionTag is many-to-many — unlike the single-folder scheme this
replaced); feeds with no tags at all fall into "untagged".
"""
user = getattr(request, "user", None)
if user is None or not user.is_authenticated:
return {}
unread_counts = unread_counts_by_feed(user)
# System tags (Starred/Saved for later) categorize articles, not feeds
# — they get their own pinned nav items above, not a feed-tag group.
tags = list(Tag.objects.filter(user=user, is_system=False))
tag_rows = (
SubscriptionTag.objects.filter(user=user)
.select_related("subscription__feed", "tag")
.order_by("tag__name", "subscription__order", "subscription__id")
)
feeds_by_tag = {tag.pk: [] for tag in tags}
tagged_subscription_ids = set()
for row in tag_rows:
subscription = row.subscription
subscription.unread_count = unread_counts.get(subscription.feed_id, 0)
feeds_by_tag.setdefault(row.tag_id, []).append(subscription)
tagged_subscription_ids.add(subscription.pk)
untagged = []
for subscription in (
Subscription.objects.filter(user=user).select_related("feed").order_by("order", "id")
):
if subscription.pk not in tagged_subscription_ids:
subscription.unread_count = unread_counts.get(subscription.feed_id, 0)
untagged.append(subscription)
return {
"sidebar_tag_groups": [
{
"tag": tag,
"subscriptions": feeds_by_tag.get(tag.pk, []),
# A feed can carry more than one tag, so this is a sum
# over whichever of the user's feeds this tag happens to
# cover right now — not a separately-tracked counter — and
# reuses the per-feed counts already computed above rather
# than a second query.
"unread_count": sum(
subscription.unread_count for subscription in feeds_by_tag.get(tag.pk, [])
),
}
for tag in tags
],
"sidebar_untagged": untagged,
"sidebar_starred_count": starred_count(user),
# The page's "newest article seen" baseline for the new-articles
# banner / tab dot; rendered into the page, not fetched, so there's
# no gap between rendering and the first poll (and a page served
# from the PWA cache still compares against what it actually shows).
"latest_article_id": Article.objects.aggregate(Max("id"))["id__max"] or 0,
}