Private
Public Access
Mark the browser tab with a red dot when new articles arrive (#31)
The in-page "new articles" banner is invisible from a background tab. Every authenticated page now polls a small JSON endpoint (reader:new_since, every 30s) and redraws the tab's favicon with a red dot when unread, non-hidden articles from the user's feeds have appeared since the page loaded. A reload (which is what clicking the banner does) resets the baseline and the icon. The endpoint is deliberately not scoped to the stream's current filter/tag/feed, so the tab lights up for anything new whichever page is open. Scope is the dot only: no desktop notifications, per the request. Verified in a dev server enforcing the production CSP: the icon swapped to the dotted version after a new article arrived and reset on reload. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fd494a9c08
commit
0975a7634b
5 files changed
+162
-1
No files matched your search
@@ -503,3 +503,54 @@ def test_article_with_no_publish_date_shows_when_it_was_fetched(client, alice):
|
||||
|
||||
article.refresh_from_db()
|
||||
assert article.published_at is None
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_new_since_requires_login(client):
|
||||
response = client.get(reverse("reader:new_since"))
|
||||
assert response.status_code == 302
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_new_since_without_a_cursor_returns_the_baseline_id(client, alice):
|
||||
feed = Feed.objects.create(feed_url="https://example.com/feed.xml")
|
||||
Subscription.objects.create(user=alice, feed=feed)
|
||||
article = Article.objects.create(feed=feed, guid="g1", title="Hello")
|
||||
|
||||
client.login(username="alice", password="pw-alice-123")
|
||||
body = client.get(reverse("reader:new_since")).json()
|
||||
|
||||
assert body == {"latest_id": article.pk, "new_count": 0}
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_new_since_counts_only_new_unread_visible_articles_from_my_feeds(client, alice, bob):
|
||||
mine = Feed.objects.create(feed_url="https://mine.example.com/feed.xml")
|
||||
theirs = Feed.objects.create(feed_url="https://theirs.example.com/feed.xml")
|
||||
Subscription.objects.create(user=alice, feed=mine)
|
||||
Subscription.objects.create(user=bob, feed=theirs)
|
||||
cursor = Article.objects.create(feed=mine, guid="cursor", title="Cursor").pk
|
||||
|
||||
new_unread = Article.objects.create(feed=mine, guid="n1", title="New unread")
|
||||
already_read = Article.objects.create(feed=mine, guid="n2", title="New but read")
|
||||
hidden = Article.objects.create(feed=mine, guid="n3", title="New but hidden")
|
||||
Article.objects.create(feed=theirs, guid="n4", title="Someone else's feed")
|
||||
UserArticleState.objects.create(user=alice, article=already_read, is_read=True)
|
||||
UserArticleState.objects.create(user=alice, article=hidden, is_hidden=True)
|
||||
|
||||
client.login(username="alice", password="pw-alice-123")
|
||||
body = client.get(reverse("reader:new_since"), {"since": cursor}).json()
|
||||
|
||||
assert body["new_count"] == 1
|
||||
assert new_unread.pk > cursor
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_authenticated_pages_load_the_tab_badge_script_with_its_endpoint(client, alice):
|
||||
client.login(username="alice", password="pw-alice-123")
|
||||
content = client.get(reverse("reader:stream")).content.decode()
|
||||
|
||||
assert "js/tab-badge" in content
|
||||
assert f'data-url="{reverse("reader:new_since")}"' in content
|
||||
client.logout()
|
||||
assert "tab-badge" not in client.get(reverse("accounts:login")).content.decode()
|
||||
@@ -7,6 +7,7 @@ app_name = "reader"
|
||||
urlpatterns = [
|
||||
path("", views.stream, name="stream"),
|
||||
path("check-new/", views.check_new_articles, name="check_new"),
|
||||
path("new-since/", views.new_since, name="new_since"),
|
||||
path("mark-all-read/", views.mark_all_read, name="mark_all_read"),
|
||||
path("articles/<int:article_id>/", views.article_detail, name="article_detail"),
|
||||
path(
|
||||
|
||||
+24
-1
@@ -5,7 +5,7 @@ from urllib.parse import urlencode
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.db.models import Max, Q
|
||||
from django.db.models.functions import Coalesce
|
||||
from django.http import Http404
|
||||
from django.http import Http404, JsonResponse
|
||||
from django.shortcuts import get_object_or_404, redirect, render
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
@@ -258,6 +258,29 @@ def check_new_articles(request):
|
||||
return render(request, "reader/_new_articles_banner.html", {"new_count": new_count})
|
||||
|
||||
|
||||
@login_required
|
||||
def new_since(request):
|
||||
"""Feeds the browser-tab dot (static/js/tab-badge.js) on every page:
|
||||
without ?since it returns the current highest article id, which the
|
||||
page keeps as its baseline; with it, how many unread, non-hidden
|
||||
articles from the user's subscriptions have appeared after that id.
|
||||
Deliberately not scoped to the stream's current filter/tag/feed — the
|
||||
tab should light up for anything new, whichever page is open."""
|
||||
latest_id = Article.objects.aggregate(Max("id"))["id__max"] or 0
|
||||
try:
|
||||
since_id = int(request.GET["since"])
|
||||
except (KeyError, ValueError):
|
||||
return JsonResponse({"latest_id": latest_id, "new_count": 0})
|
||||
|
||||
new_count = (
|
||||
_base_queryset(request.user)
|
||||
.filter(id__gt=since_id)
|
||||
.exclude(user_states__user=request.user, user_states__is_read=True)
|
||||
.count()
|
||||
)
|
||||
return JsonResponse({"latest_id": latest_id, "new_count": new_count})
|
||||
|
||||
|
||||
@login_required
|
||||
@require_POST
|
||||
def mark_all_read(request):
|
||||
|
||||
Reference in new issue
Block a user