Move timeline/status rendering to partials for reuse

This commit is contained in:
Shadowfacts 2020-04-21 22:06:48 -04:00
parent f65c5752bc
commit 6c0fc06c21
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
7 changed files with 48 additions and 43 deletions

View File

@ -6,21 +6,29 @@ defmodule Clacks.Timeline do
@public "https://www.w3.org/ns/activitystreams#Public" @public "https://www.w3.org/ns/activitystreams#Public"
@timeline_types ["Create", "Announce"] @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() Activity.t()
] ]
def actor_timeline(actor, only_public \\ true, params) do def actor_timeline(actor, params, only_public \\ true, actors \\ false) do
Activity Activity
|> restrict_to_actor(actor.ap_id) |> restrict_to_actor(actor.ap_id)
|> restrict_to_types(@timeline_types) |> restrict_to_types(@timeline_types)
|> restrict_to_public(only_public) |> restrict_to_public(only_public)
|> paginate(params) |> paginate(params)
|> limit(^Map.get(params, "limit", 20)) |> limit(^Map.get(params, "limit", 20))
|> join_with_actors(actors)
|> Repo.all() |> Repo.all()
end end
@spec home_timeline(user :: User.t(), params :: map()) :: [Activity.t()] @spec home_timeline(user :: User.t(), params :: map(), actors :: boolean()) :: [
def home_timeline(user, params) do Activity.t()
]
def home_timeline(user, params, actors \\ false) do
user = user =
case user.actor do case user.actor do
%Ecto.Association.NotLoaded{} -> %Ecto.Association.NotLoaded{} ->
@ -39,6 +47,7 @@ defmodule Clacks.Timeline do
|> restrict_to_types(@timeline_types) |> restrict_to_types(@timeline_types)
|> paginate(params) |> paginate(params)
|> limit(^Map.get(params, "limit", 20)) |> limit(^Map.get(params, "limit", 20))
|> join_with_actors(actors)
|> Repo.all() |> Repo.all()
end end
@ -59,4 +68,12 @@ defmodule Clacks.Timeline do
end end
defp restrict_to_public(query, false), do: query 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 end

View File

@ -10,7 +10,7 @@ defmodule ClacksWeb.FrontendController do
render(conn, "home.html", %{ render(conn, "home.html", %{
user: user, user: user,
actor: user.actor, actor: user.actor,
statuses: Timeline.home_timeline(user, params) statuses_with_authors: Timeline.home_timeline(user, params, true)
}) })
end end
@ -40,7 +40,7 @@ defmodule ClacksWeb.FrontendController do
end end
defp actor_statuses(actor, params, only_public: only_public) do 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 end
def status(conn, %{"id" => id}) do def status(conn, %{"id" => id}) do

View File

@ -0,0 +1,15 @@
<div class="status">
<h2>
<a href="<%= @author.ap_id %>">
<%= @author.data["preferredUsername"] %>
</a>
</h2>
<h3>
<a href="<%= @author.ap_id %>">
<%= @author.data["name"] %>
</a>
</h3>
<div class="status-content">
<%= @note["content"] %>
</div>
</div>

View File

@ -0,0 +1,7 @@
<ul>
<%= for {status, author} <- @statuses_with_authors do %>
<li>
<%= render "_status.html", author: author, note: status.data["object"] %>
</li>
<% end %>
</ul>

View File

@ -8,14 +8,4 @@ Logged in as <a href="<%= Routes.actor_path(@conn, :get, @user.username) %>"><%=
<%= submit "Post" %> <%= submit "Post" %>
<% end %> <% end %>
<ul> <%= render "_timeline.html", statuses_with_authors: @statuses_with_authors %>
<%= for status <- @statuses do %>
<li>
<div class="status">
<div class="status-content">
<%= status.data["object"]["content"] %>
</div>
</div>
</li>
<% end %>
</ul>

View File

@ -2,14 +2,4 @@
<h2>@<%= @actor.data["name"] %></h2> <h2>@<%= @actor.data["name"] %></h2>
<p><%= @actor.data["summary"] %></p> <p><%= @actor.data["summary"] %></p>
<ul> <%= render "_timeline.html", statuses_with_authors: Enum.map(@statuses, &({&1, @actor})) %>
<%= for status <- @statuses do %>
<li>
<div class="status">
<div class="status-content">
<%= status.data["object"]["content"] %>
</div>
</div>
</li>
<% end %>
</ul>

View File

@ -1,15 +1 @@
<div class="status"> <%= render "_status.html", author: @author, note: @note %>
<h2>
<a href="<%= @author.ap_id %>">
<%= @author.data["preferredUsername"] %>
</a>
</h2>
<h3>
<a href="<%= @author.ap_id %>">
<%= @author.data["name"] %>
</a>
</h3>
<div class="status-content">
<%= @note["content"] %>
</div>
</div>