Files
Evection/apps/core/context_processors.py
T
ksmithandClaude Sonnet 5 e760eeda1d Show each tag's total unread count and remember collapsed sections
Closes gitea#24: the sidebar's per-feed unread counts were already
computed by sidebar() (apps.core.context_processors); each tag group's
total is now the sum of its own feeds' counts (a feed can carry more
than one tag, so this reuses the same per-feed data rather than a
second query) and renders next to the tag name like a feed's own
count.

Closes gitea#25: each sidebar <details> section (one per tag, plus
"Untagged") now carries a stable data-sidebar-key, and
static/js/sidebar-state.js remembers which ones are collapsed in
localStorage — this isn't a SPA, so every click re-renders the sidebar
server-side, and previously every section reset back to expanded on
the next page load. Per-browser, same pattern as theme.js/theme-init.js,
not synced across devices/accounts.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 12:34:57 -06:00

66 lines
2.7 KiB
Python

from apps.articles.models import STARRED_TAG_NAME, ArticleTag, SubscriptionTag, Tag
from apps.articles.queries import unread_counts_by_feed
from apps.feeds.models import Subscription
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": ArticleTag.objects.filter(
user=user, tag__user=user, tag__name=STARRED_TAG_NAME, tag__is_system=True
).count(),
}