From a4d3a8ec03ec10bb7a9ba3c3e69d9ddc559ed2a0 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 2 Mar 2019 14:17:55 +0000 Subject: [PATCH 01/40] static fe: proof of concept --- .../web/static_fe/static_fe_controller.ex | 42 +++++ lib/pleroma/web/static_fe/static_fe_view.ex | 19 +++ .../web/templates/layout/static_fe.html.eex | 150 ++++++++++++++++++ .../static_fe/static_fe/notice.html.eex | 6 + .../static_fe/static_fe/user_card.html.eex | 11 ++ 5 files changed, 228 insertions(+) create mode 100644 lib/pleroma/web/static_fe/static_fe_controller.ex create mode 100644 lib/pleroma/web/static_fe/static_fe_view.ex create mode 100644 lib/pleroma/web/templates/layout/static_fe.html.eex create mode 100644 lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex create mode 100644 lib/pleroma/web/templates/static_fe/static_fe/user_card.html.eex diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex new file mode 100644 index 000000000..067af9816 --- /dev/null +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -0,0 +1,42 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.StaticFE.StaticFEController do + use Pleroma.Web, :controller + + alias Pleroma.Repo + alias Pleroma.Activity + alias Pleroma.Object + alias Pleroma.User + alias Pleroma.Web.ActivityPub.Visibility + + require Logger + + def show_notice(conn, %{"notice_id" => notice_id}) do + with %Activity{} = activity <- Repo.get(Activity, notice_id), + true <- Visibility.is_public?(activity), + %User{} = user <- User.get_or_fetch(activity.data["actor"]), + %Object{} = object <- Object.normalize(activity.data["object"]) do + conn + |> put_layout(:static_fe) + |> put_status(200) + |> put_view(Pleroma.Web.StaticFE.StaticFEView) + |> render("notice.html", %{notice: activity, object: object, user: user}) + else + _ -> + conn + |> put_status(404) + |> text("Not found") + end + end + + def show(%{path_info: ["notice", notice_id]} = conn, _params), do: show_notice(conn, %{"notice_id" => notice_id}) + + # Fallback for unhandled types + def show(conn, _params) do + conn + |> put_status(404) + |> text("Not found") + end +end diff --git a/lib/pleroma/web/static_fe/static_fe_view.ex b/lib/pleroma/web/static_fe/static_fe_view.ex new file mode 100644 index 000000000..7f58e1b2d --- /dev/null +++ b/lib/pleroma/web/static_fe/static_fe_view.ex @@ -0,0 +1,19 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.StaticFE.StaticFEView do + use Pleroma.Web, :view + + alias Pleroma.User + alias Pleroma.Web.MediaProxy + alias Pleroma.Formatter + + def emoji_for_user(%User{} = user) do + (user.info.source_data["tag"] || []) + |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end) + |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} -> + {String.trim(name, ":"), url} + end) + end +end diff --git a/lib/pleroma/web/templates/layout/static_fe.html.eex b/lib/pleroma/web/templates/layout/static_fe.html.eex new file mode 100644 index 000000000..c20958162 --- /dev/null +++ b/lib/pleroma/web/templates/layout/static_fe.html.eex @@ -0,0 +1,150 @@ + + + + + + + <%= Application.get_env(:pleroma, :instance)[:name] %> + + + + +
+ <%= render @view_module, @view_template, assigns %> +
+ + diff --git a/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex new file mode 100644 index 000000000..9957697ad --- /dev/null +++ b/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex @@ -0,0 +1,6 @@ +
+ <%= render("user_card.html", %{user: @user}) %> +
+
<%= @object.data["content"] %>
+
+
diff --git a/lib/pleroma/web/templates/static_fe/static_fe/user_card.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/user_card.html.eex new file mode 100644 index 000000000..8b397c639 --- /dev/null +++ b/lib/pleroma/web/templates/static_fe/static_fe/user_card.html.eex @@ -0,0 +1,11 @@ + From ff8d0902f351f871ab34ae7b127dd3e02e8af4cd Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 2 Mar 2019 14:23:26 +0000 Subject: [PATCH 02/40] static fe: formatting --- lib/pleroma/web/static_fe/static_fe_controller.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index 067af9816..2ac857759 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -31,7 +31,8 @@ def show_notice(conn, %{"notice_id" => notice_id}) do end end - def show(%{path_info: ["notice", notice_id]} = conn, _params), do: show_notice(conn, %{"notice_id" => notice_id}) + def show(%{path_info: ["notice", notice_id]} = conn, _params), + do: show_notice(conn, %{"notice_id" => notice_id}) # Fallback for unhandled types def show(conn, _params) do From 8f08da750aaa8e04c4a940a566ec831a559a4852 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 2 Mar 2019 20:10:30 +0000 Subject: [PATCH 03/40] static fe: use a generic activity representer to render activities --- .../web/static_fe/activity_representer.ex | 52 +++++++++++++++++++ .../web/static_fe/static_fe_controller.ex | 13 ++--- 2 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 lib/pleroma/web/static_fe/activity_representer.ex diff --git a/lib/pleroma/web/static_fe/activity_representer.ex b/lib/pleroma/web/static_fe/activity_representer.ex new file mode 100644 index 000000000..93f8a8813 --- /dev/null +++ b/lib/pleroma/web/static_fe/activity_representer.ex @@ -0,0 +1,52 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.StaticFE.ActivityRepresenter do + alias Pleroma.Activity + alias Pleroma.Object + alias Pleroma.User + alias Pleroma.Web.ActivityPub.Visibility + + def prepare_activity(%User{} = user, %Object{} = object) do + %{} + |> set_user(user) + |> set_object(object) + |> set_title(object) + |> set_content(object) + |> set_attachments(object) + end + + defp set_user(data, %User{} = user), do: Map.put(data, :user, user) + + defp set_object(data, %Object{} = object), do: Map.put(data, :object, object) + + defp set_title(data, %Object{data: %{"name" => name}}) when is_binary(name), + do: Map.put(data, :title, name) + + defp set_title(data, %Object{data: %{"summary" => summary}}) when is_binary(summary), + do: Map.put(data, :title, summary) + + defp set_title(data, _), do: Map.put(data, :title, nil) + + defp set_content(data, %Object{data: %{"content" => content}}) when is_binary(content), + do: Map.put(data, :content, content) + + defp set_content(data, _), do: Map.put(data, :content, nil) + + # 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), + true <- Visibility.is_public?(activity), + %Object{} = object <- Object.normalize(activity.data["object"]), + %User{} = user <- User.get_or_fetch(activity.data["actor"]), + data <- prepare_activity(user, object) do + {:ok, data} + else + e -> + {:error, e} + end + end +end diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index 2ac857759..7d7cb6ddd 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -5,24 +5,17 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do use Pleroma.Web, :controller - alias Pleroma.Repo - alias Pleroma.Activity - alias Pleroma.Object - alias Pleroma.User - alias Pleroma.Web.ActivityPub.Visibility + alias Pleroma.Web.StaticFE.ActivityRepresenter require Logger def show_notice(conn, %{"notice_id" => notice_id}) do - with %Activity{} = activity <- Repo.get(Activity, notice_id), - true <- Visibility.is_public?(activity), - %User{} = user <- User.get_or_fetch(activity.data["actor"]), - %Object{} = object <- Object.normalize(activity.data["object"]) do + with {:ok, data} <- ActivityRepresenter.represent(notice_id) do conn |> put_layout(:static_fe) |> put_status(200) |> put_view(Pleroma.Web.StaticFE.StaticFEView) - |> render("notice.html", %{notice: activity, object: object, user: user}) + |> render("notice.html", data) else _ -> conn From 2b5bd5236d793edba54366ec9fed447207a09976 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 2 Mar 2019 21:12:38 +0000 Subject: [PATCH 04/40] static fe: add user profile rendering --- .../web/static_fe/activity_representer.ex | 3 ++ .../web/static_fe/static_fe_controller.ex | 24 ++++++++++++- lib/pleroma/web/static_fe/static_fe_view.ex | 2 ++ lib/pleroma/web/static_fe/user_representer.ex | 35 +++++++++++++++++++ .../web/templates/layout/static_fe.html.eex | 4 +++ .../static_fe/static_fe/notice.html.eex | 4 +-- .../static_fe/static_fe/profile.html.eex | 7 ++++ .../static_fe/static_fe/user_card.html.eex | 2 +- 8 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 lib/pleroma/web/static_fe/user_representer.ex create mode 100644 lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex diff --git a/lib/pleroma/web/static_fe/activity_representer.ex b/lib/pleroma/web/static_fe/activity_representer.ex index 93f8a8813..9c4315610 100644 --- a/lib/pleroma/web/static_fe/activity_representer.ex +++ b/lib/pleroma/web/static_fe/activity_representer.ex @@ -17,6 +17,9 @@ def prepare_activity(%User{} = user, %Object{} = object) 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) diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index 7d7cb6ddd..78cd325c8 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -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 @@ def show_notice(conn, %{"notice_id" => notice_id}) 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 @@ def show_notice(conn, %{"notice_id" => notice_id}) 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 diff --git a/lib/pleroma/web/static_fe/static_fe_view.ex b/lib/pleroma/web/static_fe/static_fe_view.ex index 7f58e1b2d..71e47d2c1 100644 --- a/lib/pleroma/web/static_fe/static_fe_view.ex +++ b/lib/pleroma/web/static_fe/static_fe_view.ex @@ -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 index 000000000..9d2f1eb85 --- /dev/null +++ b/lib/pleroma/web/static_fe/user_representer.ex @@ -0,0 +1,35 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# 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 diff --git a/lib/pleroma/web/templates/layout/static_fe.html.eex b/lib/pleroma/web/templates/layout/static_fe.html.eex index c20958162..40a560460 100644 --- a/lib/pleroma/web/templates/layout/static_fe.html.eex +++ b/lib/pleroma/web/templates/layout/static_fe.html.eex @@ -22,6 +22,10 @@ border-radius: 4px; } + .activity { + margin-bottom: 1em; + } + .avatar { cursor: pointer; } diff --git a/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex index 9957697ad..e8d905d79 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex @@ -1,6 +1,6 @@
- <%= render("user_card.html", %{user: @user}) %> + <%= render("user_card.html", %{user: @data.user}) %>
-
<%= @object.data["content"] %>
+
<%= raw @data.content %>
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 index 000000000..9ae4139ed --- /dev/null +++ b/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex @@ -0,0 +1,7 @@ +

<%= raw (@data.user.name |> Formatter.emojify(emoji_for_user(@data.user))) %>

+

<%= raw @data.user.bio %>

+
+<%= for activity <- @data.timeline do %> + <%= render("notice.html", %{data: activity}) %> +<% end %> +
diff --git a/lib/pleroma/web/templates/static_fe/static_fe/user_card.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/user_card.html.eex index 8b397c639..c7789f9ac 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/user_card.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/user_card.html.eex @@ -4,7 +4,7 @@ - <%= @user.name |> Formatter.emojify(emoji_for_user(@user)) %> + <%= raw (@user.name |> Formatter.emojify(emoji_for_user(@user))) %> <%= @user.nickname %> From e2904b5777ecf6c8ffe7fe46f09284bb38b03fc2 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 2 Mar 2019 21:40:02 +0000 Subject: [PATCH 05/40] static fe: reformat activity representer --- lib/pleroma/web/static_fe/activity_representer.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/static_fe/activity_representer.ex b/lib/pleroma/web/static_fe/activity_representer.ex index 9c4315610..446c6023e 100644 --- a/lib/pleroma/web/static_fe/activity_representer.ex +++ b/lib/pleroma/web/static_fe/activity_representer.ex @@ -17,8 +17,8 @@ def prepare_activity(%User{} = user, %Object{} = object) do |> set_attachments(object) end - def prepare_activity(%User{} = user, %Activity{} = activity), do: - prepare_activity(user, Object.normalize(activity.data["object"])) + 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) From b33fbd58e3852fc9de58917fafbb2c575a21dde1 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 2 Mar 2019 22:35:54 +0000 Subject: [PATCH 06/40] static fe: add support for message subjects --- .../web/templates/static_fe/static_fe/notice.html.eex | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex index e8d905d79..791bd2562 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex @@ -1,6 +1,13 @@
<%= render("user_card.html", %{user: @data.user}) %>
-
<%= raw @data.content %>
+ <%= if @data.title do %> +
+ <%= raw @data.title %> + <% end %> +
<%= raw @data.content %>
+ <%= if @data.title do %> +
+ <% end %>
From ca5ef201ef8397981acf0647fe5cffea66299bfa Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sun, 3 Mar 2019 13:36:59 +0000 Subject: [PATCH 07/40] static fe: add remote follow button --- lib/pleroma/web/static_fe/static_fe_view.ex | 1 + lib/pleroma/web/templates/layout/static_fe.html.eex | 9 +++++++++ .../web/templates/static_fe/static_fe/profile.html.eex | 9 ++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/static_fe/static_fe_view.ex b/lib/pleroma/web/static_fe/static_fe_view.ex index 71e47d2c1..c01e8d40b 100644 --- a/lib/pleroma/web/static_fe/static_fe_view.ex +++ b/lib/pleroma/web/static_fe/static_fe_view.ex @@ -8,6 +8,7 @@ defmodule Pleroma.Web.StaticFE.StaticFEView do alias Pleroma.User alias Pleroma.Web.MediaProxy alias Pleroma.Formatter + alias Pleroma.Web.Router.Helpers import Phoenix.HTML diff --git a/lib/pleroma/web/templates/layout/static_fe.html.eex b/lib/pleroma/web/templates/layout/static_fe.html.eex index 40a560460..7ce9ead90 100644 --- a/lib/pleroma/web/templates/layout/static_fe.html.eex +++ b/lib/pleroma/web/templates/layout/static_fe.html.eex @@ -70,6 +70,15 @@ text-decoration: none; } + .pull-right { + float: right; + } + + .collapse { + margin: 0; + width: auto; + } + h1 { margin: 0; } 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 index 9ae4139ed..47b7d5286 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex @@ -1,4 +1,11 @@ -

