Formatter for converting model images to AP Images
Replaces reduntant properties on user and book models
This commit is contained in:
parent
dab0aeffb2
commit
5526b4773e
|
@ -261,3 +261,25 @@ def tag_formatter(items, name_field, activity_type):
|
||||||
type=activity_type
|
type=activity_type
|
||||||
))
|
))
|
||||||
return tags
|
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
|
||||||
|
|
|
@ -12,6 +12,7 @@ from bookwyrm.utils.fields import ArrayField
|
||||||
|
|
||||||
from .base_model import ActivityMapping, BookWyrmModel
|
from .base_model import ActivityMapping, BookWyrmModel
|
||||||
from .base_model import ActivitypubMixin, OrderedCollectionPageMixin
|
from .base_model import ActivitypubMixin, OrderedCollectionPageMixin
|
||||||
|
from .base_model import image_attachments_formatter
|
||||||
|
|
||||||
class Book(ActivitypubMixin, BookWyrmModel):
|
class Book(ActivitypubMixin, BookWyrmModel):
|
||||||
''' a generic book, which can mean either an edition or a work '''
|
''' 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 '''
|
''' the activitypub serialization should be a list of author ids '''
|
||||||
return [a.remote_id for a in self.authors.all()]
|
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
|
@property
|
||||||
def ap_parent_work(self):
|
def ap_parent_work(self):
|
||||||
''' reference the work via local id not remote '''
|
''' reference the work via local id not remote '''
|
||||||
|
@ -106,7 +98,10 @@ class Book(ActivitypubMixin, BookWyrmModel):
|
||||||
|
|
||||||
ActivityMapping('lccn', 'lccn'),
|
ActivityMapping('lccn', 'lccn'),
|
||||||
ActivityMapping('editions', 'editions_path'),
|
ActivityMapping('editions', 'editions_path'),
|
||||||
ActivityMapping('attachment', 'ap_cover'),
|
ActivityMapping(
|
||||||
|
'attachment', 'cover',
|
||||||
|
image_attachments_formatter
|
||||||
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
|
|
|
@ -10,8 +10,8 @@ from bookwyrm.models.shelf import Shelf
|
||||||
from bookwyrm.models.status import Status
|
from bookwyrm.models.status import Status
|
||||||
from bookwyrm.settings import DOMAIN
|
from bookwyrm.settings import DOMAIN
|
||||||
from bookwyrm.signatures import create_key_pair
|
from bookwyrm.signatures import create_key_pair
|
||||||
from .base_model import OrderedCollectionPageMixin
|
from .base_model import ActivityMapping, OrderedCollectionPageMixin
|
||||||
from .base_model import ActivityMapping
|
from .base_model import image_formatter
|
||||||
|
|
||||||
|
|
||||||
class User(OrderedCollectionPageMixin, AbstractUser):
|
class User(OrderedCollectionPageMixin, AbstractUser):
|
||||||
|
@ -78,16 +78,6 @@ class User(OrderedCollectionPageMixin, AbstractUser):
|
||||||
''' generates url for activitypub followers page '''
|
''' generates url for activitypub followers page '''
|
||||||
return '%s/followers' % self.remote_id
|
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
|
@property
|
||||||
def ap_public_key(self):
|
def ap_public_key(self):
|
||||||
''' format the public key block for activitypub '''
|
''' format the public key block for activitypub '''
|
||||||
|
@ -122,7 +112,10 @@ class User(OrderedCollectionPageMixin, AbstractUser):
|
||||||
activity_formatter=lambda x: {'sharedInbox': x},
|
activity_formatter=lambda x: {'sharedInbox': x},
|
||||||
model_formatter=lambda x: x.get('sharedInbox')
|
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(
|
ActivityMapping(
|
||||||
'manuallyApprovesFollowers',
|
'manuallyApprovesFollowers',
|
||||||
'manually_approves_followers'
|
'manually_approves_followers'
|
||||||
|
|
Loading…
Reference in New Issue