Files
Evection/apps/articles/models.py
T
ksmithandClaude Sonnet 5.5 f8cd1eb33e Stop retention from bringing deleted articles back (#29)
The nightly purge deleted articles, but a feed that still listed an
entry would re-insert it on its next poll as a brand-new, unread
article. For feeds with long archives, old items could reappear after
every retention run.

The purge now leaves a (feed, guid) tombstone per deleted article, and
ingestion skips tombstoned guids. With retention on, entries whose own
publish date is already outside the window are not ingested at all, so
a long archive isn't fetched only to be purged and tombstoned the same
night. Undated entries rely on the tombstone.

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

143 lines
5.1 KiB
Python

from django.conf import settings
from django.db import models
from apps.feeds.models import Feed, Subscription
class Article(models.Model):
"""A single fetched entry, shared across every subscriber of its feed.
Per-user state (read/starred/saved/hidden) lives in UserArticleState,
never on this model — editing an article's content on re-fetch must
never touch anyone's read/star state.
"""
feed = models.ForeignKey(Feed, on_delete=models.CASCADE, related_name="articles")
guid = models.CharField(max_length=1000)
link = models.URLField(max_length=2000, blank=True)
title = models.CharField(max_length=1000, blank=True)
author = models.CharField(max_length=500, blank=True)
content_html = models.TextField(blank=True)
summary = models.TextField(blank=True)
content_hash = models.CharField(max_length=64, blank=True)
published_at = models.DateTimeField(null=True, blank=True)
fetched_at = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = [("feed", "guid")]
indexes = [models.Index(fields=["feed", "published_at"])]
ordering = ["-published_at", "-fetched_at"]
def __str__(self):
return self.title or self.guid
class PurgedArticle(models.Model):
"""Tombstone left by the retention purge (apps.articles.tasks.
purge_old_articles) for every article it deletes: a feed that still
lists an entry after we purged it would otherwise be re-inserted as a
brand-new, unread article on its next poll. Ingestion skips any guid
that has a tombstone here. Only the (feed, guid) pair is kept — a
small fraction of the article it replaces.
"""
feed = models.ForeignKey(Feed, on_delete=models.CASCADE, related_name="purged_articles")
guid = models.CharField(max_length=1000)
purged_at = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = [("feed", "guid")]
class UserArticleState(models.Model):
"""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). Never
bulk-create one of these per subscriber on ingest.
"""
user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="article_states"
)
article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name="user_states")
is_read = models.BooleanField(default=False)
is_hidden = models.BooleanField(default=False)
read_at = models.DateTimeField(null=True, blank=True)
class Meta:
unique_together = [("user", "article")]
indexes = [
models.Index(fields=["user", "is_read"]),
]
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")]
ordering = ["name"]
def __str__(self):
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")
tag = models.ForeignKey(Tag, on_delete=models.CASCADE, related_name="article_tags")
class Meta:
unique_together = [("user", "article", "tag")]
def __str__(self):
return f"{self.article} -> {self.tag}"
class SubscriptionTag(models.Model):
"""Tags a feed itself (via the user's Subscription to it), independent
of tagging individual articles — e.g. tagging a whole feed "backend"
rather than tagging each of its articles one by one. Articles from a
tagged feed show up under that tag in the stream alongside any
individually-tagged articles (see apps.reader.views._apply_scope).
"""
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
subscription = models.ForeignKey(
Subscription, on_delete=models.CASCADE, related_name="subscription_tags"
)
tag = models.ForeignKey(Tag, on_delete=models.CASCADE, related_name="subscription_tags")
class Meta:
unique_together = [("user", "subscription", "tag")]
def __str__(self):
return f"{self.subscription} -> {self.tag}"