From 3e062aed360ca54c26733b175d00d0d4671f3591 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Sun, 11 Dec 2022 00:25:48 -0700 Subject: Timelines working --- activities/models/post.py | 47 ++++++++++++++++++++++++++++++++++++ activities/models/post_attachment.py | 30 +++++++++++++++++++++-- 2 files changed, 75 insertions(+), 2 deletions(-) (limited to 'activities') diff --git a/activities/models/post.py b/activities/models/post.py index b0c89ac..1e372c2 100644 --- a/activities/models/post.py +++ b/activities/models/post.py @@ -708,3 +708,50 @@ class Post(StatorModel): canonicalise(response.json(), include_security=True), update=True, ) + + ### Mastodon API ### + + def to_mastodon_json(self): + reply_parent = None + if self.in_reply_to: + reply_parent = Post.objects.filter(object_uri=self.in_reply_to).first() + return { + "id": self.pk, + "uri": self.object_uri, + "created_at": format_ld_date(self.published), + "account": self.author.to_mastodon_json(), + "content": self.safe_content_remote(), + "visibility": "public", + "sensitive": self.sensitive, + "spoiler_text": self.summary or "", + "media_attachments": [ + attachment.to_mastodon_json() for attachment in self.attachments.all() + ], + "mentions": [ + { + "id": mention.id, + "username": mention.username, + "url": mention.absolute_profile_uri(), + "acct": mention.handle, + } + for mention in self.mentions.all() + ], + "tags": ( + [{"name": tag, "url": "/tag/{tag}/"} for tag in self.hashtags] + if self.hashtags + else [] + ), + "emojis": [], + "reblogs_count": self.interactions.filter(type="boost").count(), + "favourites_count": self.interactions.filter(type="like").count(), + "replies_count": 0, + "url": self.absolute_object_uri(), + "in_reply_to_id": reply_parent.pk if reply_parent else None, + "in_reply_to_account_id": reply_parent.author.pk if reply_parent else None, + "reblog": None, + "poll": None, + "card": None, + "language": None, + "text": self.safe_content_plain(), + "edited_at": format_ld_date(self.edited) if self.edited else None, + } diff --git a/activities/models/post_attachment.py b/activities/models/post_attachment.py index 120a1d1..7b1ad6b 100644 --- a/activities/models/post_attachment.py +++ b/activities/models/post_attachment.py @@ -1,5 +1,6 @@ from functools import partial +from django.conf import settings from django.db import models from core.uploads import upload_namer @@ -77,13 +78,13 @@ class PostAttachment(StatorModel): elif self.file: return self.file.url else: - return f"/proxy/post_attachment/{self.pk}/" + return f"https://{settings.MAIN_DOMAIN}/proxy/post_attachment/{self.pk}/" def full_url(self): if self.file: return self.file.url else: - return f"/proxy/post_attachment/{self.pk}/" + return f"https://{settings.MAIN_DOMAIN}/proxy/post_attachment/{self.pk}/" ### ActivityPub ### @@ -97,3 +98,28 @@ class PostAttachment(StatorModel): "mediaType": self.mimetype, "http://joinmastodon.org/ns#focalPoint": [0, 0], } + + ### Mastodon Client API ### + + def to_mastodon_json(self): + return { + "id": self.pk, + "type": "image" if self.is_image() else "unknown", + "url": self.full_url(), + "preview_url": self.thumbnail_url(), + "remote_url": None, + "meta": { + "original": { + "width": self.width, + "height": self.height, + "size": f"{self.width}x{self.height}", + "aspect": self.width / self.height, + }, + "focus": { + "x": self.focal_x or 0, + "y": self.focal_y or 0, + }, + }, + "description": self.name, + "blurhash": self.blurhash, + } -- cgit v1.2.3