summaryrefslogtreecommitdiffstats
path: root/activities/models/post_attachment.py
blob: ed507719e750ee5dceac458ce824dc1639d45180 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from functools import partial

from django.db import models

from core.uploads import upload_namer
from core.uris import AutoAbsoluteUrl, RelativeAbsoluteUrl
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",
        blank=True,
        null=True,
    )

    state = StateField(graph=PostAttachmentStates)

    mimetype = models.CharField(max_length=200)

    # Files may not be populated if it's remote and not cached on our side yet
    file = models.FileField(
        upload_to=partial(upload_namer, "attachments"),
        null=True,
        blank=True,
    )
    thumbnail = models.ImageField(
        upload_to=partial(upload_namer, "attachment_thumbnails"),
        null=True,
        blank=True,
    )

    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)

    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def is_image(self):
        return self.mimetype in [
            "image/apng",
            "image/avif",
            "image/gif",
            "image/jpeg",
            "image/png",
            "image/webp",
        ]

    def thumbnail_url(self) -> RelativeAbsoluteUrl:
        if self.thumbnail:
            return RelativeAbsoluteUrl(self.thumbnail.url)
        elif self.file:
            return RelativeAbsoluteUrl(self.file.url)
        else:
            return AutoAbsoluteUrl(f"/proxy/post_attachment/{self.pk}/")

    def full_url(self):
        if self.file:
            return RelativeAbsoluteUrl(self.file.url)
        else:
            return AutoAbsoluteUrl(f"/proxy/post_attachment/{self.pk}/")

    ### ActivityPub ###

    def to_ap(self):
        return {
            "url": self.file.url,
            "name": self.name,
            "type": "Document",
            "width": self.width,
            "height": self.height,
            "mediaType": self.mimetype,
            "toot:focalPoint": [0, 0],
        }

    ### Mastodon Client API ###

    def to_mastodon_json(self):
        value = {
            "id": self.pk,
            "type": "image" if self.is_image() else "unknown",
            "url": self.full_url().absolute,
            "preview_url": self.thumbnail_url().absolute,
            "remote_url": None,
            "meta": {
                "focus": {
                    "x": self.focal_x or 0,
                    "y": self.focal_y or 0,
                },
            },
            "description": self.name,
            "blurhash": self.blurhash,
        }
        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