bookwyrm-mastodon/bookwyrm/models/favorite.py

37 lines
1010 B
Python
Raw Permalink Normal View History

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