forked from AkkomaGang/akkoma
api/v1/timelines/direct implementation
This commit is contained in:
parent
392bd9ef56
commit
c8d418acdd
3 changed files with 34 additions and 0 deletions
|
@ -224,6 +224,28 @@ def fetch_public_activities(opts \\ %{}) do
|
|||
|> Enum.reverse()
|
||||
end
|
||||
|
||||
@valid_visibilities ~w[direct unlisted public private]
|
||||
|
||||
defp restrict_visibility(query, %{visibility: "direct"}) do
|
||||
public = "https://www.w3.org/ns/activitystreams#Public"
|
||||
|
||||
from(
|
||||
activity in query,
|
||||
join: sender in User,
|
||||
on: sender.ap_id == activity.actor,
|
||||
where:
|
||||
fragment("not data->'to' \\? ?", ^public) and fragment("not data->'cc' \\? ?", ^public) and
|
||||
fragment("not data->'to' \\? ?", sender.follower_address)
|
||||
)
|
||||
end
|
||||
|
||||
defp restrict_visibility(_query, %{visibility: visibility})
|
||||
when visibility not in @valid_visibilities do
|
||||
Logger.error("Could not restrict visibility to #{visibility}")
|
||||
end
|
||||
|
||||
defp restrict_visibility(query, _visibility), do: query
|
||||
|
||||
defp restrict_since(query, %{"since_id" => since_id}) do
|
||||
from(activity in query, where: activity.id > ^since_id)
|
||||
end
|
||||
|
@ -347,6 +369,7 @@ def fetch_activities_query(recipients, opts \\ %{}) do
|
|||
|> restrict_recent(opts)
|
||||
|> restrict_blocked(opts)
|
||||
|> restrict_media(opts)
|
||||
|> restrict_visibility(opts)
|
||||
end
|
||||
|
||||
def fetch_activities(recipients, opts \\ %{}) do
|
||||
|
|
|
@ -227,6 +227,15 @@ def user_statuses(%{assigns: %{user: user}} = conn, params) do
|
|||
end
|
||||
end
|
||||
|
||||
def dm_timeline(%{assigns: %{user: user}} = conn, params) do
|
||||
query = ActivityPub.fetch_activities_query([user.ap_id], %{visibility: "direct"})
|
||||
activities = Repo.all(query)
|
||||
|
||||
conn
|
||||
|> add_link_headers(:user_statuses, activities, user.ap_id)
|
||||
|> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
|
||||
end
|
||||
|
||||
def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||
with %Activity{} = activity <- Repo.get(Activity, id),
|
||||
true <- ActivityPub.visible_for_user?(activity, user) do
|
||||
|
|
|
@ -106,6 +106,8 @@ def user_fetcher(username) do
|
|||
|
||||
get("/timelines/home", MastodonAPIController, :home_timeline)
|
||||
|
||||
get("/timelines/direct", MastodonAPIController, :dm_timeline)
|
||||
|
||||
get("/favourites", MastodonAPIController, :favourites)
|
||||
|
||||
post("/statuses", MastodonAPIController, :post_status)
|
||||
|
|
Loading…
Reference in a new issue