Use timeline module for outbox

This commit is contained in:
Shadowfacts 2020-04-21 21:20:51 -04:00
parent 2972bad192
commit 0ca9d02499
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 14 additions and 24 deletions

View File

@ -15,6 +15,7 @@ defmodule Clacks.Timeline do
|> 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))
|> Repo.all() |> Repo.all()
end end
@ -35,8 +36,9 @@ defmodule Clacks.Timeline do
fragment("?->>'actor'", a.data) == ^user.actor.ap_id or fragment("?->>'actor'", a.data) == ^user.actor.ap_id or
fragment("?->>'actor'", a.data) in ^user.actor.followers fragment("?->>'actor'", a.data) in ^user.actor.followers
) )
|> restrict_to_types([@timeline_types]) |> restrict_to_types(@timeline_types)
|> paginate(params) |> paginate(params)
|> limit(^Map.get(params, "limit", 20))
|> Repo.all() |> Repo.all()
end end

View File

@ -1,32 +1,28 @@
defmodule ClacksWeb.OutboxController do defmodule ClacksWeb.OutboxController do
use ClacksWeb, :controller use ClacksWeb, :controller
alias Clacks.{Repo, Actor, Activity} alias Clacks.{Repo, Actor, Activity, Timeline}
import Ecto.Query import Ecto.Query
@context "https://www.w3.org/ns/activitystreams" @context "https://www.w3.org/ns/activitystreams"
@outbox_types ["Create", "Announce"]
plug :get_actor plug :get_actor
defp get_actor(%Plug.Conn{path_params: %{"nickname" => nickname}} = conn, _opts) do defp get_actor(%Plug.Conn{path_params: %{"username" => username}} = conn, _opts) do
case Actor.get_by_nickname(nickname) do case Actor.get_by_nickname(username) do
nil -> %Actor{local: true} = actor ->
conn
|> put_status(404)
%Actor{local: false} ->
conn
|> put_status(404)
actor ->
assign(conn, :actor, actor) assign(conn, :actor, actor)
_ ->
conn
|> put_status(404)
|> json(%{error: "Not Found"})
end end
end end
def outbox(conn, params) when params == %{} do def outbox(conn, params) when params == %{} do
actor = conn.assigns[:actor] actor = conn.assigns[:actor]
activities = Repo.all(outbox_query(params, actor)) activities = Timeline.actor_timeline(actor, true, params)
data = %{ data = %{
"@context" => @context, "@context" => @context,
@ -43,7 +39,7 @@ defmodule ClacksWeb.OutboxController do
def outbox(conn, params) do def outbox(conn, params) do
actor = conn.assigns[:actor] actor = conn.assigns[:actor]
activities = Repo.all(outbox_query(params, actor)) activities = Timeline.actor_timeline(actor, true, params)
data = data =
outbox_page(conn, params, activities) outbox_page(conn, params, activities)
@ -54,14 +50,6 @@ defmodule ClacksWeb.OutboxController do
|> json(data) |> json(data)
end end
defp outbox_query(params, %Actor{ap_id: ap_id}) do
Activity
|> where([a], a.actor == ^ap_id)
|> where([a], fragment("?->>'type'", a.data) in @outbox_types)
|> Clacks.Paginator.paginate(params)
|> limit(^Map.get(params, "limit", 20))
end
defp outbox_page(conn, pagination_params, activities) do defp outbox_page(conn, pagination_params, activities) do
last_id = List.last(activities).id last_id = List.last(activities).id