Show images, video, and audio attachments to notices.
authorPhil Hagelberg <phil@hagelb.org>
Tue, 29 Oct 2019 02:05:45 +0000 (19:05 -0700)
committerPhil Hagelberg <phil@hagelb.org>
Sun, 10 Nov 2019 02:07:08 +0000 (18:07 -0800)
lib/pleroma/web/static_fe/activity_representer.ex
lib/pleroma/web/static_fe/static_fe_view.ex
lib/pleroma/web/templates/layout/static_fe.html.eex
lib/pleroma/web/templates/static_fe/static_fe/_attachment.html.eex [new file with mode: 0644]
lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex

index 9bee732d512337af84388dbe506bdafbc2de4505..7b7e1730cc87adebe969735468e56ba712b879aa 100644 (file)
@@ -19,6 +19,8 @@ defmodule Pleroma.Web.StaticFE.ActivityRepresenter do
     |> set_content(object)
     |> set_link(activity.id)
     |> set_published(object)
+    |> set_sensitive(object)
+    |> set_attachment(object.data["attachment"])
     |> set_attachments(object)
   end
 
@@ -39,17 +41,23 @@ defmodule Pleroma.Web.StaticFE.ActivityRepresenter do
 
   defp set_content(data, _), do: Map.put(data, :content, nil)
 
+  defp set_attachment(data, attachment), do: Map.put(data, :attachment, attachment)
+
   defp set_link(data, activity_id),
     do: Map.put(data, :link, Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity_id))
 
   defp set_published(data, %Object{data: %{"published" => published}}),
     do: Map.put(data, :published, published)
 
+  defp set_sensitive(data, %Object{data: %{"sensitive" => sensitive}}),
+    do: Map.put(data, :sensitive, sensitive)
+
   # TODO: attachments
   defp set_attachments(data, _), do: Map.put(data, :attachments, [])
 
   def represent(activity_id) do
-    with %Activity{data: %{"type" => "Create"}} = activity <- Activity.get_by_id(activity_id),
+    with %Activity{data: %{"type" => "Create"}} = activity <-
+           Activity.get_by_id_with_object(activity_id),
          true <- Visibility.is_public?(activity),
          {:ok, %User{} = user} <- User.get_or_fetch(activity.data["actor"]) do
       {:ok, prepare_activity(user, activity)}
index ba67de83514cd29839dd8d366d5c6e1baff3f1cf..2b3b968d3ff6a0e33c8f14a75ab8846d49a3e332 100644 (file)
@@ -8,10 +8,13 @@ defmodule Pleroma.Web.StaticFE.StaticFEView do
   alias Pleroma.User
   alias Pleroma.Web.MediaProxy
   alias Pleroma.Formatter
+  alias Pleroma.Web.Metadata.Utils
   alias Pleroma.Web.Router.Helpers
 
   import Phoenix.HTML
 
+  @media_types ["image", "audio", "video"]
+
   def emoji_for_user(%User{} = user) do
     (user.source_data["tag"] || [])
     |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
@@ -19,4 +22,8 @@ defmodule Pleroma.Web.StaticFE.StaticFEView do
       {String.trim(name, ":"), url}
     end)
   end
+
+  def fetch_media_type(url) do
+    Utils.fetch_media_type(@media_types, url["mediaType"])
+  end
 end
index 7ce9ead90482e0a19c63d4c323e882cff5d0ae3a..c1fbd89cd19160ada18f78375019d1be0669ed50 100644 (file)
         margin-right: 4px;
       }
 
+      .activity-content img, video {
+        max-width: 800px;
+        max-height: 800px;
+      }
+
       a {
         color: white;
       }
diff --git a/lib/pleroma/web/templates/static_fe/static_fe/_attachment.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/_attachment.html.eex
new file mode 100644 (file)
index 0000000..7e04e95
--- /dev/null
@@ -0,0 +1,8 @@
+<%= case @mediaType do %>
+<% "audio" -> %>
+<audio src="<%= @url %>" controls="controls"></audio>
+<% "video" -> %>
+<video src="<%= @url %>" controls="controls"></video>
+<% _ -> %>
+<img src="<%= @url %>" alt="<%= @name %>" title="<%= @name %>">
+<% end %>
index d305d9057f1b73f5b4dc3366509aeb468cb867de..9a7824a326a14b99496579571f89a888b70c51c3 100644 (file)
@@ -1,15 +1,29 @@
 <div class="activity">
   <%= render("user_card.html", %{user: @data.user}) %>
+  <p class="pull-right">
+    <a href="<%= @data.link %>" class="activity-link"><%= @data.published %></a></p>
   <div class="activity-content">
     <%= if @data.title != "" do %>
-    <details>
-      <summary><%= raw @data.title %></summary>
-    <% end %>
+      <details>
+        <summary><%= raw @data.title %></summary>
+        <div class="e-content"><%= raw @data.content %></div>
+      </details>
+    <% else %>
       <div class="e-content"><%= raw @data.content %></div>
-    <%= if @data.title != "" do %>
-    </details>
     <% end %>
-    <p class="pull-right">
-      <a href="<%= @data.link %>" class="activity-link"><%= @data.published %></a></p>
+    <%= for %{"name" => name, "url" => [url | _]} <- @data.attachment do %>
+      <%= if @data.sensitive do %>
+        <details class="nsfw">
+          <summary>sensitive media</summary>
+          <div>
+            <%= render("_attachment.html", %{name: name, url: url["href"],
+                                             mediaType: fetch_media_type(url)}) %>
+          </div>
+        </details>
+      <% else %>
+        <%= render("_attachment.html", %{name: name, url: url["href"],
+                                         mediaType: fetch_media_type(url)}) %>
+      <% end %>
+    <% end %>
   </div>
 </div>