Merge branch 'feature/html-scrub-policy' into 'develop'
[akkoma] / lib / pleroma / web / mastodon_api / views / status_view.ex
index df360644a515a3c71bcf64aa760f76335595e14a..8f6c4b062795fbde1040e7ac5f0dbf72697c11b2 100644 (file)
@@ -5,6 +5,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
   alias Pleroma.Web.CommonAPI.Utils
   alias Pleroma.Web.MediaProxy
   alias Pleroma.Repo
+  alias Pleroma.HTML
 
   # TODO: Add cached version.
   defp get_replied_to_activities(activities) do
@@ -54,8 +55,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     %{
       id: to_string(activity.id),
       uri: object,
-      # TODO: This might be wrong, check with mastodon.
-      url: nil,
+      url: object,
       account: AccountView.render("account.json", %{user: user}),
       in_reply_to_id: nil,
       in_reply_to_account_id: nil,
@@ -82,19 +82,6 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     }
   end
 
-  def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
-    id = activity.data["object"]["inReplyTo"]
-    replied_to_activities[activity.data["object"]["inReplyTo"]]
-  end
-
-  def get_reply_to(%{data: %{"object" => object}}, _) do
-    if object["inReplyTo"] && object["inReplyTo"] != "" do
-      Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
-    else
-      nil
-    end
-  end
-
   def render("status.json", %{activity: %{data: %{"object" => object}} = activity} = opts) do
     user = User.get_cached_by_ap_id(activity.data["actor"])
 
@@ -113,8 +100,9 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
     favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
 
-    attachments =
-      render_many(object["attachment"] || [], StatusView, "attachment.json", as: :attachment)
+    attachment_data = object["attachment"] || []
+    attachment_data = attachment_data ++ if object["type"] == "Video", do: [object], else: []
+    attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)
 
     created_at = Utils.to_masto_date(object["published"])
 
@@ -124,13 +112,13 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     emojis =
       (activity.data["object"]["emoji"] || [])
       |> Enum.map(fn {name, url} ->
-        name = HtmlSanitizeEx.strip_tags(name)
+        name = HTML.strip_tags(name)
 
         url =
-          HtmlSanitizeEx.strip_tags(url)
+          HTML.strip_tags(url)
           |> MediaProxy.url()
 
-        %{shortcode: name, url: url, static_url: url}
+        %{shortcode: name, url: url, static_url: url, visible_in_picker: false}
       end)
 
     %{
@@ -138,10 +126,10 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
       uri: object["id"],
       url: object["external_url"] || object["id"],
       account: AccountView.render("account.json", %{user: user}),
-      in_reply_to_id: reply_to && reply_to.id,
-      in_reply_to_account_id: reply_to_user && reply_to_user.id,
+      in_reply_to_id: reply_to && to_string(reply_to.id),
+      in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
       reblog: nil,
-      content: HtmlSanitizeEx.basic_html(object["content"]),
+      content: render_content(object),
       created_at: created_at,
       reblogs_count: announcement_count,
       favourites_count: like_count,
@@ -164,21 +152,10 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     }
   end
 
-  def get_visibility(object) do
-    public = "https://www.w3.org/ns/activitystreams#Public"
-    to = object["to"] || []
-    cc = object["cc"] || []
-
-    cond do
-      public in to -> "public"
-      public in cc -> "unlisted"
-      Enum.any?(to, &String.contains?(&1, "/followers")) -> "private"
-      true -> "direct"
-    end
-  end
-
   def render("attachment.json", %{attachment: attachment}) do
-    [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
+    [attachment_url | _] = attachment["url"]
+    media_type = attachment_url["mediaType"] || attachment_url["mimeType"]
+    href = attachment_url["href"]
 
     type =
       cond do
@@ -196,7 +173,72 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
       remote_url: href,
       preview_url: MediaProxy.url(href),
       text_url: href,
-      type: type
+      type: type,
+      description: attachment["name"]
     }
   end
+
+  def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
+    _id = activity.data["object"]["inReplyTo"]
+    replied_to_activities[activity.data["object"]["inReplyTo"]]
+  end
+
+  def get_reply_to(%{data: %{"object" => object}}, _) do
+    if object["inReplyTo"] && object["inReplyTo"] != "" do
+      Activity.get_create_activity_by_object_ap_id(object["inReplyTo"])
+    else
+      nil
+    end
+  end
+
+  def get_visibility(object) do
+    public = "https://www.w3.org/ns/activitystreams#Public"
+    to = object["to"] || []
+    cc = object["cc"] || []
+
+    cond do
+      public in to ->
+        "public"
+
+      public in cc ->
+        "unlisted"
+
+      # this should use the sql for the object's activity
+      Enum.any?(to, &String.contains?(&1, "/followers")) ->
+        "private"
+
+      true ->
+        "direct"
+    end
+  end
+
+  def render_content(%{"type" => "Video"} = object) do
+    name = object["name"]
+
+    content =
+      if !!name and name != "" do
+        "<p><a href=\"#{object["id"]}\">#{name}</a></p>#{object["content"]}"
+      else
+        object["content"]
+      end
+
+    HTML.filter_tags(content)
+  end
+
+  def render_content(%{"type" => "Article"} = object) do
+    summary = object["name"]
+
+    content =
+      if !!summary and summary != "" do
+        "<p><a href=\"#{object["url"]}\">#{summary}</a></p>#{object["content"]}"
+      else
+        object["content"]
+      end
+
+    HTML.filter_tags(content)
+  end
+
+  def render_content(object) do
+    HTML.filter_tags(object["content"])
+  end
 end