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}"