forked from mirrors/bookwyrm
Serialize model user and book tags to activitypub
This commit is contained in:
parent
dfc9262876
commit
73e41d568e
3 changed files with 44 additions and 4 deletions
|
@ -253,7 +253,6 @@ def handle_delete_status(activity):
|
|||
status_builder.delete_status(status)
|
||||
|
||||
|
||||
|
||||
@app.task
|
||||
def handle_favorite(activity):
|
||||
''' approval of your good good post '''
|
||||
|
|
|
@ -72,7 +72,14 @@ class ActivitypubMixin:
|
|||
value = value.remote_id
|
||||
if isinstance(value, datetime):
|
||||
value = value.isoformat()
|
||||
fields[mapping.activity_key] = mapping.activity_formatter(value)
|
||||
result = mapping.activity_formatter(value)
|
||||
if mapping.activity_key in fields and \
|
||||
isinstance(fields[mapping.activity_key], list):
|
||||
# there are two database fields that map to the same AP list
|
||||
# this happens in status, which combines user and book tags
|
||||
fields[mapping.activity_key] += result
|
||||
else:
|
||||
fields[mapping.activity_key] = result
|
||||
|
||||
if pure:
|
||||
return self.pure_activity_serializer(
|
||||
|
|
|
@ -9,6 +9,38 @@ from .base_model import ActivitypubMixin, OrderedCollectionPageMixin
|
|||
from .base_model import ActivityMapping, BookWyrmModel, PrivacyLevels
|
||||
|
||||
|
||||
# --- Formatters ----- #
|
||||
def ap_book_tags(mention_books):
|
||||
''' convert books into tags field '''
|
||||
tags = []
|
||||
for book in mention_books.all():
|
||||
tags.append(activitypub.Link(
|
||||
href=book.remote_id,
|
||||
name=book.title,
|
||||
type='Book'
|
||||
))
|
||||
return tags
|
||||
|
||||
def ap_user_tags(mention_users):
|
||||
''' convert users into tag fields '''
|
||||
tags = []
|
||||
for user in mention_users.all():
|
||||
tags.append(activitypub.Mention(
|
||||
href=user.remote_id,
|
||||
name=user.username,
|
||||
))
|
||||
return tags
|
||||
|
||||
|
||||
def model_book_tags(activity_tags):
|
||||
''' grab the tagged books out of the activity '''
|
||||
pass
|
||||
|
||||
def model_user_tags():
|
||||
''' create user mentions '''
|
||||
pass
|
||||
|
||||
|
||||
class Status(OrderedCollectionPageMixin, BookWyrmModel):
|
||||
''' any post, like a reply to a review, etc '''
|
||||
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
||||
|
@ -60,7 +92,6 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
|
|||
@property
|
||||
def ap_tag(self):
|
||||
''' references to books and/or users '''
|
||||
|
||||
tags = []
|
||||
for book in self.mention_books.all():
|
||||
tags.append(activitypub.Link(
|
||||
|
@ -75,6 +106,7 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
|
|||
))
|
||||
return tags
|
||||
|
||||
|
||||
@property
|
||||
def ap_status_image(self):
|
||||
''' attach a book cover, if relevent '''
|
||||
|
@ -94,7 +126,9 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
|
|||
ActivityMapping('to', 'ap_to'),
|
||||
ActivityMapping('cc', 'ap_cc'),
|
||||
ActivityMapping('replies', 'ap_replies'),
|
||||
ActivityMapping('tag', 'ap_tag'),
|
||||
ActivityMapping('tag', 'mention_books', ap_book_tags, model_book_tags),
|
||||
# since one activitypub field populates two model fields, we do this
|
||||
ActivityMapping('tag', 'mention_users', ap_user_tags, model_user_tags),
|
||||
]
|
||||
|
||||
# serializing to bookwyrm expanded activitypub
|
||||
|
|
Loading…
Reference in a new issue