From 5526b4773e94946e94adc066d4bc3ca1843981d5 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 23 Nov 2020 12:56:05 -0800 Subject: [PATCH] Formatter for converting model images to AP Images Replaces reduntant properties on user and book models --- bookwyrm/models/base_model.py | 22 ++++++++++++++++++++++ bookwyrm/models/book.py | 15 +++++---------- bookwyrm/models/user.py | 19 ++++++------------- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/bookwyrm/models/base_model.py b/bookwyrm/models/base_model.py index df6b4f75..2883e97a 100644 --- a/bookwyrm/models/base_model.py +++ b/bookwyrm/models/base_model.py @@ -261,3 +261,25 @@ def tag_formatter(items, name_field, activity_type): type=activity_type )) return tags + + +def image_formatter(image, default_path=None): + ''' convert images into activitypub json ''' + if image: + url = image.url + elif default_path: + url = default_path + else: + return None + url = 'https://%s%s' % (DOMAIN, url) + return activitypub.Image(url=url) + + +def image_attachments_formatter(images): + ''' create a list of image attachemnts ''' + if not isinstance(images, list): + images = [images] + attachments = [] + for image in images: + attachments.append(image_formatter(image)) + return attachments diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index d0702a3e..c4981a54 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -12,6 +12,7 @@ from bookwyrm.utils.fields import ArrayField from .base_model import ActivityMapping, BookWyrmModel from .base_model import ActivitypubMixin, OrderedCollectionPageMixin +from .base_model import image_attachments_formatter class Book(ActivitypubMixin, BookWyrmModel): ''' a generic book, which can mean either an edition or a work ''' @@ -60,15 +61,6 @@ class Book(ActivitypubMixin, BookWyrmModel): ''' the activitypub serialization should be a list of author ids ''' return [a.remote_id for a in self.authors.all()] - @property - def ap_cover(self): - ''' an image attachment ''' - if not self.cover or not hasattr(self.cover, 'url'): - return [] - return [activitypub.Image( - url='https://%s%s' % (DOMAIN, self.cover.url), - )] - @property def ap_parent_work(self): ''' reference the work via local id not remote ''' @@ -106,7 +98,10 @@ class Book(ActivitypubMixin, BookWyrmModel): ActivityMapping('lccn', 'lccn'), ActivityMapping('editions', 'editions_path'), - ActivityMapping('attachment', 'ap_cover'), + ActivityMapping( + 'attachment', 'cover', + image_attachments_formatter + ), ] def save(self, *args, **kwargs): diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index 0c7d1a18..2bea373b 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -10,8 +10,8 @@ from bookwyrm.models.shelf import Shelf from bookwyrm.models.status import Status from bookwyrm.settings import DOMAIN from bookwyrm.signatures import create_key_pair -from .base_model import OrderedCollectionPageMixin -from .base_model import ActivityMapping +from .base_model import ActivityMapping, OrderedCollectionPageMixin +from .base_model import image_formatter class User(OrderedCollectionPageMixin, AbstractUser): @@ -78,16 +78,6 @@ class User(OrderedCollectionPageMixin, AbstractUser): ''' generates url for activitypub followers page ''' return '%s/followers' % self.remote_id - @property - def ap_icon(self): - ''' send default icon if one isn't set ''' - if self.avatar: - url = self.avatar.url - else: - url = '/static/images/default_avi.jpg' - url = 'https://%s%s' % (DOMAIN, url) - return activitypub.Image(url=url) - @property def ap_public_key(self): ''' format the public key block for activitypub ''' @@ -122,7 +112,10 @@ class User(OrderedCollectionPageMixin, AbstractUser): activity_formatter=lambda x: {'sharedInbox': x}, model_formatter=lambda x: x.get('sharedInbox') ), - ActivityMapping('icon', 'ap_icon'), + ActivityMapping( + 'icon', 'avatar', + lambda x: image_formatter(x, '/static/images/default_avi.jpg') + ), ActivityMapping( 'manuallyApprovesFollowers', 'manually_approves_followers'