Private
Public Access
Rebuild stream date-group headers in the viewer's local timezone
The server still groups articles into "Received today/yesterday/<date>" 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 <noreply@anthropic.com>
This commit is contained in:
1 parent
fd68400822
commit
7bb981b545
4 files changed
+93
-1
No files matched your search
@@ -1,3 +1,5 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.urls import reverse
|
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.
|
# The only "7" on the page — the individual feeds show 3 and 4.
|
||||||
content = response.content.decode()
|
content = response.content.decode()
|
||||||
assert '<span class="unread-count">7</span>' in content
|
assert '<span class="unread-count">7</span>' 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'<li id="article-{undated.pk}".*?>', content, flags=re.DOTALL)
|
||||||
|
assert undated_row is not None
|
||||||
|
assert 'data-published-utc=""' in undated_row.group(0)
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
(function () {
|
||||||
|
// The server groups articles into "Received today/yesterday/<date>"
|
||||||
|
// 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 <li>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);
|
||||||
|
});
|
||||||
|
})();
|
||||||
@@ -54,6 +54,7 @@
|
|||||||
|
|
||||||
<script src="{% static 'js/stream.js' %}" defer></script>
|
<script src="{% static 'js/stream.js' %}" defer></script>
|
||||||
<script src="{% static 'js/localize-times.js' %}" defer></script>
|
<script src="{% static 'js/localize-times.js' %}" defer></script>
|
||||||
|
<script src="{% static 'js/date-groups.js' %}" defer></script>
|
||||||
{% if user.is_authenticated %}
|
{% if user.is_authenticated %}
|
||||||
<script src="{% static 'js/theme.js' %}" defer></script>
|
<script src="{% static 'js/theme.js' %}" defer></script>
|
||||||
<script src="{% static 'js/sidebar-state.js' %}" defer></script>
|
<script src="{% static 'js/sidebar-state.js' %}" defer></script>
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
<li id="article-{{ article.pk }}" class="article-row{% if article.is_read %} is-read{% endif %}">
|
<li id="article-{{ article.pk }}" class="article-row{% if article.is_read %} is-read{% endif %}"
|
||||||
|
data-published-utc="{% if article.published_at %}{{ article.published_at|date:"c" }}{% endif %}">
|
||||||
<div class="article-row-main">
|
<div class="article-row-main">
|
||||||
{% include "reader/_article_actions.html" %}
|
{% include "reader/_article_actions.html" %}
|
||||||
{% if article.feed.favicon_url %}
|
{% if article.feed.favicon_url %}
|
||||||
|
|||||||
Reference in new issue
Block a user