A small ActivityPub server.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

87 lines
2.2 KiB

defmodule Clacks.Timeline do
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(),
params :: map(),
only_public :: boolean()
) :: [
Activity.t()
]
def actor_timeline(actor, params, only_public \\ true) 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))
|> Repo.all()
end
@spec home_timeline(user :: User.t(), params :: map()) :: [
{Activity.t(), Actor.t()}
]
def home_timeline(user, params) do
user =
case user.actor do
%Ecto.Association.NotLoaded{} ->
Repo.preload(user, :actor)
_ ->
user
end
Activity
|> join_with_actors()
|> where(
[activity, actor],
fragment("?->>'actor'", activity.data) == ^user.actor.ap_id or
^user.actor.ap_id in actor.followers
)
|> restrict_to_types(@timeline_types)
|> paginate(params)
|> limit(^Map.get(params, "limit", 20))
|> Repo.all()
end
@spec local_timeline(params :: map()) :: [{Activity.t(), Actor.t()}]
def local_timeline(params) do
Activity
|> where([a], a.local)
|> restrict_to_public(true)
|> restrict_to_types(@timeline_types)
|> paginate(params)
|> limit(^Map.get(params, "limit", 20))
|> join_with_actors()
|> Repo.all()
end
defp restrict_to_actor(query, actor_id) do
where(query, [a], fragment("?->>'actor'", a.data) == ^actor_id)
end
defp restrict_to_types(query, types) do
where(query, [a], fragment("?->>'type'", a.data) in ^types)
end
defp restrict_to_public(query, true) do
where(
query,
[a],
fragment("?->'to' \\? ?", a.data, @public) or fragment("?->'cc' \\? ?", a.data, @public)
)
end
defp restrict_to_public(query, false), do: query
defp join_with_actors(query) do
query
|> join(:left, [o], a in Actor, on: a.ap_id == fragment("?->>'actor'", o.data))
|> select([o, a], {o, a})
end
end