85eb09d25aed39231f057a6b5ccb7f31e2122c72
[akkoma] / lib / pleroma / web / pleroma_api / views / emoji_reaction_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.EmojiReactionView do
6 use Pleroma.Web, :view
7
8 alias Pleroma.Web.MastodonAPI.AccountView
9 alias Pleroma.Web.MediaProxy
10
11 def render("index.json", %{emoji_reactions: emoji_reactions} = opts) do
12 render_many(emoji_reactions, __MODULE__, "show.json", opts)
13 end
14
15 def render("show.json", %{emoji_reaction: {emoji, user_ap_ids, url}, user: user}) do
16 users = fetch_users(user_ap_ids)
17 %{
18 name: emoji,
19 count: length(users),
20 accounts: render(AccountView, "index.json", users: users, for: user),
21 url: MediaProxy.url(url),
22 me: !!(user && user.ap_id in user_ap_ids)
23 }
24 end
25
26 defp fetch_users(user_ap_ids) do
27 user_ap_ids
28 |> Enum.map(&Pleroma.User.get_cached_by_ap_id/1)
29 |> Enum.filter(fn
30 %{is_active: true} -> true
31 _ -> false
32 end)
33 end
34 end