summaryrefslogtreecommitdiffstats
path: root/activities/models
diff options
context:
space:
mode:
authorAndrew Godwin2022-12-11 00:25:48 -0700
committerAndrew Godwin2022-12-12 11:56:49 -0700
commit3e062aed360ca54c26733b175d00d0d4671f3591 (patch)
tree6109169ac8886a4e38cf0e9816e56e74417a5ade /activities/models
parent1017c71ba1d80a1690e357a938ad46f246a456ae (diff)
downloadtakahe-3e062aed360ca54c26733b175d00d0d4671f3591.tar.gz
takahe-3e062aed360ca54c26733b175d00d0d4671f3591.tar.bz2
takahe-3e062aed360ca54c26733b175d00d0d4671f3591.zip
Timelines working
Diffstat (limited to 'activities/models')
-rw-r--r--activities/models/post.py47
-rw-r--r--activities/models/post_attachment.py30
2 files changed, 75 insertions, 2 deletions
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,
+ }