Private
Public Access
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7bb981b545 | ||
|
|
fd68400822 |
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);
|
||||||
|
});
|
||||||
|
})();
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
(function () {
|
||||||
|
// Everything is stored and server-rendered in UTC (config.settings.base
|
||||||
|
// TIME_ZONE) — these <time datetime="..."> elements carry that UTC
|
||||||
|
// instant plus a UTC-formatted fallback as their initial text, and this
|
||||||
|
// reformats them using the browser's own timezone, client-side, so the
|
||||||
|
// server never needs to know (or guess) where the viewer actually is.
|
||||||
|
var SHORT_OPTIONS = { hour: "numeric", minute: "2-digit" };
|
||||||
|
var LONG_OPTIONS = {
|
||||||
|
year: "numeric",
|
||||||
|
month: "short",
|
||||||
|
day: "numeric",
|
||||||
|
hour: "2-digit",
|
||||||
|
minute: "2-digit",
|
||||||
|
};
|
||||||
|
|
||||||
|
function localizeTimesWithin(root) {
|
||||||
|
if (!root || typeof root.querySelectorAll !== "function") return;
|
||||||
|
root.querySelectorAll("time[data-local-format]").forEach(function (el) {
|
||||||
|
var iso = el.getAttribute("datetime");
|
||||||
|
if (!iso) return;
|
||||||
|
var date = new Date(iso);
|
||||||
|
if (isNaN(date.getTime())) return;
|
||||||
|
var options = el.dataset.localFormat === "short" ? SHORT_OPTIONS : LONG_OPTIONS;
|
||||||
|
try {
|
||||||
|
el.textContent = date.toLocaleString(undefined, options);
|
||||||
|
} catch (e) {
|
||||||
|
/* Intl unavailable or threw — leave the server-rendered UTC text as-is */
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
localizeTimesWithin(document);
|
||||||
|
|
||||||
|
// Article rows/content are frequently swapped in by htmx (pagination,
|
||||||
|
// the inline expand-article view, the dashboard's retry action) without
|
||||||
|
// a full page load, so every swap needs the same pass over just the
|
||||||
|
// new content.
|
||||||
|
document.body.addEventListener("htmx:afterSwap", function (event) {
|
||||||
|
localizeTimesWithin(event.target);
|
||||||
|
});
|
||||||
|
})();
|
||||||
@@ -53,6 +53,8 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<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/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>
|
||||||
|
|||||||
@@ -31,7 +31,13 @@
|
|||||||
<td>{{ feed.subscriber_count }}</td>
|
<td>{{ feed.subscriber_count }}</td>
|
||||||
<td>{{ feed.get_last_fetch_status_display }}</td>
|
<td>{{ feed.get_last_fetch_status_display }}</td>
|
||||||
<td>{{ feed.fetch_error_count }}</td>
|
<td>{{ feed.fetch_error_count }}</td>
|
||||||
<td>{{ feed.last_fetched_at|default:"never" }}</td>
|
<td>
|
||||||
|
{% if feed.last_fetched_at %}
|
||||||
|
<time datetime="{{ feed.last_fetched_at|date:"c" }}" data-local-format="long">{{ feed.last_fetched_at|date:"M j, Y H:i" }}</time>
|
||||||
|
{% else %}
|
||||||
|
never
|
||||||
|
{% endif %}
|
||||||
|
</td>
|
||||||
<td>{{ feed.last_error_message|default:"—" }}</td>
|
<td>{{ feed.last_error_message|default:"—" }}</td>
|
||||||
<td>
|
<td>
|
||||||
<form method="post" action="{% url 'core:retry_feed' feed.id %}">
|
<form method="post" action="{% url 'core:retry_feed' feed.id %}">
|
||||||
|
|||||||
@@ -2,7 +2,10 @@
|
|||||||
<h2 class="article-content-title">{{ article.title|default:"(untitled)" }}</h2>
|
<h2 class="article-content-title">{{ article.title|default:"(untitled)" }}</h2>
|
||||||
<p class="article-meta">
|
<p class="article-meta">
|
||||||
{% if article.feed.favicon_url %}<img src="{{ article.feed.favicon_url }}" alt="" class="feed-favicon">{% endif %}
|
{% if article.feed.favicon_url %}<img src="{{ article.feed.favicon_url }}" alt="" class="feed-favicon">{% endif %}
|
||||||
{{ article.feed.title }} · {{ article.published_at|date:"M j, Y H:i" }}
|
{{ article.feed.title }}
|
||||||
|
{% if article.published_at %}
|
||||||
|
· <time datetime="{{ article.published_at|date:"c" }}" data-local-format="long">{{ article.published_at|date:"M j, Y H:i" }}</time>
|
||||||
|
{% endif %}
|
||||||
{% if article.author %} · {{ article.author }}{% endif %}
|
{% if article.author %} · {{ article.author }}{% endif %}
|
||||||
{% if article.link %} · <a href="{{ article.link }}" target="_blank" rel="noopener noreferrer">Original ↗</a>{% endif %}
|
{% if article.link %} · <a href="{{ article.link }}" target="_blank" rel="noopener noreferrer">Original ↗</a>{% endif %}
|
||||||
</p>
|
</p>
|
||||||
|
|||||||
@@ -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 %}
|
||||||
@@ -8,7 +9,9 @@
|
|||||||
<a href="{% url 'reader:article_detail' article.pk %}" class="article-title"
|
<a href="{% url 'reader:article_detail' article.pk %}" class="article-title"
|
||||||
data-inline-url="{% url 'reader:article_inline' article.pk %}"
|
data-inline-url="{% url 'reader:article_inline' article.pk %}"
|
||||||
onclick="return toggleArticle(event, {{ article.pk }})">{{ article.title|default:"(untitled)" }}</a>
|
onclick="return toggleArticle(event, {{ article.pk }})">{{ article.title|default:"(untitled)" }}</a>
|
||||||
<span class="article-time">{{ article.published_at|date:"g:i A" }}</span>
|
{% if article.published_at %}
|
||||||
|
<time class="article-time" datetime="{{ article.published_at|date:"c" }}" data-local-format="short">{{ article.published_at|date:"g:i A" }}</time>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
<div id="article-body-{{ article.pk }}" class="article-inline-body" hidden></div>
|
<div id="article-body-{{ article.pk }}" class="article-inline-body" hidden></div>
|
||||||
</li>
|
</li>
|
||||||
Reference in new issue
Block a user