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"
@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

View File

@ -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

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" %>
<% end %>
<ul>
<%= for status <- @statuses do %>
<li>
<div class="status">
<div class="status-content">
<%= status.data["object"]["content"] %>
</div>
</div>
</li>
<% end %>
</ul>
<%= render "_timeline.html", statuses_with_authors: @statuses_with_authors %>

View File

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

View File

@ -1,15 +1 @@
<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>
<%= render "_status.html", author: @author, note: @note %>