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 %}