2022-12-28 02:41:16 +00:00
|
|
|
import mimetypes
|
2022-12-15 07:50:54 +00:00
|
|
|
from functools import partial
|
2022-12-20 11:39:45 +00:00
|
|
|
from typing import ClassVar
|
2022-12-15 07:50:54 +00:00
|
|
|
|
2022-12-17 22:00:50 +00:00
|
|
|
import httpx
|
2022-12-15 07:50:54 +00:00
|
|
|
import urlman
|
|
|
|
from asgiref.sync import sync_to_async
|
2022-12-20 11:39:45 +00:00
|
|
|
from cachetools import TTLCache, cached
|
2022-12-15 07:50:54 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ValidationError
|
2023-01-14 17:35:20 +00:00
|
|
|
from django.core.files.base import ContentFile
|
2022-12-15 07:50:54 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.safestring import mark_safe
|
2023-02-25 21:47:43 +00:00
|
|
|
from PIL import Image
|
2022-12-15 07:50:54 +00:00
|
|
|
|
|
|
|
from core.files import get_remote_file
|
2023-01-30 00:46:22 +00:00
|
|
|
from core.html import FediverseHtmlParser
|
2022-12-18 00:48:33 +00:00
|
|
|
from core.ld import format_ld_date
|
2022-12-15 07:50:54 +00:00
|
|
|
from core.models import Config
|
|
|
|
from core.uploads import upload_emoji_namer
|
2022-12-28 18:57:54 +00:00
|
|
|
from core.uris import (
|
|
|
|
AutoAbsoluteUrl,
|
|
|
|
ProxyAbsoluteUrl,
|
|
|
|
RelativeAbsoluteUrl,
|
|
|
|
StaticAbsoluteUrl,
|
|
|
|
)
|
2022-12-15 07:50:54 +00:00
|
|
|
from stator.models import State, StateField, StateGraph, StatorModel
|
|
|
|
from users.models import Domain
|
|
|
|
|
|
|
|
|
|
|
|
class EmojiStates(StateGraph):
|
|
|
|
outdated = State(try_interval=300, force_initial=True)
|
|
|
|
updated = State()
|
|
|
|
|
|
|
|
outdated.transitions_to(updated)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def handle_outdated(cls, instance: "Emoji"):
|
|
|
|
"""
|
|
|
|
Fetches remote emoji and uploads to file for local caching
|
|
|
|
"""
|
|
|
|
if instance.remote_url and not instance.file:
|
2022-12-17 22:00:50 +00:00
|
|
|
try:
|
|
|
|
file, mimetype = await get_remote_file(
|
|
|
|
instance.remote_url,
|
|
|
|
timeout=settings.SETUP.REMOTE_TIMEOUT,
|
|
|
|
max_size=settings.SETUP.EMOJI_MAX_IMAGE_FILESIZE_KB * 1024,
|
|
|
|
)
|
|
|
|
except httpx.RequestError:
|
|
|
|
return
|
2023-02-25 21:47:43 +00:00
|
|
|
|
2022-12-15 07:50:54 +00:00
|
|
|
if file:
|
2023-02-25 21:47:43 +00:00
|
|
|
if mimetype == "application/octet-stream":
|
|
|
|
mimetype = Image.open(file).get_format_mimetype()
|
|
|
|
|
2022-12-15 07:50:54 +00:00
|
|
|
instance.file = file
|
|
|
|
instance.mimetype = mimetype
|
|
|
|
await sync_to_async(instance.save)()
|
|
|
|
|
|
|
|
return cls.updated
|
|
|
|
|
|
|
|
|
|
|
|
class EmojiQuerySet(models.QuerySet):
|
|
|
|
def usable(self, domain: Domain | None = None):
|
2022-12-20 11:39:45 +00:00
|
|
|
"""
|
|
|
|
Returns all usable emoji, optionally filtering by domain too.
|
|
|
|
"""
|
|
|
|
visible_q = models.Q(local=True) | models.Q(public=True)
|
|
|
|
if Config.system.emoji_unreviewed_are_public:
|
|
|
|
visible_q |= models.Q(public__isnull=True)
|
2022-12-15 18:56:48 +00:00
|
|
|
qs = self.filter(visible_q)
|
2022-12-20 11:39:45 +00:00
|
|
|
|
2022-12-15 07:50:54 +00:00
|
|
|
if domain:
|
2022-12-15 18:56:48 +00:00
|
|
|
if not domain.local:
|
2022-12-15 07:50:54 +00:00
|
|
|
qs = qs.filter(domain=domain)
|
2022-12-20 11:39:45 +00:00
|
|
|
|
2022-12-15 07:50:54 +00:00
|
|
|
return qs
|
|
|
|
|
|
|
|
|
|
|
|
class EmojiManager(models.Manager):
|
|
|
|
def get_queryset(self):
|
|
|
|
return EmojiQuerySet(self.model, using=self._db)
|
|
|
|
|
|
|
|
def usable(self, domain: Domain | None = None):
|
|
|
|
return self.get_queryset().usable(domain)
|
|
|
|
|
|
|
|
|
|
|
|
class Emoji(StatorModel):
|
|
|
|
|
|
|
|
# Normalized Emoji without the ':'
|
|
|
|
shortcode = models.SlugField(max_length=100, db_index=True)
|
|
|
|
|
|
|
|
domain = models.ForeignKey(
|
|
|
|
"users.Domain", null=True, blank=True, on_delete=models.CASCADE
|
|
|
|
)
|
|
|
|
local = models.BooleanField(default=True)
|
|
|
|
|
|
|
|
# Should this be shown in the public UI?
|
|
|
|
public = models.BooleanField(null=True)
|
|
|
|
|
|
|
|
object_uri = models.CharField(max_length=500, blank=True, null=True, unique=True)
|
|
|
|
|
|
|
|
mimetype = models.CharField(max_length=200)
|
|
|
|
|
|
|
|
# Files may not be populated if it's remote and not cached on our side yet
|
|
|
|
file = models.ImageField(
|
|
|
|
upload_to=partial(upload_emoji_namer, "emoji"),
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
# A link to the custom emoji
|
|
|
|
remote_url = models.CharField(max_length=500, blank=True, null=True)
|
|
|
|
|
|
|
|
# Used for sorting custom emoji in the picker
|
|
|
|
category = models.CharField(max_length=100, blank=True, null=True)
|
|
|
|
|
|
|
|
# State of this Emoji
|
|
|
|
state = StateField(EmojiStates)
|
|
|
|
|
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
updated = models.DateTimeField(auto_now=True)
|
|
|
|
|
|
|
|
objects = EmojiManager()
|
|
|
|
|
|
|
|
# Cache of the local emojis {shortcode: Emoji}
|
|
|
|
locals: ClassVar["dict[str, Emoji]"]
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ("domain", "shortcode")
|
2023-02-04 05:24:28 +00:00
|
|
|
index_together = StatorModel.Meta.index_together
|
2022-12-15 07:50:54 +00:00
|
|
|
|
|
|
|
class urls(urlman.Urls):
|
2023-01-11 03:31:50 +00:00
|
|
|
admin = "/admin/emoji/"
|
|
|
|
admin_create = "{admin}create/"
|
|
|
|
admin_edit = "{admin}{self.pk}/"
|
|
|
|
admin_delete = "{admin}{self.pk}/delete/"
|
|
|
|
admin_enable = "{admin}{self.pk}/enable/"
|
|
|
|
admin_disable = "{admin}{self.pk}/disable/"
|
2023-01-14 17:35:20 +00:00
|
|
|
admin_copy = "{admin}{self.pk}/copy/"
|
2022-12-15 07:50:54 +00:00
|
|
|
|
2023-01-14 17:35:20 +00:00
|
|
|
def delete(self, using=None, keep_parents=False):
|
|
|
|
if self.file:
|
|
|
|
self.file.delete()
|
|
|
|
return super().delete(using=using, keep_parents=keep_parents)
|
|
|
|
|
2022-12-15 07:50:54 +00:00
|
|
|
def clean(self):
|
|
|
|
super().clean()
|
|
|
|
if self.local ^ (self.domain is None):
|
|
|
|
raise ValidationError("Must be local or have a domain")
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"{self.id}-{self.shortcode}"
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def load_locals(cls) -> dict[str, "Emoji"]:
|
|
|
|
return {x.shortcode: x for x in Emoji.objects.usable().filter(local=True)}
|
|
|
|
|
2022-12-20 11:39:45 +00:00
|
|
|
@classmethod
|
|
|
|
@cached(cache=TTLCache(maxsize=1000, ttl=60))
|
2023-01-07 22:19:47 +00:00
|
|
|
def get_by_domain(cls, shortcode, domain: Domain | None) -> "Emoji | None":
|
2022-12-22 16:55:31 +00:00
|
|
|
"""
|
|
|
|
Given an emoji shortcode and optional domain, looks up the single
|
|
|
|
emoji and returns it. Raises Emoji.DoesNotExist if there isn't one.
|
|
|
|
"""
|
2023-01-07 22:19:47 +00:00
|
|
|
try:
|
|
|
|
if domain is None or domain.local:
|
|
|
|
return cls.objects.get(local=True, shortcode=shortcode)
|
|
|
|
else:
|
|
|
|
return cls.objects.get(domain=domain, shortcode=shortcode)
|
|
|
|
except Emoji.DoesNotExist:
|
|
|
|
return None
|
2022-12-20 11:39:45 +00:00
|
|
|
|
2022-12-15 07:50:54 +00:00
|
|
|
@property
|
|
|
|
def fullcode(self):
|
|
|
|
return f":{self.shortcode}:"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_usable(self) -> bool:
|
|
|
|
"""
|
|
|
|
Return True if this Emoji is usable.
|
|
|
|
"""
|
|
|
|
return self.public or (
|
|
|
|
self.public is None and Config.system.emoji_unreviewed_are_public
|
|
|
|
)
|
|
|
|
|
2023-01-11 03:31:50 +00:00
|
|
|
def full_url_admin(self) -> RelativeAbsoluteUrl:
|
|
|
|
return self.full_url(always_show=True)
|
|
|
|
|
|
|
|
def full_url(self, always_show=False) -> RelativeAbsoluteUrl:
|
|
|
|
if self.is_usable or always_show:
|
2022-12-15 07:50:54 +00:00
|
|
|
if self.file:
|
|
|
|
return AutoAbsoluteUrl(self.file.url)
|
|
|
|
elif self.remote_url:
|
2022-12-28 18:57:54 +00:00
|
|
|
return ProxyAbsoluteUrl(
|
|
|
|
f"/proxy/emoji/{self.pk}/",
|
|
|
|
remote_url=self.remote_url,
|
2022-12-28 18:39:40 +00:00
|
|
|
)
|
2022-12-15 07:50:54 +00:00
|
|
|
return StaticAbsoluteUrl("img/blank-emoji-128.png")
|
|
|
|
|
|
|
|
def as_html(self):
|
|
|
|
if self.is_usable:
|
|
|
|
return mark_safe(
|
|
|
|
f'<img src="{self.full_url().relative}" class="emoji" alt="Emoji {self.shortcode}">'
|
|
|
|
)
|
|
|
|
return self.fullcode
|
|
|
|
|
2023-01-14 17:35:20 +00:00
|
|
|
@property
|
|
|
|
def can_copy_local(self):
|
|
|
|
if not hasattr(Emoji, "locals"):
|
|
|
|
Emoji.locals = Emoji.load_locals()
|
|
|
|
return not self.local and self.is_usable and self.shortcode not in Emoji.locals
|
|
|
|
|
|
|
|
def copy_to_local(self, *, save: bool = True):
|
|
|
|
"""
|
|
|
|
Copy this (non-local) Emoji to local for use by Users of this instance. Returns
|
|
|
|
the Emoji instance, or None if the copy failed to happen. Specify save=False to
|
|
|
|
return the object without saving to database (for bulk saving).
|
|
|
|
"""
|
|
|
|
if not self.can_copy_local:
|
|
|
|
return None
|
|
|
|
|
|
|
|
emoji = None
|
|
|
|
if self.file:
|
|
|
|
# new emoji gets its own copy of the file
|
|
|
|
file = ContentFile(self.file.read())
|
|
|
|
file.name = self.file.name
|
|
|
|
emoji = Emoji(
|
|
|
|
shortcode=self.shortcode,
|
|
|
|
domain=None,
|
|
|
|
local=True,
|
|
|
|
mimetype=self.mimetype,
|
|
|
|
file=file,
|
|
|
|
category=self.category,
|
|
|
|
)
|
|
|
|
if save:
|
|
|
|
emoji.save()
|
|
|
|
# add this new one to the locals cache
|
|
|
|
Emoji.locals[self.shortcode] = emoji
|
|
|
|
return emoji
|
|
|
|
|
2022-12-15 07:50:54 +00:00
|
|
|
@classmethod
|
2022-12-29 17:35:14 +00:00
|
|
|
def emojis_from_content(cls, content: str, domain: Domain | None) -> list["Emoji"]:
|
2022-12-15 07:50:54 +00:00
|
|
|
"""
|
|
|
|
Return a parsed and sanitized of emoji found in content without
|
|
|
|
the surrounding ':'.
|
|
|
|
"""
|
2023-01-30 00:46:22 +00:00
|
|
|
emoji_hits = FediverseHtmlParser(
|
|
|
|
content, find_emojis=True, emoji_domain=domain
|
|
|
|
).emojis
|
2023-02-04 00:02:35 +00:00
|
|
|
emojis = sorted({emoji for emoji in emoji_hits})
|
2022-12-15 07:50:54 +00:00
|
|
|
return list(
|
2022-12-15 18:56:48 +00:00
|
|
|
cls.objects.filter(local=(domain is None) or domain.local)
|
2022-12-15 07:50:54 +00:00
|
|
|
.usable(domain)
|
|
|
|
.filter(shortcode__in=emojis)
|
|
|
|
)
|
|
|
|
|
|
|
|
def to_ap_tag(self):
|
|
|
|
"""
|
|
|
|
Return this Emoji as an ActivityPub Tag
|
|
|
|
"""
|
|
|
|
return {
|
2022-12-15 18:56:48 +00:00
|
|
|
"id": self.object_uri or f"https://{settings.MAIN_DOMAIN}/emoji/{self.pk}/",
|
2022-12-18 00:48:33 +00:00
|
|
|
"type": "Emoji",
|
|
|
|
"name": f":{self.shortcode}:",
|
2022-12-15 07:50:54 +00:00
|
|
|
"icon": {
|
|
|
|
"type": "Image",
|
|
|
|
"mediaType": self.mimetype,
|
|
|
|
"url": self.full_url().absolute,
|
|
|
|
},
|
2022-12-18 00:48:33 +00:00
|
|
|
"updated": format_ld_date(self.updated),
|
2022-12-15 07:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def by_ap_tag(cls, domain: Domain, data: dict, create: bool = False):
|
|
|
|
""" """
|
|
|
|
try:
|
|
|
|
return cls.objects.get(object_uri=data["id"])
|
|
|
|
except cls.DoesNotExist:
|
|
|
|
if not create:
|
|
|
|
raise KeyError(f"No emoji with ID {data['id']}", data)
|
|
|
|
|
2022-12-22 17:18:50 +00:00
|
|
|
# Name could be a direct property, or in a language'd value
|
|
|
|
if "name" in data:
|
|
|
|
name = data["name"]
|
|
|
|
elif "nameMap" in data:
|
|
|
|
name = data["nameMap"]["und"]
|
|
|
|
else:
|
|
|
|
raise ValueError("No name on emoji JSON")
|
|
|
|
|
2022-12-28 02:41:16 +00:00
|
|
|
icon = data["icon"]
|
|
|
|
|
|
|
|
mimetype = icon.get("mediaType")
|
|
|
|
if not mimetype:
|
|
|
|
mimetype, _ = mimetypes.guess_type(icon["url"])
|
|
|
|
|
2022-12-15 07:50:54 +00:00
|
|
|
# create
|
2023-02-04 00:02:35 +00:00
|
|
|
shortcode = name.strip(":")
|
2022-12-15 07:50:54 +00:00
|
|
|
category = (icon.get("category") or "")[:100]
|
2023-01-06 02:57:57 +00:00
|
|
|
|
|
|
|
if not domain.local:
|
|
|
|
try:
|
|
|
|
emoji = cls.objects.get(shortcode=shortcode, domain=domain)
|
|
|
|
except cls.DoesNotExist:
|
|
|
|
pass
|
|
|
|
else:
|
2023-02-25 21:47:43 +00:00
|
|
|
# default to previously discovered mimetype if not provided
|
|
|
|
# by the instance to avoid infinite outdated state
|
|
|
|
if mimetype is None:
|
|
|
|
mimetype = emoji.mimetype
|
|
|
|
|
2023-01-06 02:57:57 +00:00
|
|
|
# Domain previously provided this shortcode. Trample in the new emoji
|
|
|
|
if emoji.remote_url != icon["url"] or emoji.mimetype != mimetype:
|
|
|
|
emoji.object_uri = data["id"]
|
|
|
|
emoji.remote_url = icon["url"]
|
|
|
|
emoji.mimetype = mimetype
|
|
|
|
emoji.category = category
|
|
|
|
emoji.transition_set_state("outdated")
|
|
|
|
if emoji.file:
|
|
|
|
emoji.file.delete(save=True)
|
|
|
|
else:
|
|
|
|
emoji.save()
|
|
|
|
return emoji
|
|
|
|
|
2022-12-15 07:50:54 +00:00
|
|
|
emoji = cls.objects.create(
|
|
|
|
shortcode=shortcode,
|
|
|
|
domain=None if domain.local else domain,
|
|
|
|
local=domain.local,
|
|
|
|
object_uri=data["id"],
|
2023-02-25 21:47:43 +00:00
|
|
|
mimetype=mimetype or "application/octet-stream",
|
2022-12-15 07:50:54 +00:00
|
|
|
category=category,
|
|
|
|
remote_url=icon["url"],
|
|
|
|
)
|
|
|
|
return emoji
|
|
|
|
|
|
|
|
### Mastodon API ###
|
|
|
|
|
|
|
|
def to_mastodon_json(self):
|
|
|
|
url = self.full_url().absolute
|
|
|
|
data = {
|
|
|
|
"shortcode": self.shortcode,
|
|
|
|
"url": url,
|
|
|
|
"static_url": self.remote_url or url,
|
2022-12-17 19:20:00 +00:00
|
|
|
"visible_in_picker": (
|
|
|
|
Config.system.emoji_unreviewed_are_public
|
|
|
|
if self.public is None
|
|
|
|
else self.public
|
|
|
|
),
|
2022-12-15 07:50:54 +00:00
|
|
|
"category": self.category or "",
|
|
|
|
}
|
|
|
|
return data
|