Show home timeline on frontend when logged in
This commit is contained in:
parent
680f82664e
commit
797283985e
|
@ -1,8 +1,10 @@
|
|||
defmodule Clacks.Timeline do
|
||||
alias Clacks.{Repo, Actor, Activity, Paginator}
|
||||
alias Clacks.{Repo, Actor, Activity, User}
|
||||
import Clacks.Paginator
|
||||
import Ecto.Query
|
||||
|
||||
@public "https://www.w3.org/ns/activitystreams#Public"
|
||||
@timeline_types ["Create", "Announce"]
|
||||
|
||||
@spec actor_timeline(actor :: Actor.t(), only_public :: boolean(), params :: map()) :: [
|
||||
Activity.t()
|
||||
|
@ -10,9 +12,31 @@ defmodule Clacks.Timeline do
|
|||
def actor_timeline(actor, only_public \\ true, params) do
|
||||
Activity
|
||||
|> restrict_to_actor(actor.ap_id)
|
||||
|> restrict_to_types(["Create", "Announce"])
|
||||
|> restirct_to_public(only_public)
|
||||
|> Paginator.paginate(params)
|
||||
|> restrict_to_types(@timeline_types)
|
||||
|> restrict_to_public(only_public)
|
||||
|> 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()
|
||||
end
|
||||
|
||||
|
@ -24,7 +48,7 @@ defmodule Clacks.Timeline do
|
|||
where(query, [a], fragment("?->>'type'", a.data) in ^types)
|
||||
end
|
||||
|
||||
defp restirct_to_public(query, true) do
|
||||
defp restrict_to_public(query, true) do
|
||||
where(
|
||||
query,
|
||||
[a],
|
||||
|
@ -32,5 +56,5 @@ defmodule Clacks.Timeline do
|
|||
)
|
||||
end
|
||||
|
||||
defp restirct_to_public(query, false), do: query
|
||||
defp restrict_to_public(query, false), do: query
|
||||
end
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
defmodule ClacksWeb.ActorController do
|
||||
use ClacksWeb, :controller
|
||||
alias Clacks.{Actor, User}
|
||||
alias Clacks.{Actor, User, Repo}
|
||||
|
||||
@context "https://www.w3.org/ns/activitystreams"
|
||||
|
||||
plug :get_actor
|
||||
|
||||
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 ->
|
||||
conn
|
||||
|> put_status(404)
|
||||
|
|
|
@ -4,12 +4,13 @@ defmodule ClacksWeb.FrontendController do
|
|||
alias ClacksWeb.Router.Helpers, as: Routes
|
||||
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)
|
||||
|
||||
render(conn, "home.html", %{
|
||||
user: user,
|
||||
actor: user.actor
|
||||
actor: user.actor,
|
||||
statuses: Timeline.home_timeline(user, params)
|
||||
})
|
||||
end
|
||||
|
||||
|
|
|
@ -5,3 +5,15 @@
|
|||
<textarea id="content" name="content" cols="30" rows="10"></textarea>
|
||||
<%= 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>
|
||||
|
|
Loading…
Reference in New Issue