2020-05-19 13:50:49 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-05-19 13:50:49 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.PleromaAPI.EmojiReactionController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.Object
|
2020-11-16 11:23:25 -07:00
|
|
|
alias Pleroma.User
|
2020-05-19 13:50:49 -06:00
|
|
|
alias Pleroma.Web.CommonAPI
|
|
|
|
alias Pleroma.Web.MastodonAPI.StatusView
|
2020-06-24 04:07:47 -06:00
|
|
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
2020-05-19 13:50:49 -06:00
|
|
|
|
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
|
|
|
plug(OAuthScopesPlug, %{scopes: ["write:statuses"]} when action in [:create, :delete])
|
|
|
|
|
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["read:statuses"], fallback: :proceed_unauthenticated}
|
|
|
|
when action == :index
|
|
|
|
)
|
|
|
|
|
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.EmojiReactionOperation
|
|
|
|
|
2020-05-22 06:38:28 -06:00
|
|
|
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
|
|
|
|
|
2020-05-19 13:50:49 -06:00
|
|
|
def index(%{assigns: %{user: user}} = conn, %{id: activity_id} = params) do
|
2020-07-28 13:49:49 -06:00
|
|
|
with true <- Pleroma.Config.get([:instance, :show_reactions]),
|
|
|
|
%Activity{} = activity <- Activity.get_by_id_with_object(activity_id),
|
2020-05-19 13:50:49 -06:00
|
|
|
%Object{data: %{"reactions" => reactions}} when is_list(reactions) <-
|
2021-01-04 05:38:31 -07:00
|
|
|
Object.normalize(activity, fetch: false) do
|
2020-11-16 11:23:25 -07:00
|
|
|
reactions =
|
|
|
|
reactions
|
|
|
|
|> filter(params)
|
|
|
|
|> filter_allowed_users(user, Map.get(params, :with_muted, false))
|
|
|
|
|
2020-05-19 13:50:49 -06:00
|
|
|
render(conn, "index.json", emoji_reactions: reactions, user: user)
|
|
|
|
else
|
|
|
|
_e -> json(conn, [])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-16 11:23:25 -07:00
|
|
|
def filter_allowed_users(reactions, user, with_muted) do
|
|
|
|
exclude_ap_ids =
|
|
|
|
if is_nil(user) do
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
User.cached_blocked_users_ap_ids(user) ++
|
|
|
|
if not with_muted, do: User.cached_muted_users_ap_ids(user), else: []
|
|
|
|
end
|
|
|
|
|
2022-06-07 19:42:44 -06:00
|
|
|
filter_emoji = fn emoji, users, url ->
|
2020-11-16 11:23:25 -07:00
|
|
|
case Enum.reject(users, &(&1 in exclude_ap_ids)) do
|
|
|
|
[] -> nil
|
2022-06-07 19:42:44 -06:00
|
|
|
users -> {emoji, users, url}
|
2020-11-16 11:23:25 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
reactions
|
|
|
|
|> Stream.map(fn
|
2022-06-07 19:42:44 -06:00
|
|
|
[emoji, users, url] when is_list(users) -> filter_emoji.(emoji, users, url)
|
|
|
|
{emoji, users, url} when is_list(users) -> filter_emoji.(emoji, users, url)
|
2022-06-11 09:14:22 -06:00
|
|
|
{emoji, users} when is_list(users) -> filter_emoji.(emoji, users, nil)
|
2020-11-16 11:23:25 -07:00
|
|
|
_ -> nil
|
|
|
|
end)
|
|
|
|
|> Stream.reject(&is_nil/1)
|
|
|
|
end
|
|
|
|
|
2020-05-19 13:50:49 -06:00
|
|
|
defp filter(reactions, %{emoji: emoji}) when is_binary(emoji) do
|
2022-06-11 07:21:50 -06:00
|
|
|
Enum.filter(reactions, fn [e, _, _] -> e == emoji end)
|
2020-05-19 13:50:49 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
defp filter(reactions, _), do: reactions
|
|
|
|
|
|
|
|
def create(%{assigns: %{user: user}} = conn, %{id: activity_id, emoji: emoji}) do
|
|
|
|
with {:ok, _activity} <- CommonAPI.react_with_emoji(activity_id, user, emoji) do
|
|
|
|
activity = Activity.get_by_id(activity_id)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_view(StatusView)
|
|
|
|
|> render("show.json", activity: activity, for: user, as: :activity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(%{assigns: %{user: user}} = conn, %{id: activity_id, emoji: emoji}) do
|
|
|
|
with {:ok, _activity} <- CommonAPI.unreact_with_emoji(activity_id, user, emoji) do
|
|
|
|
activity = Activity.get_by_id(activity_id)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_view(StatusView)
|
|
|
|
|> render("show.json", activity: activity, for: user, as: :activity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|