From 7bb981b54539eacbeb8814c6e8c89c8b351232fc Mon Sep 17 00:00:00 2001 From: Keith Smith Date: Sat, 31 Oct 2026 14:36:27 -0600 Subject: [PATCH] Rebuild stream date-group headers in the viewer's local timezone The server still groups articles into "Received today/yesterday/" headers using UTC, so an article published near UTC midnight could land in a mismatched day group. Each article row now ships its raw UTC instant (data-published-utc); static/js/date-groups.js discards the server-rendered headers on load and rebuilds them using the browser's own local calendar day, same approach as the article-time localization this follows up on. Co-Authored-By: Claude Sonnet 5 --- apps/reader/tests.py | 29 ++++++++++++++ static/js/date-groups.js | 61 ++++++++++++++++++++++++++++++ templates/base.html | 1 + templates/reader/_article_row.html | 3 +- 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 static/js/date-groups.js diff --git a/apps/reader/tests.py b/apps/reader/tests.py index 8f80527..ea8c6e1 100644 --- a/apps/reader/tests.py +++ b/apps/reader/tests.py @@ -1,3 +1,5 @@ +import re + import pytest from django.contrib.auth import get_user_model from django.urls import reverse @@ -188,3 +190,30 @@ def test_sidebar_shows_a_tags_total_unread_count(client, alice): # The only "7" on the page — the individual feeds show 3 and 4. content = response.content.decode() assert '7' in content + + +@pytest.mark.django_db +def test_article_rows_carry_their_utc_instant_for_client_side_date_grouping(client, alice): + """The server still groups date headers in UTC, but ships each row's + raw UTC instant so date-groups.js can rebuild the headers using the + viewer's local calendar day instead.""" + from django.utils import timezone + + feed = Feed.objects.create(feed_url="https://example.com/feed.xml") + Subscription.objects.create(user=alice, feed=feed) + dated = Article.objects.create( + feed=feed, guid="dated", title="Dated", published_at=timezone.now() + ) + undated = Article.objects.create(feed=feed, guid="undated", title="Undated") + + client.login(username="alice", password="pw-alice-123") + response = client.get(reverse("reader:stream"), {"filter": "all"}) + content = response.content.decode() + + expected_iso = dated.published_at.isoformat() + assert f'id="article-{dated.pk}"' in content + assert f'data-published-utc="{expected_iso}"' in content + + undated_row = re.search(rf'
  • ', content, flags=re.DOTALL) + assert undated_row is not None + assert 'data-published-utc=""' in undated_row.group(0) diff --git a/static/js/date-groups.js b/static/js/date-groups.js new file mode 100644 index 0000000..a04f7cb --- /dev/null +++ b/static/js/date-groups.js @@ -0,0 +1,61 @@ +(function () { + // The server groups articles into "Received today/yesterday/" + // headers using UTC (config.settings.base TIME_ZONE), so an article + // published near UTC midnight can land in the wrong day group from the + // viewer's own perspective. Each article row carries its UTC instant in + // data-published-utc; this throws away the server's date-header
  • s + // and rebuilds them using the browser's local calendar day instead. + var MONTHS = [ + "January", "February", "March", "April", "May", "June", + "July", "August", "September", "October", "November", "December", + ]; + + function localDateOf(iso) { + if (!iso) return null; + var date = new Date(iso); + if (isNaN(date.getTime())) return null; + date.setHours(0, 0, 0, 0); + return date; + } + + function labelFor(day, today) { + if (day === null) return "Undated"; + var formatted = day.getDate() + " " + MONTHS[day.getMonth()] + " " + day.getFullYear(); + var diffDays = Math.round((today - day) / 86400000); + if (diffDays === 0) return "Received today — " + formatted; + if (diffDays === 1) return "Received yesterday — " + formatted; + return "Received " + formatted; + } + + function regroupDateHeaders(root) { + if (!root) return; + var list = root.matches && root.matches("ul.article-list") ? root : root.querySelector("ul.article-list"); + if (!list || !list.querySelector("li.article-row[data-published-utc], li.article-row")) return; + + list.querySelectorAll("li.date-header").forEach(function (el) { + el.parentNode.removeChild(el); + }); + + var today = new Date(); + today.setHours(0, 0, 0, 0); + + var currentKey; + list.querySelectorAll("li.article-row").forEach(function (row) { + var day = localDateOf(row.getAttribute("data-published-utc")); + var key = day === null ? "undated" : day.getTime(); + if (key !== currentKey) { + var header = document.createElement("li"); + header.className = "date-header"; + header.textContent = labelFor(day, today); + list.insertBefore(header, row); + currentKey = key; + } + }); + } + + regroupDateHeaders(document); + + document.body.addEventListener("htmx:afterSwap", function (event) { + regroupDateHeaders(event.target); + }); +})(); diff --git a/templates/base.html b/templates/base.html index 4045a88..e8ce4d6 100644 --- a/templates/base.html +++ b/templates/base.html @@ -54,6 +54,7 @@ + {% if user.is_authenticated %} diff --git a/templates/reader/_article_row.html b/templates/reader/_article_row.html index f0d3cc0..27a6a59 100644 --- a/templates/reader/_article_row.html +++ b/templates/reader/_article_row.html @@ -1,4 +1,5 @@ -
  • +
  • {% include "reader/_article_actions.html" %} {% if article.feed.favicon_url %}