[#675] Do not show DMs in mentions timeline
authorrinpatch <rinpatch@sdf.org>
Fri, 1 Mar 2019 06:37:29 +0000 (09:37 +0300)
committerrinpatch <rinpatch@sdf.org>
Fri, 1 Mar 2019 06:37:29 +0000 (09:37 +0300)
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
test/web/activity_pub/activity_pub_test.exs
test/web/twitter_api/twitter_api_controller_test.exs

index cc255cc9e1a7257e7ebe6855625f2f9f1549fba2..a61bfa4db70fca5b87d002cd36a3058b4784b907 100644 (file)
@@ -420,6 +420,30 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
 
   @valid_visibilities ~w[direct unlisted public private]
 
+  defp restrict_visibility(query, %{visibility: visibility})
+       when is_list(visibility) do
+    if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
+      query =
+        from(
+          a in query,
+          where:
+            fragment(
+              "activity_visibility(?, ?, ?) = ANY (?)",
+              a.actor,
+              a.recipients,
+              a.data,
+              ^visibility
+            )
+        )
+
+      Ecto.Adapters.SQL.to_sql(:all, Repo, query)
+
+      query
+    else
+      Logger.error("Could not restrict visibility to #{visibility}")
+    end
+  end
+
   defp restrict_visibility(query, %{visibility: visibility})
        when visibility in @valid_visibilities do
     query =
index 0d74c30c36266b92b545b02c2d8e46d47d35133c..41e3acc60e7c57db2a7d3228b5de78fb8b876e9f 100644 (file)
@@ -167,6 +167,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
       params
       |> Map.put("type", ["Create", "Announce", "Follow", "Like"])
       |> Map.put("blocking_user", user)
+      |> Map.put(:visibility, ~w[unlisted public private])
 
     activities = ActivityPub.fetch_activities([user.ap_id], params)
 
index 11262c5237d4da84bddea54dc41eae9c13657e46..17f48797b9eb6f00e4538cae02524feb9eb13ee3 100644 (file)
@@ -55,6 +55,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
         ActivityPub.fetch_activities([], %{:visibility => "public", "actor_id" => user.ap_id})
 
       assert activities == [public_activity]
+
+      activities =
+        ActivityPub.fetch_activities([], %{
+          :visibility => ~w[private public],
+          "actor_id" => user.ap_id
+        })
+
+      assert activities == [public_activity, private_activity]
     end
   end
 
index 05a832967643438a7a5de21fde6e17540cab6aec..ed56837798a509b61dad986f9f24ed1cf7d5c86e 100644 (file)
@@ -427,7 +427,10 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
 
     test "with credentials", %{conn: conn, user: current_user} do
       {:ok, activity} =
-        ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: current_user})
+        ActivityBuilder.insert(
+          %{"to" => [current_user.ap_id, "https://www.w3.org/ns/activitystreams#Public"]},
+          %{user: current_user}
+        )
 
       conn =
         conn
@@ -445,6 +448,20 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
                  mentioned: [current_user]
                })
     end
+
+    test "does not show DMs in mentions timeline", %{conn: conn, user: current_user} do
+      {:ok, _activity} =
+        ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: current_user})
+
+      conn =
+        conn
+        |> with_credentials(current_user.nickname, "test")
+        |> get("/api/statuses/mentions.json")
+
+      response = json_response(conn, 200)
+
+      assert length(response) == 0
+    end
   end
 
   describe "GET /api/qvitter/statuses/notifications.json" do