mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-01 22:11:16 +00:00
(De)serializers for tag fields
This commit is contained in:
parent
73e41d568e
commit
72b4c150f6
4 changed files with 44 additions and 54 deletions
|
@ -4,7 +4,7 @@ import sys
|
||||||
|
|
||||||
from .base_activity import ActivityEncoder, Image, PublicKey, Signature
|
from .base_activity import ActivityEncoder, Image, PublicKey, Signature
|
||||||
from .base_activity import Link, Mention
|
from .base_activity import Link, Mention
|
||||||
from .base_activity import ActivitySerializerError
|
from .base_activity import ActivitySerializerError, tag_formatter
|
||||||
from .note import Note, GeneratedNote, Article, Comment, Review, Quotation
|
from .note import Note, GeneratedNote, Article, Comment, Review, Quotation
|
||||||
from .note import Tombstone
|
from .note import Tombstone
|
||||||
from .interaction import Boost, Like
|
from .interaction import Boost, Like
|
||||||
|
|
|
@ -145,3 +145,23 @@ def resolve_foreign_key(model, remote_id):
|
||||||
'Could not resolve remote_id in %s model: %s' % \
|
'Could not resolve remote_id in %s model: %s' % \
|
||||||
(model.__name__, remote_id))
|
(model.__name__, remote_id))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def tag_formatter(tags):
|
||||||
|
''' helper function to extract foreign keys from tag activity json '''
|
||||||
|
items = []
|
||||||
|
types = {
|
||||||
|
'Book': models.Book,
|
||||||
|
'Mention': models.User,
|
||||||
|
}
|
||||||
|
for tag in tags:
|
||||||
|
tag_type = tag.get('type')
|
||||||
|
if not tag_type in types:
|
||||||
|
continue
|
||||||
|
remote_id = tag.get('href')
|
||||||
|
try:
|
||||||
|
item = resolve_foreign_key(types[tag_type], remote_id)
|
||||||
|
except ActivitySerializerError:
|
||||||
|
continue
|
||||||
|
items.append(item)
|
||||||
|
return items
|
||||||
|
|
|
@ -249,3 +249,15 @@ class ActivityMapping:
|
||||||
model_key: str
|
model_key: str
|
||||||
activity_formatter: Callable = lambda x: x
|
activity_formatter: Callable = lambda x: x
|
||||||
model_formatter: Callable = lambda x: x
|
model_formatter: Callable = lambda x: x
|
||||||
|
|
||||||
|
|
||||||
|
def tag_formatter(items, name_field, activity_type):
|
||||||
|
''' helper function to format lists of foreign keys into Tags '''
|
||||||
|
tags = []
|
||||||
|
for item in items.all():
|
||||||
|
tags.append(activitypub.Link(
|
||||||
|
href=item.remote_id,
|
||||||
|
name=getattr(item, name_field),
|
||||||
|
type=activity_type
|
||||||
|
))
|
||||||
|
return tags
|
||||||
|
|
|
@ -7,38 +7,7 @@ from model_utils.managers import InheritanceManager
|
||||||
from bookwyrm import activitypub
|
from bookwyrm import activitypub
|
||||||
from .base_model import ActivitypubMixin, OrderedCollectionPageMixin
|
from .base_model import ActivitypubMixin, OrderedCollectionPageMixin
|
||||||
from .base_model import ActivityMapping, BookWyrmModel, PrivacyLevels
|
from .base_model import ActivityMapping, BookWyrmModel, PrivacyLevels
|
||||||
|
from .base_model import tag_formatter
|
||||||
|
|
||||||
# --- 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):
|
class Status(OrderedCollectionPageMixin, BookWyrmModel):
|
||||||
|
@ -89,24 +58,6 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
|
||||||
''' structured replies block '''
|
''' structured replies block '''
|
||||||
return self.to_replies()
|
return self.to_replies()
|
||||||
|
|
||||||
@property
|
|
||||||
def ap_tag(self):
|
|
||||||
''' references to books and/or users '''
|
|
||||||
tags = []
|
|
||||||
for book in self.mention_books.all():
|
|
||||||
tags.append(activitypub.Link(
|
|
||||||
href=book.remote_id,
|
|
||||||
name=book.title,
|
|
||||||
type='Book'
|
|
||||||
))
|
|
||||||
for user in self.mention_users.all():
|
|
||||||
tags.append(activitypub.Mention(
|
|
||||||
href=user.remote_id,
|
|
||||||
name=user.username,
|
|
||||||
))
|
|
||||||
return tags
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def ap_status_image(self):
|
def ap_status_image(self):
|
||||||
''' attach a book cover, if relevent '''
|
''' attach a book cover, if relevent '''
|
||||||
|
@ -126,9 +77,16 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
|
||||||
ActivityMapping('to', 'ap_to'),
|
ActivityMapping('to', 'ap_to'),
|
||||||
ActivityMapping('cc', 'ap_cc'),
|
ActivityMapping('cc', 'ap_cc'),
|
||||||
ActivityMapping('replies', 'ap_replies'),
|
ActivityMapping('replies', 'ap_replies'),
|
||||||
ActivityMapping('tag', 'mention_books', ap_book_tags, model_book_tags),
|
ActivityMapping(
|
||||||
# since one activitypub field populates two model fields, we do this
|
'tag', 'mention_books',
|
||||||
ActivityMapping('tag', 'mention_users', ap_user_tags, model_user_tags),
|
lambda x: tag_formatter(x, 'title', 'Book'),
|
||||||
|
activitypub.tag_formatter
|
||||||
|
),
|
||||||
|
ActivityMapping(
|
||||||
|
'tag', 'mention_users',
|
||||||
|
lambda x: tag_formatter(x, 'username', 'Mention'),
|
||||||
|
activitypub.tag_formatter
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
# serializing to bookwyrm expanded activitypub
|
# serializing to bookwyrm expanded activitypub
|
||||||
|
|
Loading…
Reference in a new issue