From e760eeda1d9dacb75590eff113b334089832a1f1 Mon Sep 17 00:00:00 2001 From: Keith Smith Date: Sat, 31 Oct 2026 12:34:57 -0600 Subject: [PATCH] Show each tag's total unread count and remember collapsed sections MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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
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 --- apps/core/context_processors.py | 14 ++++- apps/core/test_context_processors.py | 82 ++++++++++++++++++++++++++++ apps/reader/tests.py | 24 ++++++++ static/css/base.css | 10 ++++ static/js/sidebar-state.js | 46 ++++++++++++++++ templates/base.html | 5 +- templates/core/_sidebar.html | 11 ++-- 7 files changed, 186 insertions(+), 6 deletions(-) create mode 100644 apps/core/test_context_processors.py create mode 100644 static/js/sidebar-state.js diff --git a/apps/core/context_processors.py b/apps/core/context_processors.py index fe02f42..91326d6 100644 --- a/apps/core/context_processors.py +++ b/apps/core/context_processors.py @@ -44,7 +44,19 @@ def sidebar(request): return { "sidebar_tag_groups": [ - {"tag": tag, "subscriptions": feeds_by_tag.get(tag.pk, [])} for tag in tags + { + "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( diff --git a/apps/core/test_context_processors.py b/apps/core/test_context_processors.py new file mode 100644 index 0000000..42f5bda --- /dev/null +++ b/apps/core/test_context_processors.py @@ -0,0 +1,82 @@ +import pytest +from django.contrib.auth import get_user_model +from django.test import RequestFactory +from django.urls import reverse + +from apps.articles.models import Article, SubscriptionTag, Tag +from apps.feeds.models import Feed, Subscription + +from .context_processors import sidebar + +User = get_user_model() + + +@pytest.fixture +def alice(db): + return User.objects.create_user("alice", password="pw-alice-123") + + +def _sidebar_context(user): + request = RequestFactory().get("/") + request.user = user + return sidebar(request) + + +@pytest.mark.django_db +def test_tag_unread_count_sums_its_feeds(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") + sub_a = Subscription.objects.create(user=alice, feed=feed_a) + sub_b = 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) + SubscriptionTag.objects.create(user=alice, subscription=sub_b, tag=tag) + + for i in range(3): + Article.objects.create(feed=feed_a, guid=f"a-{i}", title=f"A{i}") + for i in range(4): + Article.objects.create(feed=feed_b, guid=f"b-{i}", title=f"B{i}") + + context = _sidebar_context(alice) + [group] = context["sidebar_tag_groups"] + assert group["tag"] == tag + assert group["unread_count"] == 7 + + +@pytest.mark.django_db +def test_tag_unread_count_is_zero_with_no_unread_articles(alice): + feed = Feed.objects.create(feed_url="https://example.com/feed.xml") + sub = Subscription.objects.create(user=alice, feed=feed) + tag = Tag.objects.create(user=alice, name="empty") + SubscriptionTag.objects.create(user=alice, subscription=sub, tag=tag) + + context = _sidebar_context(alice) + [group] = context["sidebar_tag_groups"] + assert group["unread_count"] == 0 + + +@pytest.mark.django_db +def test_sidebar_sections_carry_a_stable_collapse_state_key(client, alice): + feed = Feed.objects.create(feed_url="https://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) + + other_feed = Feed.objects.create(feed_url="https://other.example.com/feed.xml") + Subscription.objects.create(user=alice, feed=other_feed) # untagged + + client.login(username="alice", password="pw-alice-123") + response = client.get(reverse("reader:stream")) + content = response.content.decode() + + # static/js/sidebar-state.js reads these to restore/persist each + # section's collapsed state across page loads. + assert f'data-sidebar-key="tag-{tag.pk}"' in content + assert 'data-sidebar-key="untagged"' in content + + +@pytest.mark.django_db +def test_base_template_loads_the_sidebar_state_script(client, alice): + client.login(username="alice", password="pw-alice-123") + response = client.get(reverse("reader:stream")) + assert "sidebar-state.js" in response.content.decode() diff --git a/apps/reader/tests.py b/apps/reader/tests.py index 2f4e489..8f80527 100644 --- a/apps/reader/tests.py +++ b/apps/reader/tests.py @@ -164,3 +164,27 @@ def test_saved_filter_only_shows_saved_articles(client, alice, alices_article): response = client.get(reverse("reader:stream"), {"filter": "starred"}) assert alices_article.title not in response.content.decode() + + +@pytest.mark.django_db +def test_sidebar_shows_a_tags_total_unread_count(client, alice): + from apps.articles.models import SubscriptionTag, Tag + + 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") + sub_a = Subscription.objects.create(user=alice, feed=feed_a) + sub_b = 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) + SubscriptionTag.objects.create(user=alice, subscription=sub_b, tag=tag) + for i in range(3): + Article.objects.create(feed=feed_a, guid=f"a-{i}", title=f"A{i}") + for i in range(4): + Article.objects.create(feed=feed_b, guid=f"b-{i}", title=f"B{i}") + + client.login(username="alice", password="pw-alice-123") + response = client.get(reverse("reader:stream")) + + # The only "7" on the page — the individual feeds show 3 and 4. + content = response.content.decode() + assert '7' in content diff --git a/static/css/base.css b/static/css/base.css index 1574f1b..3c231f1 100644 --- a/static/css/base.css +++ b/static/css/base.css @@ -299,6 +299,16 @@ form p { letter-spacing: 0.04em; } +/* An inner wrapper, not `summary` itself — `summary`'s native disclosure + triangle only renders with its default `display: list-item`, so the + flex layout for the tag name + unread count lives one level in. */ +.sidebar-tag-row { + display: flex; + align-items: center; + justify-content: space-between; + gap: 0.4rem; +} + .sidebar-tag-link { display: inline-block; padding: 0.15rem 0.4rem; diff --git a/static/js/sidebar-state.js b/static/js/sidebar-state.js new file mode 100644 index 0000000..e79a274 --- /dev/null +++ b/static/js/sidebar-state.js @@ -0,0 +1,46 @@ +(function () { + // Remembers which sidebar
sections (one per tag, plus + // "Untagged" — see templates/core/_sidebar.html's data-sidebar-key) + // the user has collapsed, so a full page navigation (this isn't an + // SPA — every click re-renders the sidebar server-side) doesn't keep + // snapping them back open. Per-browser via localStorage, same pattern + // as theme.js/theme-init.js — not synced across devices/accounts. + var STORAGE_KEY = "evection-sidebar-collapsed"; + + function readCollapsed() { + try { + var raw = localStorage.getItem(STORAGE_KEY); + return raw ? JSON.parse(raw) : []; + } catch (e) { + return []; + } + } + + function writeCollapsed(keys) { + try { + localStorage.setItem(STORAGE_KEY, JSON.stringify(keys)); + } catch (e) { + /* localStorage unavailable (private mode, etc.) — state just won't persist */ + } + } + + var collapsed = readCollapsed(); + + document.querySelectorAll(".sidebar-section[data-sidebar-key]").forEach(function (details) { + var key = details.dataset.sidebarKey; + if (collapsed.indexOf(key) !== -1) { + details.open = false; + } + + details.addEventListener("toggle", function () { + var current = readCollapsed(); + var index = current.indexOf(key); + if (details.open) { + if (index !== -1) current.splice(index, 1); + } else if (index === -1) { + current.push(key); + } + writeCollapsed(current); + }); + }); +})(); diff --git a/templates/base.html b/templates/base.html index cb89068..497f582 100644 --- a/templates/base.html +++ b/templates/base.html @@ -53,7 +53,10 @@ {% endif %} - {% if user.is_authenticated %}{% endif %} + {% if user.is_authenticated %} + + + {% endif %} diff --git a/templates/core/_sidebar.html b/templates/core/_sidebar.html index ac67f85..cc2fa9d 100644 --- a/templates/core/_sidebar.html +++ b/templates/core/_sidebar.html @@ -31,10 +31,13 @@ {% for entry in sidebar_tag_groups %} -