Make Starred and Saved-for-later pre-configured system tags

UserArticleState.is_starred/is_saved_for_later/starred_at are gone —
starring and saving now add/remove an ArticleTag against a per-user
"Starred" / "Saved for later" Tag, created lazily on first use and marked
Tag.is_system=True. is_read/is_hidden stay on UserArticleState since
they're genuine reading state, not categorization, which is what made
these two candidates for unifying into the general tag mechanism.

System tags are excluded from the Manage Tags page and from delete_tag
(get_object_or_404 now requires is_system=False, so attempting to delete
one 404s) and from the subscription-tag autocomplete, since they
categorize articles, not feeds. The star/save icons are still exactly
where they were — this only changes what powers them.

apps.rules.engine's star action now does get_system_tag + ArticleTag.
get_or_create instead of setting a boolean field. A data migration
converts every existing is_starred=True/is_saved_for_later=True row into
its ArticleTag equivalent before the fields are dropped — verified by
seeding the pre-removal schema directly (including a row with both flags
set) and confirming the migration produces the right tags.

apps.articles.queries.with_user_state gained one more query (tag lookup
for the current page's articles) to stay in the same "fixed number of
queries regardless of article count" shape it already had.

Verified end to end against a running dev server: toggling star/saved,
the starred/saved filters, the sidebar's Favourites count, and that the
Starred tag doesn't appear on the Manage Tags page.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5 committed 2026-10-30 21:36:44 -06:00
1 parent 0e132e9ab9
commit 753ae9efc7
15 files changed
+267 -82

No files matched your search

+20 -7
View File
@@ -1,12 +1,14 @@
from django.db.models import Count
from .models import Article, UserArticleState
from .models import SAVED_TAG_NAME, STARRED_TAG_NAME, Article, ArticleTag, UserArticleState
def with_user_state(articles, user):
"""Annotates an iterable of Article instances with is_read/is_starred/
is_saved_for_later booleans for the given user, in two queries total
regardless of how many articles there are (no N+1).
regardless of how many articles there are (no N+1). Starred/saved come
from ArticleTag against the pre-configured system tags, not a field on
UserArticleState — see Tag.is_system.
Accepts either a queryset or a plain list (search backends return
plain lists already ordered by relevance), materializing it either
@@ -16,17 +18,28 @@ def with_user_state(articles, user):
if not articles:
return articles
article_ids = [a.pk for a in articles]
states = {
state.article_id: state
for state in UserArticleState.objects.filter(
user=user, article_id__in=[a.pk for a in articles]
)
for state in UserArticleState.objects.filter(user=user, article_id__in=article_ids)
}
starred_ids = set()
saved_ids = set()
tag_rows = ArticleTag.objects.filter(
user=user,
article_id__in=article_ids,
tag__is_system=True,
tag__name__in=[STARRED_TAG_NAME, SAVED_TAG_NAME],
).values_list("article_id", "tag__name")
for article_id, tag_name in tag_rows:
(starred_ids if tag_name == STARRED_TAG_NAME else saved_ids).add(article_id)
for article in articles:
state = states.get(article.pk)
article.is_read = state.is_read if state else False
article.is_starred = state.is_starred if state else False
article.is_saved_for_later = state.is_saved_for_later if state else False
article.is_starred = article.pk in starred_ids
article.is_saved_for_later = article.pk in saved_ids
return articles