takahe/activities/models/post_attachment.py

126 lines
3.5 KiB
Python
Raw Normal View History

from functools import partial
2022-12-10 23:25:48 -08:00
from django.conf import settings
2022-11-16 22:00:10 -08:00
from django.db import models
from core.uploads import upload_namer
2022-11-16 22:00:10 -08:00
from stator.models import State, StateField, StateGraph, StatorModel
class PostAttachmentStates(StateGraph):
new = State(try_interval=30000)
fetched = State()
new.transitions_to(fetched)
@classmethod
async def handle_new(cls, instance):
# TODO: Fetch images to our own media storage
pass
class PostAttachment(StatorModel):
"""
An attachment to a Post. Could be an image, a video, etc.
"""
post = models.ForeignKey(
"activities.post",
on_delete=models.CASCADE,
related_name="attachments",
2022-12-01 17:46:49 -08:00
blank=True,
null=True,
2022-11-16 22:00:10 -08:00
)
state = StateField(graph=PostAttachmentStates)
mimetype = models.CharField(max_length=200)
2022-12-01 17:46:49 -08:00
# Files may not be populated if it's remote and not cached on our side yet
file = models.FileField(
2022-12-01 17:46:49 -08:00
upload_to=partial(upload_namer, "attachments"),
null=True,
blank=True,
)
thumbnail = models.ImageField(
upload_to=partial(upload_namer, "attachment_thumbnails"),
null=True,
blank=True,
)
2022-11-16 22:00:10 -08:00
remote_url = models.CharField(max_length=500, null=True, blank=True)
# This is the description for images, at least
name = models.TextField(null=True, blank=True)
width = models.IntegerField(null=True, blank=True)
height = models.IntegerField(null=True, blank=True)
focal_x = models.IntegerField(null=True, blank=True)
focal_y = models.IntegerField(null=True, blank=True)
blurhash = models.TextField(null=True, blank=True)
2022-12-01 17:46:49 -08:00
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
2022-11-16 22:00:10 -08:00
def is_image(self):
return self.mimetype in [
"image/apng",
"image/avif",
"image/gif",
"image/jpeg",
"image/png",
"image/webp",
]
2022-12-01 17:46:49 -08:00
def thumbnail_url(self):
if self.thumbnail:
return self.thumbnail.url
elif self.file:
return self.file.url
else:
2022-12-10 23:25:48 -08:00
return f"https://{settings.MAIN_DOMAIN}/proxy/post_attachment/{self.pk}/"
2022-12-01 17:46:49 -08:00
def full_url(self):
if self.file:
return self.file.url
else:
2022-12-10 23:25:48 -08:00
return f"https://{settings.MAIN_DOMAIN}/proxy/post_attachment/{self.pk}/"
2022-12-01 17:46:49 -08:00
### ActivityPub ###
def to_ap(self):
return {
"url": self.file.url,
"name": self.name,
"type": "Document",
"width": self.width,
"height": self.height,
"mediaType": self.mimetype,
2022-12-10 20:03:14 -08:00
"http://joinmastodon.org/ns#focalPoint": [0, 0],
2022-12-01 17:46:49 -08:00
}
2022-12-10 23:25:48 -08:00
### 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,
}