2021-03-08 16:49:10 +00:00
|
|
|
""" like/fav/star a status """
|
2021-02-10 22:31:39 +00:00
|
|
|
from django.apps import apps
|
2021-01-01 18:46:15 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
from bookwyrm import activitypub
|
2021-02-04 22:36:57 +00:00
|
|
|
from .activitypub_mixin import ActivityMixin
|
2021-02-04 18:47:03 +00:00
|
|
|
from .base_model import BookWyrmModel
|
2021-01-01 18:46:15 +00:00
|
|
|
from . import fields
|
2021-03-07 21:13:16 +00:00
|
|
|
from .status import Status
|
2021-01-01 18:46:15 +00:00
|
|
|
|
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
|
|
|
|
2021-01-01 18:46:15 +00:00
|
|
|
user = fields.ForeignKey(
|
2021-03-08 16:49:10 +00:00
|
|
|
"User", on_delete=models.PROTECT, activitypub_field="actor"
|
|
|
|
)
|
2021-01-01 18:46:15 +00:00
|
|
|
status = fields.ForeignKey(
|
2021-03-08 16:49:10 +00:00
|
|
|
"Status", on_delete=models.PROTECT, activitypub_field="object"
|
|
|
|
)
|
2021-01-01 18:46:15 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-01-01 18:46:15 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""update user active time"""
|
2021-01-01 18:46:15 +00:00
|
|
|
self.user.last_active_date = timezone.now()
|
2021-08-03 16:12:22 +00:00
|
|
|
self.user.save(broadcast=False, update_fields=["last_active_date"])
|
2021-01-01 18:46:15 +00:00
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2021-02-10 22:31:39 +00:00
|
|
|
if self.status.user.local and self.status.user != self.user:
|
|
|
|
notification_model = apps.get_model(
|
2021-03-08 16:49:10 +00:00
|
|
|
"bookwyrm.Notification", require_ready=True
|
|
|
|
)
|
2021-02-10 22:31:39 +00:00
|
|
|
notification_model.objects.create(
|
|
|
|
user=self.status.user,
|
2021-03-08 16:49:10 +00:00
|
|
|
notification_type="FAVORITE",
|
2021-02-10 22:31:39 +00:00
|
|
|
related_user=self.user,
|
2021-03-08 16:49:10 +00:00
|
|
|
related_status=self.status,
|
2021-02-10 22:31:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def delete(self, *args, **kwargs):
|
2021-04-26 16:15:42 +00:00
|
|
|
"""delete and delete notifications"""
|
2021-02-10 22:31:39 +00:00
|
|
|
# check for notification
|
|
|
|
if self.status.user.local:
|
|
|
|
notification_model = apps.get_model(
|
2021-03-08 16:49:10 +00:00
|
|
|
"bookwyrm.Notification", require_ready=True
|
|
|
|
)
|
2021-02-10 22:31:39 +00:00
|
|
|
notification = notification_model.objects.filter(
|
2021-03-08 16:49:10 +00:00
|
|
|
user=self.status.user,
|
|
|
|
related_user=self.user,
|
|
|
|
related_status=self.status,
|
|
|
|
notification_type="FAVORITE",
|
2021-02-10 22:31:39 +00:00
|
|
|
).first()
|
|
|
|
if notification:
|
|
|
|
notification.delete()
|
|
|
|
super().delete(*args, **kwargs)
|
|
|
|
|
2021-01-01 18:46:15 +00:00
|
|
|
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")
|