Emoji: Unify tag building, fix tests.

This commit is contained in:
Lain Soykaf 2026-01-05 11:57:38 +04:00 committed by Henry Jameson
commit 72fea0c901
7 changed files with 22 additions and 29 deletions

View file

@ -189,6 +189,18 @@ defmodule Pleroma.Emoji do
def emoji_url(_), do: nil def emoji_url(_), do: nil
def build_emoji_tag({name, url}) do
url = URI.encode(url)
%{
"icon" => %{"url" => "#{url}", "type" => "Image"},
"name" => ":" <> name <> ":",
"type" => "Emoji",
"updated" => "1970-01-01T00:00:00Z",
"id" => url
}
end
def emoji_name_with_instance(name, url) do def emoji_name_with_instance(name, url) do
url = url |> URI.parse() |> Map.get(:host) url = url |> URI.parse() |> Map.get(:host)
"#{name}@#{url}" "#{name}@#{url}"

View file

@ -64,15 +64,7 @@ defmodule Pleroma.Web.ActivityPub.Builder do
defp add_emoji_content(data, emoji, url) do defp add_emoji_content(data, emoji, url) do
tag = [ tag = [
%{ Emoji.build_emoji_tag({Emoji.maybe_strip_name(emoji), url})
"id" => url,
"type" => "Emoji",
"name" => Emoji.maybe_quote(emoji),
"icon" => %{
"type" => "Image",
"url" => url
}
}
] ]
data data

View file

@ -8,6 +8,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
""" """
@behaviour Pleroma.Web.ActivityPub.Transmogrifier.API @behaviour Pleroma.Web.ActivityPub.Transmogrifier.API
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Emoji
alias Pleroma.EctoType.ActivityPub.ObjectValidators alias Pleroma.EctoType.ActivityPub.ObjectValidators
alias Pleroma.Maps alias Pleroma.Maps
alias Pleroma.Object alias Pleroma.Object
@ -1005,32 +1006,20 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
def take_emoji_tags(%User{emoji: emoji}) do def take_emoji_tags(%User{emoji: emoji}) do
emoji emoji
|> Map.to_list() |> Map.to_list()
|> Enum.map(&build_emoji_tag/1) |> Enum.map(&Emoji.build_emoji_tag/1)
end end
# TODO: we should probably send mtime instead of unix epoch time for updated # TODO: we should probably send mtime instead of unix epoch time for updated
def add_emoji_tags(%{"emoji" => emoji} = object) do def add_emoji_tags(%{"emoji" => emoji} = object) do
tags = object["tag"] || [] tags = object["tag"] || []
out = Enum.map(emoji, &build_emoji_tag/1) out = Enum.map(emoji, &Emoji.build_emoji_tag/1)
Map.put(object, "tag", tags ++ out) Map.put(object, "tag", tags ++ out)
end end
def add_emoji_tags(object), do: object def add_emoji_tags(object), do: object
def build_emoji_tag({name, url}) do
url = URI.encode(url)
%{
"icon" => %{"url" => "#{url}", "type" => "Image"},
"name" => ":" <> name <> ":",
"type" => "Emoji",
"updated" => "1970-01-01T00:00:00Z",
"id" => url
}
end
def set_conversation(object) do def set_conversation(object) do
Map.put(object, "conversation", object["context"]) Map.put(object, "conversation", object["context"])
end end

View file

@ -7,7 +7,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.EmojiTagBuildingTest do
name = "hanapog" name = "hanapog"
url = "https://misskey.local.live/emojis/hana pog.png" url = "https://misskey.local.live/emojis/hana pog.png"
tag = Transmogrifier.build_emoji_tag({name, url}) tag = Pleroma.Emoji.build_emoji_tag({name, url})
assert tag["id"] == "https://misskey.local.live/emojis/hana%20pog.png" assert tag["id"] == "https://misskey.local.live/emojis/hana%20pog.png"
end end

View file

@ -219,7 +219,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
data: %{ data: %{
"reactions" => [ "reactions" => [
["👍", [user.ap_id], nil], ["👍", [user.ap_id], nil],
["dinosaur", [user.ap_id], "http://localhost:4001/emoji/dino walking.gif"] ["dinosaur", [user.ap_id], "http://localhost:4001/emoji/dino%20walking.gif"]
] ]
} }
) )
@ -243,7 +243,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
account: AccountView.render("show.json", %{user: other_user, for: user}), account: AccountView.render("show.json", %{user: other_user, for: user}),
status: StatusView.render("show.json", %{activity: activity, for: user}), status: StatusView.render("show.json", %{activity: activity, for: user}),
created_at: Utils.to_masto_date(notification.inserted_at), created_at: Utils.to_masto_date(notification.inserted_at),
emoji_url: "http://localhost:4001/emoji/dino walking.gif" emoji_url: "http://localhost:4001/emoji/dino%20walking.gif"
} }
test_notifications_rendering([notification], user, [expected]) test_notifications_rendering([notification], user, [expected])

View file

@ -54,7 +54,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
count: 2, count: 2,
me: false, me: false,
name: "dinosaur", name: "dinosaur",
url: "http://localhost:4001/emoji/dino walking.gif", url: "http://localhost:4001/emoji/dino%20walking.gif",
account_ids: [other_user.id, user.id] account_ids: [other_user.id, user.id]
}, },
%{name: "🍵", count: 1, me: false, url: nil, account_ids: [third_user.id]} %{name: "🍵", count: 1, me: false, url: nil, account_ids: [third_user.id]}
@ -70,7 +70,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
count: 2, count: 2,
me: true, me: true,
name: "dinosaur", name: "dinosaur",
url: "http://localhost:4001/emoji/dino walking.gif", url: "http://localhost:4001/emoji/dino%20walking.gif",
account_ids: [other_user.id, user.id] account_ids: [other_user.id, user.id]
}, },
%{name: "🍵", count: 1, me: false, url: nil, account_ids: [third_user.id]} %{name: "🍵", count: 1, me: false, url: nil, account_ids: [third_user.id]}

View file

@ -100,7 +100,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiReactionControllerTest do
"name" => "dinosaur", "name" => "dinosaur",
"count" => 1, "count" => 1,
"me" => true, "me" => true,
"url" => "http://localhost:4001/emoji/dino walking.gif", "url" => "http://localhost:4001/emoji/dino%20walking.gif",
"account_ids" => [other_user.id] "account_ids" => [other_user.id]
} }
] ]