From c8d418acddd72e628caad9a6b11ff6debd3386e9 Mon Sep 17 00:00:00 2001 From: csaurus Date: Thu, 10 May 2018 22:17:33 -0400 Subject: [PATCH] api/v1/timelines/direct implementation --- lib/pleroma/web/activity_pub/activity_pub.ex | 23 +++++++++++++++++++ .../mastodon_api/mastodon_api_controller.ex | 9 ++++++++ lib/pleroma/web/router.ex | 2 ++ 3 files changed, 34 insertions(+) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 8c1ba1ea3..d5d00d4a2 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -224,6 +224,28 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub 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 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do |> restrict_recent(opts) |> restrict_blocked(opts) |> restrict_media(opts) + |> restrict_visibility(opts) end def fetch_activities(recipients, opts \\ %{}) do diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 9f4261143..d190cdc3f 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -227,6 +227,15 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController 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 diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index c025dea33..dbef19d44 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -106,6 +106,8 @@ defmodule Pleroma.Web.Router do get("/timelines/home", MastodonAPIController, :home_timeline) + get("/timelines/direct", MastodonAPIController, :dm_timeline) + get("/favourites", MastodonAPIController, :favourites) post("/statuses", MastodonAPIController, :post_status)