Files
J621/backend/apps/follows/models.py
T
JakeBreath 03dd235f3a Follows: followed tags/pools, feeds, unseen badges and blacklist cloud
Backend (new apps.follows):
- FollowedTag/FollowedPool/FollowedPost models; per-user follows with
  unseen tracking, plus FollowCloud for the cached blacklist cloud.
- Two periodic commands sharing one fetch path: sync_followed_tags and
  sync_followed_pools fetch each followed tag/pool's newest posts (one
  e621 search per unique follow), store unseen feed rows, refresh covers
  and pool metadata; both fall back to anonymous e621 access.
- API: /api/follows/tags|pools (follow, unfollow, mark seen), a merged
  feed with per-follow filtering, and /api/follows/cloud/ which rebuilds
  the blacklisted-tag cloud in a daemon thread when its 10 min cache is
  stale (polling returns building/ready).
- e621 client now supports anonymous reads; trimmed posts carry preview
  URLs for covers and feed tiles.

Frontend:
- /followed page: follow forms, cover cards with unseen badges and
  Mark seen, merged feed with filter/unseen toggle, and a blacklist
  cloud panel that polls while building. Followed nav entry added.
2026-09-17 14:09:30 -05:00

115 lines
3.4 KiB
Python

from django.conf import settings
from django.db import models
class FollowedTag(models.Model):
"""A tag one user subscribes to; feeds are filled by the sync command."""
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="followed_tags",
)
tag = models.CharField(max_length=200)
cover_data = models.JSONField(null=True, blank=True)
last_synced_at = models.DateTimeField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ["tag"]
constraints = [
models.UniqueConstraint(
fields=["user", "tag"], name="uniq_followed_tag_per_user"
),
]
def __str__(self):
return f"{self.user} follows tag:{self.tag}"
class FollowedPool(models.Model):
"""A pool one user subscribes to; feeds are filled by the sync command."""
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="followed_pools",
)
pool_id = models.IntegerField()
name = models.CharField(max_length=200, blank=True, default="")
post_count = models.IntegerField(default=0)
cover_data = models.JSONField(null=True, blank=True)
last_synced_at = models.DateTimeField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ["pool_id"]
constraints = [
models.UniqueConstraint(
fields=["user", "pool_id"], name="uniq_followed_pool_per_user"
),
]
def __str__(self):
return f"{self.user} follows pool:{self.pool_id}"
class FollowedPost(models.Model):
"""One post seen in a followed feed (stored once per follow source)."""
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="followed_posts",
)
tag = models.ForeignKey(
FollowedTag,
null=True,
blank=True,
on_delete=models.CASCADE,
related_name="posts",
)
pool = models.ForeignKey(
FollowedPool,
null=True,
blank=True,
on_delete=models.CASCADE,
related_name="posts",
)
post_id = models.IntegerField(db_index=True)
data = models.JSONField(default=dict)
seen = models.BooleanField(default=False, db_index=True)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ["-post_id"]
indexes = [models.Index(fields=["user", "post_id"])]
constraints = [
models.UniqueConstraint(
fields=["tag", "post_id"], name="uniq_followed_tag_post"
),
models.UniqueConstraint(
fields=["pool", "post_id"], name="uniq_followed_pool_post"
),
]
def __str__(self):
return f"#{self.post_id} for {self.user}"
class FollowCloud(models.Model):
"""Cached blacklisted-tag cloud for the Followed page, built in a thread."""
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name="follow_cloud",
)
data = models.JSONField(null=True, blank=True)
computed_at = models.DateTimeField(null=True, blank=True)
building = models.BooleanField(default=False)
building_since = models.DateTimeField(null=True, blank=True)
def __str__(self):
return f"Follow cloud for {self.user}"