Show home timeline on frontend when logged in

This commit is contained in:
Shadowfacts 2020-04-20 18:37:07 -04:00
parent 680f82664e
commit 797283985e
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 47 additions and 10 deletions

View File

@ -1,8 +1,10 @@
defmodule Clacks.Timeline do defmodule Clacks.Timeline do
alias Clacks.{Repo, Actor, Activity, Paginator} alias Clacks.{Repo, Actor, Activity, User}
import Clacks.Paginator
import Ecto.Query import Ecto.Query
@public "https://www.w3.org/ns/activitystreams#Public" @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(), only_public :: boolean(), params :: map()) :: [
Activity.t() Activity.t()
@ -10,9 +12,31 @@ defmodule Clacks.Timeline do
def actor_timeline(actor, only_public \\ true, params) do def actor_timeline(actor, only_public \\ true, params) do
Activity Activity
|> restrict_to_actor(actor.ap_id) |> restrict_to_actor(actor.ap_id)
|> restrict_to_types(["Create", "Announce"]) |> restrict_to_types(@timeline_types)
|> restirct_to_public(only_public) |> restrict_to_public(only_public)
|> Paginator.paginate(params) |> paginate(params)
|> Repo.all()
end
@spec home_timeline(user :: User.t(), params :: map()) :: [Activity.t()]
def home_timeline(user, params) do
user =
case user.actor do
%Ecto.Association.NotLoaded{} ->
Repo.preload(user, :actor)
_ ->
user
end
Activity
|> where(
[a],
fragment("?->>'actor'", a.data) == ^user.actor.ap_id or
fragment("?->>'actor'", a.data) in ^user.actor.followers
)
|> restrict_to_types([@timeline_types])
|> paginate(params)
|> Repo.all() |> Repo.all()
end end
@ -24,7 +48,7 @@ defmodule Clacks.Timeline do
where(query, [a], fragment("?->>'type'", a.data) in ^types) where(query, [a], fragment("?->>'type'", a.data) in ^types)
end end
defp restirct_to_public(query, true) do defp restrict_to_public(query, true) do
where( where(
query, query,
[a], [a],
@ -32,5 +56,5 @@ defmodule Clacks.Timeline do
) )
end end
defp restirct_to_public(query, false), do: query defp restrict_to_public(query, false), do: query
end end

View File

@ -1,13 +1,13 @@
defmodule ClacksWeb.ActorController do defmodule ClacksWeb.ActorController do
use ClacksWeb, :controller use ClacksWeb, :controller
alias Clacks.{Actor, User} alias Clacks.{Actor, User, Repo}
@context "https://www.w3.org/ns/activitystreams" @context "https://www.w3.org/ns/activitystreams"
plug :get_actor plug :get_actor
defp get_actor(%Plug.Conn{path_params: %{"username" => username}} = conn, _opts) do defp get_actor(%Plug.Conn{path_params: %{"username" => username}} = conn, _opts) do
case User.get_by_username(username) do case User.get_by_username(username) |> Repo.preload(:actor) do
nil -> nil ->
conn conn
|> put_status(404) |> put_status(404)

View File

@ -4,12 +4,13 @@ defmodule ClacksWeb.FrontendController do
alias ClacksWeb.Router.Helpers, as: Routes alias ClacksWeb.Router.Helpers, as: Routes
alias ClacksWeb.Endpoint alias ClacksWeb.Endpoint
def index(%Plug.Conn{assigns: %{user: user}} = conn, _params) do def index(%Plug.Conn{assigns: %{user: user}} = conn, params) do
user = Repo.preload(user, :actor) user = Repo.preload(user, :actor)
render(conn, "home.html", %{ render(conn, "home.html", %{
user: user, user: user,
actor: user.actor actor: user.actor,
statuses: Timeline.home_timeline(user, params)
}) })
end end

View File

@ -5,3 +5,15 @@
<textarea id="content" name="content" cols="30" rows="10"></textarea> <textarea id="content" name="content" cols="30" rows="10"></textarea>
<%= submit "Post" %> <%= submit "Post" %>
<% end %> <% end %>
<ul>
<%= for status <- @statuses do %>
<li>
<div class="status">
<div class="status-content">
<%= status.data["object"]["content"] %>
</div>
</div>
</li>
<% end %>
</ul>