Allow reacting with remote emoji when they exist on the post (#200)
[akkoma] / lib / pleroma / web / mastodon_api / views / status_view.ex
index 463f3419855f75d0c4960f09b43d9b3900c87abb..0d2571ab8eeb8dc2db98c7b754acac06e11ca8d8 100644 (file)
@@ -57,11 +57,19 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     end)
   end
 
-  defp get_context_id(%{data: %{"context_id" => context_id}}) when not is_nil(context_id),
-    do: context_id
-
-  defp get_context_id(%{data: %{"context" => context}}) when is_binary(context),
-    do: Utils.context_to_conversation_id(context)
+  # DEPRECATED This field seems to be a left-over from the StatusNet era.
+  # If your application uses `pleroma.conversation_id`: this field is deprecated.
+  # It is currently stubbed instead by doing a CRC32 of the context, and
+  # clearing the MSB to avoid overflow exceptions with signed integers on the
+  # different clients using this field (Java/Kotlin code, mostly; see Husky.)
+  # This should be removed in a future version of Pleroma. Pleroma-FE currently
+  # depends on this field, as well.
+  defp get_context_id(%{data: %{"context" => context}}) when is_binary(context) do
+    use Bitwise
+
+    :erlang.crc32(context)
+    |> band(bnot(0x8000_0000))
+  end
 
   defp get_context_id(_), do: nil
 
@@ -80,7 +88,6 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
 
   def render("index.json", opts) do
     reading_user = opts[:for]
-
     # To do: check AdminAPIControllerTest on the reasons behind nil activities in the list
     activities = Enum.filter(opts.activities, & &1)
 
@@ -312,8 +319,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
         opts[:for],
         Map.get(opts, :with_muted, false)
       )
-      |> Stream.map(fn {emoji, users} ->
-        build_emoji_map(emoji, users, opts[:for])
+      |> Stream.map(fn {emoji, users, url} ->
+        build_emoji_map(emoji, users, url, opts[:for])
       end)
       |> Enum.to_list()
 
@@ -330,6 +337,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
 
     {pinned?, pinned_at} = pin_data(object, user)
 
+    quote = Activity.get_quoted_activity_from_object(object)
+
     %{
       id: to_string(activity.id),
       uri: object.data["id"],
@@ -364,9 +373,13 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
       application: build_application(object.data["generator"]),
       language: nil,
       emojis: build_emojis(object.data["emoji"]),
+      quote_id: if(quote, do: quote.id, else: nil),
+      quote: maybe_render_quote(quote, opts),
+      emoji_reactions: emoji_reactions,
       pleroma: %{
         local: activity.local,
         conversation_id: get_context_id(activity),
+        context: object.data["context"],
         in_reply_to_account_acct: reply_to_user && reply_to_user.nickname,
         content: %{"text/plain" => content_plaintext},
         spoiler_text: %{"text/plain" => summary},
@@ -376,6 +389,9 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
         emoji_reactions: emoji_reactions,
         parent_visible: visible_for_user?(reply_to, opts[:for]),
         pinned_at: pinned_at
+      },
+      akkoma: %{
+        source: object.data["source"]
       }
     }
   end
@@ -569,11 +585,13 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     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),
-      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
 
@@ -601,4 +619,23 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
   end
 
   defp build_image_url(_, _), do: nil
+
+  defp maybe_render_quote(nil, _), do: nil
+
+  defp maybe_render_quote(quote, opts) do
+    with %User{} = quoted_user <- User.get_cached_by_ap_id(quote.actor),
+         false <- Map.get(opts, :do_not_recurse, false),
+         true <- visible_for_user?(quote, opts[:for]),
+         false <- User.blocks?(opts[:for], quoted_user),
+         false <- User.mutes?(opts[:for], quoted_user) do
+      opts =
+        opts
+        |> Map.put(:activity, quote)
+        |> Map.put(:do_not_recurse, true)
+
+      render("show.json", opts)
+    else
+      _ -> nil
+    end
+  end
 end