ensure emoji reactions are fully-qualified

This commit is contained in:
FloatingGhost 2022-08-27 16:53:09 +01:00
parent e40d45a585
commit 8de9033e18
5 changed files with 18 additions and 16 deletions

View file

@ -329,7 +329,7 @@ def add_emoji_reaction_to_object(
object object
) do ) do
reactions = get_cached_emoji_reactions(object) reactions = get_cached_emoji_reactions(object)
emoji = stripped_emoji_name(emoji) emoji = Pleroma.Emoji.stripped_name(emoji)
url = emoji_url(emoji, activity) url = emoji_url(emoji, activity)
new_reactions = new_reactions =
@ -356,12 +356,6 @@ def add_emoji_reaction_to_object(
update_element_in_object("reaction", new_reactions, object, count) update_element_in_object("reaction", new_reactions, object, count)
end end
defp stripped_emoji_name(name) do
name
|> String.replace_leading(":", "")
|> String.replace_trailing(":", "")
end
defp emoji_url( defp emoji_url(
name, name,
%Activity{ %Activity{
@ -384,7 +378,7 @@ def remove_emoji_reaction_from_object(
%Activity{data: %{"content" => emoji, "actor" => actor}} = activity, %Activity{data: %{"content" => emoji, "actor" => actor}} = activity,
object object
) do ) do
emoji = stripped_emoji_name(emoji) emoji = Pleroma.Emoji.stripped_name(emoji)
reactions = get_cached_emoji_reactions(object) reactions = get_cached_emoji_reactions(object)
url = emoji_url(emoji, activity) url = emoji_url(emoji, activity)

View file

@ -209,8 +209,7 @@ def react_with_emoji(id, user, emoji) do
{:ok, activity, _} <- Pipeline.common_pipeline(emoji_react, local: true) do {:ok, activity, _} <- Pipeline.common_pipeline(emoji_react, local: true) do
{:ok, activity} {:ok, activity}
else else
e -> _ ->
IO.inspect(e)
{:error, dgettext("errors", "Could not add reaction emoji")} {:error, dgettext("errors", "Could not add reaction emoji")}
end end
end end

View file

@ -74,7 +74,10 @@ defp filter(reactions, %{emoji: emoji}) when is_binary(emoji) do
defp filter(reactions, _), do: reactions defp filter(reactions, _), do: reactions
def create(%{assigns: %{user: user}} = conn, %{id: activity_id, emoji: emoji}) do def create(%{assigns: %{user: user}} = conn, %{id: activity_id, emoji: emoji}) do
emoji = Pleroma.Emoji.maybe_quote(emoji) emoji =
emoji
|> Pleroma.Emoji.fully_qualify_emoji()
|> Pleroma.Emoji.maybe_quote()
with {:ok, _activity} <- CommonAPI.react_with_emoji(activity_id, user, emoji) do with {:ok, _activity} <- CommonAPI.react_with_emoji(activity_id, user, emoji) do
activity = Activity.get_by_id(activity_id) activity = Activity.get_by_id(activity_id)
@ -86,6 +89,11 @@ def create(%{assigns: %{user: user}} = conn, %{id: activity_id, emoji: emoji}) d
end end
def delete(%{assigns: %{user: user}} = conn, %{id: activity_id, emoji: emoji}) do def delete(%{assigns: %{user: user}} = conn, %{id: activity_id, emoji: emoji}) do
emoji =
emoji
|> Pleroma.Emoji.fully_qualify_emoji()
|> Pleroma.Emoji.maybe_quote()
with {:ok, _activity} <- CommonAPI.unreact_with_emoji(activity_id, user, emoji) do with {:ok, _activity} <- CommonAPI.unreact_with_emoji(activity_id, user, emoji) do
activity = Activity.get_by_id(activity_id) activity = Activity.get_by_id(activity_id)

View file

@ -13,7 +13,7 @@ def emoji_name(emoji, nil), do: emoji
def emoji_name(emoji, url) do def emoji_name(emoji, url) do
url = URI.parse(url) url = URI.parse(url)
if url.host == Endpoint.host() do if url.host == Pleroma.Web.Endpoint.host() do
emoji emoji
else else
"#{emoji}@#{url.host}" "#{emoji}@#{url.host}"

View file

@ -17,22 +17,23 @@ test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
user = insert(:user) user = insert(:user)
other_user = insert(:user) other_user = insert(:user)
{:ok, activity} = CommonAPI.post(user, %{status: "#cofe"}) note = insert(:note, user: user, reactions: %{reactions: %{"👍" => [other_user.ap_id]}})
activity = insert(:note_activity, note: note, user: user)
result = result =
conn conn
|> assign(:user, other_user) |> assign(:user, other_user)
|> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"])) |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
|> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/") |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/\u26A0")
|> json_response_and_validate_schema(200) |> json_response_and_validate_schema(200)
# We return the status, but this our implementation detail. # We return the status, but this our implementation detail.
assert %{"id" => id} = result assert %{"id" => id} = result
assert to_string(activity.id) == id assert to_string(activity.id) == id
IO.inspect(result)
assert result["pleroma"]["emoji_reactions"] == [ assert result["pleroma"]["emoji_reactions"] == [
%{ %{
"name" => "", "name" => "\u26A0\uFE0F",
"count" => 1, "count" => 1,
"me" => true, "me" => true,
"url" => nil, "url" => nil,