<%= raw (@data.user.name |> Formatter.emojify(emoji_for_user(@data.user))) %>

+

+
+ + + +
+ <%= raw (@data.user.name |> Formatter.emojify(emoji_for_user(@data.user))) %> +

<%= raw @data.user.bio %>

<%= for activity <- @data.timeline do %> From d1320160f436c719ecca8b31463dd16a1ab2bdb9 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Sun, 27 Oct 2019 15:20:43 -0700 Subject: [PATCH 08/40] Looks like source_data is on user directly now. --- lib/pleroma/web/static_fe/static_fe_view.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/web/static_fe/static_fe_view.ex b/lib/pleroma/web/static_fe/static_fe_view.ex index c01e8d40b..ba67de835 100644 --- a/lib/pleroma/web/static_fe/static_fe_view.ex +++ b/lib/pleroma/web/static_fe/static_fe_view.ex @@ -13,7 +13,7 @@ defmodule Pleroma.Web.StaticFE.StaticFEView do import Phoenix.HTML def emoji_for_user(%User{} = user) do - (user.info.source_data["tag"] || []) + (user.source_data["tag"] || []) |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end) |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} -> {String.trim(name, ":"), url} From c1fc1399860c57460cee173ce8ddb980aabf10de Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Sun, 27 Oct 2019 16:37:57 -0700 Subject: [PATCH 09/40] Add permalinks to the static-fe notice rendering. --- lib/pleroma/web/static_fe/activity_representer.ex | 15 ++++++++++++--- .../templates/static_fe/static_fe/notice.html.eex | 6 ++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/web/static_fe/activity_representer.ex b/lib/pleroma/web/static_fe/activity_representer.ex index 446c6023e..e383b8415 100644 --- a/lib/pleroma/web/static_fe/activity_representer.ex +++ b/lib/pleroma/web/static_fe/activity_representer.ex @@ -7,18 +7,21 @@ defmodule Pleroma.Web.StaticFE.ActivityRepresenter do alias Pleroma.Object alias Pleroma.User alias Pleroma.Web.ActivityPub.Visibility + alias Pleroma.Web.Router.Helpers - def prepare_activity(%User{} = user, %Object{} = object) do + def prepare_activity(%User{} = user, %Object{} = object, activity_id) do %{} |> set_user(user) |> set_object(object) |> set_title(object) |> set_content(object) + |> set_link(activity_id) + |> set_published(object) |> set_attachments(object) end def prepare_activity(%User{} = user, %Activity{} = activity), - do: prepare_activity(user, Object.normalize(activity.data["object"])) + do: prepare_activity(user, Object.normalize(activity.data["object"]), activity.id) defp set_user(data, %User{} = user), do: Map.put(data, :user, user) @@ -37,6 +40,12 @@ defp set_content(data, %Object{data: %{"content" => content}}) when is_binary(co defp set_content(data, _), do: Map.put(data, :content, nil) + 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) + # TODO: attachments defp set_attachments(data, _), do: Map.put(data, :attachments, []) @@ -45,7 +54,7 @@ def represent(activity_id) do true <- Visibility.is_public?(activity), %Object{} = object <- Object.normalize(activity.data["object"]), %User{} = user <- User.get_or_fetch(activity.data["actor"]), - data <- prepare_activity(user, object) do + data <- prepare_activity(user, object, activity_id) do {:ok, data} else e -> diff --git a/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex index 791bd2562..d305d9057 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex @@ -1,13 +1,15 @@
<%= render("user_card.html", %{user: @data.user}) %>
- <%= if @data.title do %> + <%= if @data.title != "" do %>
<%= raw @data.title %> <% end %>
<%= raw @data.content %>
- <%= if @data.title do %> + <%= if @data.title != "" do %>
<% end %> +

+ <%= @data.published %>

From e79d8985ab90bcad33d9ff13c6a16f006c6abac9 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Sun, 27 Oct 2019 17:53:20 -0700 Subject: [PATCH 10/40] Don't show 404 in static-fe controller unless it's actually not found. --- lib/pleroma/web/static_fe/static_fe_controller.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index 78cd325c8..8bbd06aa9 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -18,7 +18,7 @@ def show_notice(conn, %{"notice_id" => notice_id}) do |> put_view(Pleroma.Web.StaticFE.StaticFEView) |> render("notice.html", %{data: data}) else - _ -> + {:error, nil} -> conn |> put_status(404) |> text("Not found") @@ -33,7 +33,7 @@ def show_user(conn, %{"username_or_id" => username_or_id}) do |> put_view(Pleroma.Web.StaticFE.StaticFEView) |> render("profile.html", %{data: data}) else - _ -> + {:error, nil} -> conn |> put_status(404) |> text("Not found") From 0cf04e10880fb0779f34102cacb40950a119d4f8 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Sun, 27 Oct 2019 19:01:18 -0700 Subject: [PATCH 11/40] Fix OStatus controller to know about StaticFEController. But only when it's configured to be on. --- lib/pleroma/web/ostatus/ostatus_controller.ex | 62 ++++++++++--------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex index 6958519de..76a244d0f 100644 --- a/lib/pleroma/web/ostatus/ostatus_controller.ex +++ b/lib/pleroma/web/ostatus/ostatus_controller.ex @@ -76,37 +76,41 @@ def activity(%{assigns: %{format: format}} = conn, %{"uuid" => uuid}) do end def notice(%{assigns: %{format: format}} = conn, %{"id" => id}) do - with {_, %Activity{} = activity} <- {:activity, Activity.get_by_id_with_object(id)}, - {_, true} <- {:public?, Visibility.is_public?(activity)}, - %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do - cond do - format == "html" && activity.data["type"] == "Create" -> - %Object{} = object = Object.normalize(activity) - - RedirectController.redirector_with_meta( - conn, - %{ - activity_id: activity.id, - object: object, - url: Router.Helpers.o_status_url(Endpoint, :notice, activity.id), - user: user - } - ) - - format == "html" -> - RedirectController.redirector(conn, nil) - - true -> - represent_activity(conn, format, activity, user) - end + if Pleroma.Config.get([:instance, :static_fe], false) do + Pleroma.Web.StaticFE.StaticFEController.show(conn, %{"notice_id" => id}) else - reason when reason in [{:public?, false}, {:activity, nil}] -> - conn - |> put_status(404) - |> RedirectController.redirector(nil, 404) + with {_, %Activity{} = activity} <- {:activity, Activity.get_by_id_with_object(id)}, + {_, true} <- {:public?, Visibility.is_public?(activity)}, + %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do + cond do + format == "html" && activity.data["type"] == "Create" -> + %Object{} = object = Object.normalize(activity) - e -> - e + RedirectController.redirector_with_meta( + conn, + %{ + activity_id: activity.id, + object: object, + url: Router.Helpers.o_status_url(Endpoint, :notice, activity.id), + user: user + } + ) + + format == "html" -> + RedirectController.redirector(conn, nil) + + true -> + represent_activity(conn, format, activity, user) + end + else + reason when reason in [{:public?, false}, {:activity, nil}] -> + conn + |> put_status(404) + |> RedirectController.redirector(nil, 404) + + e -> + e + end end end From 1d8950798c8aef2cee4458c68d34a72da630ec41 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Sun, 27 Oct 2019 19:02:19 -0700 Subject: [PATCH 12/40] Fix activity_representer to work with User.get_or_fetch returning tuple. --- lib/pleroma/web/ostatus/ostatus_controller.ex | 2 +- lib/pleroma/web/static_fe/activity_representer.ex | 15 ++++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex index 76a244d0f..ab5fdbc78 100644 --- a/lib/pleroma/web/ostatus/ostatus_controller.ex +++ b/lib/pleroma/web/ostatus/ostatus_controller.ex @@ -81,7 +81,7 @@ def notice(%{assigns: %{format: format}} = conn, %{"id" => id}) do else with {_, %Activity{} = activity} <- {:activity, Activity.get_by_id_with_object(id)}, {_, true} <- {:public?, Visibility.is_public?(activity)}, - %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do + %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do cond do format == "html" && activity.data["type"] == "Create" -> %Object{} = object = Object.normalize(activity) diff --git a/lib/pleroma/web/static_fe/activity_representer.ex b/lib/pleroma/web/static_fe/activity_representer.ex index e383b8415..9bee732d5 100644 --- a/lib/pleroma/web/static_fe/activity_representer.ex +++ b/lib/pleroma/web/static_fe/activity_representer.ex @@ -9,20 +9,19 @@ defmodule Pleroma.Web.StaticFE.ActivityRepresenter do alias Pleroma.Web.ActivityPub.Visibility alias Pleroma.Web.Router.Helpers - def prepare_activity(%User{} = user, %Object{} = object, activity_id) do + def prepare_activity(%User{} = user, %Activity{} = activity) do + object = Object.normalize(activity.data["object"]) + %{} |> set_user(user) |> set_object(object) |> set_title(object) |> set_content(object) - |> set_link(activity_id) + |> set_link(activity.id) |> set_published(object) |> set_attachments(object) end - def prepare_activity(%User{} = user, %Activity{} = activity), - do: prepare_activity(user, Object.normalize(activity.data["object"]), activity.id) - defp set_user(data, %User{} = user), do: Map.put(data, :user, user) defp set_object(data, %Object{} = object), do: Map.put(data, :object, object) @@ -52,10 +51,8 @@ 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), true <- Visibility.is_public?(activity), - %Object{} = object <- Object.normalize(activity.data["object"]), - %User{} = user <- User.get_or_fetch(activity.data["actor"]), - data <- prepare_activity(user, object, activity_id) do - {:ok, data} + {:ok, %User{} = user} <- User.get_or_fetch(activity.data["actor"]) do + {:ok, prepare_activity(user, activity)} else e -> {:error, e} From 748d800acb56cd1e66adf78e5938623b8da7cde1 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Mon, 28 Oct 2019 19:05:45 -0700 Subject: [PATCH 13/40] Show images, video, and audio attachments to notices. --- .../web/static_fe/activity_representer.ex | 10 ++++++- lib/pleroma/web/static_fe/static_fe_view.ex | 7 +++++ .../web/templates/layout/static_fe.html.eex | 5 ++++ .../static_fe/static_fe/_attachment.html.eex | 8 ++++++ .../static_fe/static_fe/notice.html.eex | 28 ++++++++++++++----- 5 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 lib/pleroma/web/templates/static_fe/static_fe/_attachment.html.eex diff --git a/lib/pleroma/web/static_fe/activity_representer.ex b/lib/pleroma/web/static_fe/activity_representer.ex index 9bee732d5..7b7e1730c 100644 --- a/lib/pleroma/web/static_fe/activity_representer.ex +++ b/lib/pleroma/web/static_fe/activity_representer.ex @@ -19,6 +19,8 @@ def prepare_activity(%User{} = user, %Activity{} = activity) 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 @@ defp set_content(data, %Object{data: %{"content" => content}}) when is_binary(co 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)} diff --git a/lib/pleroma/web/static_fe/static_fe_view.ex b/lib/pleroma/web/static_fe/static_fe_view.ex index ba67de835..2b3b968d3 100644 --- a/lib/pleroma/web/static_fe/static_fe_view.ex +++ b/lib/pleroma/web/static_fe/static_fe_view.ex @@ -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 @@ def emoji_for_user(%User{} = user) do {String.trim(name, ":"), url} end) end + + def fetch_media_type(url) do + Utils.fetch_media_type(@media_types, url["mediaType"]) + end end diff --git a/lib/pleroma/web/templates/layout/static_fe.html.eex b/lib/pleroma/web/templates/layout/static_fe.html.eex index 7ce9ead90..c1fbd89cd 100644 --- a/lib/pleroma/web/templates/layout/static_fe.html.eex +++ b/lib/pleroma/web/templates/layout/static_fe.html.eex @@ -36,6 +36,11 @@ 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 index 000000000..7e04e9550 --- /dev/null +++ b/lib/pleroma/web/templates/static_fe/static_fe/_attachment.html.eex @@ -0,0 +1,8 @@ +<%= case @mediaType do %> +<% "audio" -> %> + +<% "video" -> %> + +<% _ -> %> +<%= @name %> +<% end %> diff --git a/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex index d305d9057..9a7824a32 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex @@ -1,15 +1,29 @@
<%= render("user_card.html", %{user: @data.user}) %> +

