Allow reacting with remote emoji when they exist on the post (#200)
Co-authored-by: FloatingGhost <hannah@coffee-and-dreams.uk> Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/200
This commit is contained in:
parent
714bf0cb23
commit
787e30c5fd
@ -16,6 +16,7 @@ defmodule Pleroma.Web.ActivityPub.Builder do
|
|||||||
alias Pleroma.Web.ActivityPub.Utils
|
alias Pleroma.Web.ActivityPub.Utils
|
||||||
alias Pleroma.Web.ActivityPub.Visibility
|
alias Pleroma.Web.ActivityPub.Visibility
|
||||||
alias Pleroma.Web.CommonAPI.ActivityDraft
|
alias Pleroma.Web.CommonAPI.ActivityDraft
|
||||||
|
alias Pleroma.Web.Endpoint
|
||||||
|
|
||||||
require Pleroma.Constants
|
require Pleroma.Constants
|
||||||
|
|
||||||
@ -54,13 +55,85 @@ defmodule Pleroma.Web.ActivityPub.Builder do
|
|||||||
{:ok, data, []}
|
{:ok, data, []}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp unicode_emoji_react(_object, data, emoji) do
|
||||||
|
data
|
||||||
|
|> Map.put("content", emoji)
|
||||||
|
|> Map.put("type", "EmojiReact")
|
||||||
|
end
|
||||||
|
|
||||||
|
defp add_emoji_content(data, emoji, url) do
|
||||||
|
data
|
||||||
|
|> Map.put("content", Emoji.maybe_quote(emoji))
|
||||||
|
|> Map.put("type", "EmojiReact")
|
||||||
|
|> Map.put("tag", [
|
||||||
|
%{}
|
||||||
|
|> Map.put("id", url)
|
||||||
|
|> Map.put("type", "Emoji")
|
||||||
|
|> Map.put("name", Emoji.maybe_quote(emoji))
|
||||||
|
|> Map.put(
|
||||||
|
"icon",
|
||||||
|
%{}
|
||||||
|
|> Map.put("type", "Image")
|
||||||
|
|> Map.put("url", url)
|
||||||
|
)
|
||||||
|
])
|
||||||
|
end
|
||||||
|
|
||||||
|
defp remote_custom_emoji_react(
|
||||||
|
%{data: %{"reactions" => existing_reactions}},
|
||||||
|
data,
|
||||||
|
emoji
|
||||||
|
) do
|
||||||
|
[emoji_code, instance] = String.split(Emoji.stripped_name(emoji), "@")
|
||||||
|
|
||||||
|
matching_reaction =
|
||||||
|
Enum.find(
|
||||||
|
existing_reactions,
|
||||||
|
fn [name, _, url] ->
|
||||||
|
url = URI.parse(url)
|
||||||
|
url.host == instance && name == emoji_code
|
||||||
|
end
|
||||||
|
)
|
||||||
|
|
||||||
|
if matching_reaction do
|
||||||
|
[name, _, url] = matching_reaction
|
||||||
|
add_emoji_content(data, name, url)
|
||||||
|
else
|
||||||
|
{:error, "Could not react"}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp remote_custom_emoji_react(_object, _data, _emoji) do
|
||||||
|
{:error, "Could not react"}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp local_custom_emoji_react(data, emoji) do
|
||||||
|
with %{} = emojo <- Emoji.get(emoji) do
|
||||||
|
path = emojo |> Map.get(:file)
|
||||||
|
url = "#{Endpoint.url()}#{path}"
|
||||||
|
add_emoji_content(data, emojo.code, url)
|
||||||
|
else
|
||||||
|
_ -> {:error, "Emoji does not exist"}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp custom_emoji_react(object, data, emoji) do
|
||||||
|
if String.contains?(emoji, "@") do
|
||||||
|
remote_custom_emoji_react(object, data, emoji)
|
||||||
|
else
|
||||||
|
local_custom_emoji_react(data, emoji)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@spec emoji_react(User.t(), Object.t(), String.t()) :: {:ok, map(), keyword()}
|
@spec emoji_react(User.t(), Object.t(), String.t()) :: {:ok, map(), keyword()}
|
||||||
def emoji_react(actor, object, emoji) do
|
def emoji_react(actor, object, emoji) do
|
||||||
with {:ok, data, meta} <- object_action(actor, object) do
|
with {:ok, data, meta} <- object_action(actor, object) do
|
||||||
data =
|
data =
|
||||||
data
|
if Emoji.is_unicode_emoji?(emoji) do
|
||||||
|> Map.put("content", emoji)
|
unicode_emoji_react(object, data, emoji)
|
||||||
|> Map.put("type", "EmojiReact")
|
else
|
||||||
|
custom_emoji_react(object, data, emoji)
|
||||||
|
end
|
||||||
|
|
||||||
{:ok, data, meta}
|
{:ok, data, meta}
|
||||||
end
|
end
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do
|
defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
|
|
||||||
|
alias Pleroma.Emoji
|
||||||
alias Pleroma.Object
|
alias Pleroma.Object
|
||||||
alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
|
alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
|
||||||
|
|
||||||
@ -12,6 +13,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do
|
|||||||
import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
|
import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
|
||||||
|
|
||||||
@primary_key false
|
@primary_key false
|
||||||
|
@emoji_regex ~r/:[A-Za-z0-9_-]+(@.+)?:/
|
||||||
|
|
||||||
embedded_schema do
|
embedded_schema do
|
||||||
quote do
|
quote do
|
||||||
@ -19,6 +21,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do
|
|||||||
import Elixir.Pleroma.Web.ActivityPub.ObjectValidators.CommonFields
|
import Elixir.Pleroma.Web.ActivityPub.ObjectValidators.CommonFields
|
||||||
message_fields()
|
message_fields()
|
||||||
activity_fields()
|
activity_fields()
|
||||||
|
tag_fields()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -43,7 +46,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do
|
|||||||
|
|
||||||
def changeset(struct, data) do
|
def changeset(struct, data) do
|
||||||
struct
|
struct
|
||||||
|> cast(data, __schema__(:fields))
|
|> cast(data, __schema__(:fields) -- [:tag])
|
||||||
|
|> cast_embed(:tag)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp fix(data) do
|
defp fix(data) do
|
||||||
@ -53,12 +57,21 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do
|
|||||||
|> CommonFixes.fix_actor()
|
|> CommonFixes.fix_actor()
|
||||||
|> CommonFixes.fix_activity_addressing()
|
|> CommonFixes.fix_activity_addressing()
|
||||||
|
|
||||||
with %Object{} = object <- Object.normalize(data["object"]) do
|
data =
|
||||||
data
|
if Map.has_key?(data, "tag") do
|
||||||
|> CommonFixes.fix_activity_context(object)
|
data
|
||||||
|> CommonFixes.fix_object_action_recipients(object)
|
else
|
||||||
else
|
Map.put(data, "tag", [])
|
||||||
_ -> data
|
end
|
||||||
|
|
||||||
|
case Object.normalize(data["object"]) do
|
||||||
|
%Object{} = object ->
|
||||||
|
data
|
||||||
|
|> CommonFixes.fix_activity_context(object)
|
||||||
|
|> CommonFixes.fix_object_action_recipients(object)
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
data
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -79,14 +92,37 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do
|
|||||||
|
|
||||||
defp fix_emoji_qualification(data), do: data
|
defp fix_emoji_qualification(data), do: data
|
||||||
|
|
||||||
|
defp matches_shortcode?(nil), do: false
|
||||||
|
defp matches_shortcode?(s), do: Regex.match?(@emoji_regex, s)
|
||||||
|
|
||||||
defp validate_emoji(cng) do
|
defp validate_emoji(cng) do
|
||||||
content = get_field(cng, :content)
|
content = get_field(cng, :content)
|
||||||
|
|
||||||
if Pleroma.Emoji.is_unicode_emoji?(content) do
|
if Emoji.is_unicode_emoji?(content) || matches_shortcode?(content) do
|
||||||
cng
|
cng
|
||||||
else
|
else
|
||||||
cng
|
cng
|
||||||
|> add_error(:content, "must be a single character emoji")
|
|> add_error(:content, "is not a valid emoji")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_validate_tag_presence(cng) do
|
||||||
|
content = get_field(cng, :content)
|
||||||
|
|
||||||
|
if Emoji.is_unicode_emoji?(content) do
|
||||||
|
cng
|
||||||
|
else
|
||||||
|
tag = get_field(cng, :tag)
|
||||||
|
emoji_name = Emoji.stripped_name(content)
|
||||||
|
|
||||||
|
case tag do
|
||||||
|
[%{name: ^emoji_name, type: "Emoji", icon: %{url: _}}] ->
|
||||||
|
cng
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
cng
|
||||||
|
|> add_error(:tag, "does not contain an Emoji tag")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -97,5 +133,6 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do
|
|||||||
|> validate_actor_presence()
|
|> validate_actor_presence()
|
||||||
|> validate_object_presence()
|
|> validate_object_presence()
|
||||||
|> validate_emoji()
|
|> validate_emoji()
|
||||||
|
|> maybe_validate_tag_presence()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -325,21 +325,29 @@ defmodule Pleroma.Web.ActivityPub.Utils do
|
|||||||
{:ok, Object.t()} | {:error, Ecto.Changeset.t()}
|
{:ok, Object.t()} | {:error, Ecto.Changeset.t()}
|
||||||
|
|
||||||
def add_emoji_reaction_to_object(
|
def add_emoji_reaction_to_object(
|
||||||
%Activity{data: %{"content" => emoji, "actor" => actor}},
|
%Activity{data: %{"content" => emoji, "actor" => actor}} = activity,
|
||||||
object
|
object
|
||||||
) do
|
) do
|
||||||
reactions = get_cached_emoji_reactions(object)
|
reactions = get_cached_emoji_reactions(object)
|
||||||
|
emoji = Pleroma.Emoji.stripped_name(emoji)
|
||||||
|
url = emoji_url(emoji, activity)
|
||||||
|
|
||||||
new_reactions =
|
new_reactions =
|
||||||
case Enum.find_index(reactions, fn [candidate, _] -> emoji == candidate end) do
|
case Enum.find_index(reactions, fn [candidate, _, candidate_url] ->
|
||||||
|
if is_nil(candidate_url) do
|
||||||
|
emoji == candidate
|
||||||
|
else
|
||||||
|
url == candidate_url
|
||||||
|
end
|
||||||
|
end) do
|
||||||
nil ->
|
nil ->
|
||||||
reactions ++ [[emoji, [actor]]]
|
reactions ++ [[emoji, [actor], url]]
|
||||||
|
|
||||||
index ->
|
index ->
|
||||||
List.update_at(
|
List.update_at(
|
||||||
reactions,
|
reactions,
|
||||||
index,
|
index,
|
||||||
fn [emoji, users] -> [emoji, Enum.uniq([actor | users])] end
|
fn [emoji, users, url] -> [emoji, Enum.uniq([actor | users]), url] end
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -348,18 +356,40 @@ defmodule Pleroma.Web.ActivityPub.Utils do
|
|||||||
update_element_in_object("reaction", new_reactions, object, count)
|
update_element_in_object("reaction", new_reactions, object, count)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp emoji_url(
|
||||||
|
name,
|
||||||
|
%Activity{
|
||||||
|
data: %{
|
||||||
|
"tag" => [
|
||||||
|
%{"type" => "Emoji", "name" => name, "icon" => %{"url" => url}}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
),
|
||||||
|
do: url
|
||||||
|
|
||||||
|
defp emoji_url(_, _), do: nil
|
||||||
|
|
||||||
def emoji_count(reactions_list) do
|
def emoji_count(reactions_list) do
|
||||||
Enum.reduce(reactions_list, 0, fn [_, users], acc -> acc + length(users) end)
|
Enum.reduce(reactions_list, 0, fn [_, users, _], acc -> acc + length(users) end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_emoji_reaction_from_object(
|
def remove_emoji_reaction_from_object(
|
||||||
%Activity{data: %{"content" => emoji, "actor" => actor}},
|
%Activity{data: %{"content" => emoji, "actor" => actor}} = activity,
|
||||||
object
|
object
|
||||||
) do
|
) do
|
||||||
|
emoji = Pleroma.Emoji.stripped_name(emoji)
|
||||||
reactions = get_cached_emoji_reactions(object)
|
reactions = get_cached_emoji_reactions(object)
|
||||||
|
url = emoji_url(emoji, activity)
|
||||||
|
|
||||||
new_reactions =
|
new_reactions =
|
||||||
case Enum.find_index(reactions, fn [candidate, _] -> emoji == candidate end) do
|
case Enum.find_index(reactions, fn [candidate, _, candidate_url] ->
|
||||||
|
if is_nil(candidate_url) do
|
||||||
|
emoji == candidate
|
||||||
|
else
|
||||||
|
url == candidate_url
|
||||||
|
end
|
||||||
|
end) do
|
||||||
nil ->
|
nil ->
|
||||||
reactions
|
reactions
|
||||||
|
|
||||||
@ -367,9 +397,9 @@ defmodule Pleroma.Web.ActivityPub.Utils do
|
|||||||
List.update_at(
|
List.update_at(
|
||||||
reactions,
|
reactions,
|
||||||
index,
|
index,
|
||||||
fn [emoji, users] -> [emoji, List.delete(users, actor)] end
|
fn [emoji, users, url] -> [emoji, List.delete(users, actor), url] end
|
||||||
)
|
)
|
||||||
|> Enum.reject(fn [_, users] -> Enum.empty?(users) end)
|
|> Enum.reject(fn [_, users, _] -> Enum.empty?(users) end)
|
||||||
end
|
end
|
||||||
|
|
||||||
count = emoji_count(new_reactions)
|
count = emoji_count(new_reactions)
|
||||||
@ -489,17 +519,37 @@ defmodule Pleroma.Web.ActivityPub.Utils do
|
|||||||
|
|
||||||
def get_latest_reaction(internal_activity_id, %{ap_id: ap_id}, emoji) do
|
def get_latest_reaction(internal_activity_id, %{ap_id: ap_id}, emoji) do
|
||||||
%{data: %{"object" => object_ap_id}} = Activity.get_by_id(internal_activity_id)
|
%{data: %{"object" => object_ap_id}} = Activity.get_by_id(internal_activity_id)
|
||||||
|
emoji = Pleroma.Emoji.maybe_quote(emoji)
|
||||||
|
|
||||||
"EmojiReact"
|
"EmojiReact"
|
||||||
|> Activity.Queries.by_type()
|
|> Activity.Queries.by_type()
|
||||||
|> where(actor: ^ap_id)
|
|> where(actor: ^ap_id)
|
||||||
|> where([activity], fragment("?->>'content' = ?", activity.data, ^emoji))
|
|> custom_emoji_discriminator(emoji)
|
||||||
|> Activity.Queries.by_object_id(object_ap_id)
|
|> Activity.Queries.by_object_id(object_ap_id)
|
||||||
|> order_by([activity], fragment("? desc nulls last", activity.id))
|
|> order_by([activity], fragment("? desc nulls last", activity.id))
|
||||||
|> limit(1)
|
|> limit(1)
|
||||||
|> Repo.one()
|
|> Repo.one()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp custom_emoji_discriminator(query, emoji) do
|
||||||
|
if String.contains?(emoji, "@") do
|
||||||
|
stripped = Pleroma.Emoji.stripped_name(emoji)
|
||||||
|
[name, domain] = String.split(stripped, "@")
|
||||||
|
domain_pattern = "%" <> domain <> "%"
|
||||||
|
emoji_pattern = Pleroma.Emoji.maybe_quote(name)
|
||||||
|
|
||||||
|
query
|
||||||
|
|> where([activity], fragment("?->>'content' = ?
|
||||||
|
AND EXISTS (
|
||||||
|
SELECT FROM jsonb_array_elements(?->'tag') elem
|
||||||
|
WHERE elem->>'id' ILIKE ?
|
||||||
|
)", activity.data, ^emoji_pattern, activity.data, ^domain_pattern))
|
||||||
|
else
|
||||||
|
query
|
||||||
|
|> where([activity], fragment("?->>'content' = ?", activity.data, ^emoji))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
#### Announce-related helpers
|
#### Announce-related helpers
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
@ -340,8 +340,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
|
|||||||
opts[:for],
|
opts[:for],
|
||||||
Map.get(opts, :with_muted, false)
|
Map.get(opts, :with_muted, false)
|
||||||
)
|
)
|
||||||
|> Stream.map(fn {emoji, users} ->
|
|> Stream.map(fn {emoji, users, url} ->
|
||||||
build_emoji_map(emoji, users, opts[:for])
|
build_emoji_map(emoji, users, url, opts[:for])
|
||||||
end)
|
end)
|
||||||
|> Enum.to_list()
|
|> Enum.to_list()
|
||||||
|
|
||||||
@ -702,11 +702,13 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp build_emoji_map(emoji, users, current_user) do
|
defp build_emoji_map(emoji, users, url, current_user) do
|
||||||
%{
|
%{
|
||||||
name: emoji,
|
name: Pleroma.Web.PleromaAPI.EmojiReactionView.emoji_name(emoji, url),
|
||||||
count: length(users),
|
count: length(users),
|
||||||
me: !!(current_user && current_user.ap_id in users)
|
url: MediaProxy.url(url),
|
||||||
|
me: !!(current_user && current_user.ap_id in users),
|
||||||
|
account_ids: Enum.map(users, fn user -> User.get_cached_by_ap_id(user).id end)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -50,29 +50,35 @@ defmodule Pleroma.Web.PleromaAPI.EmojiReactionController do
|
|||||||
if not with_muted, do: User.cached_muted_users_ap_ids(user), else: []
|
if not with_muted, do: User.cached_muted_users_ap_ids(user), else: []
|
||||||
end
|
end
|
||||||
|
|
||||||
filter_emoji = fn emoji, users ->
|
filter_emoji = fn emoji, users, url ->
|
||||||
case Enum.reject(users, &(&1 in exclude_ap_ids)) do
|
case Enum.reject(users, &(&1 in exclude_ap_ids)) do
|
||||||
[] -> nil
|
[] -> nil
|
||||||
users -> {emoji, users}
|
users -> {emoji, users, url}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
reactions
|
reactions
|
||||||
|> Stream.map(fn
|
|> Stream.map(fn
|
||||||
[emoji, users] when is_list(users) -> filter_emoji.(emoji, users)
|
[emoji, users, url] when is_list(users) -> filter_emoji.(emoji, users, url)
|
||||||
{emoji, users} when is_list(users) -> filter_emoji.(emoji, users)
|
{emoji, users, url} when is_list(users) -> filter_emoji.(emoji, users, url)
|
||||||
|
{emoji, users} when is_list(users) -> filter_emoji.(emoji, users, nil)
|
||||||
_ -> nil
|
_ -> nil
|
||||||
end)
|
end)
|
||||||
|> Stream.reject(&is_nil/1)
|
|> Stream.reject(&is_nil/1)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp filter(reactions, %{emoji: emoji}) when is_binary(emoji) do
|
defp filter(reactions, %{emoji: emoji}) when is_binary(emoji) do
|
||||||
Enum.filter(reactions, fn [e, _] -> e == emoji end)
|
Enum.filter(reactions, fn [e, _, _] -> e == emoji end)
|
||||||
end
|
end
|
||||||
|
|
||||||
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 =
|
||||||
|
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)
|
||||||
|
|
||||||
@ -83,6 +89,11 @@ defmodule Pleroma.Web.PleromaAPI.EmojiReactionController do
|
|||||||
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)
|
||||||
|
|
||||||
|
@ -7,17 +7,31 @@ defmodule Pleroma.Web.PleromaAPI.EmojiReactionView do
|
|||||||
|
|
||||||
alias Pleroma.Web.MastodonAPI.AccountView
|
alias Pleroma.Web.MastodonAPI.AccountView
|
||||||
|
|
||||||
|
def emoji_name(emoji, nil), do: emoji
|
||||||
|
alias Pleroma.Web.MediaProxy
|
||||||
|
|
||||||
|
def emoji_name(emoji, url) do
|
||||||
|
url = URI.parse(url)
|
||||||
|
|
||||||
|
if url.host == Pleroma.Web.Endpoint.host() do
|
||||||
|
emoji
|
||||||
|
else
|
||||||
|
"#{emoji}@#{url.host}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def render("index.json", %{emoji_reactions: emoji_reactions} = opts) do
|
def render("index.json", %{emoji_reactions: emoji_reactions} = opts) do
|
||||||
render_many(emoji_reactions, __MODULE__, "show.json", opts)
|
render_many(emoji_reactions, __MODULE__, "show.json", opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def render("show.json", %{emoji_reaction: {emoji, user_ap_ids}, user: user}) do
|
def render("show.json", %{emoji_reaction: {emoji, user_ap_ids, url}, user: user}) do
|
||||||
users = fetch_users(user_ap_ids)
|
users = fetch_users(user_ap_ids)
|
||||||
|
|
||||||
%{
|
%{
|
||||||
name: emoji,
|
name: emoji_name(emoji, url),
|
||||||
count: length(users),
|
count: length(users),
|
||||||
accounts: render(AccountView, "index.json", users: users, for: user),
|
accounts: render(AccountView, "index.json", users: users, for: user),
|
||||||
|
url: MediaProxy.url(url),
|
||||||
me: !!(user && user.ap_id in user_ap_ids)
|
me: !!(user && user.ap_id in user_ap_ids)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
@ -17,23 +17,101 @@ defmodule Pleroma.Web.PleromaAPI.EmojiReactionControllerTest 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, data: %{"reactions" => [["👍", [other_user.ap_id], nil]]})
|
||||||
|
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.
|
|
||||||
assert %{"id" => id} = result
|
assert %{"id" => id} = result
|
||||||
assert to_string(activity.id) == id
|
assert to_string(activity.id) == id
|
||||||
|
|
||||||
assert result["pleroma"]["emoji_reactions"] == [
|
assert result["pleroma"]["emoji_reactions"] == [
|
||||||
%{"name" => "☕", "count" => 1, "me" => true}
|
%{
|
||||||
|
"name" => "👍",
|
||||||
|
"count" => 1,
|
||||||
|
"me" => true,
|
||||||
|
"url" => nil,
|
||||||
|
"account_ids" => [other_user.id]
|
||||||
|
},
|
||||||
|
%{
|
||||||
|
"name" => "\u26A0\uFE0F",
|
||||||
|
"count" => 1,
|
||||||
|
"me" => true,
|
||||||
|
"url" => nil,
|
||||||
|
"account_ids" => [other_user.id]
|
||||||
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
{:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
|
||||||
|
|
||||||
|
ObanHelpers.perform_all()
|
||||||
|
|
||||||
|
# Reacting with a custom emoji
|
||||||
|
result =
|
||||||
|
conn
|
||||||
|
|> assign(:user, other_user)
|
||||||
|
|> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
|
||||||
|
|> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:dinosaur:")
|
||||||
|
|> json_response_and_validate_schema(200)
|
||||||
|
|
||||||
|
assert %{"id" => id} = result
|
||||||
|
assert to_string(activity.id) == id
|
||||||
|
|
||||||
|
assert result["pleroma"]["emoji_reactions"] == [
|
||||||
|
%{
|
||||||
|
"name" => "dinosaur",
|
||||||
|
"count" => 1,
|
||||||
|
"me" => true,
|
||||||
|
"url" => "http://localhost:4001/emoji/dino walking.gif",
|
||||||
|
"account_ids" => [other_user.id]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
# Reacting with a remote emoji
|
||||||
|
note =
|
||||||
|
insert(:note,
|
||||||
|
user: user,
|
||||||
|
data: %{"reactions" => [["wow", [other_user.ap_id], "https://remote/emoji/wow"]]}
|
||||||
|
)
|
||||||
|
|
||||||
|
activity = insert(:note_activity, note: note, user: user)
|
||||||
|
|
||||||
|
result =
|
||||||
|
conn
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> assign(:token, insert(:oauth_token, user: user, scopes: ["write:statuses"]))
|
||||||
|
|> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:wow@remote:")
|
||||||
|
|> json_response(200)
|
||||||
|
|
||||||
|
assert result["pleroma"]["emoji_reactions"] == [
|
||||||
|
%{
|
||||||
|
"name" => "wow@remote",
|
||||||
|
"count" => 2,
|
||||||
|
"me" => true,
|
||||||
|
"url" => "https://remote/emoji/wow",
|
||||||
|
"account_ids" => [user.id, other_user.id]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
# Reacting with a remote custom emoji that hasn't been reacted with yet
|
||||||
|
note =
|
||||||
|
insert(:note,
|
||||||
|
user: user
|
||||||
|
)
|
||||||
|
|
||||||
|
activity = insert(:note_activity, note: note, user: user)
|
||||||
|
|
||||||
|
assert conn
|
||||||
|
|> assign(:user, user)
|
||||||
|
|> assign(:token, insert(:oauth_token, user: user, scopes: ["write:statuses"]))
|
||||||
|
|> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/:wow@remote:")
|
||||||
|
|> json_response(400)
|
||||||
|
|
||||||
# Reacting with a non-emoji
|
# Reacting with a non-emoji
|
||||||
assert conn
|
assert conn
|
||||||
|> assign(:user, other_user)
|
|> assign(:user, other_user)
|
||||||
@ -46,8 +124,21 @@ defmodule Pleroma.Web.PleromaAPI.EmojiReactionControllerTest 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,
|
||||||
|
data: %{"reactions" => [["wow", [user.ap_id], "https://remote/emoji/wow"]]}
|
||||||
|
)
|
||||||
|
|
||||||
|
activity = insert(:note_activity, note: note, user: user)
|
||||||
|
|
||||||
|
ObanHelpers.perform_all()
|
||||||
|
|
||||||
{:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
|
{:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
|
||||||
|
{:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, ":dinosaur:")
|
||||||
|
|
||||||
|
{:ok, _reaction_activity} =
|
||||||
|
CommonAPI.react_with_emoji(activity.id, other_user, ":wow@remote:")
|
||||||
|
|
||||||
ObanHelpers.perform_all()
|
ObanHelpers.perform_all()
|
||||||
|
|
||||||
@ -60,11 +151,47 @@ defmodule Pleroma.Web.PleromaAPI.EmojiReactionControllerTest do
|
|||||||
assert %{"id" => id} = json_response_and_validate_schema(result, 200)
|
assert %{"id" => id} = json_response_and_validate_schema(result, 200)
|
||||||
assert to_string(activity.id) == id
|
assert to_string(activity.id) == id
|
||||||
|
|
||||||
|
# Remove custom emoji
|
||||||
|
|
||||||
|
result =
|
||||||
|
conn
|
||||||
|
|> assign(:user, other_user)
|
||||||
|
|> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
|
||||||
|
|> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/:dinosaur:")
|
||||||
|
|
||||||
|
assert %{"id" => id} = json_response_and_validate_schema(result, 200)
|
||||||
|
assert to_string(activity.id) == id
|
||||||
|
|
||||||
ObanHelpers.perform_all()
|
ObanHelpers.perform_all()
|
||||||
|
|
||||||
object = Object.get_by_ap_id(activity.data["object"])
|
object = Object.get_by_ap_id(activity.data["object"])
|
||||||
|
|
||||||
assert object.data["reaction_count"] == 0
|
assert object.data["reaction_count"] == 2
|
||||||
|
|
||||||
|
# Remove custom remote emoji
|
||||||
|
result =
|
||||||
|
conn
|
||||||
|
|> assign(:user, other_user)
|
||||||
|
|> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
|
||||||
|
|> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/:wow@remote:")
|
||||||
|
|> json_response(200)
|
||||||
|
|
||||||
|
assert result["pleroma"]["emoji_reactions"] == [
|
||||||
|
%{
|
||||||
|
"name" => "wow@remote",
|
||||||
|
"count" => 1,
|
||||||
|
"me" => false,
|
||||||
|
"url" => "https://remote/emoji/wow",
|
||||||
|
"account_ids" => [user.id]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
# Remove custom remote emoji that hasn't been reacted with yet
|
||||||
|
assert conn
|
||||||
|
|> assign(:user, other_user)
|
||||||
|
|> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
|
||||||
|
|> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/:zoop@remote:")
|
||||||
|
|> json_response(400)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
|
test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
|
||||||
@ -181,7 +308,15 @@ defmodule Pleroma.Web.PleromaAPI.EmojiReactionControllerTest do
|
|||||||
{:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
|
{:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
|
||||||
{:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
|
{:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
|
||||||
|
|
||||||
assert [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] =
|
assert [
|
||||||
|
%{
|
||||||
|
"name" => "🎅",
|
||||||
|
"count" => 1,
|
||||||
|
"accounts" => [represented_user],
|
||||||
|
"me" => false,
|
||||||
|
"url" => nil
|
||||||
|
}
|
||||||
|
] =
|
||||||
conn
|
conn
|
||||||
|> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
|
|> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
|
||||||
|> json_response_and_validate_schema(200)
|
|> json_response_and_validate_schema(200)
|
||||||
|
Loading…
Reference in New Issue
Block a user