static fe: add user profile rendering
authorWilliam Pitcock <nenolod@dereferenced.org>
Sat, 2 Mar 2019 21:12:38 +0000 (21:12 +0000)
committerPhil Hagelberg <phil@hagelb.org>
Sun, 10 Nov 2019 02:06:51 +0000 (18:06 -0800)
lib/pleroma/web/static_fe/activity_representer.ex
lib/pleroma/web/static_fe/static_fe_controller.ex
lib/pleroma/web/static_fe/static_fe_view.ex
lib/pleroma/web/static_fe/user_representer.ex [new file with mode: 0644]
lib/pleroma/web/templates/layout/static_fe.html.eex
lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex
lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex [new file with mode: 0644]
lib/pleroma/web/templates/static_fe/static_fe/user_card.html.eex

index 93f8a8813e584f6bcdfface2c7200fe7b25fe3da..9c43156109b1f5b4a87967c81c237fe58680824d 100644 (file)
@@ -17,6 +17,9 @@ defmodule Pleroma.Web.StaticFE.ActivityRepresenter do
     |> set_attachments(object)
   end
 
+  def prepare_activity(%User{} = user, %Activity{} = activity), do:
+    prepare_activity(user, Object.normalize(activity.data["object"]))
+
   defp set_user(data, %User{} = user), do: Map.put(data, :user, user)
 
   defp set_object(data, %Object{} = object), do: Map.put(data, :object, object)
index 7d7cb6ddd55d77aac23004f1b2c6d8628d7b0dfd..78cd325c8ba1a16e806a6021a0a4e4926628aa0a 100644 (file)
@@ -6,6 +6,7 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do
   use Pleroma.Web, :controller
 
   alias Pleroma.Web.StaticFE.ActivityRepresenter
+  alias Pleroma.Web.StaticFE.UserRepresenter
 
   require Logger
 
@@ -15,7 +16,22 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do
       |> put_layout(:static_fe)
       |> put_status(200)
       |> put_view(Pleroma.Web.StaticFE.StaticFEView)
-      |> render("notice.html", data)
+      |> render("notice.html", %{data: data})
+    else
+      _ ->
+        conn
+        |> put_status(404)
+        |> text("Not found")
+    end
+  end
+
+  def show_user(conn, %{"username_or_id" => username_or_id}) do
+    with {:ok, data} <- UserRepresenter.represent(username_or_id) do
+      conn
+      |> put_layout(:static_fe)
+      |> put_status(200)
+      |> put_view(Pleroma.Web.StaticFE.StaticFEView)
+      |> render("profile.html", %{data: data})
     else
       _ ->
         conn
@@ -27,6 +43,12 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do
   def show(%{path_info: ["notice", notice_id]} = conn, _params),
     do: show_notice(conn, %{"notice_id" => notice_id})
 
+  def show(%{path_info: ["users", user_id]} = conn, _params),
+    do: show_user(conn, %{"username_or_id" => user_id})
+
+  def show(%{path_info: [user_id]} = conn, _params),
+    do: show_user(conn, %{"username_or_id" => user_id})
+
   # Fallback for unhandled types
   def show(conn, _params) do
     conn
index 7f58e1b2d84139c26de2487686b3763c44ad1b49..71e47d2c16fba1f05a646d66d7f1848810c1b9b4 100644 (file)
@@ -9,6 +9,8 @@ defmodule Pleroma.Web.StaticFE.StaticFEView do
   alias Pleroma.Web.MediaProxy
   alias Pleroma.Formatter
 
+  import Phoenix.HTML
+
   def emoji_for_user(%User{} = user) do
     (user.info.source_data["tag"] || [])
     |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
diff --git a/lib/pleroma/web/static_fe/user_representer.ex b/lib/pleroma/web/static_fe/user_representer.ex
new file mode 100644 (file)
index 0000000..9d2f1eb
--- /dev/null
@@ -0,0 +1,35 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.StaticFE.UserRepresenter do
+  alias Pleroma.User
+  alias Pleroma.Web.ActivityPub.ActivityPub
+  alias Pleroma.Web.StaticFE.ActivityRepresenter
+
+  def prepare_user(%User{} = user) do
+    %{}
+    |> set_user(user)
+    |> set_timeline(user)
+  end
+
+  defp set_user(data, %User{} = user), do: Map.put(data, :user, user)
+
+  defp set_timeline(data, %User{} = user) do
+    activities =
+      ActivityPub.fetch_user_activities(user, nil, %{})
+      |> Enum.map(fn activity -> ActivityRepresenter.prepare_activity(user, activity) end)
+
+    Map.put(data, :timeline, activities)
+  end
+
+  def represent(username_or_id) do
+    with %User{} = user <- User.get_cached_by_nickname_or_id(username_or_id),
+         data <- prepare_user(user) do
+      {:ok, data}
+    else
+      e ->
+        {:error, e}
+    end
+  end
+end
index c20958162177d2b5a03f9469bb00660158b2c57f..40a5604608e24a917db443fa4d8df0145ddf7a0e 100644 (file)
         border-radius: 4px;
       }
 
+      .activity {
+        margin-bottom: 1em;
+      }
+
       .avatar {
         cursor: pointer;
       }
index 9957697ad5aa16bbdc512d14149d74b301853892..e8d905d79b005c9718fc2e9ff69586f8358b5dc3 100644 (file)
@@ -1,6 +1,6 @@
 <div class="activity">
-  <%= render("user_card.html", %{user: @user}) %>
+  <%= render("user_card.html", %{user: @data.user}) %>
   <div class="activity-content">
-    <div class="e-content"><%= @object.data["content"] %></div>
+    <div class="e-content"><%= raw @data.content %></div>
   </div>
 </div>
diff --git a/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex
new file mode 100644 (file)
index 0000000..9ae4139
--- /dev/null
@@ -0,0 +1,7 @@
+<h1><%= raw (@data.user.name |> Formatter.emojify(emoji_for_user(@data.user))) %></h1>
+<p><%= raw @data.user.bio %></p>
+<div class="activity-stream">
+<%= for activity <- @data.timeline do %>
+  <%= render("notice.html", %{data: activity}) %>
+<% end %>
+</div>
index 8b397c639a457b2f61fae74f934bb6ae136d990d..c7789f9acb146096d8b772ff9393687161c4f5f7 100644 (file)
@@ -4,7 +4,7 @@
       <img src="<%= User.avatar_url(@user) |> MediaProxy.url %>" width="48" height="48" alt="">
     </div>
     <span class="display-name">
-      <bdi><%= @user.name |> Formatter.emojify(emoji_for_user(@user)) %></bdi>
+      <bdi><%= raw (@user.name |> Formatter.emojify(emoji_for_user(@user))) %></bdi>
       <span class="nickname"><%= @user.nickname %></span>
     </span>
   </a>