moviewyrm/bookwyrm/models/favorite.py

37 lines
1,010 B
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" like/fav/star a status """
from django.db import models
from bookwyrm import activitypub
2021-02-04 22:36:57 +00:00
from .activitypub_mixin import ActivityMixin
from .base_model import BookWyrmModel
from . import fields
2021-03-07 21:13:16 +00:00
from .status import Status
2021-03-08 16:49:10 +00:00
2021-02-04 22:36:57 +00:00
class Favorite(ActivityMixin, BookWyrmModel):
2021-04-26 16:15:42 +00:00
"""fav'ing a post"""
2021-03-08 16:49:10 +00:00
user = fields.ForeignKey(
2021-03-08 16:49:10 +00:00
"User", on_delete=models.PROTECT, activitypub_field="actor"
)
status = fields.ForeignKey(
2021-03-08 16:49:10 +00:00
"Status", on_delete=models.PROTECT, activitypub_field="object"
)
activity_serializer = activitypub.Like
2021-03-07 17:42:31 +00:00
@classmethod
def ignore_activity(cls, activity):
2021-04-26 16:15:42 +00:00
"""don't bother with incoming favs of unknown statuses"""
2021-03-07 21:13:16 +00:00
return not Status.objects.filter(remote_id=activity.object).exists()
2021-03-07 17:42:31 +00:00
def save(self, *args, **kwargs):
2021-04-26 16:15:42 +00:00
"""update user active time"""
self.user.update_active_date()
super().save(*args, **kwargs)
class Meta:
2021-04-26 16:15:42 +00:00
"""can't fav things twice"""
2021-03-08 16:49:10 +00:00
unique_together = ("user", "status")