Merge pull request 'Remove "default" image description' (#493) from ilja/akkoma:remov...
[akkoma] / lib / pleroma / web / mastodon_api / views / status_view.ex
index 8fbf9b6d91b199d3dc11f17442d45acb808ff798..47d1616c47b36794be2e830f8a33e45ea3f3255b 100644 (file)
@@ -21,6 +21,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
   alias Pleroma.Web.MastodonAPI.StatusView
   alias Pleroma.Web.MediaProxy
   alias Pleroma.Web.PleromaAPI.EmojiReactionController
+  require Logger
 
   import Pleroma.Web.ActivityPub.Visibility, only: [get_visibility: 1, visible_for_user?: 2]
 
@@ -28,7 +29,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
   # to fetch the preview. However it should be fine considering
   # pagination is restricted to 40 activities at a time
   defp fetch_rich_media_for_activities(activities) do
-    Enum.map(activities, fn activity ->
+    Enum.each(activities, fn activity ->
       spawn(fn ->
         Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
       end)
@@ -87,6 +88,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
   defp reblogged?(_activity, _user), do: false
 
   def render("index.json", opts) do
+    Logger.debug("Rendering index")
     reading_user = opts[:for]
     # To do: check AdminAPIControllerTest on the reasons behind nil activities in the list
     activities = Enum.filter(opts.activities, & &1)
@@ -131,13 +133,15 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
       |> Map.put(:parent_activities, parent_activities)
       |> Map.put(:relationships, relationships_opt)
 
-    safe_render_many(activities, StatusView, "show.json", opts)
+    render_many(activities, StatusView, "show.json", opts)
   end
 
   def render(
         "show.json",
-        %{activity: %{data: %{"type" => "Announce", "object" => _object}} = activity} = opts
+        %{activity: %{id: id, data: %{"type" => "Announce", "object" => _object}} = activity} =
+          opts
       ) do
+    Logger.debug("Rendering reblog #{id}")
     user = CommonAPI.get_user(activity.data["actor"])
     created_at = Utils.to_masto_date(activity.data["published"])
     object = Object.normalize(activity, fetch: false)
@@ -169,6 +173,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
       |> Enum.map(fn user -> AccountView.render("mention.json", %{user: user}) end)
 
     {pinned?, pinned_at} = pin_data(object, user)
+    lang = language(object)
 
     %{
       id: to_string(activity.id),
@@ -182,7 +187,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
       in_reply_to_id: nil,
       in_reply_to_account_id: nil,
       reblog: reblogged,
-      content: reblogged[:content] || "",
+      content: "",
       created_at: created_at,
       reblogs_count: 0,
       replies_count: 0,
@@ -199,7 +204,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
       mentions: mentions,
       tags: reblogged[:tags] || [],
       application: build_application(object.data["generator"]),
-      language: nil,
+      language: lang,
       emojis: [],
       pleroma: %{
         local: activity.local,
@@ -208,7 +213,9 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     }
   end
 
-  def render("show.json", %{activity: %{data: %{"object" => _object}} = activity} = opts) do
+  def render("show.json", %{activity: %{id: id, data: %{"object" => _object}} = activity} = opts) do
+    Logger.debug("Rendering status #{id}")
+
     with %Object{} = object <- Object.normalize(activity, fetch: false) do
       user = CommonAPI.get_user(activity.data["actor"])
       user_follower_address = user.follower_address
@@ -226,8 +233,10 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
         |> Enum.filter(fn tag -> is_map(tag) and tag["type"] == "Mention" end)
         |> Enum.map(fn tag -> tag["href"] end)
 
+      to_data = if is_nil(object.data["to"]), do: [], else: object.data["to"]
+
       mentions =
-        (object.data["to"] ++ tag_mentions)
+        (to_data ++ tag_mentions)
         |> Enum.uniq()
         |> Enum.map(fn
           Pleroma.Constants.as_public() -> nil
@@ -357,6 +366,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
       {pinned?, pinned_at} = pin_data(object, user)
 
       quote = Activity.get_quoted_activity_from_object(object)
+      lang = language(object)
 
       %{
         id: to_string(activity.id),
@@ -391,7 +401,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
         mentions: mentions,
         tags: build_tags(tags),
         application: build_application(object.data["generator"]),
-        language: nil,
+        language: lang,
         emojis: build_emojis(object.data["emoji"]),
         quote_id: if(quote, do: quote.id, else: nil),
         quote: maybe_render_quote(quote, opts),
@@ -424,6 +434,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
   end
 
   def render("history.json", %{activity: %{data: %{"object" => _object}} = activity} = opts) do
+    Logger.debug("Rendering history for #{activity.id}")
     object = Object.normalize(activity, fetch: false)
 
     hashtags = Object.hashtags(object)
@@ -610,6 +621,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
   def render("attachment_meta.json", _), do: nil
 
   def render("context.json", %{activity: activity, activities: activities, user: user}) do
+    Logger.debug("Rendering context for #{activity.id}")
+
     %{ancestors: ancestors, descendants: descendants} =
       activities
       |> Enum.reverse()
@@ -784,4 +797,12 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
   defp get_source_content_type(_source) do
     Utils.get_content_type(nil)
   end
+
+  defp language(%Object{data: %{"contentMap" => contentMap}}) when is_map(contentMap) do
+    contentMap
+    |> Map.keys()
+    |> Enum.at(0)
+  end
+
+  defp language(_), do: nil
 end