Private
Public Access
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0975a7634b |
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):
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
// Marks the browser tab with a red dot on its favicon when new unread
|
||||
// articles have arrived since this page loaded — visible from a background
|
||||
// tab, unlike the in-page "new articles" banner. Runs on every
|
||||
// authenticated page; a full page load resets the baseline (and the icon),
|
||||
// which is exactly when the user has seen the new articles.
|
||||
(function () {
|
||||
var POLL_MS = 30000;
|
||||
var url = document.currentScript && document.currentScript.dataset.url;
|
||||
var icon = document.querySelector('link[rel="icon"]');
|
||||
if (!url || !icon) return;
|
||||
|
||||
var originalHref = icon.href;
|
||||
var baseline = null;
|
||||
var badged = null; // the dotted icon's data: URL, drawn once on first need
|
||||
var showing = false;
|
||||
|
||||
function drawDot() {
|
||||
return new Promise(function (resolve) {
|
||||
var image = new Image();
|
||||
image.onload = function () {
|
||||
var size = 64;
|
||||
var canvas = document.createElement("canvas");
|
||||
canvas.width = canvas.height = size;
|
||||
var ctx = canvas.getContext("2d");
|
||||
ctx.drawImage(image, 0, 0, size, size);
|
||||
ctx.beginPath();
|
||||
ctx.arc(size * 0.74, size * 0.26, size * 0.23, 0, 2 * Math.PI);
|
||||
ctx.fillStyle = "#e53935";
|
||||
ctx.fill();
|
||||
ctx.lineWidth = size * 0.07;
|
||||
ctx.strokeStyle = "#ffffff";
|
||||
ctx.stroke();
|
||||
resolve(canvas.toDataURL("image/png"));
|
||||
};
|
||||
image.onerror = function () {
|
||||
resolve(null);
|
||||
};
|
||||
image.src = originalHref;
|
||||
});
|
||||
}
|
||||
|
||||
// Replace the <link> rather than editing href: some browsers ignore an
|
||||
// in-place change to an existing favicon element.
|
||||
function setIcon(href) {
|
||||
var fresh = icon.cloneNode();
|
||||
fresh.href = href;
|
||||
icon.parentNode.replaceChild(fresh, icon);
|
||||
icon = fresh;
|
||||
}
|
||||
|
||||
function showDot(on) {
|
||||
if (on === showing) return Promise.resolve();
|
||||
showing = on;
|
||||
if (!on) {
|
||||
setIcon(originalHref);
|
||||
return Promise.resolve();
|
||||
}
|
||||
var ready = badged ? Promise.resolve(badged) : drawDot();
|
||||
return ready.then(function (href) {
|
||||
badged = href;
|
||||
if (href && showing) setIcon(href);
|
||||
});
|
||||
}
|
||||
|
||||
function poll() {
|
||||
var query = baseline === null ? "" : "?since=" + baseline;
|
||||
fetch(url + query, { credentials: "same-origin", headers: { Accept: "application/json" } })
|
||||
.then(function (response) {
|
||||
// A redirect means the session ended (we'd be reading the login
|
||||
// page) — stop polling instead of hammering it.
|
||||
if (!response.ok || response.redirected) throw new Error("stop");
|
||||
return response.json();
|
||||
})
|
||||
.then(function (data) {
|
||||
if (baseline === null) baseline = data.latest_id;
|
||||
else showDot(data.new_count > 0);
|
||||
setTimeout(poll, POLL_MS);
|
||||
})
|
||||
.catch(function (error) {
|
||||
if (error.message !== "stop") setTimeout(poll, POLL_MS);
|
||||
});
|
||||
}
|
||||
|
||||
poll();
|
||||
})();
|
||||
@@ -58,6 +58,7 @@
|
||||
{% if user.is_authenticated %}
|
||||
<script src="{% static 'js/theme.js' %}" defer></script>
|
||||
<script src="{% static 'js/sidebar-state.js' %}" defer></script>
|
||||
<script src="{% static 'js/tab-badge.js' %}" data-url="{% url 'reader:new_since' %}" defer></script>
|
||||
{% endif %}
|
||||
<script src="{% static 'js/register-sw.js' %}" defer></script>
|
||||
</body>
|
||||
|
||||
Reference in new issue
Block a user