Files
Evection/apps/reader/views.py
T
ksmithandClaude Sonnet 5.5 8d2900424a Use keyset pagination for the stream (C4)
The stream did a COUNT(*) and OFFSET slice on every request, ordered by
a non-unique (published_at, fetched_at) — deep pages got slower and
articles sharing a timestamp could repeat or vanish across pages.

It now orders by (Coalesce(published_at, fetched_at), id) and pages with
before/after cursors, fetching one extra row to know whether another
page exists instead of counting. Pagination links also keep the feed
scope, which the old links silently dropped.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-01 08:51:20 -07:00

377 lines
14 KiB
Python

from datetime import datetime
from itertools import groupby
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.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
from django.utils import timezone
from django.views.decorators.http import require_POST
from apps.articles.models import (
SAVED_TAG_NAME,
STARRED_TAG_NAME,
Article,
ArticleTag,
Tag,
UserArticleState,
get_system_tag,
)
from apps.articles.queries import with_user_state
from apps.core.cache_utils import invalidate_sidebar_unread_cache
from apps.feeds.models import Feed
FILTER_CHOICES = [
("unread", "Unread"),
("all", "All"),
("starred", "Starred"),
("saved", "Saved"),
]
VALID_FILTERS = {key for key, _ in FILTER_CHOICES}
PAGE_SIZE = 30
def _base_queryset(user):
"""Articles from the user's subscriptions, with rule-hidden articles
excluded regardless of which filter tab is active."""
return (
Article.objects.filter(feed__subscriptions__user=user)
.exclude(user_states__user=user, user_states__is_hidden=True)
.select_related("feed")
)
def _get_or_create_state(user, article):
state, _ = UserArticleState.objects.get_or_create(user=user, article=article)
return state
def _article_tag_flags(user, article):
"""is_starred/is_saved_for_later for a single article — the toggle
views' equivalent of with_user_state's bulk version."""
tag_names = set(
ArticleTag.objects.filter(
user=user,
article=article,
tag__is_system=True,
tag__name__in=[STARRED_TAG_NAME, SAVED_TAG_NAME],
).values_list("tag__name", flat=True)
)
return STARRED_TAG_NAME in tag_names, SAVED_TAG_NAME in tag_names
def _apply_scope(request, queryset):
"""Narrows an already-base-scoped Article queryset by the optional
?tag=/?feed= params the sidebar links use. feed_id alone is enough to
scope by feed (the base queryset already restricts to the user's own
subscriptions)."""
active_tag = None
tag_id = request.GET.get("tag")
if tag_id:
active_tag = Tag.objects.filter(user=request.user, pk=tag_id).first()
if active_tag:
# A tag matches an article either directly (ArticleTag) or by
# the article's feed being tagged via the user's Subscription
# (SubscriptionTag) — tagging a whole feed surfaces all of its
# articles under that tag, not just individually-tagged ones.
queryset = queryset.filter(
Q(article_tags__user=request.user, article_tags__tag=active_tag)
| Q(
feed__subscriptions__user=request.user,
feed__subscriptions__subscription_tags__user=request.user,
feed__subscriptions__subscription_tags__tag=active_tag,
)
).distinct()
active_feed = None
feed_id = request.GET.get("feed")
if feed_id:
active_feed = Feed.objects.filter(pk=feed_id, subscriptions__user=request.user).first()
if active_feed:
queryset = queryset.filter(feed_id=active_feed.pk)
return queryset, active_tag, active_feed
def _date_label(day, today):
if day is None:
return "Undated"
diff = (today - day).days
if diff == 0:
return f"Received today — {day.strftime('%-d %B %Y')}"
if diff == 1:
return f"Received yesterday — {day.strftime('%-d %B %Y')}"
return f"Received {day.strftime('%-d %B %Y')}"
def _group_by_date(articles):
"""Articles are already ordered newest-first (Article.Meta.ordering),
so same-day articles are contiguous and groupby doesn't need a sort."""
today = timezone.localdate()
def day_of(article):
return timezone.localtime(article.published_at).date() if article.published_at else None
return [
{"label": _date_label(day, today), "articles": list(items)}
for day, items in groupby(articles, key=day_of)
]
def _resolve_active_filter(request):
requested_filter = request.GET.get("filter")
if requested_filter in VALID_FILTERS:
active_filter = requested_filter
else:
# No (valid) filter in the URL — e.g. a sidebar feed/folder/tag
# link, or just "/" — falls back to whatever the user last had
# selected, rather than always resetting to "unread".
active_filter = request.session.get("stream_filter", "unread")
request.session["stream_filter"] = active_filter
return active_filter
def _filtered_queryset(request, active_filter):
"""The stream's fully-scoped queryset (filter tab + tag/feed scope),
unpaginated — shared by stream() and check_new_articles() so the "any
new articles?" check always matches exactly what the stream itself
would show."""
queryset = _base_queryset(request.user)
if active_filter == "unread":
queryset = queryset.exclude(user_states__user=request.user, user_states__is_read=True)
elif active_filter == "starred":
queryset = queryset.filter(
article_tags__user=request.user,
article_tags__tag__user=request.user,
article_tags__tag__name=STARRED_TAG_NAME,
article_tags__tag__is_system=True,
)
elif active_filter == "saved":
queryset = queryset.filter(
article_tags__user=request.user,
article_tags__tag__user=request.user,
article_tags__tag__name=SAVED_TAG_NAME,
article_tags__tag__is_system=True,
)
return _apply_scope(request, queryset)
def _encode_cursor(article) -> str:
return f"{article.sort_at.isoformat()},{article.pk}"
def _decode_cursor(raw):
"""(sort_at, id) from a cursor produced by _encode_cursor, or None for
a missing/garbled one (treated as "no cursor" — the first page)."""
if not raw:
return None
try:
timestamp, pk = raw.rsplit(",", 1)
return datetime.fromisoformat(timestamp), int(pk)
except ValueError:
return None
@login_required
def stream(request):
active_filter = _resolve_active_filter(request)
queryset, active_tag, active_feed = _filtered_queryset(request, active_filter)
scope_params = {"filter": active_filter}
if active_tag:
scope_params["tag"] = active_tag.pk
if active_feed:
scope_params["feed"] = active_feed.pk
# Keyset pagination (C4) over (sort_at, id) — sort_at falls back to
# fetched_at for entries a feed gave no published date, and id breaks
# ties so pages neither shift nor repeat articles that share a
# timestamp. No COUNT(*): fetching one row past the page size is
# enough to know whether another page exists in that direction.
ordered = queryset.annotate(sort_at=Coalesce("published_at", "fetched_at"))
after = _decode_cursor(request.GET.get("after"))
before = _decode_cursor(request.GET.get("before"))
if after:
sort_at, pk = after
rows = list(
ordered.filter(Q(sort_at__gt=sort_at) | Q(sort_at=sort_at, id__gt=pk)).order_by(
"sort_at", "id"
)[: PAGE_SIZE + 1]
)
has_newer_page = len(rows) > PAGE_SIZE
has_older_page = True
rows = rows[:PAGE_SIZE][::-1]
else:
if before:
sort_at, pk = before
ordered = ordered.filter(Q(sort_at__lt=sort_at) | Q(sort_at=sort_at, id__lt=pk))
rows = list(ordered.order_by("-sort_at", "-id")[: PAGE_SIZE + 1])
has_older_page = len(rows) > PAGE_SIZE
has_newer_page = bool(before)
rows = rows[:PAGE_SIZE]
articles = with_user_state(rows, request.user)
newer_qs = older_qs = ""
if articles:
newer_qs = urlencode({**scope_params, "after": _encode_cursor(articles[0])})
older_qs = urlencode({**scope_params, "before": _encode_cursor(articles[-1])})
context = {
"date_groups": _group_by_date(articles),
"active_filter": active_filter,
"filters": FILTER_CHOICES,
"active_tag": active_tag,
"active_feed": active_feed,
"user_tags": Tag.objects.filter(user=request.user),
"has_newer_page": has_newer_page and bool(articles),
"has_older_page": has_older_page and bool(articles),
"newer_qs": newer_qs,
"older_qs": older_qs,
# Lets the "new articles" banner ask, on this exact same scope,
# whether anything with a higher id has shown up since this page
# was rendered — id (not published_at) because it's monotonic
# regardless of how a feed backdates an entry's published date.
"latest_article_id": queryset.aggregate(Max("id"))["id__max"] or 0,
"new_check_qs": urlencode(scope_params),
}
return render(request, "reader/stream.html", context)
@login_required
def check_new_articles(request):
active_filter = _resolve_active_filter(request)
queryset, _active_tag, _active_feed = _filtered_queryset(request, active_filter)
try:
since_id = int(request.GET.get("since", 0))
except ValueError:
since_id = 0
new_count = queryset.filter(id__gt=since_id).count()
return render(request, "reader/_new_articles_banner.html", {"new_count": new_count})
@login_required
@require_POST
def mark_all_read(request):
"""Marks every unread article currently matching the stream's filter
scope (tag included, "unread"/"all" filter excluded since it's a no-op
choice here) as read — bulk, not one UserArticleState per article."""
queryset = _base_queryset(request.user)
queryset, _active_tag, _active_feed = _apply_scope(request, queryset)
unread_articles = queryset.exclude(user_states__user=request.user, user_states__is_read=True)
unread_ids = list(unread_articles.values_list("id", flat=True))
if unread_ids:
now = timezone.now()
existing_state_article_ids = set(
UserArticleState.objects.filter(
user=request.user, article_id__in=unread_ids
).values_list("article_id", flat=True)
)
UserArticleState.objects.filter(
user=request.user, article_id__in=existing_state_article_ids
).update(is_read=True, read_at=now)
new_article_ids = [aid for aid in unread_ids if aid not in existing_state_article_ids]
UserArticleState.objects.bulk_create(
[
UserArticleState(user=request.user, article_id=aid, is_read=True, read_at=now)
for aid in new_article_ids
]
)
invalidate_sidebar_unread_cache(request.user.pk)
stream_url = reverse("reader:stream")
query = request.GET.urlencode()
return redirect(f"{stream_url}?{query}" if query else stream_url)
def _load_article_and_mark_read(user, article_id):
article = get_object_or_404(_base_queryset(user), pk=article_id)
state = _get_or_create_state(user, article)
if not state.is_read:
state.is_read = True
state.read_at = timezone.now()
state.save(update_fields=["is_read", "read_at"])
invalidate_sidebar_unread_cache(user.pk)
article.is_read = state.is_read
article.is_starred, article.is_saved_for_later = _article_tag_flags(user, article)
return article
@login_required
def article_detail(request, article_id):
article = _load_article_and_mark_read(request.user, article_id)
return render(request, "reader/article_detail.html", {"article": article})
@login_required
def article_inline(request, article_id):
"""Fetched via htmx when a row in the stream is expanded in place —
mirrors article_detail's mark-read side effect, but renders just the
content fragment plus an out-of-band update of the row's own read/star
icons (see templates/reader/_article_inline.html)."""
article = _load_article_and_mark_read(request.user, article_id)
return render(request, "reader/_article_inline.html", {"article": article})
@login_required
@require_POST
def toggle_read(request, article_id):
article = _owned_article(request.user, article_id)
state = _get_or_create_state(request.user, article)
state.is_read = not state.is_read
state.read_at = timezone.now() if state.is_read else None
state.save(update_fields=["is_read", "read_at"])
invalidate_sidebar_unread_cache(request.user.pk)
article.is_read = state.is_read
article.is_starred, article.is_saved_for_later = _article_tag_flags(request.user, article)
return render(request, "reader/_article_actions.html", {"article": article})
@login_required
@require_POST
def toggle_star(request, article_id):
return _toggle_system_tag(request, article_id, STARRED_TAG_NAME)
@login_required
@require_POST
def toggle_saved(request, article_id):
return _toggle_system_tag(request, article_id, SAVED_TAG_NAME)
def _owned_article(user, article_id):
try:
return _base_queryset(user).get(pk=article_id)
except Article.DoesNotExist:
raise Http404
def _toggle_system_tag(request, article_id, tag_name):
article = _owned_article(request.user, article_id)
tag = get_system_tag(request.user, tag_name)
existing = ArticleTag.objects.filter(user=request.user, article=article, tag=tag)
if existing.exists():
existing.delete()
else:
ArticleTag.objects.create(user=request.user, article=article, tag=tag)
state = UserArticleState.objects.filter(user=request.user, article=article).first()
article.is_read = state.is_read if state else False
article.is_starred, article.is_saved_for_later = _article_tag_flags(request.user, article)
return render(request, "reader/_article_actions.html", {"article": article})