From debd68641889026f90a31ecf2f27c0be26885a16 Mon Sep 17 00:00:00 2001 From: Oneric Date: Tue, 9 Apr 2024 00:52:22 +0200 Subject: [PATCH 1/2] Add tests for our own custom emoji format --- .../emoji_react_handling_test.exs | 70 ++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/test/pleroma/web/activity_pub/transmogrifier/emoji_react_handling_test.exs b/test/pleroma/web/activity_pub/transmogrifier/emoji_react_handling_test.exs index 977950434..2de5ee636 100644 --- a/test/pleroma/web/activity_pub/transmogrifier/emoji_react_handling_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier/emoji_react_handling_test.exs @@ -37,7 +37,54 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.EmojiReactHandlingTest do assert match?([["๐Ÿ‘Œ", _, nil]], object.data["reactions"]) end - test "it works for incoming custom emoji reactions" do + test "it works for incoming custom emoji with nil id" do + user = insert(:user) + other_user = insert(:user, local: false) + {:ok, activity} = CommonAPI.post(user, %{status: "hello"}) + + shortcode = "blobcatgoogly" + emoji = emoji_object(shortcode) + data = react_with_custom(activity.data["object"], other_user.ap_id, emoji) + + {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) + + assert data["actor"] == other_user.ap_id + assert data["type"] == "EmojiReact" + assert data["object"] == activity.data["object"] + assert data["content"] == ":" <> shortcode <> ":" + [%{}] = data["tag"] + + object = Object.get_by_ap_id(data["object"]) + + assert object.data["reaction_count"] == 1 + assert match?([[^shortcode, _, _]], object.data["reactions"]) + end + + test "it works for incoming custom emoji with image url as id" do + user = insert(:user) + other_user = insert(:user, local: false) + {:ok, activity} = CommonAPI.post(user, %{status: "hello"}) + + shortcode = "blobcatgoogly" + imgurl = "https://example.org/emoji/a.png" + emoji = emoji_object(shortcode, imgurl, imgurl) + data = react_with_custom(activity.data["object"], other_user.ap_id, emoji) + + {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) + + assert data["actor"] == other_user.ap_id + assert data["type"] == "EmojiReact" + assert data["object"] == activity.data["object"] + assert data["content"] == ":" <> shortcode <> ":" + assert [%{}] = data["tag"] + + object = Object.get_by_ap_id(data["object"]) + + assert object.data["reaction_count"] == 1 + assert match?([[^shortcode, _, ^imgurl]], object.data["reactions"]) + end + + test "it works for incoming custom emoji reactions from Misskey" do user = insert(:user) other_user = insert(:user, local: false) {:ok, activity} = CommonAPI.post(user, %{status: "hello"}) @@ -138,4 +185,25 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.EmojiReactHandlingTest do assert {:error, _} = Transmogrifier.handle_incoming(data) end + + defp emoji_object(shortcode, id \\ nil, url \\ "https://example.org/emoji.png") do + %{ + "type" => "Emoji", + "id" => id, + "name" => shortcode |> String.replace_prefix(":", "") |> String.replace_suffix(":", ""), + "icon" => %{ + "type" => "Image", + "url" => url + } + } + end + + defp react_with_custom(object_id, as_actor, emoji) do + File.read!("test/fixtures/emoji-reaction.json") + |> Jason.decode!() + |> Map.put("object", object_id) + |> Map.put("actor", as_actor) + |> Map.put("content", ":" <> emoji["name"] <> ":") + |> Map.put("tag", [emoji]) + end end -- 2.34.1 From 462225880ab672b1fc2e6b7d6660d5fcf13e3876 Mon Sep 17 00:00:00 2001 From: Oneric Date: Tue, 9 Apr 2024 01:04:16 +0200 Subject: [PATCH 2/2] Accept EmojiReacts with non-array tag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JSON-LD compaction strips the array since itโ€™s just one object Fixes: https://akkoma.dev/AkkomaGang/akkoma/issues/720 --- .../emoji_react_validator.ex | 2 ++ .../emoji_react_handling_test.exs | 32 +++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) 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 80ec65cd7..bda67feee 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 @@ -8,6 +8,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do alias Pleroma.Emoji alias Pleroma.Object alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes + alias Pleroma.Web.ActivityPub.Transmogrifier import Ecto.Changeset import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations @@ -52,6 +53,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do defp fix(data) do data = data + |> Transmogrifier.fix_tag() |> fix_emoji_qualification() |> CommonFixes.fix_actor() |> CommonFixes.fix_activity_addressing() diff --git a/test/pleroma/web/activity_pub/transmogrifier/emoji_react_handling_test.exs b/test/pleroma/web/activity_pub/transmogrifier/emoji_react_handling_test.exs index 2de5ee636..b40e7f4da 100644 --- a/test/pleroma/web/activity_pub/transmogrifier/emoji_react_handling_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier/emoji_react_handling_test.exs @@ -84,6 +84,32 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.EmojiReactHandlingTest do assert match?([[^shortcode, _, ^imgurl]], object.data["reactions"]) end + test "it works for incoming custom emoji without tag array" do + user = insert(:user) + other_user = insert(:user, local: false) + {:ok, activity} = CommonAPI.post(user, %{status: "hello"}) + + shortcode = "blobcatgoogly" + imgurl = "https://example.org/emoji/b.png" + emoji = emoji_object(shortcode, imgurl, imgurl) + data = react_with_custom(activity.data["object"], other_user.ap_id, emoji, false) + + assert %{} = data["tag"] + + {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) + + assert data["actor"] == other_user.ap_id + assert data["type"] == "EmojiReact" + assert data["object"] == activity.data["object"] + assert data["content"] == ":" <> shortcode <> ":" + assert [%{}] = data["tag"] + + object = Object.get_by_ap_id(data["object"]) + + assert object.data["reaction_count"] == 1 + assert match?([[^shortcode, _, _]], object.data["reactions"]) + end + test "it works for incoming custom emoji reactions from Misskey" do user = insert(:user) other_user = insert(:user, local: false) @@ -198,12 +224,14 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.EmojiReactHandlingTest do } end - defp react_with_custom(object_id, as_actor, emoji) do + defp react_with_custom(object_id, as_actor, emoji, tag_array \\ true) do + tag = if tag_array, do: [emoji], else: emoji + File.read!("test/fixtures/emoji-reaction.json") |> Jason.decode!() |> Map.put("object", object_id) |> Map.put("actor", as_actor) |> Map.put("content", ":" <> emoji["name"] <> ":") - |> Map.put("tag", [emoji]) + |> Map.put("tag", tag) end end -- 2.34.1