From 6c0fc06c2180316a5392222582e015c701d7762e Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 21 Apr 2020 22:06:48 -0400 Subject: [PATCH] Move timeline/status rendering to partials for reuse --- lib/clacks/timeline.ex | 25 ++++++++++++++++--- .../controllers/frontend_controller.ex | 4 +-- .../templates/frontend/_status.html.eex | 15 +++++++++++ .../templates/frontend/_timeline.html.eex | 7 ++++++ .../templates/frontend/home.html.eex | 12 +-------- .../templates/frontend/profile.html.eex | 12 +-------- .../templates/frontend/status.html.eex | 16 +----------- 7 files changed, 48 insertions(+), 43 deletions(-) create mode 100644 lib/clacks_web/templates/frontend/_status.html.eex create mode 100644 lib/clacks_web/templates/frontend/_timeline.html.eex diff --git a/lib/clacks/timeline.ex b/lib/clacks/timeline.ex index 4e4e185..af4b2e6 100644 --- a/lib/clacks/timeline.ex +++ b/lib/clacks/timeline.ex @@ -6,21 +6,29 @@ defmodule Clacks.Timeline do @public "https://www.w3.org/ns/activitystreams#Public" @timeline_types ["Create", "Announce"] - @spec actor_timeline(actor :: Actor.t(), only_public :: boolean(), params :: map()) :: [ + @spec actor_timeline( + actor :: Actor.t(), + params :: map(), + only_public :: boolean(), + actors :: boolean() + ) :: [ Activity.t() ] - def actor_timeline(actor, only_public \\ true, params) do + def actor_timeline(actor, params, only_public \\ true, actors \\ false) do Activity |> restrict_to_actor(actor.ap_id) |> restrict_to_types(@timeline_types) |> restrict_to_public(only_public) |> paginate(params) |> limit(^Map.get(params, "limit", 20)) + |> join_with_actors(actors) |> Repo.all() end - @spec home_timeline(user :: User.t(), params :: map()) :: [Activity.t()] - def home_timeline(user, params) do + @spec home_timeline(user :: User.t(), params :: map(), actors :: boolean()) :: [ + Activity.t() + ] + def home_timeline(user, params, actors \\ false) do user = case user.actor do %Ecto.Association.NotLoaded{} -> @@ -39,6 +47,7 @@ defmodule Clacks.Timeline do |> restrict_to_types(@timeline_types) |> paginate(params) |> limit(^Map.get(params, "limit", 20)) + |> join_with_actors(actors) |> Repo.all() end @@ -59,4 +68,12 @@ defmodule Clacks.Timeline do end defp restrict_to_public(query, false), do: query + + defp join_with_actors(query, true) do + query + |> join(:left, [o], a in Actor, on: a.ap_id == fragment("?->>'actor'", o.data)) + |> select([o, a], {o, a}) + end + + defp join_with_actors(query, false), do: query end diff --git a/lib/clacks_web/controllers/frontend_controller.ex b/lib/clacks_web/controllers/frontend_controller.ex index 5a67a55..67a48ae 100644 --- a/lib/clacks_web/controllers/frontend_controller.ex +++ b/lib/clacks_web/controllers/frontend_controller.ex @@ -10,7 +10,7 @@ defmodule ClacksWeb.FrontendController do render(conn, "home.html", %{ user: user, actor: user.actor, - statuses: Timeline.home_timeline(user, params) + statuses_with_authors: Timeline.home_timeline(user, params, true) }) end @@ -40,7 +40,7 @@ defmodule ClacksWeb.FrontendController do end defp actor_statuses(actor, params, only_public: only_public) do - Timeline.actor_timeline(actor, only_public, params) + Timeline.actor_timeline(actor, params, only_public) end def status(conn, %{"id" => id}) do diff --git a/lib/clacks_web/templates/frontend/_status.html.eex b/lib/clacks_web/templates/frontend/_status.html.eex new file mode 100644 index 0000000..58fb5f2 --- /dev/null +++ b/lib/clacks_web/templates/frontend/_status.html.eex @@ -0,0 +1,15 @@ +
+

+ + <%= @author.data["preferredUsername"] %> + +

+

+ + <%= @author.data["name"] %> + +

+
+ <%= @note["content"] %> +
+
diff --git a/lib/clacks_web/templates/frontend/_timeline.html.eex b/lib/clacks_web/templates/frontend/_timeline.html.eex new file mode 100644 index 0000000..b38efa0 --- /dev/null +++ b/lib/clacks_web/templates/frontend/_timeline.html.eex @@ -0,0 +1,7 @@ + diff --git a/lib/clacks_web/templates/frontend/home.html.eex b/lib/clacks_web/templates/frontend/home.html.eex index aa4fed5..2bc4df7 100644 --- a/lib/clacks_web/templates/frontend/home.html.eex +++ b/lib/clacks_web/templates/frontend/home.html.eex @@ -8,14 +8,4 @@ Logged in as <%= <%= submit "Post" %> <% end %> - +<%= render "_timeline.html", statuses_with_authors: @statuses_with_authors %> diff --git a/lib/clacks_web/templates/frontend/profile.html.eex b/lib/clacks_web/templates/frontend/profile.html.eex index ab15525..42009fc 100644 --- a/lib/clacks_web/templates/frontend/profile.html.eex +++ b/lib/clacks_web/templates/frontend/profile.html.eex @@ -2,14 +2,4 @@

@<%= @actor.data["name"] %>

<%= @actor.data["summary"] %>

- +<%= render "_timeline.html", statuses_with_authors: Enum.map(@statuses, &({&1, @actor})) %> diff --git a/lib/clacks_web/templates/frontend/status.html.eex b/lib/clacks_web/templates/frontend/status.html.eex index 58fb5f2..0789470 100644 --- a/lib/clacks_web/templates/frontend/status.html.eex +++ b/lib/clacks_web/templates/frontend/status.html.eex @@ -1,15 +1 @@ -
-

- - <%= @author.data["preferredUsername"] %> - -

-

- - <%= @author.data["name"] %> - -

-
- <%= @note["content"] %> -
-
+<%= render "_status.html", author: @author, note: @note %>