From c5467d04e4be8e33dd3bfac0b1f5e6cafc170d2b Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Wed, 8 Jun 2022 12:36:38 +0100 Subject: [PATCH] add outbound reacts --- lib/pleroma/emoji.ex | 8 +++++++ lib/pleroma/web/activity_pub/builder.ex | 22 ++++++++++++++++++- .../emoji_react_validator.ex | 1 - .../web/activity_pub/transmogrifier.ex | 1 + lib/pleroma/web/common_api.ex | 3 +-- 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/emoji.ex b/lib/pleroma/emoji.ex index 124850114..f484f96a0 100644 --- a/lib/pleroma/emoji.ex +++ b/lib/pleroma/emoji.ex @@ -50,6 +50,14 @@ def reload do @doc "Returns the path of the emoji `name`." @spec get(String.t()) :: String.t() | nil def get(name) do + name = if String.starts_with?(name, ":") do + name + |> String.replace_leading(":", "") + |> String.replace_trailing(":", "") + else + name + end + case :ets.lookup(@ets, name) do [{_, path}] -> path _ -> nil diff --git a/lib/pleroma/web/activity_pub/builder.ex b/lib/pleroma/web/activity_pub/builder.ex index 647ccf432..0ba17a4d5 100644 --- a/lib/pleroma/web/activity_pub/builder.ex +++ b/lib/pleroma/web/activity_pub/builder.ex @@ -16,6 +16,7 @@ defmodule Pleroma.Web.ActivityPub.Builder do alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.ActivityPub.Visibility alias Pleroma.Web.CommonAPI.ActivityDraft + alias Pleroma.Web.Endpoint require Pleroma.Constants @@ -57,10 +58,29 @@ def follow(follower, followed) do @spec emoji_react(User.t(), Object.t(), String.t()) :: {:ok, map(), keyword()} def emoji_react(actor, object, emoji) do with {:ok, data, meta} <- object_action(actor, object) do - data = + data = if Emoji.is_unicode_emoji?(emoji) do data |> Map.put("content", emoji) |> Map.put("type", "EmojiReact") + else + emojo = Emoji.get(emoji) + path = emojo |> Map.get(:file) + url = "#{Endpoint.url()}#{path}" + data + |> Map.put("content", emoji) + |> Map.put("type", "EmojiReact") + |> Map.put("tag", [ + %{} + |> Map.put("id", url) + |> Map.put("type", "Emoji") + |> Map.put("name", emojo.code) + |> Map.put("icon", + %{} + |> Map.put("type", "Image") + |> Map.put("url", url) + ) + ]) + end {:ok, data, meta} end diff --git a/lib/pleroma/web/activity_pub/object_validators/emoji_react_validator.ex b/lib/pleroma/web/activity_pub/object_validators/emoji_react_validator.ex index 45c28beed..f0eb5417c 100644 --- a/lib/pleroma/web/activity_pub/object_validators/emoji_react_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/emoji_react_validator.ex @@ -72,7 +72,6 @@ defp fix(data) do defp validate_emoji(cng) do content = get_field(cng, :content) - IO.inspect(Pleroma.Emoji.is_unicode_emoji?(content)) if Pleroma.Emoji.is_unicode_emoji?(content) || Regex.match?(@emoji_regex, content) do cng else diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 25aaba1a6..f5a7866b6 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -424,6 +424,7 @@ def handle_incoming( } = data, options ) do + IO.inspect(data) data |> Map.put("type", "EmojiReact") |> Map.put("content", reaction) diff --git a/lib/pleroma/web/common_api.ex b/lib/pleroma/web/common_api.ex index 3266b1c60..92afd5cb6 100644 --- a/lib/pleroma/web/common_api.ex +++ b/lib/pleroma/web/common_api.ex @@ -261,8 +261,7 @@ def react_with_emoji(id, user, emoji) do {:ok, activity, _} <- Pipeline.common_pipeline(emoji_react, local: true) do {:ok, activity} else - _ -> - {:error, dgettext("errors", "Could not add reaction emoji")} + _ -> {:error, dgettext("errors", "Could not add reaction emoji")} end end