api/v1/timelines/direct implementation
authorcsaurus <csaurus@mailbox.org>
Fri, 11 May 2018 02:17:33 +0000 (22:17 -0400)
committercsaurus <csaurus@mailbox.org>
Sun, 13 May 2018 01:50:59 +0000 (21:50 -0400)
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
lib/pleroma/web/router.ex

index 8c1ba1ea39977099e4929157b17e52086e7e995d..d5d00d4a2726f0e5802af5b901836683a7c1b04d 100644 (file)
@@ -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
index 9f4261143889976834586ff51080e21510478f51..d190cdc3f0a6076f5a7d16f7727f05ad8a9cb183 100644 (file)
@@ -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
index c025dea338e6727cd8206e1b805e00b17a9adcde..dbef19d4463ce2afce50df29d3886e1e663fb895 100644 (file)
@@ -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)