+ <%= @data.published %>

<%= if @data.title != "" do %> -
- <%= raw @data.title %> - <% end %> +
+ <%= raw @data.title %> +
<%= raw @data.content %>
+
+ <% else %>
<%= raw @data.content %>
- <%= if @data.title != "" do %> -
<% end %> -

- <%= @data.published %>

+ <%= for %{"name" => name, "url" => [url | _]} <- @data.attachment do %> + <%= if @data.sensitive do %> +
+ sensitive media +
+ <%= render("_attachment.html", %{name: name, url: url["href"], + mediaType: fetch_media_type(url)}) %> +
+
+ <% else %> + <%= render("_attachment.html", %{name: name, url: url["href"], + mediaType: fetch_media_type(url)}) %> + <% end %> + <% end %>
From cc1b07132f1c532c623530ed2375ff7fbdc6d559 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Mon, 28 Oct 2019 19:47:20 -0700 Subject: [PATCH 14/40] Notices should show entire thread from context. --- lib/pleroma/web/static_fe/static_fe_controller.ex | 12 +++++++++++- lib/pleroma/web/templates/layout/static_fe.html.eex | 5 +++++ .../static_fe/{notice.html.eex => _notice.html.eex} | 4 ++-- .../static_fe/static_fe/conversation.html.eex | 5 +++++ .../templates/static_fe/static_fe/profile.html.eex | 6 +++--- 5 files changed, 26 insertions(+), 6 deletions(-) rename lib/pleroma/web/templates/static_fe/static_fe/{notice.html.eex => _notice.html.eex} (93%) create mode 100644 lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index 8bbd06aa9..d2b55767d 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -5,6 +5,7 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do use Pleroma.Web, :controller + alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.StaticFE.ActivityRepresenter alias Pleroma.Web.StaticFE.UserRepresenter @@ -12,11 +13,20 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do def show_notice(conn, %{"notice_id" => notice_id}) do with {:ok, data} <- ActivityRepresenter.represent(notice_id) do + context = data.object.data["context"] + activities = ActivityPub.fetch_activities_for_context(context, %{}) + + data = + for a <- Enum.reverse(activities) do + ActivityRepresenter.prepare_activity(data.user, a) + |> Map.put(:selected, a.object.id == data.object.id) + end + conn |> put_layout(:static_fe) |> put_status(200) |> put_view(Pleroma.Web.StaticFE.StaticFEView) - |> render("notice.html", %{data: data}) + |> render("conversation.html", %{data: data}) else {:error, nil} -> conn diff --git a/lib/pleroma/web/templates/layout/static_fe.html.eex b/lib/pleroma/web/templates/layout/static_fe.html.eex index c1fbd89cd..9d7ee366a 100644 --- a/lib/pleroma/web/templates/layout/static_fe.html.eex +++ b/lib/pleroma/web/templates/layout/static_fe.html.eex @@ -23,6 +23,7 @@ } .activity { + padding: 1em; margin-bottom: 1em; } @@ -41,6 +42,10 @@ max-height: 800px; } + #selected { + background-color: #1b2735; + } + a { color: white; } diff --git a/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex similarity index 93% rename from lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex rename to lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex index 9a7824a32..90b5ef67c 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/notice.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex @@ -1,7 +1,7 @@ -
- <%= render("user_card.html", %{user: @data.user}) %> +
id="selected" <% end %>>

<%= @data.published %>

+ <%= render("user_card.html", %{user: @data.user}) %>
<%= if @data.title != "" do %>
diff --git a/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex new file mode 100644 index 000000000..35c3c17cd --- /dev/null +++ b/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex @@ -0,0 +1,5 @@ +
+ <%= for notice <- @data do %> + <%= render("_notice.html", %{data: notice}) %> + <% end %> +
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 index 47b7d5286..79bf5a729 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex @@ -8,7 +8,7 @@

<%= raw @data.user.bio %>

-<%= for activity <- @data.timeline do %> - <%= render("notice.html", %{data: activity}) %> -<% end %> + <%= for activity <- @data.timeline do %> + <%= render("_notice.html", %{data: activity}) %> + <% end %>
From 2d1897e8a739a54a07ab0eae5cf11c260428e532 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Tue, 29 Oct 2019 17:45:26 -0700 Subject: [PATCH 15/40] Apply all suggested changes from reviewers. --- lib/pleroma/web/ostatus/ostatus_controller.ex | 2 +- .../web/static_fe/activity_representer.ex | 4 +- .../web/static_fe/static_fe_controller.ex | 47 +++++++------------ lib/pleroma/web/static_fe/static_fe_view.ex | 9 ++-- lib/pleroma/web/static_fe/user_representer.ex | 9 ++-- .../static_fe/static_fe/_notice.html.eex | 3 +- 6 files changed, 30 insertions(+), 44 deletions(-) diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex index ab5fdbc78..be275977e 100644 --- a/lib/pleroma/web/ostatus/ostatus_controller.ex +++ b/lib/pleroma/web/ostatus/ostatus_controller.ex @@ -77,7 +77,7 @@ def activity(%{assigns: %{format: format}} = conn, %{"uuid" => uuid}) do def notice(%{assigns: %{format: format}} = conn, %{"id" => id}) do if Pleroma.Config.get([:instance, :static_fe], false) do - Pleroma.Web.StaticFE.StaticFEController.show(conn, %{"notice_id" => id}) + Pleroma.Web.StaticFE.StaticFEController.call(conn, :show_notice) else with {_, %Activity{} = activity} <- {:activity, Activity.get_by_id_with_object(id)}, {_, true} <- {:public?, Visibility.is_public?(activity)}, diff --git a/lib/pleroma/web/static_fe/activity_representer.ex b/lib/pleroma/web/static_fe/activity_representer.ex index 7b7e1730c..8a499195c 100644 --- a/lib/pleroma/web/static_fe/activity_representer.ex +++ b/lib/pleroma/web/static_fe/activity_representer.ex @@ -62,8 +62,8 @@ def represent(activity_id) do {:ok, %User{} = user} <- User.get_or_fetch(activity.data["actor"]) do {:ok, prepare_activity(user, activity)} else - e -> - {:error, e} + {:error, reason} -> {:error, reason} + _error -> {:error, "Not found"} end end end diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index d2b55767d..6e8d0d622 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -9,9 +9,12 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do alias Pleroma.Web.StaticFE.ActivityRepresenter alias Pleroma.Web.StaticFE.UserRepresenter - require Logger + plug(:put_layout, :static_fe) + plug(:put_view, Pleroma.Web.StaticFE.StaticFEView) + plug(:assign_id) + action_fallback(:not_found) - def show_notice(conn, %{"notice_id" => notice_id}) do + def show_notice(%{assigns: %{notice_id: notice_id}} = conn, _params) do with {:ok, data} <- ActivityRepresenter.represent(notice_id) do context = data.object.data["context"] activities = ActivityPub.fetch_activities_for_context(context, %{}) @@ -22,45 +25,29 @@ def show_notice(conn, %{"notice_id" => notice_id}) do |> Map.put(:selected, a.object.id == data.object.id) end - conn - |> put_layout(:static_fe) - |> put_status(200) - |> put_view(Pleroma.Web.StaticFE.StaticFEView) - |> render("conversation.html", %{data: data}) - else - {:error, nil} -> - conn - |> put_status(404) - |> text("Not found") + render(conn, "conversation.html", data: data) end end - def show_user(conn, %{"username_or_id" => username_or_id}) do + def show_user(%{assigns: %{username_or_id: username_or_id}} = conn, _params) 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 - {:error, nil} -> - conn - |> put_status(404) - |> text("Not found") + render(conn, "profile.html", data: data) end end - def show(%{path_info: ["notice", notice_id]} = conn, _params), - do: show_notice(conn, %{"notice_id" => notice_id}) + def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts), + do: assign(conn, :notice_id, notice_id) - def show(%{path_info: ["users", user_id]} = conn, _params), - do: show_user(conn, %{"username_or_id" => user_id}) + def assign_id(%{path_info: ["users", user_id]} = conn, _opts), + do: assign(conn, :username_or_id, user_id) - def show(%{path_info: [user_id]} = conn, _params), - do: show_user(conn, %{"username_or_id" => user_id}) + def assign_id(%{path_info: [user_id]} = conn, _opts), + do: assign(conn, :username_or_id, user_id) + + def assign_id(conn, _opts), do: conn # Fallback for unhandled types - def show(conn, _params) do + def not_found(conn, _opts) do conn |> put_status(404) |> text("Not found") diff --git a/lib/pleroma/web/static_fe/static_fe_view.ex b/lib/pleroma/web/static_fe/static_fe_view.ex index 2b3b968d3..d633751dd 100644 --- a/lib/pleroma/web/static_fe/static_fe_view.ex +++ b/lib/pleroma/web/static_fe/static_fe_view.ex @@ -11,19 +11,20 @@ defmodule Pleroma.Web.StaticFE.StaticFEView do alias Pleroma.Web.Metadata.Utils alias Pleroma.Web.Router.Helpers - import Phoenix.HTML + use Phoenix.HTML @media_types ["image", "audio", "video"] def emoji_for_user(%User{} = user) do - (user.source_data["tag"] || []) + user.source_data + |> Map.get("tag", []) |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end) |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} -> {String.trim(name, ":"), url} end) end - def fetch_media_type(url) do - Utils.fetch_media_type(@media_types, url["mediaType"]) + def fetch_media_type(%{"mediaType" => mediaType}) do + Utils.fetch_media_type(@media_types, mediaType) end end diff --git a/lib/pleroma/web/static_fe/user_representer.ex b/lib/pleroma/web/static_fe/user_representer.ex index 9d2f1eb85..26320ea69 100644 --- a/lib/pleroma/web/static_fe/user_representer.ex +++ b/lib/pleroma/web/static_fe/user_representer.ex @@ -24,12 +24,9 @@ defp set_timeline(data, %User{} = user) do 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} + case User.get_cached_by_nickname_or_id(username_or_id) do + %User{} = user -> {:ok, prepare_user(user)} + nil -> {:error, "User not found"} end end end diff --git a/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex index 90b5ef67c..ed43ae838 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex @@ -1,6 +1,7 @@
id="selected" <% end %>>

- <%= @data.published %>

+ <%= link @data.published, to: @data.link, class: "activity-link" %> +

<%= render("user_card.html", %{user: @data.user}) %>
<%= if @data.title != "" do %> From e944a2213dd5eaaf69b9e8d8f5e035dbba2fdab1 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Tue, 29 Oct 2019 17:53:58 -0700 Subject: [PATCH 16/40] Use gettext for sensitive media warning. --- lib/pleroma/web/static_fe/static_fe_view.ex | 1 + lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/static_fe/static_fe_view.ex b/lib/pleroma/web/static_fe/static_fe_view.ex index d633751dd..1194b7ecc 100644 --- a/lib/pleroma/web/static_fe/static_fe_view.ex +++ b/lib/pleroma/web/static_fe/static_fe_view.ex @@ -6,6 +6,7 @@ defmodule Pleroma.Web.StaticFE.StaticFEView do use Pleroma.Web, :view alias Pleroma.User + alias Pleroma.Web.Gettext alias Pleroma.Web.MediaProxy alias Pleroma.Formatter alias Pleroma.Web.Metadata.Utils diff --git a/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex index ed43ae838..c4cdb1029 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex @@ -15,7 +15,7 @@ <%= for %{"name" => name, "url" => [url | _]} <- @data.attachment do %> <%= if @data.sensitive do %>
- sensitive media + <%= Gettext.gettext("sensitive media") %>
<%= render("_attachment.html", %{name: name, url: url["href"], mediaType: fetch_media_type(url)}) %> From 41fde63defd332a84b6c4d4ca78848e623a9d122 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Tue, 29 Oct 2019 19:27:42 -0700 Subject: [PATCH 17/40] Get rid of @data in views and use separate fields. --- .../web/static_fe/static_fe_controller.ex | 12 +++++------- .../static_fe/static_fe/_notice.html.eex | 18 +++++++++--------- .../static_fe/static_fe/conversation.html.eex | 4 ++-- .../static_fe/static_fe/profile.html.eex | 10 +++++----- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index 6e8d0d622..c77df8e7d 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -17,22 +17,20 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do def show_notice(%{assigns: %{notice_id: notice_id}} = conn, _params) do with {:ok, data} <- ActivityRepresenter.represent(notice_id) do context = data.object.data["context"] - activities = ActivityPub.fetch_activities_for_context(context, %{}) - data = - for a <- Enum.reverse(activities) do + activities = + for a <- Enum.reverse(ActivityPub.fetch_activities_for_context(context, %{})) do ActivityRepresenter.prepare_activity(data.user, a) |> Map.put(:selected, a.object.id == data.object.id) end - render(conn, "conversation.html", data: data) + render(conn, "conversation.html", activities: activities) end end def show_user(%{assigns: %{username_or_id: username_or_id}} = conn, _params) do - with {:ok, data} <- UserRepresenter.represent(username_or_id) do - render(conn, "profile.html", data: data) - end + {:ok, data} = UserRepresenter.represent(username_or_id) + render(conn, "profile.html", %{user: data.user, timeline: data.timeline}) end def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts), diff --git a/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex index c4cdb1029..b16d19a2c 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex @@ -1,19 +1,19 @@ -
id="selected" <% end %>> +
id="selected" <% end %>>

