Allow restricting public timeline by instance
authorAlex Gleason <alex@alexgleason.me>
Thu, 2 Jul 2020 02:12:59 +0000 (21:12 -0500)
committerAlex Gleason <alex@alexgleason.me>
Sun, 19 Jul 2020 00:35:07 +0000 (19:35 -0500)
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/api_spec/operations/timeline_operation.ex
lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex
test/web/mastodon_api/controllers/timeline_controller_test.exs

index bc7b5d95a6925e98d5f67237d832f6be50322785..9ce2b04dd5d512e064993bb684c52fbd8498d9e3 100644 (file)
@@ -927,16 +927,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
 
   defp restrict_muted_reblogs(query, _), do: query
 
-  defp restrict_instance(query, %{instance: instance}) do
-    users =
-      from(
-        u in User,
-        select: u.ap_id,
-        where: fragment("? LIKE ?", u.nickname, ^"%@#{instance}")
-      )
-      |> Repo.all()
-
-    from(activity in query, where: activity.actor in ^users)
+  defp restrict_instance(query, %{instance: instance}) when is_binary(instance) do
+    from(activity in query, where: ilike(activity.actor, ^"%://#{instance}/%"))
   end
 
   defp restrict_instance(query, _), do: query
index 8e19bace7a9285a5a6b18814a0120b72529376be..83cdbad6964abcbcf87c4c0f2cd08d09322d9352 100644 (file)
@@ -59,6 +59,7 @@ defmodule Pleroma.Web.ApiSpec.TimelineOperation do
       security: [%{"oAuth" => ["read:statuses"]}],
       parameters: [
         local_param(),
+        instance_param(),
         only_media_param(),
         with_muted_param(),
         exclude_visibilities_param(),
@@ -158,6 +159,15 @@ defmodule Pleroma.Web.ApiSpec.TimelineOperation do
     )
   end
 
+  defp instance_param do
+    Operation.parameter(
+      :instance,
+      :query,
+      %Schema{type: :string},
+      "Show only statuses from the given domain"
+    )
+  end
+
   defp with_muted_param do
     Operation.parameter(:with_muted, :query, BooleanLike, "Includeactivities by muted users")
   end
index ab7b1d6aa5859025cd66630ff27eb89d3041ad9c..7dccc00056c56cf42a67ba6aa62c049a5d67cec0 100644 (file)
@@ -110,6 +110,7 @@ defmodule Pleroma.Web.MastodonAPI.TimelineController do
         |> Map.put(:blocking_user, user)
         |> Map.put(:muting_user, user)
         |> Map.put(:reply_filtering_user, user)
+        |> Map.put(:instance, params[:instance])
         |> ActivityPub.fetch_public_activities()
 
       conn
index 50e0d783d4c48bcf4b792844080982513ae39bd6..6acd512c78c30977b7f24d9567bcee974571cfdd 100644 (file)
@@ -140,6 +140,18 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do
       activities = json_response_and_validate_schema(res_conn, 200)
       [%{"id" => ^activity_id}] = activities
     end
+
+    test "can be filtered by instance", %{conn: conn} do
+      user = insert(:user, ap_id: "https://lain.com/users/lain")
+      insert(:note_activity, local: false)
+      insert(:note_activity, local: false)
+
+      {:ok, _} = CommonAPI.post(user, %{status: "test"})
+
+      conn = get(conn, "/api/v1/timelines/public?instance=lain.com")
+
+      assert length(json_response_and_validate_schema(conn, :ok)) == 1
+    end
   end
 
   defp local_and_remote_activities do