Fix outbound emoji federation

This commit is contained in:
Andrew Godwin 2022-12-15 11:56:48 -07:00
parent 61fbda0ebf
commit 5e912ecac5
3 changed files with 15 additions and 14 deletions

View File

@ -106,7 +106,7 @@ class PostAttachmentInline(admin.StackedInline):
class PostAdmin(admin.ModelAdmin): class PostAdmin(admin.ModelAdmin):
list_display = ["id", "state", "author", "created"] list_display = ["id", "state", "author", "created"]
list_filter = ("local", "visibility", "state", "created") list_filter = ("local", "visibility", "state", "created")
raw_id_fields = ["to", "mentions", "author"] raw_id_fields = ["to", "mentions", "author", "emojis"]
actions = ["force_fetch", "reparse_hashtags"] actions = ["force_fetch", "reparse_hashtags"]
search_fields = ["content"] search_fields = ["content"]
inlines = [PostAttachmentInline] inlines = [PostAttachmentInline]

View File

@ -45,15 +45,16 @@ class EmojiStates(StateGraph):
class EmojiQuerySet(models.QuerySet): class EmojiQuerySet(models.QuerySet):
def usable(self, domain: Domain | None = None): def usable(self, domain: Domain | None = None):
public_q = models.Q(public=True) if domain is None or domain.local:
if Config.system.emoji_unreviewed_are_public: visible_q = models.Q(local=True)
public_q |= models.Q(public__isnull=True)
qs = self.filter(public_q)
if domain:
if domain.local:
qs = qs.filter(local=True)
else: else:
visible_q = models.Q(public=True)
if Config.system.emoji_unreviewed_are_public:
visible_q |= models.Q(public__isnull=True)
qs = self.filter(visible_q)
if domain:
if not domain.local:
qs = qs.filter(domain=domain) qs = qs.filter(domain=domain)
return qs return qs
@ -194,7 +195,7 @@ class Emoji(StatorModel):
return mark_safe(Emoji.emoji_regex.sub(replacer, content)) return mark_safe(Emoji.emoji_regex.sub(replacer, content))
@classmethod @classmethod
def emojis_from_content(cls, content: str, domain: Domain) -> list[str]: def emojis_from_content(cls, content: str, domain: Domain | None) -> list[str]:
""" """
Return a parsed and sanitized of emoji found in content without Return a parsed and sanitized of emoji found in content without
the surrounding ':'. the surrounding ':'.
@ -202,7 +203,7 @@ class Emoji(StatorModel):
emoji_hits = cls.emoji_regex.findall(strip_html(content)) emoji_hits = cls.emoji_regex.findall(strip_html(content))
emojis = sorted({emoji.lower() for emoji in emoji_hits}) emojis = sorted({emoji.lower() for emoji in emoji_hits})
return list( return list(
cls.objects.filter(local=domain is None) cls.objects.filter(local=(domain is None) or domain.local)
.usable(domain) .usable(domain)
.filter(shortcode__in=emojis) .filter(shortcode__in=emojis)
) )
@ -213,7 +214,7 @@ class Emoji(StatorModel):
http://joinmastodon.org/ns#Emoji http://joinmastodon.org/ns#Emoji
""" """
return { return {
"id": self.object_uri, "id": self.object_uri or f"https://{settings.MAIN_DOMAIN}/emoji/{self.pk}/",
"type": "Emoji", "type": "Emoji",
"name": self.shortcode, "name": self.shortcode,
"icon": { "icon": {

View File

@ -363,7 +363,7 @@ class Post(StatorModel):
""" """
return ( return (
await Post.objects.select_related("author", "author__domain") await Post.objects.select_related("author", "author__domain")
.prefetch_related("mentions", "mentions__domain", "attachments") .prefetch_related("mentions", "mentions__domain", "attachments", "emojis")
.aget(pk=self.pk) .aget(pk=self.pk)
) )
@ -391,7 +391,7 @@ class Post(StatorModel):
# Find hashtags in this post # Find hashtags in this post
hashtags = Hashtag.hashtags_from_content(content) or None hashtags = Hashtag.hashtags_from_content(content) or None
# Find emoji in this post # Find emoji in this post
emojis = Emoji.emojis_from_content(content, author.domain) emojis = Emoji.emojis_from_content(content, None)
# Strip all HTML and apply linebreaks filter # Strip all HTML and apply linebreaks filter
content = linebreaks_filter(strip_html(content)) content = linebreaks_filter(strip_html(content))
# Make the Post object # Make the Post object
@ -430,7 +430,7 @@ class Post(StatorModel):
self.edited = timezone.now() self.edited = timezone.now()
self.hashtags = Hashtag.hashtags_from_content(content) or None self.hashtags = Hashtag.hashtags_from_content(content) or None
self.mentions.set(self.mentions_from_content(content, self.author)) self.mentions.set(self.mentions_from_content(content, self.author))
self.emojis.set(Emoji.emojis_from_content(content, self.author.domain)) self.emojis.set(Emoji.emojis_from_content(content, None))
self.attachments.set(attachments or []) self.attachments.set(attachments or [])
self.save() self.save()