Fix array-less EmojiReacts #739
2 changed files with 99 additions and 1 deletions
|
@ -8,6 +8,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do
|
||||||
alias Pleroma.Emoji
|
alias Pleroma.Emoji
|
||||||
alias Pleroma.Object
|
alias Pleroma.Object
|
||||||
alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
|
alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
|
||||||
|
alias Pleroma.Web.ActivityPub.Transmogrifier
|
||||||
|
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
|
import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
|
||||||
|
@ -52,6 +53,7 @@ def changeset(struct, data) do
|
||||||
defp fix(data) do
|
defp fix(data) do
|
||||||
data =
|
data =
|
||||||
data
|
data
|
||||||
|
|> Transmogrifier.fix_tag()
|
||||||
|> fix_emoji_qualification()
|
|> fix_emoji_qualification()
|
||||||
|> CommonFixes.fix_actor()
|
|> CommonFixes.fix_actor()
|
||||||
|> CommonFixes.fix_activity_addressing()
|
|> CommonFixes.fix_activity_addressing()
|
||||||
|
|
|
@ -37,7 +37,80 @@ test "it works for incoming emoji reactions" do
|
||||||
assert match?([["👌", _, nil]], object.data["reactions"])
|
assert match?([["👌", _, nil]], object.data["reactions"])
|
||||||
end
|
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 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)
|
user = insert(:user)
|
||||||
other_user = insert(:user, local: false)
|
other_user = insert(:user, local: false)
|
||||||
{:ok, activity} = CommonAPI.post(user, %{status: "hello"})
|
{:ok, activity} = CommonAPI.post(user, %{status: "hello"})
|
||||||
|
@ -138,4 +211,27 @@ test "it reject invalid emoji reactions" do
|
||||||
|
|
||||||
assert {:error, _} = Transmogrifier.handle_incoming(data)
|
assert {:error, _} = Transmogrifier.handle_incoming(data)
|
||||||
end
|
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, 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", tag)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue