1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.PleromaAPI.EmojiReactionController do
6 use Pleroma.Web, :controller
10 alias Pleroma.Plugs.OAuthScopesPlug
11 alias Pleroma.Web.CommonAPI
12 alias Pleroma.Web.MastodonAPI.StatusView
14 plug(Pleroma.Web.ApiSpec.CastAndValidate)
15 plug(OAuthScopesPlug, %{scopes: ["write:statuses"]} when action in [:create, :delete])
19 %{scopes: ["read:statuses"], fallback: :proceed_unauthenticated}
23 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.EmojiReactionOperation
25 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
27 def index(%{assigns: %{user: user}} = conn, %{id: activity_id} = params) do
28 with true <- Pleroma.Config.get([:instance, :show_reactions]),
29 %Activity{} = activity <- Activity.get_by_id_with_object(activity_id),
30 %Object{data: %{"reactions" => reactions}} when is_list(reactions) <-
31 Object.normalize(activity) do
32 reactions = filter(reactions, params)
33 render(conn, "index.json", emoji_reactions: reactions, user: user)
39 defp filter(reactions, %{emoji: emoji}) when is_binary(emoji) do
40 Enum.filter(reactions, fn [e, _] -> e == emoji end)
43 defp filter(reactions, _), do: reactions
45 def create(%{assigns: %{user: user}} = conn, %{id: activity_id, emoji: emoji}) do
46 with {:ok, _activity} <- CommonAPI.react_with_emoji(activity_id, user, emoji) do
47 activity = Activity.get_by_id(activity_id)
50 |> put_view(StatusView)
51 |> render("show.json", activity: activity, for: user, as: :activity)
55 def delete(%{assigns: %{user: user}} = conn, %{id: activity_id, emoji: emoji}) do
56 with {:ok, _activity} <- CommonAPI.unreact_with_emoji(activity_id, user, emoji) do
57 activity = Activity.get_by_id(activity_id)
60 |> put_view(StatusView)
61 |> render("show.json", activity: activity, for: user, as: :activity)