2022-11-18 02:21:00 +00:00
|
|
|
from functools import partial
|
|
|
|
|
2022-11-17 06:00:10 +00:00
|
|
|
from django.db import models
|
|
|
|
|
2022-11-18 02:21:00 +00:00
|
|
|
from core.uploads import upload_namer
|
2022-12-28 18:57:54 +00:00
|
|
|
from core.uris import ProxyAbsoluteUrl, RelativeAbsoluteUrl
|
2022-11-17 06:00:10 +00:00
|
|
|
from stator.models import State, StateField, StateGraph, StatorModel
|
|
|
|
|
|
|
|
|
|
|
|
class PostAttachmentStates(StateGraph):
|
2023-05-15 22:59:34 +00:00
|
|
|
new = State(externally_progressed=True)
|
2022-11-17 06:00:10 +00:00
|
|
|
fetched = State()
|
|
|
|
|
|
|
|
new.transitions_to(fetched)
|
|
|
|
|
|
|
|
|
|
|
|
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-02 01:46:49 +00:00
|
|
|
blank=True,
|
|
|
|
null=True,
|
2022-11-17 06:00:10 +00:00
|
|
|
)
|
2023-03-12 22:19:40 +00:00
|
|
|
author = models.ForeignKey(
|
|
|
|
"users.Identity",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="attachments",
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
)
|
2022-11-17 06:00:10 +00:00
|
|
|
|
|
|
|
state = StateField(graph=PostAttachmentStates)
|
|
|
|
|
|
|
|
mimetype = models.CharField(max_length=200)
|
|
|
|
|
2022-12-02 01:46:49 +00:00
|
|
|
# Files may not be populated if it's remote and not cached on our side yet
|
2022-11-18 02:21:00 +00:00
|
|
|
file = models.FileField(
|
2022-12-02 01:46:49 +00: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-18 02:21:00 +00:00
|
|
|
)
|
2022-11-17 06:00:10 +00: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-02 01:46:49 +00:00
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
updated = models.DateTimeField(auto_now=True)
|
|
|
|
|
2022-11-17 06:00:10 +00:00
|
|
|
def is_image(self):
|
|
|
|
return self.mimetype in [
|
|
|
|
"image/apng",
|
|
|
|
"image/avif",
|
|
|
|
"image/gif",
|
|
|
|
"image/jpeg",
|
|
|
|
"image/png",
|
|
|
|
"image/webp",
|
|
|
|
]
|
2022-12-02 01:46:49 +00:00
|
|
|
|
2022-12-24 05:54:03 +00:00
|
|
|
def is_video(self):
|
2023-01-15 21:49:04 +00:00
|
|
|
return self.mimetype in [
|
|
|
|
"video/mp4",
|
|
|
|
"video/ogg",
|
|
|
|
"video/webm",
|
|
|
|
]
|
2022-12-24 05:54:03 +00:00
|
|
|
|
2022-12-12 14:22:11 +00:00
|
|
|
def thumbnail_url(self) -> RelativeAbsoluteUrl:
|
2022-12-02 01:46:49 +00:00
|
|
|
if self.thumbnail:
|
2022-12-12 14:22:11 +00:00
|
|
|
return RelativeAbsoluteUrl(self.thumbnail.url)
|
2022-12-02 01:46:49 +00:00
|
|
|
elif self.file:
|
2022-12-12 14:22:11 +00:00
|
|
|
return RelativeAbsoluteUrl(self.file.url)
|
2022-12-02 01:46:49 +00:00
|
|
|
else:
|
2022-12-28 18:57:54 +00:00
|
|
|
return ProxyAbsoluteUrl(
|
|
|
|
f"/proxy/post_attachment/{self.pk}/",
|
|
|
|
remote_url=self.remote_url,
|
2022-12-28 18:39:40 +00:00
|
|
|
)
|
2022-12-02 01:46:49 +00:00
|
|
|
|
|
|
|
def full_url(self):
|
|
|
|
if self.file:
|
2022-12-12 14:22:11 +00:00
|
|
|
return RelativeAbsoluteUrl(self.file.url)
|
2023-01-15 21:49:04 +00:00
|
|
|
if self.is_image():
|
2022-12-28 18:57:54 +00:00
|
|
|
return ProxyAbsoluteUrl(
|
|
|
|
f"/proxy/post_attachment/{self.pk}/",
|
|
|
|
remote_url=self.remote_url,
|
2022-12-28 18:39:40 +00:00
|
|
|
)
|
2023-01-15 21:49:04 +00:00
|
|
|
return RelativeAbsoluteUrl(self.remote_url)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def file_display_name(self):
|
|
|
|
if self.remote_url:
|
|
|
|
return self.remote_url.rsplit("/", 1)[-1]
|
|
|
|
if self.file:
|
|
|
|
return self.file.name
|
|
|
|
return f"attachment ({self.mimetype})"
|
2022-12-02 01:46:49 +00: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-18 00:48:33 +00:00
|
|
|
"blurhash": self.blurhash,
|
2022-12-02 01:46:49 +00:00
|
|
|
}
|
2022-12-11 07:25:48 +00:00
|
|
|
|
|
|
|
### Mastodon Client API ###
|
|
|
|
|
|
|
|
def to_mastodon_json(self):
|
2023-04-02 00:07:38 +00:00
|
|
|
type_ = "unknown"
|
|
|
|
if self.is_image():
|
|
|
|
type_ = "image"
|
|
|
|
elif self.is_video():
|
|
|
|
type_ = "video"
|
2022-12-17 22:45:31 +00:00
|
|
|
value = {
|
2022-12-11 07:25:48 +00:00
|
|
|
"id": self.pk,
|
2023-04-02 00:07:38 +00:00
|
|
|
"type": type_,
|
2022-12-12 14:22:11 +00:00
|
|
|
"url": self.full_url().absolute,
|
|
|
|
"preview_url": self.thumbnail_url().absolute,
|
2022-12-11 07:25:48 +00:00
|
|
|
"remote_url": None,
|
|
|
|
"meta": {
|
|
|
|
"focus": {
|
|
|
|
"x": self.focal_x or 0,
|
|
|
|
"y": self.focal_y or 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"description": self.name,
|
|
|
|
"blurhash": self.blurhash,
|
|
|
|
}
|
2022-12-17 22:45:31 +00:00
|
|
|
if self.width and self.height:
|
|
|
|
value["meta"]["original"] = {
|
|
|
|
"width": self.width,
|
|
|
|
"height": self.height,
|
|
|
|
"size": f"{self.width}x{self.height}",
|
|
|
|
"aspect": self.width / self.height,
|
|
|
|
}
|
|
|
|
return value
|