From 1341ee650ecde656f385454264f43b21051e86f2 Mon Sep 17 00:00:00 2001 From: rinpatch Date: Fri, 1 Mar 2019 09:37:29 +0300 Subject: [PATCH 1/2] [#675] Do not show DMs in mentions timeline --- lib/pleroma/web/activity_pub/activity_pub.ex | 24 +++++++++++++++++++ .../web/twitter_api/twitter_api_controller.ex | 1 + test/web/activity_pub/activity_pub_test.exs | 8 +++++++ .../twitter_api_controller_test.exs | 19 ++++++++++++++- 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index cc255cc9e..a61bfa4db 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -420,6 +420,30 @@ def fetch_public_activities(opts \\ %{}) 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 = diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 0d74c30c3..41e3acc60 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -167,6 +167,7 @@ def mentions_timeline(%{assigns: %{user: user}} = conn, params) 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) diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 11262c523..17f48797b 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -55,6 +55,14 @@ test "it restricts by the appropriate visibility" 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 diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 05a832967..ed5683779 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -427,7 +427,10 @@ test "without valid credentials", %{conn: conn} 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 @@ test "with credentials", %{conn: conn, user: current_user} 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 From 9c6abec4d8b2487edeb124aa197a5dd6d771e345 Mon Sep 17 00:00:00 2001 From: rinpatch Date: Fri, 1 Mar 2019 15:48:04 +0300 Subject: [PATCH 2/2] use commonapi.post instead of activitybulder --- .../web/twitter_api/twitter_api_controller_test.exs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index ed5683779..7125d85ab 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -427,10 +427,10 @@ test "without valid credentials", %{conn: conn} do test "with credentials", %{conn: conn, user: current_user} do {:ok, activity} = - ActivityBuilder.insert( - %{"to" => [current_user.ap_id, "https://www.w3.org/ns/activitystreams#Public"]}, - %{user: current_user} - ) + CommonAPI.post(current_user, %{ + "status" => "why is tenshi eating a corndog so cute?", + "visibility" => "public" + }) conn = conn @@ -451,7 +451,10 @@ test "with credentials", %{conn: conn, user: current_user} do 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}) + CommonAPI.post(current_user, %{ + "status" => "Have you guys ever seen how cute tenshi eating a corndog is?", + "visibility" => "direct" + }) conn = conn