Use social media preview images

This commit is contained in:
Mouse Reeve 2021-11-10 10:58:02 -08:00
parent d61595abb9
commit 717da918cf
2 changed files with 28 additions and 15 deletions

View file

@ -415,7 +415,7 @@ class ImageField(ActivitypubFieldMixin, models.ImageField):
activity[key] = formatted activity[key] = formatted
def field_to_activity(self, value, alt=None): def field_to_activity(self, value, alt=None):
url = self.get_absolute_url(value) url = get_absolute_url(value)
if not url: if not url:
return None return None
@ -456,19 +456,19 @@ class ImageField(ActivitypubFieldMixin, models.ImageField):
} }
) )
# pylint: disable=no-self-use
def get_absolute_url(self, value):
"""returns an absolute URL for the image"""
name = getattr(value, "name")
if not name:
return None
url = filepath_to_uri(name) def get_absolute_url(value):
if url is not None: """returns an absolute URL for the image"""
url = url.lstrip("/") name = getattr(value, "name")
url = urljoin(MEDIA_FULL_URL, url) if not name:
return None
return url url = filepath_to_uri(name)
if url is not None:
url = url.lstrip("/")
url = urljoin(MEDIA_FULL_URL, url)
return url
class DateTimeField(ActivitypubFieldMixin, models.DateTimeField): class DateTimeField(ActivitypubFieldMixin, models.DateTimeField):

View file

@ -190,9 +190,22 @@ class Status(OrderedCollectionPageMixin, BookWyrmModel):
activity.name = self.pure_name activity.name = self.pure_name
activity.type = self.pure_type activity.type = self.pure_type
books = [getattr(self, "book", None)] + list(self.mention_books.all()) books = [getattr(self, "book", None)] + list(self.mention_books.all())
covers = [ if len(books) == 1 and books[0].preview_image:
b.to_activity().get("cover") for b in books if b covers = [
] activitypub.Document(
url=fields.get_absolute_url(books[0].preview_image),
name=books[0].alt_text,
)
]
else:
covers = [
activitypub.Document(
url=fields.get_absolute_url(b.cover),
name=b.alt_text,
)
for b in books
if b and b.cover
]
activity.attachment = covers activity.attachment = covers
return activity return activity