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
@@ -33,11 +33,15 @@ class Article(models.Model):
class UserArticleState(models.Model):
"""Per-user read/star/save/hide state for an Article.
"""Per-user read/hide state for an Article.
Starred and saved-for-later are ArticleTags against pre-configured
system tags instead of fields here (see Tag.is_system) — read/hidden
stay here since they're genuine reading state, not categorization.
Rows are created lazily, only on first interaction — a row with no
explicit True flags is equivalent to "no row exists" (unread, not
starred). Never bulk-create one of these per subscriber on ingest.
explicit True flags is equivalent to "no row exists" (unread). Never
bulk-create one of these per subscriber on ingest.
"""
user = models.ForeignKey(
@@ -46,30 +50,34 @@ class UserArticleState(models.Model):
article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name="user_states")
is_read = models.BooleanField(default=False)
is_starred = models.BooleanField(default=False)
is_saved_for_later = models.BooleanField(default=False)
is_hidden = models.BooleanField(default=False)
read_at = models.DateTimeField(null=True, blank=True)
starred_at = models.DateTimeField(null=True, blank=True)
class Meta:
unique_together = [("user", "article")]
indexes = [
models.Index(fields=["user", "is_read"]),
models.Index(fields=["user", "is_starred"]),
]
def __str__(self):
return f"{self.user}: {self.article}"
# Pre-configured per-user tags backing the star/save-for-later icons —
# created lazily on first use (see get_system_tag), same as any other tag,
# just under a name the app itself controls rather than the user.
STARRED_TAG_NAME = "Starred"
SAVED_TAG_NAME = "Saved for later"
class Tag(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="tags"
)
name = models.CharField(max_length=100)
color = models.CharField(max_length=7, blank=True)
is_system = models.BooleanField(default=False)
class Meta:
unique_together = [("user", "name")]
@@ -79,6 +87,11 @@ class Tag(models.Model):
return self.name
def get_system_tag(user, name):
tag, _ = Tag.objects.get_or_create(user=user, name=name, defaults={"is_system": True})
return tag
class ArticleTag(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name="article_tags")