- <%= link @data.published, to: @data.link, class: "activity-link" %> + <%= link @published, to: @link, class: "activity-link" %>

- <%= render("user_card.html", %{user: @data.user}) %> + <%= render("user_card.html", %{user: @user}) %>
- <%= if @data.title != "" do %> + <%= if @title != "" do %>
- <%= raw @data.title %> -
<%= raw @data.content %>
+ <%= raw @title %> +
<%= raw @content %>
<% else %> -
<%= raw @data.content %>
+
<%= raw @content %>
<% end %> - <%= for %{"name" => name, "url" => [url | _]} <- @data.attachment do %> - <%= if @data.sensitive do %> + <%= for %{"name" => name, "url" => [url | _]} <- @attachment do %> + <%= if @sensitive do %>
<%= Gettext.gettext("sensitive media") %>
diff --git a/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex index 35c3c17cd..f0d3b5972 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex @@ -1,5 +1,5 @@
- <%= for notice <- @data do %> - <%= render("_notice.html", %{data: notice}) %> + <%= for activity <- @activities do %> + <%= render("_notice.html", activity) %> <% end %>
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 index 79bf5a729..da23be1e5 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex @@ -1,14 +1,14 @@

- +
- <%= raw (@data.user.name |> Formatter.emojify(emoji_for_user(@data.user))) %> + <%= raw (@user.name |> Formatter.emojify(emoji_for_user(@user))) %>

-

<%= raw @data.user.bio %>

+

<%= raw @user.bio %>

- <%= for activity <- @data.timeline do %> - <%= render("_notice.html", %{data: activity}) %> + <%= for activity <- @timeline do %> + <%= render("_notice.html", Map.put(activity, :selected, false)) %> <% end %>
From 33a26b61c30ad8084003f0f1c646bc997a8d88ac Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Tue, 29 Oct 2019 20:30:43 -0700 Subject: [PATCH 18/40] Remove activity/user representer; move logic to controller. --- .../web/static_fe/activity_representer.ex | 69 ------------------- .../web/static_fe/static_fe_controller.ex | 59 ++++++++++++---- lib/pleroma/web/static_fe/user_representer.ex | 32 --------- 3 files changed, 46 insertions(+), 114 deletions(-) delete mode 100644 lib/pleroma/web/static_fe/activity_representer.ex delete mode 100644 lib/pleroma/web/static_fe/user_representer.ex diff --git a/lib/pleroma/web/static_fe/activity_representer.ex b/lib/pleroma/web/static_fe/activity_representer.ex deleted file mode 100644 index 8a499195c..000000000 --- a/lib/pleroma/web/static_fe/activity_representer.ex +++ /dev/null @@ -1,69 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2019 Pleroma Authors -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Web.StaticFE.ActivityRepresenter do - alias Pleroma.Activity - alias Pleroma.Object - alias Pleroma.User - alias Pleroma.Web.ActivityPub.Visibility - alias Pleroma.Web.Router.Helpers - - def prepare_activity(%User{} = user, %Activity{} = activity) do - object = Object.normalize(activity.data["object"]) - - %{} - |> set_user(user) - |> set_object(object) - |> set_title(object) - |> set_content(object) - |> set_link(activity.id) - |> set_published(object) - |> set_sensitive(object) - |> set_attachment(object.data["attachment"]) - |> set_attachments(object) - end - - defp set_user(data, %User{} = user), do: Map.put(data, :user, user) - - defp set_object(data, %Object{} = object), do: Map.put(data, :object, object) - - defp set_title(data, %Object{data: %{"name" => name}}) when is_binary(name), - do: Map.put(data, :title, name) - - defp set_title(data, %Object{data: %{"summary" => summary}}) when is_binary(summary), - do: Map.put(data, :title, summary) - - defp set_title(data, _), do: Map.put(data, :title, nil) - - defp set_content(data, %Object{data: %{"content" => content}}) when is_binary(content), - do: Map.put(data, :content, content) - - 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_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)} - else - {:error, reason} -> {:error, reason} - _error -> {:error, "Not found"} - end - end -end diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index c77df8e7d..a5cb76167 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -5,32 +5,65 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do use Pleroma.Web, :controller + alias Pleroma.Activity + alias Pleroma.Object + alias Pleroma.User alias Pleroma.Web.ActivityPub.ActivityPub - alias Pleroma.Web.StaticFE.ActivityRepresenter - alias Pleroma.Web.StaticFE.UserRepresenter + alias Pleroma.Web.Router.Helpers plug(:put_layout, :static_fe) plug(:put_view, Pleroma.Web.StaticFE.StaticFEView) plug(:assign_id) action_fallback(:not_found) + defp get_title(%Object{data: %{"name" => name}}) when is_binary(name), + do: name + + defp get_title(%Object{data: %{"summary" => summary}}) when is_binary(summary), + do: summary + + defp get_title(_), do: nil + + def represent(%Activity{} = activity, %User{} = user, selected) do + %{ + user: user, + title: get_title(activity.object), + content: activity.object.data["content"] || nil, + attachment: activity.object.data["attachment"], + link: Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity.id), + published: activity.object.data["published"], + sensitive: activity.object.data["sensitive"], + selected: selected + } + end + + def represent(%Activity{} = activity, selected) do + {:ok, user} = User.get_or_fetch(activity.data["actor"]) + represent(activity, user, selected) + end + def show_notice(%{assigns: %{notice_id: notice_id}} = conn, _params) do - with {:ok, data} <- ActivityRepresenter.represent(notice_id) do - context = data.object.data["context"] + activity = Activity.get_by_id_with_object(notice_id) + context = activity.object.data["context"] + activities = ActivityPub.fetch_activities_for_context(context, %{}) - activities = - for a <- Enum.reverse(ActivityPub.fetch_activities_for_context(context, %{})) do - ActivityRepresenter.prepare_activity(data.user, a) - |> Map.put(:selected, a.object.id == data.object.id) - end + represented = + for a <- Enum.reverse(activities) do + represent(activity, a.object.id == activity.object.id) + end - render(conn, "conversation.html", activities: activities) - end + render(conn, "conversation.html", activities: represented) end def show_user(%{assigns: %{username_or_id: username_or_id}} = conn, _params) do - {:ok, data} = UserRepresenter.represent(username_or_id) - render(conn, "profile.html", %{user: data.user, timeline: data.timeline}) + %User{} = user = User.get_cached_by_nickname_or_id(username_or_id) + + timeline = + for activity <- ActivityPub.fetch_user_activities(user, nil, %{}) do + represent(activity, user, false) + end + + render(conn, "profile.html", %{user: user, timeline: timeline}) end def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts), diff --git a/lib/pleroma/web/static_fe/user_representer.ex b/lib/pleroma/web/static_fe/user_representer.ex deleted file mode 100644 index 26320ea69..000000000 --- a/lib/pleroma/web/static_fe/user_representer.ex +++ /dev/null @@ -1,32 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2019 Pleroma Authors -# 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 - case User.get_cached_by_nickname_or_id(username_or_id) do - %User{} = user -> {:ok, prepare_user(user)} - nil -> {:error, "User not found"} - end - end -end From 918e1353f6bc7f6dfe317a87d942dfa2e53064af Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Tue, 29 Oct 2019 20:55:43 -0700 Subject: [PATCH 19/40] Add header to profile/notice pages linking to pleroma-fe. --- lib/pleroma/web/static_fe/static_fe_controller.ex | 6 ++++-- .../web/templates/static_fe/static_fe/_notice.html.eex | 2 +- .../static_fe/{user_card.html.eex => _user_card.html.eex} | 0 .../web/templates/static_fe/static_fe/conversation.html.eex | 2 ++ .../web/templates/static_fe/static_fe/profile.html.eex | 6 ++++-- 5 files changed, 11 insertions(+), 5 deletions(-) rename lib/pleroma/web/templates/static_fe/static_fe/{user_card.html.eex => _user_card.html.eex} (100%) diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index a5cb76167..fe2fb09c4 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -43,6 +43,7 @@ def represent(%Activity{} = activity, selected) do end def show_notice(%{assigns: %{notice_id: notice_id}} = conn, _params) do + instance_name = Pleroma.Config.get([:instance, :name], "Pleroma") activity = Activity.get_by_id_with_object(notice_id) context = activity.object.data["context"] activities = ActivityPub.fetch_activities_for_context(context, %{}) @@ -52,10 +53,11 @@ def show_notice(%{assigns: %{notice_id: notice_id}} = conn, _params) do represent(activity, a.object.id == activity.object.id) end - render(conn, "conversation.html", activities: represented) + render(conn, "conversation.html", %{activities: represented, instance_name: instance_name}) end def show_user(%{assigns: %{username_or_id: username_or_id}} = conn, _params) do + instance_name = Pleroma.Config.get([:instance, :name], "Pleroma") %User{} = user = User.get_cached_by_nickname_or_id(username_or_id) timeline = @@ -63,7 +65,7 @@ def show_user(%{assigns: %{username_or_id: username_or_id}} = conn, _params) do represent(activity, user, false) end - render(conn, "profile.html", %{user: user, timeline: timeline}) + render(conn, "profile.html", %{user: user, timeline: timeline, instance_name: instance_name}) end def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts), diff --git a/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex index b16d19a2c..d1daa281c 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex @@ -2,7 +2,7 @@

<%= link @published, to: @link, class: "activity-link" %>

- <%= render("user_card.html", %{user: @user}) %> + <%= render("_user_card.html", %{user: @user}) %>
<%= if @title != "" do %>
diff --git a/lib/pleroma/web/templates/static_fe/static_fe/user_card.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/_user_card.html.eex similarity index 100% rename from lib/pleroma/web/templates/static_fe/static_fe/user_card.html.eex rename to lib/pleroma/web/templates/static_fe/static_fe/_user_card.html.eex diff --git a/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex index f0d3b5972..3a1249df2 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex @@ -1,3 +1,5 @@ +

<%= link @instance_name, to: "/" %>

+
<%= for activity <- @activities do %> <%= render("_notice.html", activity) %> 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 index da23be1e5..8f2c74627 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex @@ -1,11 +1,13 @@ -

+

<%= link @instance_name, to: "/" %>

+ +

<%= raw (@user.name |> Formatter.emojify(emoji_for_user(@user))) %> -

+

<%= raw @user.bio %>

<%= for activity <- @timeline do %> From 93e9c0cedf0e2b4ab5966832cc912369c7aaf3ad Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Tue, 29 Oct 2019 21:09:05 -0700 Subject: [PATCH 20/40] Format dates using CommonAPI utils. --- lib/pleroma/web/static_fe/static_fe_view.ex | 5 +++++ .../web/templates/static_fe/static_fe/_notice.html.eex | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/static_fe/static_fe_view.ex b/lib/pleroma/web/static_fe/static_fe_view.ex index 1194b7ecc..c19aa07e1 100644 --- a/lib/pleroma/web/static_fe/static_fe_view.ex +++ b/lib/pleroma/web/static_fe/static_fe_view.ex @@ -28,4 +28,9 @@ def emoji_for_user(%User{} = user) do def fetch_media_type(%{"mediaType" => mediaType}) do Utils.fetch_media_type(@media_types, mediaType) end + + def format_date(date) do + {:ok, date, _} = DateTime.from_iso8601(date) + Pleroma.Web.CommonAPI.Utils.format_asctime(date) + end end diff --git a/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex index d1daa281c..9841fcf84 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex @@ -1,6 +1,6 @@
id="selected" <% end %>>

- <%= link @published, to: @link, class: "activity-link" %> + <%= link format_date(@published), to: @link, class: "activity-link" %>

