Formatter for converting model images to AP Images

Replaces reduntant properties on user and book models
This commit is contained in:
Mouse Reeve 2020-11-23 12:56:05 -08:00
parent dab0aeffb2
commit 5526b4773e
3 changed files with 33 additions and 23 deletions

View file

@ -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

View file

@ -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):

View file

@ -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'