2020-02-11 23:17:21 +00:00
|
|
|
''' models for storing different kinds of Activities '''
|
2020-03-30 00:40:51 +00:00
|
|
|
from django.utils import timezone
|
2020-02-11 23:17:21 +00:00
|
|
|
from django.core.validators import MaxValueValidator, MinValueValidator
|
|
|
|
from django.db import models
|
|
|
|
from model_utils.managers import InheritanceManager
|
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import activitypub
|
2020-09-17 20:09:11 +00:00
|
|
|
from .base_model import ActivitypubMixin, OrderedCollectionPageMixin
|
2020-10-30 18:21:02 +00:00
|
|
|
from .base_model import ActivityMapping, BookWyrmModel, PrivacyLevels
|
2020-02-11 23:17:21 +00:00
|
|
|
|
2020-02-17 03:17:11 +00:00
|
|
|
|
2020-09-21 15:16:34 +00:00
|
|
|
class Status(OrderedCollectionPageMixin, BookWyrmModel):
|
2020-02-17 02:22:01 +00:00
|
|
|
''' any post, like a reply to a review, etc '''
|
2020-02-15 19:13:49 +00:00
|
|
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
2020-02-21 01:33:50 +00:00
|
|
|
content = models.TextField(blank=True, null=True)
|
2020-02-17 02:45:25 +00:00
|
|
|
mention_users = models.ManyToManyField('User', related_name='mention_user')
|
2020-04-22 13:53:22 +00:00
|
|
|
mention_books = models.ManyToManyField(
|
|
|
|
'Edition', related_name='mention_book')
|
2020-02-15 22:38:46 +00:00
|
|
|
local = models.BooleanField(default=True)
|
2020-10-26 21:33:02 +00:00
|
|
|
privacy = models.CharField(
|
|
|
|
max_length=255,
|
|
|
|
default='public',
|
|
|
|
choices=PrivacyLevels.choices
|
|
|
|
)
|
2020-02-17 02:22:01 +00:00
|
|
|
sensitive = models.BooleanField(default=False)
|
2020-03-07 21:29:57 +00:00
|
|
|
# the created date can't be this, because of receiving federated posts
|
2020-03-30 00:40:51 +00:00
|
|
|
published_date = models.DateTimeField(default=timezone.now)
|
2020-10-08 19:32:45 +00:00
|
|
|
deleted = models.BooleanField(default=False)
|
2020-10-21 01:50:39 +00:00
|
|
|
deleted_date = models.DateTimeField(blank=True, null=True)
|
2020-02-19 07:26:42 +00:00
|
|
|
favorites = models.ManyToManyField(
|
|
|
|
'User',
|
|
|
|
symmetrical=False,
|
|
|
|
through='Favorite',
|
|
|
|
through_fields=('status', 'user'),
|
|
|
|
related_name='user_favorites'
|
|
|
|
)
|
2020-02-15 19:13:49 +00:00
|
|
|
reply_parent = models.ForeignKey(
|
|
|
|
'self',
|
|
|
|
null=True,
|
|
|
|
on_delete=models.PROTECT
|
|
|
|
)
|
|
|
|
objects = InheritanceManager()
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
# ---- activitypub serialization settings for this model ----- #
|
2020-05-10 01:55:45 +00:00
|
|
|
@property
|
2020-09-17 20:02:52 +00:00
|
|
|
def ap_to(self):
|
|
|
|
''' should be related to post privacy I think '''
|
|
|
|
return ['https://www.w3.org/ns/activitystreams#Public']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ap_cc(self):
|
|
|
|
''' should be related to post privacy I think '''
|
|
|
|
return [self.user.ap_followers]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def ap_replies(self):
|
|
|
|
''' structured replies block '''
|
|
|
|
return self.to_replies()
|
|
|
|
|
2020-10-31 00:04:10 +00:00
|
|
|
@property
|
|
|
|
def ap_tag(self):
|
2020-11-01 18:13:51 +00:00
|
|
|
''' references to books and/or users '''
|
2020-11-01 19:09:16 +00:00
|
|
|
|
2020-10-31 00:04:10 +00:00
|
|
|
tags = []
|
|
|
|
for book in self.mention_books.all():
|
|
|
|
tags.append(activitypub.Link(
|
|
|
|
href=book.local_id,
|
|
|
|
name=book.title,
|
|
|
|
type='Book'
|
|
|
|
))
|
2020-11-01 18:13:51 +00:00
|
|
|
for user in self.mention_users.all():
|
|
|
|
tags.append(activitypub.Mention(
|
|
|
|
href=user.remote_id,
|
|
|
|
name=user.username,
|
|
|
|
))
|
2020-10-31 00:04:10 +00:00
|
|
|
return tags
|
|
|
|
|
2020-11-07 18:56:03 +00:00
|
|
|
@property
|
|
|
|
def ap_status_image(self):
|
|
|
|
''' attach a book cover, if relevent '''
|
|
|
|
if hasattr(self, 'book'):
|
|
|
|
return self.book.ap_cover
|
|
|
|
if self.mention_books.first():
|
|
|
|
return self.mention_books.first().ap_cover
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
shared_mappings = [
|
2020-11-05 00:28:32 +00:00
|
|
|
ActivityMapping('url', 'remote_id', lambda x: None),
|
2020-11-04 23:23:52 +00:00
|
|
|
ActivityMapping('id', 'remote_id'),
|
2020-09-17 20:02:52 +00:00
|
|
|
ActivityMapping('inReplyTo', 'reply_parent'),
|
2020-10-30 22:22:20 +00:00
|
|
|
ActivityMapping('published', 'published_date'),
|
2020-09-17 20:02:52 +00:00
|
|
|
ActivityMapping('attributedTo', 'user'),
|
|
|
|
ActivityMapping('to', 'ap_to'),
|
|
|
|
ActivityMapping('cc', 'ap_cc'),
|
|
|
|
ActivityMapping('replies', 'ap_replies'),
|
2020-10-31 00:04:10 +00:00
|
|
|
ActivityMapping('tag', 'ap_tag'),
|
2020-09-17 20:02:52 +00:00
|
|
|
]
|
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
# serializing to bookwyrm expanded activitypub
|
2020-09-17 20:02:52 +00:00
|
|
|
activity_mappings = shared_mappings + [
|
|
|
|
ActivityMapping('name', 'name'),
|
|
|
|
ActivityMapping('inReplyToBook', 'book'),
|
|
|
|
ActivityMapping('rating', 'rating'),
|
|
|
|
ActivityMapping('quote', 'quote'),
|
|
|
|
ActivityMapping('content', 'content'),
|
|
|
|
]
|
|
|
|
|
|
|
|
# for serializing to standard activitypub without extended types
|
|
|
|
pure_activity_mappings = shared_mappings + [
|
2020-09-29 19:00:10 +00:00
|
|
|
ActivityMapping('name', 'ap_pure_name'),
|
2020-09-17 20:02:52 +00:00
|
|
|
ActivityMapping('content', 'ap_pure_content'),
|
2020-11-07 18:56:03 +00:00
|
|
|
ActivityMapping('attachment', 'ap_status_image'),
|
2020-09-17 20:02:52 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
activity_serializer = activitypub.Note
|
|
|
|
|
|
|
|
#----- replies collection activitypub ----#
|
|
|
|
@classmethod
|
|
|
|
def replies(cls, status):
|
|
|
|
''' load all replies to a status. idk if there's a better way
|
|
|
|
to write this so it's just a property '''
|
|
|
|
return cls.objects.filter(reply_parent=status).select_subclasses()
|
|
|
|
|
2020-09-28 22:57:31 +00:00
|
|
|
@property
|
|
|
|
def status_type(self):
|
|
|
|
''' expose the type of status for the ui using activity type '''
|
|
|
|
return self.activity_serializer.__name__
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
def to_replies(self, **kwargs):
|
|
|
|
''' helper function for loading AP serialized replies to a status '''
|
|
|
|
return self.to_ordered_collection(
|
|
|
|
self.replies(self),
|
|
|
|
remote_id='%s/replies' % self.remote_id,
|
|
|
|
**kwargs
|
|
|
|
)
|
2020-05-10 01:55:45 +00:00
|
|
|
|
2020-11-01 16:54:10 +00:00
|
|
|
def to_activity(self, pure=False):
|
2020-10-08 19:32:45 +00:00
|
|
|
''' return tombstone if the status is deleted '''
|
|
|
|
if self.deleted:
|
|
|
|
return activitypub.Tombstone(
|
|
|
|
id=self.remote_id,
|
|
|
|
url=self.remote_id,
|
2020-10-30 22:22:20 +00:00
|
|
|
deleted=self.deleted_date.isoformat(),
|
|
|
|
published=self.deleted_date.isoformat()
|
2020-10-08 19:32:45 +00:00
|
|
|
).serialize()
|
2020-11-01 16:54:10 +00:00
|
|
|
return ActivitypubMixin.to_activity(self, pure=pure)
|
2020-10-08 19:32:45 +00:00
|
|
|
|
2020-11-01 17:16:49 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2020-11-05 00:28:32 +00:00
|
|
|
''' update user active time '''
|
2020-11-01 17:16:49 +00:00
|
|
|
self.user.last_active_date = timezone.now()
|
|
|
|
self.user.save()
|
|
|
|
super().save(*args, **kwargs)
|
2020-10-08 19:32:45 +00:00
|
|
|
|
|
|
|
|
2020-10-30 22:22:20 +00:00
|
|
|
class GeneratedNote(Status):
|
2020-09-29 00:26:15 +00:00
|
|
|
''' these are app-generated messages about user activity '''
|
|
|
|
@property
|
|
|
|
def ap_pure_content(self):
|
|
|
|
''' indicate the book in question for mastodon (or w/e) users '''
|
|
|
|
message = self.content
|
|
|
|
books = ', '.join(
|
|
|
|
'<a href="%s">"%s"</a>' % (self.book.local_id, self.book.title) \
|
2020-10-08 19:32:45 +00:00
|
|
|
for book in self.mention_books.all()
|
2020-09-29 00:26:15 +00:00
|
|
|
)
|
|
|
|
return '%s %s' % (message, books)
|
|
|
|
|
|
|
|
activity_serializer = activitypub.GeneratedNote
|
|
|
|
pure_activity_serializer = activitypub.Note
|
|
|
|
|
2020-05-10 01:55:45 +00:00
|
|
|
|
2020-03-21 23:50:49 +00:00
|
|
|
class Comment(Status):
|
|
|
|
''' like a review but without a rating and transient '''
|
2020-03-30 21:12:18 +00:00
|
|
|
book = models.ForeignKey('Edition', on_delete=models.PROTECT)
|
2020-03-21 23:50:49 +00:00
|
|
|
|
2020-05-10 01:55:45 +00:00
|
|
|
@property
|
2020-09-17 20:02:52 +00:00
|
|
|
def ap_pure_content(self):
|
|
|
|
''' indicate the book in question for mastodon (or w/e) users '''
|
|
|
|
return self.content + '<br><br>(comment on <a href="%s">"%s"</a>)' % \
|
|
|
|
(self.book.local_id, self.book.title)
|
|
|
|
|
|
|
|
activity_serializer = activitypub.Comment
|
|
|
|
pure_activity_serializer = activitypub.Note
|
2020-05-10 01:55:45 +00:00
|
|
|
|
|
|
|
|
2020-04-08 16:40:47 +00:00
|
|
|
class Quotation(Status):
|
|
|
|
''' like a review but without a rating and transient '''
|
|
|
|
quote = models.TextField()
|
2020-09-17 20:02:52 +00:00
|
|
|
book = models.ForeignKey('Edition', on_delete=models.PROTECT)
|
2020-03-21 23:50:49 +00:00
|
|
|
|
2020-05-10 01:55:45 +00:00
|
|
|
@property
|
2020-09-17 20:02:52 +00:00
|
|
|
def ap_pure_content(self):
|
|
|
|
''' indicate the book in question for mastodon (or w/e) users '''
|
2020-11-04 22:01:28 +00:00
|
|
|
return '"%s"<br>-- <a href="%s">"%s"</a><br><br>%s' % (
|
2020-09-17 20:02:52 +00:00
|
|
|
self.quote,
|
|
|
|
self.book.local_id,
|
|
|
|
self.book.title,
|
|
|
|
self.content,
|
|
|
|
)
|
|
|
|
|
|
|
|
activity_serializer = activitypub.Quotation
|
2020-09-29 19:00:10 +00:00
|
|
|
pure_activity_serializer = activitypub.Note
|
2020-05-10 01:55:45 +00:00
|
|
|
|
|
|
|
|
2020-02-15 19:13:49 +00:00
|
|
|
class Review(Status):
|
|
|
|
''' a book review '''
|
2020-04-03 19:43:49 +00:00
|
|
|
name = models.CharField(max_length=255, null=True)
|
2020-03-30 21:12:18 +00:00
|
|
|
book = models.ForeignKey('Edition', on_delete=models.PROTECT)
|
2020-02-15 19:13:49 +00:00
|
|
|
rating = models.IntegerField(
|
2020-04-01 18:22:22 +00:00
|
|
|
default=None,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
validators=[MinValueValidator(1), MaxValueValidator(5)]
|
2020-02-15 19:13:49 +00:00
|
|
|
)
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
@property
|
|
|
|
def ap_pure_name(self):
|
|
|
|
''' clarify review names for mastodon serialization '''
|
2020-10-26 22:10:32 +00:00
|
|
|
if self.rating:
|
|
|
|
return 'Review of "%s" (%d stars): %s' % (
|
|
|
|
self.book.title,
|
|
|
|
self.rating,
|
|
|
|
self.name
|
|
|
|
)
|
|
|
|
return 'Review of "%s": %s' % (
|
2020-09-17 20:02:52 +00:00
|
|
|
self.book.title,
|
|
|
|
self.name
|
|
|
|
)
|
2020-03-07 21:29:57 +00:00
|
|
|
|
2020-05-10 01:55:45 +00:00
|
|
|
@property
|
2020-09-17 20:02:52 +00:00
|
|
|
def ap_pure_content(self):
|
|
|
|
''' indicate the book in question for mastodon (or w/e) users '''
|
|
|
|
return self.content + '<br><br>(<a href="%s">"%s"</a>)' % \
|
|
|
|
(self.book.local_id, self.book.title)
|
|
|
|
|
|
|
|
activity_serializer = activitypub.Review
|
2020-09-29 19:00:10 +00:00
|
|
|
pure_activity_serializer = activitypub.Article
|
2020-05-10 01:55:45 +00:00
|
|
|
|
|
|
|
|
2020-09-21 15:16:34 +00:00
|
|
|
class Favorite(ActivitypubMixin, BookWyrmModel):
|
2020-02-19 07:26:42 +00:00
|
|
|
''' fav'ing a post '''
|
|
|
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
|
|
|
status = models.ForeignKey('Status', on_delete=models.PROTECT)
|
2020-03-21 22:21:27 +00:00
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
# ---- activitypub serialization settings for this model ----- #
|
|
|
|
activity_mappings = [
|
|
|
|
ActivityMapping('id', 'remote_id'),
|
|
|
|
ActivityMapping('actor', 'user'),
|
|
|
|
ActivityMapping('object', 'status'),
|
|
|
|
]
|
|
|
|
|
|
|
|
activity_serializer = activitypub.Like
|
|
|
|
|
2020-11-01 17:16:49 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2020-11-05 00:28:32 +00:00
|
|
|
''' update user active time '''
|
2020-11-01 17:16:49 +00:00
|
|
|
self.user.last_active_date = timezone.now()
|
|
|
|
self.user.save()
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
|
2020-02-19 08:22:55 +00:00
|
|
|
class Meta:
|
2020-09-17 20:02:52 +00:00
|
|
|
''' can't fav things twice '''
|
2020-02-19 08:22:55 +00:00
|
|
|
unique_together = ('user', 'status')
|
|
|
|
|
2020-02-21 06:19:19 +00:00
|
|
|
|
2020-03-30 14:13:32 +00:00
|
|
|
class Boost(Status):
|
|
|
|
''' boost'ing a post '''
|
|
|
|
boosted_status = models.ForeignKey(
|
|
|
|
'Status',
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name="boosters")
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
activity_mappings = [
|
|
|
|
ActivityMapping('id', 'remote_id'),
|
|
|
|
ActivityMapping('actor', 'user'),
|
|
|
|
ActivityMapping('object', 'boosted_status'),
|
|
|
|
]
|
|
|
|
|
2020-09-29 01:25:05 +00:00
|
|
|
activity_serializer = activitypub.Boost
|
2020-04-01 21:55:32 +00:00
|
|
|
|
2020-03-30 14:13:32 +00:00
|
|
|
# This constraint can't work as it would cross tables.
|
|
|
|
# class Meta:
|
|
|
|
# unique_together = ('user', 'boosted_status')
|
|
|
|
|
2020-09-17 20:02:52 +00:00
|
|
|
|
2020-09-21 15:16:34 +00:00
|
|
|
class ReadThrough(BookWyrmModel):
|
2020-04-02 18:05:10 +00:00
|
|
|
''' Store progress through a book in the database. '''
|
|
|
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
|
|
|
book = models.ForeignKey('Book', on_delete=models.PROTECT)
|
|
|
|
pages_read = models.IntegerField(
|
|
|
|
null=True,
|
|
|
|
blank=True)
|
|
|
|
start_date = models.DateTimeField(
|
|
|
|
blank=True,
|
|
|
|
null=True)
|
|
|
|
finish_date = models.DateTimeField(
|
|
|
|
blank=True,
|
|
|
|
null=True)
|
|
|
|
|
2020-11-01 17:16:49 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2020-11-05 00:28:32 +00:00
|
|
|
''' update user active time '''
|
2020-11-01 17:16:49 +00:00
|
|
|
self.user.last_active_date = timezone.now()
|
|
|
|
self.user.save()
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2020-04-02 18:05:10 +00:00
|
|
|
|
2020-03-13 13:38:09 +00:00
|
|
|
NotificationType = models.TextChoices(
|
2020-04-20 16:10:19 +00:00
|
|
|
'NotificationType',
|
2020-11-01 18:13:51 +00:00
|
|
|
'FAVORITE REPLY MENTION TAG FOLLOW FOLLOW_REQUEST BOOST IMPORT')
|
2020-03-13 13:38:09 +00:00
|
|
|
|
2020-09-21 15:16:34 +00:00
|
|
|
class Notification(BookWyrmModel):
|
2020-03-07 22:50:29 +00:00
|
|
|
''' you've been tagged, liked, followed, etc '''
|
|
|
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
|
|
|
related_book = models.ForeignKey(
|
2020-03-30 21:12:18 +00:00
|
|
|
'Edition', on_delete=models.PROTECT, null=True)
|
2020-03-07 22:50:29 +00:00
|
|
|
related_user = models.ForeignKey(
|
|
|
|
'User',
|
|
|
|
on_delete=models.PROTECT, null=True, related_name='related_user')
|
|
|
|
related_status = models.ForeignKey(
|
|
|
|
'Status', on_delete=models.PROTECT, null=True)
|
2020-04-22 11:43:10 +00:00
|
|
|
related_import = models.ForeignKey(
|
|
|
|
'ImportJob', on_delete=models.PROTECT, null=True)
|
2020-03-07 22:50:29 +00:00
|
|
|
read = models.BooleanField(default=False)
|
2020-03-13 13:38:09 +00:00
|
|
|
notification_type = models.CharField(
|
|
|
|
max_length=255, choices=NotificationType.choices)
|
2020-09-17 20:02:52 +00:00
|
|
|
|
2020-03-13 13:38:09 +00:00
|
|
|
class Meta:
|
2020-09-17 20:02:52 +00:00
|
|
|
''' checks if notifcation is in enum list for valid types '''
|
2020-03-13 13:38:09 +00:00
|
|
|
constraints = [
|
|
|
|
models.CheckConstraint(
|
|
|
|
check=models.Q(notification_type__in=NotificationType.values),
|
|
|
|
name="notification_type_valid",
|
|
|
|
)
|
2020-03-07 22:50:29 +00:00
|
|
|
]
|