<%= render("_user_card.html", %{user: @user}) %>
From e4b9784c3938772edf45340107a34a58aeeea690 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Tue, 29 Oct 2019 22:05:18 -0700 Subject: [PATCH 21/40] Show counts for replies, likes, and announces for selected notice. Using text instead of an icon, for now. --- lib/pleroma/web/static_fe/static_fe_controller.ex | 15 +++++++++++++-- .../web/templates/layout/static_fe.html.eex | 6 ++++++ .../static_fe/static_fe/_notice.html.eex | 7 +++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index fe2fb09c4..d2e72b476 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -24,6 +24,16 @@ defp get_title(%Object{data: %{"summary" => summary}}) when is_binary(summary), defp get_title(_), do: nil + def get_counts(%Activity{} = activity) do + %Object{data: data} = Object.normalize(activity) + + %{ + likes: data["like_count"] || 0, + replies: data["repliesCount"] || 0, + announces: data["announcement_count"] || 0 + } + end + def represent(%Activity{} = activity, %User{} = user, selected) do %{ user: user, @@ -33,7 +43,8 @@ def represent(%Activity{} = activity, %User{} = user, selected) do link: Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity.id), published: activity.object.data["published"], sensitive: activity.object.data["sensitive"], - selected: selected + selected: selected, + counts: get_counts(activity) } end @@ -50,7 +61,7 @@ def show_notice(%{assigns: %{notice_id: notice_id}} = conn, _params) do represented = for a <- Enum.reverse(activities) do - represent(activity, a.object.id == activity.object.id) + represent(a, a.object.id == activity.object.id) end render(conn, "conversation.html", %{activities: represented, instance_name: instance_name}) diff --git a/lib/pleroma/web/templates/layout/static_fe.html.eex b/lib/pleroma/web/templates/layout/static_fe.html.eex index 9d7ee366a..e42047de9 100644 --- a/lib/pleroma/web/templates/layout/static_fe.html.eex +++ b/lib/pleroma/web/templates/layout/static_fe.html.eex @@ -24,6 +24,7 @@ .activity { padding: 1em; + padding-bottom: 2em; margin-bottom: 1em; } @@ -46,6 +47,11 @@ background-color: #1b2735; } + .counts dt, .counts dd { + float: left; + margin-left: 1em; + } + a { color: white; } diff --git a/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex index 9841fcf84..2a46dadb4 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex @@ -27,4 +27,11 @@ <% end %> <% end %>
+ <%= if @selected do %> +
+
<%= Gettext.gettext("replies") %>
<%= @counts.replies %>
+
<%= Gettext.gettext("announces") %>
<%= @counts.announces %>
+
<%= Gettext.gettext("likes") %>
<%= @counts.likes %>
+
+ <% end %>
From 1dc785b74be6dc790d2b24e833642060303ecee2 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Wed, 30 Oct 2019 20:20:26 -0700 Subject: [PATCH 22/40] Move static-fe CSS to a separate file. --- .../web/templates/layout/static_fe.html.eex | 165 +---------------- priv/static/static/static-fe.css | 172 ++++++++++++++++++ 2 files changed, 173 insertions(+), 164 deletions(-) create mode 100644 priv/static/static/static-fe.css diff --git a/lib/pleroma/web/templates/layout/static_fe.html.eex b/lib/pleroma/web/templates/layout/static_fe.html.eex index e42047de9..62b59f17c 100644 --- a/lib/pleroma/web/templates/layout/static_fe.html.eex +++ b/lib/pleroma/web/templates/layout/static_fe.html.eex @@ -6,170 +6,7 @@ <%= Application.get_env(:pleroma, :instance)[:name] %> - +
diff --git a/priv/static/static/static-fe.css b/priv/static/static/static-fe.css new file mode 100644 index 000000000..d6bb58c33 --- /dev/null +++ b/priv/static/static/static-fe.css @@ -0,0 +1,172 @@ +body { + background-color: #282c37; + font-family: sans-serif; + color: white; +} + +.container { + margin: 50px auto; + max-width: 960px; + padding: 40px; + background-color: #313543; + border-radius: 4px; +} + +header { + border-bottom: 2em solid #282c37; +} + +.activity { + border-radius: 4px; + padding: 1em; + padding-bottom: 2em; + margin-bottom: 1em; +} + +.avatar { + cursor: pointer; +} + +.avatar img { + float: left; + border-radius: 4px; + margin-right: 4px; +} + +.activity-content img, video, audio { + padding: 1em; + max-width: 800px; + max-height: 800px; +} + +#selected { + background-color: #1b2735; +} + +.counts dt, .counts dd { + float: left; + margin-left: 1em; +} + +a { + color: white; +} + +.h-card { + min-height: 48px; + margin-bottom: 8px; +} + +.h-card a { + text-decoration: none; +} + +.h-card a:hover { + text-decoration: underline; +} + +.display-name { + padding-top: 4px; + display: block; + text-overflow: ellipsis; + overflow: hidden; + color: white; +} + +/* keep emoji from being hilariously huge */ +.display-name img { + max-height: 1em; +} + +.display-name .nickname { + padding-top: 4px; + display: block; +} + +.nickname:hover { + text-decoration: none; +} + +.pull-right { + float: right; +} + +.collapse { + margin: 0; + width: auto; +} + +h1 { + margin: 0; +} + +h2 { + color: #9baec8; + font-weight: normal; + font-size: 20px; + margin-bottom: 40px; +} + +form { + width: 100%; +} + +input { + box-sizing: border-box; + width: 100%; + padding: 10px; + margin-top: 20px; + background-color: rgba(0,0,0,.1); + color: white; + border: 0; + border-bottom: 2px solid #9baec8; + font-size: 14px; +} + +input:focus { + border-bottom: 2px solid #4b8ed8; +} + +input[type="checkbox"] { + width: auto; +} + +button { + box-sizing: border-box; + width: 100%; + color: white; + background-color: #419bdd; + border-radius: 4px; + border: none; + padding: 10px; + margin-top: 30px; + text-transform: uppercase; + font-weight: 500; + font-size: 16px; +} + +.alert-danger { + box-sizing: border-box; + width: 100%; + color: #D8000C; + background-color: #FFD2D2; + border-radius: 4px; + border: none; + padding: 10px; + margin-top: 20px; + font-weight: 500; + font-size: 16px; +} + +.alert-info { + box-sizing: border-box; + width: 100%; + color: #00529B; + background-color: #BDE5F8; + border-radius: 4px; + border: none; + padding: 10px; + margin-top: 20px; + font-weight: 500; + font-size: 16px; +} From 5d7c44266ba0355557e5a62ecca69428c5784d88 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Wed, 30 Oct 2019 20:20:54 -0700 Subject: [PATCH 23/40] Change date formatting. --- lib/pleroma/web/static_fe/static_fe_view.ex | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/static_fe/static_fe_view.ex b/lib/pleroma/web/static_fe/static_fe_view.ex index c19aa07e1..6128b2497 100644 --- a/lib/pleroma/web/static_fe/static_fe_view.ex +++ b/lib/pleroma/web/static_fe/static_fe_view.ex @@ -5,6 +5,8 @@ defmodule Pleroma.Web.StaticFE.StaticFEView do use Pleroma.Web, :view + alias Calendar.Strftime + alias Pleroma.Emoji.Formatter alias Pleroma.User alias Pleroma.Web.Gettext alias Pleroma.Web.MediaProxy @@ -31,6 +33,6 @@ def fetch_media_type(%{"mediaType" => mediaType}) do def format_date(date) do {:ok, date, _} = DateTime.from_iso8601(date) - Pleroma.Web.CommonAPI.Utils.format_asctime(date) + Strftime.strftime!(date, "%Y/%m/%d %l:%M:%S %p UTC") end end From 2ac1ece652621df9adf591255f4506564a8ace68 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Wed, 30 Oct 2019 20:21:10 -0700 Subject: [PATCH 24/40] Fix a bug where reblogs were displayed under the wrong user. --- lib/pleroma/web/static_fe/static_fe_controller.ex | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index d2e72b476..9b565d07d 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -34,7 +34,9 @@ def get_counts(%Activity{} = activity) do } end - def represent(%Activity{} = activity, %User{} = user, selected) do + def represent(%Activity{} = activity, selected) do + {:ok, user} = User.get_or_fetch(activity.object.data["actor"]) + %{ user: user, title: get_title(activity.object), @@ -48,11 +50,6 @@ def represent(%Activity{} = activity, %User{} = user, selected) do } end - def represent(%Activity{} = activity, selected) do - {:ok, user} = User.get_or_fetch(activity.data["actor"]) - represent(activity, user, selected) - end - def show_notice(%{assigns: %{notice_id: notice_id}} = conn, _params) do instance_name = Pleroma.Config.get([:instance, :name], "Pleroma") activity = Activity.get_by_id_with_object(notice_id) @@ -73,7 +70,7 @@ def show_user(%{assigns: %{username_or_id: username_or_id}} = conn, _params) do timeline = for activity <- ActivityPub.fetch_user_activities(user, nil, %{}) do - represent(activity, user, false) + represent(activity, false) end render(conn, "profile.html", %{user: user, timeline: timeline, instance_name: instance_name}) From 274cc18e8a585bd72353f9135c18aec0cb8e7ce3 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Thu, 31 Oct 2019 17:44:43 -0700 Subject: [PATCH 25/40] Visually separate header. --- .../web/static_fe/static_fe_controller.ex | 10 +++--- lib/pleroma/web/static_fe/static_fe_view.ex | 1 + .../static_fe/static_fe/conversation.html.eex | 16 +++++---- .../static_fe/static_fe/profile.html.eex | 36 +++++++++++-------- priv/static/static/static-fe.css | 12 ++++--- 5 files changed, 45 insertions(+), 30 deletions(-) diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index 9b565d07d..c35657d8e 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -34,17 +34,17 @@ def get_counts(%Activity{} = activity) do } end - def represent(%Activity{} = activity, selected) do + def represent(%Activity{object: %Object{data: data}} = activity, selected) do {:ok, user} = User.get_or_fetch(activity.object.data["actor"]) %{ user: user, title: get_title(activity.object), - content: activity.object.data["content"] || nil, - attachment: activity.object.data["attachment"], + content: data["content"] || nil, + attachment: data["attachment"], link: Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity.id), - published: activity.object.data["published"], - sensitive: activity.object.data["sensitive"], + published: data["published"], + sensitive: data["sensitive"], selected: selected, counts: get_counts(activity) } diff --git a/lib/pleroma/web/static_fe/static_fe_view.ex b/lib/pleroma/web/static_fe/static_fe_view.ex index 6128b2497..160261af9 100644 --- a/lib/pleroma/web/static_fe/static_fe_view.ex +++ b/lib/pleroma/web/static_fe/static_fe_view.ex @@ -8,6 +8,7 @@ defmodule Pleroma.Web.StaticFE.StaticFEView do alias Calendar.Strftime alias Pleroma.Emoji.Formatter alias Pleroma.User + alias Pleroma.Web.Endpoint alias Pleroma.Web.Gettext alias Pleroma.Web.MediaProxy alias Pleroma.Formatter diff --git a/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex index 3a1249df2..7ac4a9e5f 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex @@ -1,7 +1,11 @@ -

<%= link @instance_name, to: "/" %>

+
+

<%= link @instance_name, to: "/" %>

+
-
- <%= for activity <- @activities do %> - <%= render("_notice.html", activity) %> - <% end %> -
+
+
+ <%= for activity <- @activities do %> + <%= render("_notice.html", activity) %> + <% end %> +
+
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 index 8f2c74627..9b3d0509e 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex @@ -1,16 +1,22 @@ -

<%= link @instance_name, to: "/" %>

+
+

<%= link @instance_name, to: "/" %>

-

-
- - - -
- <%= raw (@user.name |> Formatter.emojify(emoji_for_user(@user))) %> -

-

<%= raw @user.bio %>

-
- <%= for activity <- @timeline do %> - <%= render("_notice.html", Map.put(activity, :selected, false)) %> - <% end %> -
+

+
+ + + +
+ <%= raw Formatter.emojify(@user.name, emoji_for_user(@user)) %> | + <%= link "@#{@user.nickname}@#{Endpoint.host()}", to: User.profile_url(@user) %> +

+

<%= raw @user.bio %>

+
+ +
+
+ <%= for activity <- @timeline do %> + <%= render("_notice.html", Map.put(activity, :selected, false)) %> + <% end %> +
+
diff --git a/priv/static/static/static-fe.css b/priv/static/static/static-fe.css index d6bb58c33..19c56387b 100644 --- a/priv/static/static/static-fe.css +++ b/priv/static/static/static-fe.css @@ -4,7 +4,7 @@ body { color: white; } -.container { +main { margin: 50px auto; max-width: 960px; padding: 40px; @@ -13,7 +13,11 @@ .container { } header { - border-bottom: 2em solid #282c37; + margin: 50px auto; + max-width: 960px; + padding: 40px; + background-color: #313543; + border-radius: 4px; } .activity { @@ -57,11 +61,11 @@ .h-card { margin-bottom: 8px; } -.h-card a { +header a, .h-card a { text-decoration: none; } -.h-card a:hover { +header a:hover, .h-card a:hover { text-decoration: underline; } From c6c706161e462bb6190cb4471e81e5a8c3b66d20 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Thu, 31 Oct 2019 18:26:34 -0700 Subject: [PATCH 26/40] Make sure notice link is remote if the post is remote. --- lib/pleroma/web/static_fe/static_fe_controller.ex | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index c35657d8e..5f69218ce 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -37,12 +37,19 @@ def get_counts(%Activity{} = activity) do def represent(%Activity{object: %Object{data: data}} = activity, selected) do {:ok, user} = User.get_or_fetch(activity.object.data["actor"]) + link = + if user.local do + Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity) + else + data["url"] || data["external_url"] || data["id"] + end + %{ user: user, title: get_title(activity.object), content: data["content"] || nil, attachment: data["attachment"], - link: Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity.id), + link: link, published: data["published"], sensitive: data["sensitive"], selected: selected, From dc3b87d153415bee6a169b4c787f79dbee74c622 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Fri, 1 Nov 2019 19:47:18 -0700 Subject: [PATCH 27/40] Move static FE routing into its own plug. Previously it was piggybacking on FallbackRedirectController for users and OStatusController for notices; now it's all in one place. --- lib/pleroma/plugs/static_fe_plug.ex | 14 +++++ lib/pleroma/web/ostatus/ostatus_controller.ex | 58 +++++++++---------- lib/pleroma/web/router.ex | 1 + .../web/static_fe/static_fe_controller.ex | 33 ++++------- 4 files changed, 53 insertions(+), 53 deletions(-) create mode 100644 lib/pleroma/plugs/static_fe_plug.ex diff --git a/lib/pleroma/plugs/static_fe_plug.ex b/lib/pleroma/plugs/static_fe_plug.ex new file mode 100644 index 000000000..d3abaf4cc --- /dev/null +++ b/lib/pleroma/plugs/static_fe_plug.ex @@ -0,0 +1,14 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Plugs.StaticFEPlug do + def init(options), do: options + + def call(conn, _) do + case Pleroma.Config.get([:instance, :static_fe], false) do + true -> Pleroma.Web.StaticFE.StaticFEController.call(conn, :show) + _ -> conn + end + end +end diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex index be275977e..6958519de 100644 --- a/lib/pleroma/web/ostatus/ostatus_controller.ex +++ b/lib/pleroma/web/ostatus/ostatus_controller.ex @@ -76,41 +76,37 @@ def activity(%{assigns: %{format: format}} = conn, %{"uuid" => uuid}) do end def notice(%{assigns: %{format: format}} = conn, %{"id" => id}) do - if Pleroma.Config.get([:instance, :static_fe], false) do - Pleroma.Web.StaticFE.StaticFEController.call(conn, :show_notice) - else - with {_, %Activity{} = activity} <- {:activity, Activity.get_by_id_with_object(id)}, - {_, true} <- {:public?, Visibility.is_public?(activity)}, - %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do - cond do - format == "html" && activity.data["type"] == "Create" -> - %Object{} = object = Object.normalize(activity) + with {_, %Activity{} = activity} <- {:activity, Activity.get_by_id_with_object(id)}, + {_, true} <- {:public?, Visibility.is_public?(activity)}, + %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do + cond do + format == "html" && activity.data["type"] == "Create" -> + %Object{} = object = Object.normalize(activity) - RedirectController.redirector_with_meta( - conn, - %{ - activity_id: activity.id, - object: object, - url: Router.Helpers.o_status_url(Endpoint, :notice, activity.id), - user: user - } - ) + RedirectController.redirector_with_meta( + conn, + %{ + activity_id: activity.id, + object: object, + url: Router.Helpers.o_status_url(Endpoint, :notice, activity.id), + user: user + } + ) - format == "html" -> - RedirectController.redirector(conn, nil) + format == "html" -> + RedirectController.redirector(conn, nil) - true -> - represent_activity(conn, format, activity, user) - end - else - reason when reason in [{:public?, false}, {:activity, nil}] -> - conn - |> put_status(404) - |> RedirectController.redirector(nil, 404) - - e -> - e + true -> + represent_activity(conn, format, activity, user) end + else + reason when reason in [{:public?, false}, {:activity, nil}] -> + conn + |> put_status(404) + |> RedirectController.redirector(nil, 404) + + e -> + e end end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 8fb4aec13..ecf5f744c 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -495,6 +495,7 @@ defmodule Pleroma.Web.Router do pipeline :ostatus do plug(:accepts, ["html", "xml", "atom", "activity+json", "json"]) + plug(Pleroma.Plugs.StaticFEPlug) end pipeline :oembed do diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index 5f69218ce..96e30f317 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -14,7 +14,6 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do plug(:put_layout, :static_fe) plug(:put_view, Pleroma.Web.StaticFE.StaticFEView) plug(:assign_id) - action_fallback(:not_found) defp get_title(%Object{data: %{"name" => name}}) when is_binary(name), do: name @@ -34,14 +33,15 @@ def get_counts(%Activity{} = activity) do } end + def represent(%Activity{} = activity), do: represent(activity, false) + def represent(%Activity{object: %Object{data: data}} = activity, selected) do {:ok, user} = User.get_or_fetch(activity.object.data["actor"]) link = - if user.local do - Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity) - else - data["url"] || data["external_url"] || data["id"] + case user.local do + true -> Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, activity) + _ -> data["url"] || data["external_url"] || data["id"] end %{ @@ -57,28 +57,27 @@ def represent(%Activity{object: %Object{data: data}} = activity, selected) do } end - def show_notice(%{assigns: %{notice_id: notice_id}} = conn, _params) do + def show(%{assigns: %{notice_id: notice_id}} = conn, _params) do instance_name = Pleroma.Config.get([:instance, :name], "Pleroma") activity = Activity.get_by_id_with_object(notice_id) context = activity.object.data["context"] activities = ActivityPub.fetch_activities_for_context(context, %{}) - represented = + timeline = for a <- Enum.reverse(activities) do represent(a, a.object.id == activity.object.id) end - render(conn, "conversation.html", %{activities: represented, instance_name: instance_name}) + render(conn, "conversation.html", %{activities: timeline, instance_name: instance_name}) end - def show_user(%{assigns: %{username_or_id: username_or_id}} = conn, _params) do + def show(%{assigns: %{username_or_id: username_or_id}} = conn, _params) do instance_name = Pleroma.Config.get([:instance, :name], "Pleroma") %User{} = user = User.get_cached_by_nickname_or_id(username_or_id) timeline = - for activity <- ActivityPub.fetch_user_activities(user, nil, %{}) do - represent(activity, false) - end + ActivityPub.fetch_user_activities(user, nil, %{}) + |> Enum.map(&represent/1) render(conn, "profile.html", %{user: user, timeline: timeline, instance_name: instance_name}) end @@ -89,15 +88,5 @@ def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts), def assign_id(%{path_info: ["users", user_id]} = conn, _opts), do: assign(conn, :username_or_id, user_id) - def assign_id(%{path_info: [user_id]} = conn, _opts), - do: assign(conn, :username_or_id, user_id) - def assign_id(conn, _opts), do: conn - - # Fallback for unhandled types - def not_found(conn, _opts) do - conn - |> put_status(404) - |> text("Not found") - end end From e8bee35578fbbc442657baa4dee0047906b247a9 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Sun, 3 Nov 2019 12:29:17 -0800 Subject: [PATCH 28/40] Static FE plug should only respond to text/html requests. --- lib/pleroma/plugs/static_fe_plug.ex | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/plugs/static_fe_plug.ex b/lib/pleroma/plugs/static_fe_plug.ex index d3abaf4cc..dcbabc9df 100644 --- a/lib/pleroma/plugs/static_fe_plug.ex +++ b/lib/pleroma/plugs/static_fe_plug.ex @@ -5,9 +5,14 @@ defmodule Pleroma.Plugs.StaticFEPlug do def init(options), do: options + def accepts_html?({"accept", a}), do: String.contains?(a, "text/html") + def accepts_html?({_, _}), do: false + def call(conn, _) do - case Pleroma.Config.get([:instance, :static_fe], false) do - true -> Pleroma.Web.StaticFE.StaticFEController.call(conn, :show) + with true <- Pleroma.Config.get([:instance, :static_fe], false), + {_, _} <- Enum.find(conn.req_headers, &accepts_html?/1) do + Pleroma.Web.StaticFE.StaticFEController.call(conn, :show) + else _ -> conn end end From 8969c5522d0ff4b95705ce8dd1249aa76414fe0e Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Mon, 4 Nov 2019 22:02:10 -0800 Subject: [PATCH 29/40] Make many of the improvements suggested in review. --- lib/pleroma/plugs/static_fe_plug.ex | 21 ++++++++++++------- .../web/static_fe/static_fe_controller.ex | 16 ++++++-------- lib/pleroma/web/static_fe/static_fe_view.ex | 2 ++ .../web/templates/layout/static_fe.html.eex | 2 +- .../static_fe/static_fe/conversation.html.eex | 2 +- .../static_fe/static_fe/profile.html.eex | 2 +- 6 files changed, 25 insertions(+), 20 deletions(-) diff --git a/lib/pleroma/plugs/static_fe_plug.ex b/lib/pleroma/plugs/static_fe_plug.ex index dcbabc9df..2af45e52a 100644 --- a/lib/pleroma/plugs/static_fe_plug.ex +++ b/lib/pleroma/plugs/static_fe_plug.ex @@ -3,17 +3,24 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Plugs.StaticFEPlug do + import Plug.Conn + alias Pleroma.Web.StaticFE.StaticFEController + def init(options), do: options - def accepts_html?({"accept", a}), do: String.contains?(a, "text/html") - def accepts_html?({_, _}), do: false - def call(conn, _) do - with true <- Pleroma.Config.get([:instance, :static_fe], false), - {_, _} <- Enum.find(conn.req_headers, &accepts_html?/1) do - Pleroma.Web.StaticFE.StaticFEController.call(conn, :show) + if enabled?() and accepts_html?(conn) do + conn + |> StaticFEController.call(:show) + |> halt() else - _ -> conn + conn end end + + defp enabled?, do: Pleroma.Config.get([:instance, :static_fe], false) + + defp accepts_html?(conn) do + conn |> get_req_header("accept") |> List.first() |> String.contains?("text/html") + end end diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index 96e30f317..a00c6db4f 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -58,28 +58,24 @@ def represent(%Activity{object: %Object{data: data}} = activity, selected) do end def show(%{assigns: %{notice_id: notice_id}} = conn, _params) do - instance_name = Pleroma.Config.get([:instance, :name], "Pleroma") activity = Activity.get_by_id_with_object(notice_id) - context = activity.object.data["context"] - activities = ActivityPub.fetch_activities_for_context(context, %{}) - timeline = - for a <- Enum.reverse(activities) do - represent(a, a.object.id == activity.object.id) - end + activity.object.data["context"] + |> ActivityPub.fetch_activities_for_context(%{}) + |> Enum.reverse() + |> Enum.map(&represent(&1, &1.object.id == activity.object.id)) - render(conn, "conversation.html", %{activities: timeline, instance_name: instance_name}) + render(conn, "conversation.html", %{activities: timeline}) end def show(%{assigns: %{username_or_id: username_or_id}} = conn, _params) do - instance_name = Pleroma.Config.get([:instance, :name], "Pleroma") %User{} = user = User.get_cached_by_nickname_or_id(username_or_id) timeline = ActivityPub.fetch_user_activities(user, nil, %{}) |> Enum.map(&represent/1) - render(conn, "profile.html", %{user: user, timeline: timeline, instance_name: instance_name}) + render(conn, "profile.html", %{user: user, timeline: timeline}) end def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts), diff --git a/lib/pleroma/web/static_fe/static_fe_view.ex b/lib/pleroma/web/static_fe/static_fe_view.ex index 160261af9..5612a06bb 100644 --- a/lib/pleroma/web/static_fe/static_fe_view.ex +++ b/lib/pleroma/web/static_fe/static_fe_view.ex @@ -36,4 +36,6 @@ def format_date(date) do {:ok, date, _} = DateTime.from_iso8601(date) Strftime.strftime!(date, "%Y/%m/%d %l:%M:%S %p UTC") end + + def instance_name, do: Pleroma.Config.get([:instance, :name], "Pleroma") end diff --git a/lib/pleroma/web/templates/layout/static_fe.html.eex b/lib/pleroma/web/templates/layout/static_fe.html.eex index 62b59f17c..4b889bb19 100644 --- a/lib/pleroma/web/templates/layout/static_fe.html.eex +++ b/lib/pleroma/web/templates/layout/static_fe.html.eex @@ -4,7 +4,7 @@ - <%= Application.get_env(:pleroma, :instance)[:name] %> + <%= Pleroma.Config.get([:instance, :name]) %> diff --git a/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex index 7ac4a9e5f..2acd84828 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/conversation.html.eex @@ -1,5 +1,5 @@
-

<%= link @instance_name, to: "/" %>

+

<%= link instance_name(), to: "/" %>

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 index 9b3d0509e..fa3df3b4e 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex @@ -1,5 +1,5 @@
-

<%= link @instance_name, to: "/" %>

+

<%= link instance_name(), to: "/" %>

From df2f59be911acd4626886befbc0c6bcd75752080 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Mon, 4 Nov 2019 22:49:05 -0800 Subject: [PATCH 30/40] Pagination for user profiles. --- .../web/static_fe/static_fe_controller.ex | 22 +++++++++++++++---- .../static_fe/static_fe/profile.html.eex | 9 ++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index a00c6db4f..9f4eeaa36 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -15,6 +15,8 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do plug(:put_view, Pleroma.Web.StaticFE.StaticFEView) plug(:assign_id) + @page_keys ["max_id", "min_id", "limit", "since_id", "order"] + defp get_title(%Object{data: %{"name" => name}}) when is_binary(name), do: name @@ -53,7 +55,8 @@ def represent(%Activity{object: %Object{data: data}} = activity, selected) do published: data["published"], sensitive: data["sensitive"], selected: selected, - counts: get_counts(activity) + counts: get_counts(activity), + id: activity.id } end @@ -68,14 +71,25 @@ def show(%{assigns: %{notice_id: notice_id}} = conn, _params) do render(conn, "conversation.html", %{activities: timeline}) end - def show(%{assigns: %{username_or_id: username_or_id}} = conn, _params) do + def show(%{assigns: %{username_or_id: username_or_id}} = conn, params) do %User{} = user = User.get_cached_by_nickname_or_id(username_or_id) timeline = - ActivityPub.fetch_user_activities(user, nil, %{}) + ActivityPub.fetch_user_activities(user, nil, Map.take(params, @page_keys)) |> Enum.map(&represent/1) - render(conn, "profile.html", %{user: user, timeline: timeline}) + prev_page_id = + (params["min_id"] || params["max_id"]) && + List.first(timeline) && List.first(timeline).id + + next_page_id = List.last(timeline) && List.last(timeline).id + + render(conn, "profile.html", %{ + user: user, + timeline: timeline, + prev_page_id: prev_page_id, + next_page_id: next_page_id + }) end def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts), 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 index fa3df3b4e..94063c92d 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/profile.html.eex @@ -18,5 +18,14 @@ <%= for activity <- @timeline do %> <%= render("_notice.html", Map.put(activity, :selected, false)) %> <% end %> +

+ <%= if @prev_page_id do %> + <%= link "«", to: "?min_id=" <> @prev_page_id %> + <% end %> + <%= if @prev_page_id && @next_page_id, do: " | " %> + <%= if @next_page_id do %> + <%= link "»", to: "?max_id=" <> @next_page_id %> + <% end %> +

From 828259fb6517d35b5f950e07601bab0bdc5b5efd Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Mon, 4 Nov 2019 22:56:51 -0800 Subject: [PATCH 31/40] Catch 404s. --- .../web/static_fe/static_fe_controller.ex | 55 ++++++++++++------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index 9f4eeaa36..4798cad24 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -61,35 +61,48 @@ def represent(%Activity{object: %Object{data: data}} = activity, selected) do end def show(%{assigns: %{notice_id: notice_id}} = conn, _params) do - activity = Activity.get_by_id_with_object(notice_id) - timeline = - activity.object.data["context"] - |> ActivityPub.fetch_activities_for_context(%{}) - |> Enum.reverse() - |> Enum.map(&represent(&1, &1.object.id == activity.object.id)) + case Activity.get_by_id_with_object(notice_id) do + %Activity{} = activity -> + timeline = + activity.object.data["context"] + |> ActivityPub.fetch_activities_for_context(%{}) + |> Enum.reverse() + |> Enum.map(&represent(&1, &1.object.id == activity.object.id)) - render(conn, "conversation.html", %{activities: timeline}) + render(conn, "conversation.html", %{activities: timeline}) + + _ -> + conn + |> put_status(404) + |> render_error(:not_found, "Notice not found") + end end def show(%{assigns: %{username_or_id: username_or_id}} = conn, params) do - %User{} = user = User.get_cached_by_nickname_or_id(username_or_id) + case User.get_cached_by_nickname_or_id(username_or_id) do + %User{} = user -> + timeline = + ActivityPub.fetch_user_activities(user, nil, Map.take(params, @page_keys)) + |> Enum.map(&represent/1) - timeline = - ActivityPub.fetch_user_activities(user, nil, Map.take(params, @page_keys)) - |> Enum.map(&represent/1) + prev_page_id = + (params["min_id"] || params["max_id"]) && + List.first(timeline) && List.first(timeline).id - prev_page_id = - (params["min_id"] || params["max_id"]) && - List.first(timeline) && List.first(timeline).id + next_page_id = List.last(timeline) && List.last(timeline).id - next_page_id = List.last(timeline) && List.last(timeline).id + render(conn, "profile.html", %{ + user: user, + timeline: timeline, + prev_page_id: prev_page_id, + next_page_id: next_page_id + }) - render(conn, "profile.html", %{ - user: user, - timeline: timeline, - prev_page_id: prev_page_id, - next_page_id: next_page_id - }) + _ -> + conn + |> put_status(404) + |> render_error(:not_found, "User not found") + end end def assign_id(%{path_info: ["notice", notice_id]} = conn, _opts), From bfd5d798262f0ecc7ebc260d92c766d39c0766de Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Tue, 5 Nov 2019 21:28:36 -0800 Subject: [PATCH 32/40] Include metadata in static FE conversations and profiles. --- lib/pleroma/web/static_fe/static_fe_controller.ex | 11 +++++++++-- lib/pleroma/web/templates/layout/static_fe.html.eex | 5 ++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index 4798cad24..10bd3fecd 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -9,6 +9,7 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do alias Pleroma.Object alias Pleroma.User alias Pleroma.Web.ActivityPub.ActivityPub + alias Pleroma.Web.Metadata alias Pleroma.Web.Router.Helpers plug(:put_layout, :static_fe) @@ -63,13 +64,16 @@ def represent(%Activity{object: %Object{data: data}} = activity, selected) do def show(%{assigns: %{notice_id: notice_id}} = conn, _params) do case Activity.get_by_id_with_object(notice_id) do %Activity{} = activity -> + %User{} = user = User.get_by_ap_id(activity.object.data["actor"]) + meta = Metadata.build_tags(%{activity_id: notice_id, object: activity.object, user: user}) + timeline = activity.object.data["context"] |> ActivityPub.fetch_activities_for_context(%{}) |> Enum.reverse() |> Enum.map(&represent(&1, &1.object.id == activity.object.id)) - render(conn, "conversation.html", %{activities: timeline}) + render(conn, "conversation.html", %{activities: timeline, meta: meta}) _ -> conn @@ -81,6 +85,8 @@ def show(%{assigns: %{notice_id: notice_id}} = conn, _params) do def show(%{assigns: %{username_or_id: username_or_id}} = conn, params) do case User.get_cached_by_nickname_or_id(username_or_id) do %User{} = user -> + meta = Metadata.build_tags(%{user: user}) + timeline = ActivityPub.fetch_user_activities(user, nil, Map.take(params, @page_keys)) |> Enum.map(&represent/1) @@ -95,7 +101,8 @@ def show(%{assigns: %{username_or_id: username_or_id}} = conn, params) do user: user, timeline: timeline, prev_page_id: prev_page_id, - next_page_id: next_page_id + next_page_id: next_page_id, + meta: meta }) _ -> diff --git a/lib/pleroma/web/templates/layout/static_fe.html.eex b/lib/pleroma/web/templates/layout/static_fe.html.eex index 4b889bb19..5d820bb4b 100644 --- a/lib/pleroma/web/templates/layout/static_fe.html.eex +++ b/lib/pleroma/web/templates/layout/static_fe.html.eex @@ -3,9 +3,8 @@ - - <%= Pleroma.Config.get([:instance, :name]) %> - + <%= Pleroma.Config.get([:instance, :name]) %> + <%= Phoenix.HTML.raw(@meta || "") %> From e27c61218d292c5fbf268f27e81dbe22f93ba90f Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Tue, 5 Nov 2019 21:37:46 -0800 Subject: [PATCH 33/40] Expand subject content automatically when config is set. --- lib/pleroma/web/static_fe/static_fe_view.ex | 7 +++++++ .../web/templates/static_fe/static_fe/_notice.html.eex | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/static_fe/static_fe_view.ex b/lib/pleroma/web/static_fe/static_fe_view.ex index 5612a06bb..72e667728 100644 --- a/lib/pleroma/web/static_fe/static_fe_view.ex +++ b/lib/pleroma/web/static_fe/static_fe_view.ex @@ -38,4 +38,11 @@ def format_date(date) do end def instance_name, do: Pleroma.Config.get([:instance, :name], "Pleroma") + + def open_content? do + Pleroma.Config.get( + [:frontend_configurations, :collapse_message_with_subjects], + true + ) + end end diff --git a/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex index 2a46dadb4..df5e5eedd 100644 --- a/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex +++ b/lib/pleroma/web/templates/static_fe/static_fe/_notice.html.eex @@ -5,7 +5,7 @@ <%= render("_user_card.html", %{user: @user}) %>
<%= if @title != "" do %> -
+
open<% end %>> <%= raw @title %>
<%= raw @content %>
From b0080fa73010cda34215baeee230481b5c56dbca Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Tue, 5 Nov 2019 22:00:19 -0800 Subject: [PATCH 34/40] Render errors in HTML, not with JS. --- lib/pleroma/web/static_fe/static_fe_controller.ex | 4 ++-- lib/pleroma/web/templates/layout/static_fe.html.eex | 2 +- .../web/templates/static_fe/static_fe/error.html.eex | 7 +++++++ 3 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 lib/pleroma/web/templates/static_fe/static_fe/error.html.eex diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index 10bd3fecd..0be47d6b3 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -78,7 +78,7 @@ def show(%{assigns: %{notice_id: notice_id}} = conn, _params) do _ -> conn |> put_status(404) - |> render_error(:not_found, "Notice not found") + |> render("error.html", %{message: "Post not found.", meta: ""}) end end @@ -108,7 +108,7 @@ def show(%{assigns: %{username_or_id: username_or_id}} = conn, params) do _ -> conn |> put_status(404) - |> render_error(:not_found, "User not found") + |> render("error.html", %{message: "User not found.", meta: ""}) end end diff --git a/lib/pleroma/web/templates/layout/static_fe.html.eex b/lib/pleroma/web/templates/layout/static_fe.html.eex index 5d820bb4b..819632cec 100644 --- a/lib/pleroma/web/templates/layout/static_fe.html.eex +++ b/lib/pleroma/web/templates/layout/static_fe.html.eex @@ -4,7 +4,7 @@ <%= Pleroma.Config.get([:instance, :name]) %> - <%= Phoenix.HTML.raw(@meta || "") %> + <%= Phoenix.HTML.raw(assigns[:meta] || "") %> diff --git a/lib/pleroma/web/templates/static_fe/static_fe/error.html.eex b/lib/pleroma/web/templates/static_fe/static_fe/error.html.eex new file mode 100644 index 000000000..d98a1eba7 --- /dev/null +++ b/lib/pleroma/web/templates/static_fe/static_fe/error.html.eex @@ -0,0 +1,7 @@ +
+

<%= gettext("Oops") %>

+
+ +
+

<%= @message %>

+
From 886a07ba573a7dc566f51cfb44e69dac49f401cd Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Thu, 7 Nov 2019 19:31:28 -0800 Subject: [PATCH 35/40] Move static_fe config to its own section instead of in :instance. --- config/config.exs | 2 ++ lib/pleroma/plugs/static_fe_plug.ex | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/config/config.exs b/config/config.exs index 787809b27..685c18380 100644 --- a/config/config.exs +++ b/config/config.exs @@ -599,6 +599,8 @@ config :pleroma, Pleroma.Plugs.RemoteIp, enabled: false +config :pleroma, :static_fe, enabled: false + config :pleroma, :web_cache_ttl, activity_pub: nil, activity_pub_question: 30_000 diff --git a/lib/pleroma/plugs/static_fe_plug.ex b/lib/pleroma/plugs/static_fe_plug.ex index 2af45e52a..b3fb3c582 100644 --- a/lib/pleroma/plugs/static_fe_plug.ex +++ b/lib/pleroma/plugs/static_fe_plug.ex @@ -18,7 +18,7 @@ def call(conn, _) do end end - defp enabled?, do: Pleroma.Config.get([:instance, :static_fe], false) + defp enabled?, do: Pleroma.Config.get([:static_fe, :enabled], false) defp accepts_html?(conn) do conn |> get_req_header("accept") |> List.first() |> String.contains?("text/html") From 4729027f91852a921cf74f507fbc1ac8761a07f0 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Thu, 7 Nov 2019 21:43:21 -0800 Subject: [PATCH 36/40] Prevent non-local notices from rendering. --- lib/pleroma/web/static_fe/static_fe_controller.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index 0be47d6b3..66d2d0367 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -63,7 +63,7 @@ def represent(%Activity{object: %Object{data: data}} = activity, selected) do def show(%{assigns: %{notice_id: notice_id}} = conn, _params) do case Activity.get_by_id_with_object(notice_id) do - %Activity{} = activity -> + %Activity{local: true} = activity -> %User{} = user = User.get_by_ap_id(activity.object.data["actor"]) meta = Metadata.build_tags(%{activity_id: notice_id, object: activity.object, user: user}) From 2bf592f5dc16752bf640da94c169c9cd2d7a5ebb Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Thu, 7 Nov 2019 22:29:46 -0800 Subject: [PATCH 37/40] Add tests for static_fe controller. --- .../static_fe/static_fe_controller_test.exs | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 test/web/static_fe/static_fe_controller_test.exs diff --git a/test/web/static_fe/static_fe_controller_test.exs b/test/web/static_fe/static_fe_controller_test.exs new file mode 100644 index 000000000..9099540bd --- /dev/null +++ b/test/web/static_fe/static_fe_controller_test.exs @@ -0,0 +1,91 @@ +defmodule Pleroma.Web.StaticFE.StaticFEControllerTest do + use Pleroma.Web.ConnCase + alias Pleroma.Web.CommonAPI + import Pleroma.Factory + + clear_config_all([:static_fe, :enabled]) do + Pleroma.Config.put([:static_fe, :enabled], true) + end + + describe "user profile page" do + test "just the profile as HTML", %{conn: conn} do + user = insert(:user) + conn = conn + |> put_req_header("accept", "text/html") + |> get("/users/#{user.nickname}") + + assert html_response(conn, 200) =~ user.nickname + end + + test "renders json unless there's an html accept header", %{conn: conn} do + user = insert(:user) + conn = conn + |> put_req_header("accept", "application/json") + |> get("/users/#{user.nickname}") + + assert json_response(conn, 200) + end + + test "404 when user not found", %{conn: conn} do + conn = conn + |> put_req_header("accept", "text/html") + |> get("/users/limpopo") + + assert html_response(conn, 404) =~ "not found" + end + + test "pagination", %{conn: conn} do + user = insert(:user) + Enum.map(1..30, fn i -> CommonAPI.post(user, %{"status" => "test#{i}"}) end) + conn = conn + |> put_req_header("accept", "text/html") + |> get("/users/#{user.nickname}") + html = html_response(conn, 200) + + assert html =~ ">test30<" + assert html =~ ">test11<" + refute html =~ ">test10<" + refute html =~ ">test1<" + end + + test "pagination, page 2", %{conn: conn} do + user = insert(:user) + activities = + Enum.map(1..30, fn i -> CommonAPI.post(user, %{"status" => "test#{i}"}) end) + {:ok, a11} = Enum.at(activities, 11) + conn = conn + |> put_req_header("accept", "text/html") + |> get("/users/#{user.nickname}?max_id=#{a11.id}") + html = html_response(conn, 200) + + assert html =~ ">test1<" + assert html =~ ">test10<" + refute html =~ ">test20<" + refute html =~ ">test29<" + end + end + + describe "notice rendering" do + test "single notice page", %{conn: conn} do + user = insert(:user) + {:ok, activity} = CommonAPI.post(user, %{"status" => "testing a thing!"}) + + conn = conn + |> put_req_header("accept", "text/html") + |> get("/notice/#{activity.id}") + + html = html_response(conn, 200) + assert html =~ "
" + assert html =~ user.nickname + assert html =~ "testing a thing!" + end + + test "404 when notice not found", %{conn: conn} do + conn = conn + |> put_req_header("accept", "text/html") + |> get("/notice/88c9c317") + + assert html_response(conn, 404) =~ "not found" + end + end +end From ef7c3bdc7a5d4047eca15b8469e1f7d7ab3bd39e Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Fri, 8 Nov 2019 08:55:32 -0800 Subject: [PATCH 38/40] Add some further test cases. Including like ... private visibility, cos that's super important. --- .../web/static_fe/static_fe_controller.ex | 24 ++-- .../static_fe/static_fe_controller_test.exs | 136 +++++++++++++++--- 2 files changed, 126 insertions(+), 34 deletions(-) diff --git a/lib/pleroma/web/static_fe/static_fe_controller.ex b/lib/pleroma/web/static_fe/static_fe_controller.ex index 66d2d0367..5e60c82b0 100644 --- a/lib/pleroma/web/static_fe/static_fe_controller.ex +++ b/lib/pleroma/web/static_fe/static_fe_controller.ex @@ -9,6 +9,7 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do alias Pleroma.Object alias Pleroma.User alias Pleroma.Web.ActivityPub.ActivityPub + alias Pleroma.Web.ActivityPub.Visibility alias Pleroma.Web.Metadata alias Pleroma.Web.Router.Helpers @@ -62,19 +63,20 @@ def represent(%Activity{object: %Object{data: data}} = activity, selected) do end def show(%{assigns: %{notice_id: notice_id}} = conn, _params) do - case Activity.get_by_id_with_object(notice_id) do - %Activity{local: true} = activity -> - %User{} = user = User.get_by_ap_id(activity.object.data["actor"]) - meta = Metadata.build_tags(%{activity_id: notice_id, object: activity.object, user: user}) + with %Activity{local: true} = activity <- + Activity.get_by_id_with_object(notice_id), + true <- Visibility.is_public?(activity.object), + %User{} = user <- User.get_by_ap_id(activity.object.data["actor"]) do + meta = Metadata.build_tags(%{activity_id: notice_id, object: activity.object, user: user}) - timeline = - activity.object.data["context"] - |> ActivityPub.fetch_activities_for_context(%{}) - |> Enum.reverse() - |> Enum.map(&represent(&1, &1.object.id == activity.object.id)) - - render(conn, "conversation.html", %{activities: timeline, meta: meta}) + timeline = + activity.object.data["context"] + |> ActivityPub.fetch_activities_for_context(%{}) + |> Enum.reverse() + |> Enum.map(&represent(&1, &1.object.id == activity.object.id)) + render(conn, "conversation.html", %{activities: timeline, meta: meta}) + else _ -> conn |> put_status(404) diff --git a/test/web/static_fe/static_fe_controller_test.exs b/test/web/static_fe/static_fe_controller_test.exs index 9099540bd..e4bb78b01 100644 --- a/test/web/static_fe/static_fe_controller_test.exs +++ b/test/web/static_fe/static_fe_controller_test.exs @@ -1,6 +1,8 @@ defmodule Pleroma.Web.StaticFE.StaticFEControllerTest do use Pleroma.Web.ConnCase alias Pleroma.Web.CommonAPI + alias Pleroma.Web.ActivityPub.Transmogrifier + import Pleroma.Factory clear_config_all([:static_fe, :enabled]) do @@ -10,36 +12,60 @@ defmodule Pleroma.Web.StaticFE.StaticFEControllerTest do describe "user profile page" do test "just the profile as HTML", %{conn: conn} do user = insert(:user) - conn = conn - |> put_req_header("accept", "text/html") - |> get("/users/#{user.nickname}") + + conn = + conn + |> put_req_header("accept", "text/html") + |> get("/users/#{user.nickname}") assert html_response(conn, 200) =~ user.nickname end test "renders json unless there's an html accept header", %{conn: conn} do user = insert(:user) - conn = conn - |> put_req_header("accept", "application/json") - |> get("/users/#{user.nickname}") + + conn = + conn + |> put_req_header("accept", "application/json") + |> get("/users/#{user.nickname}") assert json_response(conn, 200) end test "404 when user not found", %{conn: conn} do - conn = conn - |> put_req_header("accept", "text/html") - |> get("/users/limpopo") + conn = + conn + |> put_req_header("accept", "text/html") + |> get("/users/limpopo") assert html_response(conn, 404) =~ "not found" end + test "profile does not include private messages", %{conn: conn} do + user = insert(:user) + CommonAPI.post(user, %{"status" => "public"}) + CommonAPI.post(user, %{"status" => "private", "visibility" => "private"}) + + conn = + conn + |> put_req_header("accept", "text/html") + |> get("/users/#{user.nickname}") + + html = html_response(conn, 200) + + assert html =~ ">public<" + refute html =~ ">private<" + end + test "pagination", %{conn: conn} do user = insert(:user) Enum.map(1..30, fn i -> CommonAPI.post(user, %{"status" => "test#{i}"}) end) - conn = conn - |> put_req_header("accept", "text/html") - |> get("/users/#{user.nickname}") + + conn = + conn + |> put_req_header("accept", "text/html") + |> get("/users/#{user.nickname}") + html = html_response(conn, 200) assert html =~ ">test30<" @@ -50,12 +76,14 @@ test "pagination", %{conn: conn} do test "pagination, page 2", %{conn: conn} do user = insert(:user) - activities = - Enum.map(1..30, fn i -> CommonAPI.post(user, %{"status" => "test#{i}"}) end) + activities = Enum.map(1..30, fn i -> CommonAPI.post(user, %{"status" => "test#{i}"}) end) {:ok, a11} = Enum.at(activities, 11) - conn = conn - |> put_req_header("accept", "text/html") - |> get("/users/#{user.nickname}?max_id=#{a11.id}") + + conn = + conn + |> put_req_header("accept", "text/html") + |> get("/users/#{user.nickname}?max_id=#{a11.id}") + html = html_response(conn, 200) assert html =~ ">test1<" @@ -70,9 +98,10 @@ test "single notice page", %{conn: conn} do user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "testing a thing!"}) - conn = conn - |> put_req_header("accept", "text/html") - |> get("/notice/#{activity.id}") + conn = + conn + |> put_req_header("accept", "text/html") + |> get("/notice/#{activity.id}") html = html_response(conn, 200) assert html =~ "
" @@ -80,10 +109,71 @@ test "single notice page", %{conn: conn} do assert html =~ "testing a thing!" end + test "shows the whole thread", %{conn: conn} do + user = insert(:user) + {:ok, activity} = CommonAPI.post(user, %{"status" => "space: the final frontier"}) + + CommonAPI.post(user, %{ + "status" => "these are the voyages or something", + "in_reply_to_status_id" => activity.id + }) + + conn = + conn + |> put_req_header("accept", "text/html") + |> get("/notice/#{activity.id}") + + html = html_response(conn, 200) + assert html =~ "the final frontier" + assert html =~ "voyages" + end + test "404 when notice not found", %{conn: conn} do - conn = conn - |> put_req_header("accept", "text/html") - |> get("/notice/88c9c317") + conn = + conn + |> put_req_header("accept", "text/html") + |> get("/notice/88c9c317") + + assert html_response(conn, 404) =~ "not found" + end + + test "404 for private status", %{conn: conn} do + user = insert(:user) + + {:ok, activity} = + CommonAPI.post(user, %{"status" => "don't show me!", "visibility" => "private"}) + + conn = + conn + |> put_req_header("accept", "text/html") + |> get("/notice/#{activity.id}") + + assert html_response(conn, 404) =~ "not found" + end + + test "404 for remote cached status", %{conn: conn} do + user = insert(:user) + + message = %{ + "@context" => "https://www.w3.org/ns/activitystreams", + "to" => user.follower_address, + "cc" => "https://www.w3.org/ns/activitystreams#Public", + "type" => "Create", + "object" => %{ + "content" => "blah blah blah", + "type" => "Note", + "attributedTo" => user.ap_id, + "inReplyTo" => nil + }, + "actor" => user.ap_id + } + + assert {:ok, activity} = Transmogrifier.handle_incoming(message) + + conn = + conn + |> put_req_header("accept", "text/html") + |> get("/notice/#{activity.id}") assert html_response(conn, 404) =~ "not found" end From 6ef804966461ff4351e6c8d3c867131c2af5d26a Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Sat, 9 Nov 2019 09:50:45 -0800 Subject: [PATCH 39/40] Add changelog entry, cheatsheet docs, and alphabetize. --- CHANGELOG.md | 1 + docs/configuration/cheatsheet.md | 7 +++++++ test/web/static_fe/static_fe_controller_test.exs | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b33d61819..7ae52a28b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - Refreshing poll results for remote polls - Authentication: Added rate limit for password-authorized actions / login existence checks +- Static Frontend: Add the ability to render user profiles and notices server-side without requiring JS app. - Mix task to re-count statuses for all users (`mix pleroma.count_statuses`) - Support for `X-Forwarded-For` and similar HTTP headers which used by reverse proxies to pass a real user IP address to the backend. Must not be enabled unless your instance is behind at least one reverse proxy (such as Nginx, Apache HTTPD or Varnish Cache).
diff --git a/docs/configuration/cheatsheet.md b/docs/configuration/cheatsheet.md index 8f609fcfd..dddd379d3 100644 --- a/docs/configuration/cheatsheet.md +++ b/docs/configuration/cheatsheet.md @@ -794,3 +794,10 @@ config :auto_linker, ] ``` +## :static_fe + +Render profiles and posts using server-generated HTML that is viewable without using JavaScript. + +Available options: + +* `enabled` - Enables the rendering of static HTML. Defaults to `false`. diff --git a/test/web/static_fe/static_fe_controller_test.exs b/test/web/static_fe/static_fe_controller_test.exs index e4bb78b01..effdfbeb3 100644 --- a/test/web/static_fe/static_fe_controller_test.exs +++ b/test/web/static_fe/static_fe_controller_test.exs @@ -1,7 +1,7 @@ defmodule Pleroma.Web.StaticFE.StaticFEControllerTest do use Pleroma.Web.ConnCase - alias Pleroma.Web.CommonAPI alias Pleroma.Web.ActivityPub.Transmogrifier + alias Pleroma.Web.CommonAPI import Pleroma.Factory From 3cc49cdb78bf14897030c476b00fb07064f2d74e Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Sat, 9 Nov 2019 18:26:19 -0800 Subject: [PATCH 40/40] Formatter moved to new module. --- lib/pleroma/web/static_fe/static_fe_view.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/pleroma/web/static_fe/static_fe_view.ex b/lib/pleroma/web/static_fe/static_fe_view.ex index 72e667728..821ece9a9 100644 --- a/lib/pleroma/web/static_fe/static_fe_view.ex +++ b/lib/pleroma/web/static_fe/static_fe_view.ex @@ -11,7 +11,6 @@ defmodule Pleroma.Web.StaticFE.StaticFEView do alias Pleroma.Web.Endpoint alias Pleroma.Web.Gettext alias Pleroma.Web.MediaProxy - alias Pleroma.Formatter alias Pleroma.Web.Metadata.Utils alias Pleroma.Web.Router.Helpers