Allow reacting with remote emoji when they exist on the post (#200)
[akkoma] / lib / pleroma / web / pleroma_api / views / emoji_reaction_view.ex
index 84d2d303dae6223a9c53a8f48a72d38d64cd6c91..4335228b6b10ee00ed0c09438c68921bdeac8775 100644 (file)
@@ -1,23 +1,37 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.Web.PleromaAPI.EmojiReactionView do
   use Pleroma.Web, :view
 
   alias Pleroma.Web.MastodonAPI.AccountView
+  alias Pleroma.Web.MediaProxy
+
+  def emoji_name(emoji, nil), do: emoji
+
+  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
     render_many(emoji_reactions, __MODULE__, "show.json", opts)
   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)
 
     %{
-      name: emoji,
+      name: emoji_name(emoji, url),
       count: length(users),
-      accounts: render(AccountView, "index.json", users: users, for: user, as: :user),
+      accounts: render(AccountView, "index.json", users: users, for: user),
+      url: MediaProxy.url(url),
       me: !!(user && user.ap_id in user_ap_ids)
     }
   end
@@ -26,7 +40,7 @@ defmodule Pleroma.Web.PleromaAPI.EmojiReactionView do
     user_ap_ids
     |> Enum.map(&Pleroma.User.get_cached_by_ap_id/1)
     |> Enum.filter(fn
-      %{deactivated: false} -> true
+      %{is_active: true} -> true
       _ -> false
     end)
   end