Browse Source

Show home timeline on frontend when logged in

master
Shadowfacts 1 year ago
parent
commit
797283985e
Signed by: shadowfacts GPG Key ID: 94A5AB95422746E5
4 changed files with 47 additions and 10 deletions
  1. +30
    -6
      lib/clacks/timeline.ex
  2. +2
    -2
      lib/clacks_web/controllers/actor_controller.ex
  3. +3
    -2
      lib/clacks_web/controllers/frontend_controller.ex
  4. +12
    -0
      lib/clacks_web/templates/frontend/home.html.eex

+ 30
- 6
lib/clacks/timeline.ex View File

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

+ 2
- 2
lib/clacks_web/controllers/actor_controller.ex View File

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


+ 3
- 2
lib/clacks_web/controllers/frontend_controller.ex View File

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


+ 12
- 0
lib/clacks_web/templates/frontend/home.html.eex View File

@ -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…
Cancel
Save