From c3110c46f36c1cdfb1fb30855dfba709373548eb Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Tue, 26 Jan 2021 11:58:54 +0300 Subject: [PATCH 1/7] expanding filtration for home timeline added local & remote statuses filtration for home timeline --- lib/pleroma/web/activity_pub/activity_pub.ex | 7 ++ .../api_spec/operations/timeline_operation.ex | 10 +++ .../controllers/timeline_controller.ex | 2 + .../controllers/timeline_controller_test.exs | 76 +++++++++++++++++++ 4 files changed, 95 insertions(+) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index d0bb07aab..58e868119 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -735,6 +735,12 @@ defp restrict_local(query, %{local_only: true}) do defp restrict_local(query, _), do: query + defp restrict_remote(query, %{only_remote: true}) do + from(activity in query, where: activity.local == false) + end + + defp restrict_remote(query, _), do: query + defp restrict_actor(query, %{actor_id: actor_id}) do from(activity in query, where: activity.actor == ^actor_id) end @@ -1111,6 +1117,7 @@ def fetch_activities_query(recipients, opts \\ %{}) do |> restrict_tag_all(opts) |> restrict_since(opts) |> restrict_local(opts) + |> restrict_remote(opts) |> restrict_actor(opts) |> restrict_type(opts) |> restrict_state(opts) diff --git a/lib/pleroma/web/api_spec/operations/timeline_operation.ex b/lib/pleroma/web/api_spec/operations/timeline_operation.ex index e1ebdab38..2f44cb70d 100644 --- a/lib/pleroma/web/api_spec/operations/timeline_operation.ex +++ b/lib/pleroma/web/api_spec/operations/timeline_operation.ex @@ -25,6 +25,7 @@ def home_operation do security: [%{"oAuth" => ["read:statuses"]}], parameters: [ local_param(), + remote_param(), with_muted_param(), exclude_visibilities_param(), reply_visibility_param() | pagination_params() @@ -198,4 +199,13 @@ defp only_media_param do "Show only statuses with media attached?" ) end + + defp remote_param do + Operation.parameter( + :only_remote, + :query, + %Schema{allOf: [BooleanLike], default: false}, + "Show only remote statuses?" + ) + end end diff --git a/lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex b/lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex index 08e6f23b9..b63945912 100644 --- a/lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex @@ -51,6 +51,8 @@ def home(%{assigns: %{user: user}} = conn, params) do |> Map.put(:reply_filtering_user, user) |> Map.put(:announce_filtering_user, user) |> Map.put(:user, user) + |> Map.put(:local_only, params[:local]) + |> Map.delete(:local) activities = [user.ap_id | User.following(user)] diff --git a/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs index 664375fef..30118f74e 100644 --- a/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs @@ -90,6 +90,82 @@ test "muted emotions", %{user: user, conn: conn} do } ] = result end + + test "local/remote filtering", %{conn: conn, user: user} do + local = insert(:user) + remote = insert(:user, local: false) + + {:ok, user, local} = User.follow(user, local) + {:ok, _user, remote} = User.follow(user, remote) + + object1 = + insert(:note, %{ + data: %{ + "to" => ["https://www.w3.org/ns/activitystreams#Public", User.ap_followers(local)] + }, + user: local + }) + + activity1 = + insert(:note_activity, %{ + note: object1, + recipients: ["https://www.w3.org/ns/activitystreams#Public", User.ap_followers(local)], + user: local + }) + + object2 = + insert(:note, %{ + data: %{ + "to" => ["https://www.w3.org/ns/activitystreams#Public", User.ap_followers(remote)] + }, + user: remote + }) + + activity2 = + insert(:note_activity, %{ + note: object2, + recipients: ["https://www.w3.org/ns/activitystreams#Public", User.ap_followers(remote)], + user: remote, + local: false + }) + + resp1 = + conn + |> get("/api/v1/timelines/home") + |> json_response_and_validate_schema(200) + + without_filter_ids = Enum.map(resp1, & &1["id"]) + + assert activity1.id in without_filter_ids + assert activity2.id in without_filter_ids + + resp2 = + conn + |> get("/api/v1/timelines/home?local=true") + |> json_response_and_validate_schema(200) + + only_local_ids = Enum.map(resp2, & &1["id"]) + + assert activity1.id in only_local_ids + refute activity2.id in only_local_ids + + resp3 = + conn + |> get("/api/v1/timelines/home?only_remote=true") + |> json_response_and_validate_schema(200) + + only_remote_ids = Enum.map(resp3, & &1["id"]) + + refute activity1.id in only_remote_ids + assert activity2.id in only_remote_ids + + resp4 = + conn + |> get("/api/v1/timelines/home?only_remote=true&local=true") + |> json_response_and_validate_schema(200) + + assert resp4 == [] + end end describe "public" do From b6a72680e2a20e30fa4e9dbc3f4f60e4c51dd63f Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Tue, 26 Jan 2021 14:35:31 +0300 Subject: [PATCH 2/7] added only_media flag to home timeline --- .../api_spec/operations/timeline_operation.ex | 1 + .../controllers/timeline_controller_test.exs | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/lib/pleroma/web/api_spec/operations/timeline_operation.ex b/lib/pleroma/web/api_spec/operations/timeline_operation.ex index 2f44cb70d..7b2fe48a5 100644 --- a/lib/pleroma/web/api_spec/operations/timeline_operation.ex +++ b/lib/pleroma/web/api_spec/operations/timeline_operation.ex @@ -26,6 +26,7 @@ def home_operation do parameters: [ local_param(), remote_param(), + only_media_param(), with_muted_param(), exclude_visibilities_param(), reply_visibility_param() | pagination_params() diff --git a/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs index 30118f74e..d8cc3c9b9 100644 --- a/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs @@ -166,6 +166,66 @@ test "local/remote filtering", %{conn: conn, user: user} do assert resp4 == [] end + + test "only_media flag", %{conn: conn, user: user} do + other = insert(:user) + {:ok, _, other} = User.follow(user, other) + + without_media = + insert(:note_activity, + user: other, + recipients: ["https://www.w3.org/ns/activitystreams#Public", User.ap_followers(other)] + ) + + obj = + insert(:note, %{ + data: %{ + "attachment" => [ + %{ + "mediaType" => "image/jpeg", + "name" => "an_image.jpg", + "type" => "Document", + "url" => [ + %{ + "href" => + "http://localhost:4001/media/8270697e-104f-4a54-a7c1-514bb6713f2c/some_image.jpg", + "mediaType" => "image/jpeg", + "type" => "Link" + } + ] + } + ] + }, + user: other + }) + + with_media = + insert(:note_activity, %{ + note: obj, + recipients: ["https://www.w3.org/ns/activitystreams#Public", User.ap_followers(other)], + user: other + }) + + resp1 = + conn + |> get("/api/v1/timelines/home") + |> json_response_and_validate_schema(200) + + without_filter_ids = Enum.map(resp1, & &1["id"]) + + assert without_media.id in without_filter_ids + assert with_media.id in without_filter_ids + + resp2 = + conn + |> get("/api/v1/timelines/home?only_media=true") + |> json_response_and_validate_schema(200) + + only_media_ids = Enum.map(resp2, & &1["id"]) + + refute without_media.id in only_media_ids + assert with_media.id in only_media_ids + end end describe "public" do From e21af1cfe48779427b6abf815022ebb88b6815d7 Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Tue, 26 Jan 2021 14:42:03 +0300 Subject: [PATCH 3/7] only_media & only_remote docs and changelog --- CHANGELOG.md | 3 ++- docs/development/API/differences_in_mastoapi_responses.md | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4f3867a2..0d1039be8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,7 +48,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Mastodon API: User and conversation mutes can now auto-expire if `expires_in` parameter was given while adding the mute. - Admin API: An endpoint to manage frontends. - Streaming API: Add follow relationships updates. -- WebPush: Introduce `pleroma:chat_mention` and `pleroma:emoji_reaction` notification types +- WebPush: Introduce `pleroma:chat_mention` and `pleroma:emoji_reaction` notification types. +- Mastodon API: Added `only_media` & `only_remote` parameters to the home timeline. ### Fixed diff --git a/docs/development/API/differences_in_mastoapi_responses.md b/docs/development/API/differences_in_mastoapi_responses.md index 84430408b..cb34324ab 100644 --- a/docs/development/API/differences_in_mastoapi_responses.md +++ b/docs/development/API/differences_in_mastoapi_responses.md @@ -16,6 +16,10 @@ Adding the parameter `reply_visibility` to the public and home timelines queries Adding the parameter `instance=lain.com` to the public timeline will show only statuses originating from `lain.com` (or any remote instance). +Adding the parameter `only_media=true` to the home timeline will show only statuses with media attachments. + +Adding the parameter `only_remote=true` to the home timeline will show only remote statuses. + ## Statuses - `visibility`: has additional possible values `list` and `local` (for local-only statuses) From 2cb6dc5a3a3bd7e2326fe632a34b74cb150c5821 Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Tue, 26 Jan 2021 16:55:44 +0300 Subject: [PATCH 4/7] list timeline filtration by params --- .../api_spec/operations/timeline_operation.ex | 7 +- .../controllers/timeline_controller.ex | 1 + .../controllers/timeline_controller_test.exs | 182 +++++++++++------- test/support/factory.ex | 31 +++ 4 files changed, 145 insertions(+), 76 deletions(-) diff --git a/lib/pleroma/web/api_spec/operations/timeline_operation.ex b/lib/pleroma/web/api_spec/operations/timeline_operation.ex index 7b2fe48a5..e5bf18d4d 100644 --- a/lib/pleroma/web/api_spec/operations/timeline_operation.ex +++ b/lib/pleroma/web/api_spec/operations/timeline_operation.ex @@ -25,7 +25,7 @@ def home_operation do security: [%{"oAuth" => ["read:statuses"]}], parameters: [ local_param(), - remote_param(), + only_remote_param(), only_media_param(), with_muted_param(), exclude_visibilities_param(), @@ -134,6 +134,9 @@ def list_operation do required: true ), with_muted_param(), + local_param(), + only_remote_param(), + only_media_param(), exclude_visibilities_param() | pagination_params() ], operationId: "TimelineController.list", @@ -201,7 +204,7 @@ defp only_media_param do ) end - defp remote_param do + defp only_remote_param do Operation.parameter( :only_remote, :query, diff --git a/lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex b/lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex index b63945912..cef299aa4 100644 --- a/lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex @@ -192,6 +192,7 @@ def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do |> Map.put(:blocking_user, user) |> Map.put(:user, user) |> Map.put(:muting_user, user) + |> Map.put(:local_only, params[:local]) # we must filter the following list for the user to avoid leaking statuses the user # does not actually have permission to see (for more info, peruse security issue #270). diff --git a/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs index d8cc3c9b9..75a008f4c 100644 --- a/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs @@ -92,42 +92,13 @@ test "muted emotions", %{user: user, conn: conn} do end test "local/remote filtering", %{conn: conn, user: user} do - local = insert(:user) - remote = insert(:user, local: false) + local_user = insert(:user) + {:ok, user, local_user} = User.follow(user, local_user) + {:ok, local_activity} = CommonAPI.post(local_user, %{status: "Status"}) - {:ok, user, local} = User.follow(user, local) - {:ok, _user, remote} = User.follow(user, remote) - - object1 = - insert(:note, %{ - data: %{ - "to" => ["https://www.w3.org/ns/activitystreams#Public", User.ap_followers(local)] - }, - user: local - }) - - activity1 = - insert(:note_activity, %{ - note: object1, - recipients: ["https://www.w3.org/ns/activitystreams#Public", User.ap_followers(local)], - user: local - }) - - object2 = - insert(:note, %{ - data: %{ - "to" => ["https://www.w3.org/ns/activitystreams#Public", User.ap_followers(remote)] - }, - user: remote - }) - - activity2 = - insert(:note_activity, %{ - note: object2, - recipients: ["https://www.w3.org/ns/activitystreams#Public", User.ap_followers(remote)], - user: remote, - local: false - }) + remote_user = insert(:user, local: false) + {:ok, _user, remote_user} = User.follow(user, remote_user) + remote_activity = create_remote_activity(remote_user) resp1 = conn @@ -136,8 +107,8 @@ test "local/remote filtering", %{conn: conn, user: user} do without_filter_ids = Enum.map(resp1, & &1["id"]) - assert activity1.id in without_filter_ids - assert activity2.id in without_filter_ids + assert local_activity.id in without_filter_ids + assert remote_activity.id in without_filter_ids resp2 = conn @@ -146,8 +117,8 @@ test "local/remote filtering", %{conn: conn, user: user} do only_local_ids = Enum.map(resp2, & &1["id"]) - assert activity1.id in only_local_ids - refute activity2.id in only_local_ids + assert local_activity.id in only_local_ids + refute remote_activity.id in only_local_ids resp3 = conn @@ -156,8 +127,8 @@ test "local/remote filtering", %{conn: conn, user: user} do only_remote_ids = Enum.map(resp3, & &1["id"]) - refute activity1.id in only_remote_ids - assert activity2.id in only_remote_ids + refute local_activity.id in only_remote_ids + assert remote_activity.id in only_remote_ids resp4 = conn @@ -171,40 +142,9 @@ test "only_media flag", %{conn: conn, user: user} do other = insert(:user) {:ok, _, other} = User.follow(user, other) - without_media = - insert(:note_activity, - user: other, - recipients: ["https://www.w3.org/ns/activitystreams#Public", User.ap_followers(other)] - ) + {:ok, without_media} = CommonAPI.post(other, %{status: "some status"}) - obj = - insert(:note, %{ - data: %{ - "attachment" => [ - %{ - "mediaType" => "image/jpeg", - "name" => "an_image.jpg", - "type" => "Document", - "url" => [ - %{ - "href" => - "http://localhost:4001/media/8270697e-104f-4a54-a7c1-514bb6713f2c/some_image.jpg", - "mediaType" => "image/jpeg", - "type" => "Link" - } - ] - } - ] - }, - user: other - }) - - with_media = - insert(:note_activity, %{ - note: obj, - recipients: ["https://www.w3.org/ns/activitystreams#Public", User.ap_followers(other)], - user: other - }) + with_media = create_with_media_activity(other) resp1 = conn @@ -680,6 +620,67 @@ test "muted emotions", %{user: user, conn: conn} do } ] = result end + + test "filering with params", %{user: user, conn: conn} do + {:ok, list} = Pleroma.List.create("name", user) + + local_user = insert(:user) + {:ok, local_activity} = CommonAPI.post(local_user, %{status: "Marisa is stupid."}) + with_media = create_with_media_activity(local_user) + {:ok, list} = Pleroma.List.follow(list, local_user) + + remote_user = insert(:user, local: false) + remote_activity = create_remote_activity(remote_user) + {:ok, list} = Pleroma.List.follow(list, remote_user) + + resp1 = + conn |> get("/api/v1/timelines/list/#{list.id}") |> json_response_and_validate_schema(200) + + all_ids = Enum.map(resp1, & &1["id"]) + + assert local_activity.id in all_ids + assert with_media.id in all_ids + assert remote_activity.id in all_ids + + resp2 = + conn + |> get("/api/v1/timelines/list/#{list.id}?local=true") + |> json_response_and_validate_schema(200) + + only_local_ids = Enum.map(resp2, & &1["id"]) + + assert local_activity.id in only_local_ids + assert with_media.id in only_local_ids + refute remote_activity.id in only_local_ids + + resp3 = + conn + |> get("/api/v1/timelines/list/#{list.id}?only_remote=true") + |> json_response_and_validate_schema(200) + + only_remote_ids = Enum.map(resp3, & &1["id"]) + + refute local_activity.id in only_remote_ids + refute with_media.id in only_remote_ids + assert remote_activity.id in only_remote_ids + + resp4 = + conn + |> get("/api/v1/timelines/list/#{list.id}?only_media=true") + |> json_response_and_validate_schema(200) + + only_media_ids = Enum.map(resp4, & &1["id"]) + + refute local_activity.id in only_media_ids + assert with_media.id in only_media_ids + refute remote_activity.id in only_media_ids + + assert conn + |> get( + "/api/v1/timelines/list/#{list.id}?only_media=true&local=true&only_remote=true" + ) + |> json_response_and_validate_schema(200) == [] + end end describe "hashtag" do @@ -862,4 +863,37 @@ test "with `%{local: true, federated: false}`, forbids unauthenticated access to ensure_authenticated_access(base_uri) end end + + defp create_remote_activity(user) do + obj = + insert(:note, %{ + data: %{ + "to" => [ + "https://www.w3.org/ns/activitystreams#Public", + User.ap_followers(user) + ] + }, + user: user + }) + + insert(:note_activity, %{ + note: obj, + recipients: [ + "https://www.w3.org/ns/activitystreams#Public", + User.ap_followers(user) + ], + user: user, + local: false + }) + end + + defp create_with_media_activity(user) do + obj = insert(:attachment_note, user: user) + + insert(:note_activity, %{ + note: obj, + recipients: ["https://www.w3.org/ns/activitystreams#Public", User.ap_followers(user)], + user: user + }) + end end diff --git a/test/support/factory.ex b/test/support/factory.ex index bf9592064..436e19409 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -104,6 +104,37 @@ def note_factory(attrs \\ %{}) do } end + def attachment_note_factory(attrs \\ %{}) do + user = attrs[:user] || insert(:user) + {length, attrs} = Map.pop(attrs, :length, 1) + + data = %{ + "attachment" => + Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end) + |> Enum.take(length) + } + + build(:note, Map.put(attrs, :data, data)) + end + + defp attachment_data(ap_id, href) do + href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg") + + %{ + "url" => [ + %{ + "href" => href, + "type" => "Link", + "mediaType" => "image/jpeg" + } + ], + "name" => "some name", + "type" => "Document", + "actor" => ap_id, + "mediaType" => "image/jpeg" + } + end + def audio_factory(attrs \\ %{}) do text = sequence(:text, &"lain radio episode #{&1}") From 77f0a0af7df3ad4cf566a8c68560a09ba6a50cd5 Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Tue, 26 Jan 2021 17:43:49 +0300 Subject: [PATCH 5/7] more tests and update for docs and changelog --- CHANGELOG.md | 2 +- .../API/differences_in_mastoapi_responses.md | 6 +- .../api_spec/operations/timeline_operation.ex | 2 + .../controllers/timeline_controller_test.exs | 262 +++++++++++++----- 4 files changed, 195 insertions(+), 77 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d1039be8..70d2ac0a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,7 +49,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Admin API: An endpoint to manage frontends. - Streaming API: Add follow relationships updates. - WebPush: Introduce `pleroma:chat_mention` and `pleroma:emoji_reaction` notification types. -- Mastodon API: Added `only_media` & `only_remote` parameters to the home timeline. +- Mastodon API: Home, public, hashtag & list timelines accept `only_media`, `only_remote` & `local` parameters for filtration. ### Fixed diff --git a/docs/development/API/differences_in_mastoapi_responses.md b/docs/development/API/differences_in_mastoapi_responses.md index cb34324ab..e9ab896b7 100644 --- a/docs/development/API/differences_in_mastoapi_responses.md +++ b/docs/development/API/differences_in_mastoapi_responses.md @@ -16,9 +16,11 @@ Adding the parameter `reply_visibility` to the public and home timelines queries Adding the parameter `instance=lain.com` to the public timeline will show only statuses originating from `lain.com` (or any remote instance). -Adding the parameter `only_media=true` to the home timeline will show only statuses with media attachments. +Home, public, hashtag & list timelines can filter statuses by accepting these parameters: -Adding the parameter `only_remote=true` to the home timeline will show only remote statuses. +- `only_media`: show only statuses with media attached +- `local`: show only local statuses +- `only_remote`: show only remote statuses ## Statuses diff --git a/lib/pleroma/web/api_spec/operations/timeline_operation.ex b/lib/pleroma/web/api_spec/operations/timeline_operation.ex index e5bf18d4d..52008e27c 100644 --- a/lib/pleroma/web/api_spec/operations/timeline_operation.ex +++ b/lib/pleroma/web/api_spec/operations/timeline_operation.ex @@ -63,6 +63,7 @@ def public_operation do local_param(), instance_param(), only_media_param(), + only_remote_param(), with_muted_param(), exclude_visibilities_param(), reply_visibility_param() | pagination_params() @@ -109,6 +110,7 @@ def hashtag_operation do ), local_param(), only_media_param(), + only_remote_param(), with_muted_param(), exclude_visibilities_param() | pagination_params() ], diff --git a/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs index 75a008f4c..066762748 100644 --- a/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs @@ -91,80 +91,63 @@ test "muted emotions", %{user: user, conn: conn} do ] = result end - test "local/remote filtering", %{conn: conn, user: user} do + test "filtering", %{conn: conn, user: user} do local_user = insert(:user) {:ok, user, local_user} = User.follow(user, local_user) {:ok, local_activity} = CommonAPI.post(local_user, %{status: "Status"}) + with_media = create_with_media_activity(local_user) remote_user = insert(:user, local: false) {:ok, _user, remote_user} = User.follow(user, remote_user) remote_activity = create_remote_activity(remote_user) - resp1 = + without_filter_ids = conn |> get("/api/v1/timelines/home") |> json_response_and_validate_schema(200) - - without_filter_ids = Enum.map(resp1, & &1["id"]) + |> Enum.map(& &1["id"]) assert local_activity.id in without_filter_ids assert remote_activity.id in without_filter_ids + assert with_media.id in without_filter_ids - resp2 = + only_local_ids = conn |> get("/api/v1/timelines/home?local=true") |> json_response_and_validate_schema(200) - - only_local_ids = Enum.map(resp2, & &1["id"]) + |> Enum.map(& &1["id"]) assert local_activity.id in only_local_ids refute remote_activity.id in only_local_ids + assert with_media.id in only_local_ids - resp3 = + only_local_media_ids = + conn + |> get("/api/v1/timelines/home?local=true&only_media=true") + |> json_response_and_validate_schema(200) + |> Enum.map(& &1["id"]) + + refute local_activity.id in only_local_media_ids + refute remote_activity.id in only_local_media_ids + assert with_media.id in only_local_media_ids + + only_remote_ids = conn |> get("/api/v1/timelines/home?only_remote=true") |> json_response_and_validate_schema(200) - - only_remote_ids = Enum.map(resp3, & &1["id"]) + |> Enum.map(& &1["id"]) refute local_activity.id in only_remote_ids assert remote_activity.id in only_remote_ids + refute with_media.id in only_remote_ids - resp4 = - conn - |> get("/api/v1/timelines/home?only_remote=true&local=true") - |> json_response_and_validate_schema(200) + assert conn + |> get("/api/v1/timelines/home?only_remote=true&only_media=true") + |> json_response_and_validate_schema(200) == [] - assert resp4 == [] - end - - test "only_media flag", %{conn: conn, user: user} do - other = insert(:user) - {:ok, _, other} = User.follow(user, other) - - {:ok, without_media} = CommonAPI.post(other, %{status: "some status"}) - - with_media = create_with_media_activity(other) - - resp1 = - conn - |> get("/api/v1/timelines/home") - |> json_response_and_validate_schema(200) - - without_filter_ids = Enum.map(resp1, & &1["id"]) - - assert without_media.id in without_filter_ids - assert with_media.id in without_filter_ids - - resp2 = - conn - |> get("/api/v1/timelines/home?only_media=true") - |> json_response_and_validate_schema(200) - - only_media_ids = Enum.map(resp2, & &1["id"]) - - refute without_media.id in only_media_ids - assert with_media.id in only_media_ids + assert conn + |> get("/api/v1/timelines/home?only_remote=true&local=true") + |> json_response_and_validate_schema(200) == [] end end @@ -174,27 +157,80 @@ test "the public timeline", %{conn: conn} do user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{status: "test"}) + with_media = create_with_media_activity(user) - _activity = insert(:note_activity, local: false) + remote = insert(:note_activity, local: false) - conn = get(conn, "/api/v1/timelines/public?local=False") + assert conn + |> get("/api/v1/timelines/public?local=False") + |> json_response_and_validate_schema(:ok) + |> length == 3 - assert length(json_response_and_validate_schema(conn, :ok)) == 2 + local_ids = + conn + |> get("/api/v1/timelines/public?local=True") + |> json_response_and_validate_schema(:ok) + |> Enum.map(& &1["id"]) - conn = get(build_conn(), "/api/v1/timelines/public?local=True") + assert activity.id in local_ids + assert with_media.id in local_ids + refute remote.id in local_ids - assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok) + local_ids = + conn + |> get("/api/v1/timelines/public?local=True") + |> json_response_and_validate_schema(:ok) + |> Enum.map(& &1["id"]) - conn = get(build_conn(), "/api/v1/timelines/public?local=1") + assert activity.id in local_ids + assert with_media.id in local_ids + refute remote.id in local_ids - assert [%{"content" => "test"}] = json_response_and_validate_schema(conn, :ok) + local_ids = + conn + |> get("/api/v1/timelines/public?local=True&only_media=true") + |> json_response_and_validate_schema(:ok) + |> Enum.map(& &1["id"]) + + refute activity.id in local_ids + assert with_media.id in local_ids + refute remote.id in local_ids + + local_ids = + conn + |> get("/api/v1/timelines/public?local=1") + |> json_response_and_validate_schema(:ok) + |> Enum.map(& &1["id"]) + + assert activity.id in local_ids + assert with_media.id in local_ids + refute remote.id in local_ids + + remote_id = remote.id + + assert [%{"id" => ^remote_id}] = + conn + |> get("/api/v1/timelines/public?only_remote=true") + |> json_response_and_validate_schema(:ok) + + with_media_id = with_media.id + + assert [%{"id" => ^with_media_id}] = + conn + |> get("/api/v1/timelines/public?only_media=true") + |> json_response_and_validate_schema(:ok) + + assert conn + |> get("/api/v1/timelines/public?only_remote=true&only_media=true") + |> json_response_and_validate_schema(:ok) == [] # does not contain repeats {:ok, _} = CommonAPI.repeat(activity.id, user) - conn = get(build_conn(), "/api/v1/timelines/public?local=true") - - assert [_] = json_response_and_validate_schema(conn, :ok) + assert [_, _] = + conn + |> get("/api/v1/timelines/public?local=true") + |> json_response_and_validate_schema(:ok) end test "the public timeline includes only public statuses for an authenticated user" do @@ -621,7 +657,7 @@ test "muted emotions", %{user: user, conn: conn} do ] = result end - test "filering with params", %{user: user, conn: conn} do + test "filering", %{user: user, conn: conn} do {:ok, list} = Pleroma.List.create("name", user) local_user = insert(:user) @@ -633,43 +669,55 @@ test "filering with params", %{user: user, conn: conn} do remote_activity = create_remote_activity(remote_user) {:ok, list} = Pleroma.List.follow(list, remote_user) - resp1 = - conn |> get("/api/v1/timelines/list/#{list.id}") |> json_response_and_validate_schema(200) - - all_ids = Enum.map(resp1, & &1["id"]) + all_ids = + conn + |> get("/api/v1/timelines/list/#{list.id}") + |> json_response_and_validate_schema(200) + |> Enum.map(& &1["id"]) assert local_activity.id in all_ids assert with_media.id in all_ids assert remote_activity.id in all_ids - resp2 = + only_local_ids = conn |> get("/api/v1/timelines/list/#{list.id}?local=true") |> json_response_and_validate_schema(200) - - only_local_ids = Enum.map(resp2, & &1["id"]) + |> Enum.map(& &1["id"]) assert local_activity.id in only_local_ids assert with_media.id in only_local_ids refute remote_activity.id in only_local_ids - resp3 = + only_local_media_ids = + conn + |> get("/api/v1/timelines/list/#{list.id}?local=true&only_media=true") + |> json_response_and_validate_schema(200) + |> Enum.map(& &1["id"]) + + refute local_activity.id in only_local_media_ids + assert with_media.id in only_local_media_ids + refute remote_activity.id in only_local_media_ids + + only_remote_ids = conn |> get("/api/v1/timelines/list/#{list.id}?only_remote=true") |> json_response_and_validate_schema(200) - - only_remote_ids = Enum.map(resp3, & &1["id"]) + |> Enum.map(& &1["id"]) refute local_activity.id in only_remote_ids refute with_media.id in only_remote_ids assert remote_activity.id in only_remote_ids - resp4 = + assert conn + |> get("/api/v1/timelines/list/#{list.id}?only_remote=true&only_media=true") + |> json_response_and_validate_schema(200) == [] + + only_media_ids = conn |> get("/api/v1/timelines/list/#{list.id}?only_media=true") |> json_response_and_validate_schema(200) - - only_media_ids = Enum.map(resp4, & &1["id"]) + |> Enum.map(& &1["id"]) refute local_activity.id in only_media_ids assert with_media.id in only_media_ids @@ -691,19 +739,85 @@ test "hashtag timeline", %{conn: conn} do following = insert(:user) {:ok, activity} = CommonAPI.post(following, %{status: "test #2hu"}) + with_media = create_with_media_activity(following) - nconn = get(conn, "/api/v1/timelines/tag/2hu") + remote = insert(:user, local: false) + remote_activity = create_remote_activity(remote) - assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok) + all_ids = + conn + |> get("/api/v1/timelines/tag/2hu") + |> json_response_and_validate_schema(:ok) + |> Enum.map(& &1["id"]) - assert id == to_string(activity.id) + assert activity.id in all_ids + assert with_media.id in all_ids + assert remote_activity.id in all_ids # works for different capitalization too - nconn = get(conn, "/api/v1/timelines/tag/2HU") + all_ids = + conn + |> get("/api/v1/timelines/tag/2HU") + |> json_response_and_validate_schema(:ok) + |> Enum.map(& &1["id"]) - assert [%{"id" => id}] = json_response_and_validate_schema(nconn, :ok) + assert activity.id in all_ids + assert with_media.id in all_ids + assert remote_activity.id in all_ids - assert id == to_string(activity.id) + local_ids = + conn + |> get("/api/v1/timelines/tag/2hu?local=true") + |> json_response_and_validate_schema(:ok) + |> Enum.map(& &1["id"]) + + assert activity.id in local_ids + assert with_media.id in local_ids + refute remote_activity.id in local_ids + + remote_ids = + conn + |> get("/api/v1/timelines/tag/2hu?only_remote=true") + |> json_response_and_validate_schema(:ok) + |> Enum.map(& &1["id"]) + + refute activity.id in remote_ids + refute with_media.id in remote_ids + assert remote_activity.id in remote_ids + + media_ids = + conn + |> get("/api/v1/timelines/tag/2hu?only_media=true") + |> json_response_and_validate_schema(:ok) + |> Enum.map(& &1["id"]) + + refute activity.id in media_ids + assert with_media.id in media_ids + refute remote_activity.id in media_ids + + media_local_ids = + conn + |> get("/api/v1/timelines/tag/2hu?only_media=true&local=true") + |> json_response_and_validate_schema(:ok) + |> Enum.map(& &1["id"]) + + refute activity.id in media_local_ids + assert with_media.id in media_local_ids + refute remote_activity.id in media_local_ids + + ids = + conn + |> get("/api/v1/timelines/tag/2hu?only_media=true&local=true&only_remote=true") + |> json_response_and_validate_schema(:ok) + |> Enum.map(& &1["id"]) + + refute activity.id in ids + refute with_media.id in ids + refute remote_activity.id in ids + + assert conn + |> get("/api/v1/timelines/tag/2hu?only_media=true&only_remote=true") + |> json_response_and_validate_schema(:ok) == [] end test "multi-hashtag timeline", %{conn: conn} do From ba512cbea42fc0a628d74d5680f0b34c3b1f1b5f Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Tue, 26 Jan 2021 17:55:43 +0300 Subject: [PATCH 6/7] `/api/v1/accounts/:id/statuses` docs update --- .../API/differences_in_mastoapi_responses.md | 12 +++++++++++- .../web/api_spec/operations/account_operation.ex | 4 ++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/docs/development/API/differences_in_mastoapi_responses.md b/docs/development/API/differences_in_mastoapi_responses.md index e9ab896b7..7a4979154 100644 --- a/docs/development/API/differences_in_mastoapi_responses.md +++ b/docs/development/API/differences_in_mastoapi_responses.md @@ -16,7 +16,7 @@ Adding the parameter `reply_visibility` to the public and home timelines queries Adding the parameter `instance=lain.com` to the public timeline will show only statuses originating from `lain.com` (or any remote instance). -Home, public, hashtag & list timelines can filter statuses by accepting these parameters: +Home, public, hashtag & list timelines accept these parameters: - `only_media`: show only statuses with media attached - `local`: show only local statuses @@ -60,6 +60,16 @@ The `id` parameter can also be the `nickname` of the user. This only works in th - `/api/v1/accounts/:id` - `/api/v1/accounts/:id/statuses` +`/api/v1/accounts/:id/statuses` endpoint accepts these parameters: + +- `pinned`: include only pinned statuses +- `tagged`: with tag +- `only_media`: include only statuses with media attached +- `with_muted`: include statuses/reactions from muted accounts +- `exclude_reblogs`: exclude reblogs +- `exclude_replies`: exclude replies +- `exclude_visibilities`: exclude visibilities + Has these additional fields under the `pleroma` object: - `ap_id`: nullable URL string, ActivityPub id of the user diff --git a/lib/pleroma/web/api_spec/operations/account_operation.ex b/lib/pleroma/web/api_spec/operations/account_operation.ex index 80acee2f7..a301ce090 100644 --- a/lib/pleroma/web/api_spec/operations/account_operation.ex +++ b/lib/pleroma/web/api_spec/operations/account_operation.ex @@ -130,7 +130,7 @@ def statuses_operation do :with_muted, :query, BooleanLike, - "Include statuses from muted acccounts." + "Include statuses from muted accounts." ), Operation.parameter(:exclude_reblogs, :query, BooleanLike, "Exclude reblogs"), Operation.parameter(:exclude_replies, :query, BooleanLike, "Exclude replies"), @@ -144,7 +144,7 @@ def statuses_operation do :with_muted, :query, BooleanLike, - "Include reactions from muted acccounts." + "Include reactions from muted accounts." ) ] ++ pagination_params(), responses: %{ From fdf1dfed560e27cd4e367cc2952fcb1b4e2580db Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Mon, 1 Feb 2021 14:09:23 +0300 Subject: [PATCH 7/7] only_remote -> remote renaming --- CHANGELOG.md | 2 +- .../API/differences_in_mastoapi_responses.md | 2 +- lib/pleroma/web/activity_pub/activity_pub.ex | 2 +- .../api_spec/operations/timeline_operation.ex | 12 +++--- .../controllers/timeline_controller_test.exs | 42 +++++++++---------- 5 files changed, 29 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70d2ac0a8..628f6f17d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,7 +49,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Admin API: An endpoint to manage frontends. - Streaming API: Add follow relationships updates. - WebPush: Introduce `pleroma:chat_mention` and `pleroma:emoji_reaction` notification types. -- Mastodon API: Home, public, hashtag & list timelines accept `only_media`, `only_remote` & `local` parameters for filtration. +- Mastodon API: Home, public, hashtag & list timelines accept `only_media`, `remote` & `local` parameters for filtration. ### Fixed diff --git a/docs/development/API/differences_in_mastoapi_responses.md b/docs/development/API/differences_in_mastoapi_responses.md index 7a4979154..c83be2faa 100644 --- a/docs/development/API/differences_in_mastoapi_responses.md +++ b/docs/development/API/differences_in_mastoapi_responses.md @@ -20,7 +20,7 @@ Home, public, hashtag & list timelines accept these parameters: - `only_media`: show only statuses with media attached - `local`: show only local statuses -- `only_remote`: show only remote statuses +- `remote`: show only remote statuses ## Statuses diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 58e868119..98051032a 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -735,7 +735,7 @@ defp restrict_local(query, %{local_only: true}) do defp restrict_local(query, _), do: query - defp restrict_remote(query, %{only_remote: true}) do + defp restrict_remote(query, %{remote: true}) do from(activity in query, where: activity.local == false) end diff --git a/lib/pleroma/web/api_spec/operations/timeline_operation.ex b/lib/pleroma/web/api_spec/operations/timeline_operation.ex index 52008e27c..01396642c 100644 --- a/lib/pleroma/web/api_spec/operations/timeline_operation.ex +++ b/lib/pleroma/web/api_spec/operations/timeline_operation.ex @@ -25,7 +25,7 @@ def home_operation do security: [%{"oAuth" => ["read:statuses"]}], parameters: [ local_param(), - only_remote_param(), + remote_param(), only_media_param(), with_muted_param(), exclude_visibilities_param(), @@ -63,7 +63,7 @@ def public_operation do local_param(), instance_param(), only_media_param(), - only_remote_param(), + remote_param(), with_muted_param(), exclude_visibilities_param(), reply_visibility_param() | pagination_params() @@ -110,7 +110,7 @@ def hashtag_operation do ), local_param(), only_media_param(), - only_remote_param(), + remote_param(), with_muted_param(), exclude_visibilities_param() | pagination_params() ], @@ -137,7 +137,7 @@ def list_operation do ), with_muted_param(), local_param(), - only_remote_param(), + remote_param(), only_media_param(), exclude_visibilities_param() | pagination_params() ], @@ -206,9 +206,9 @@ defp only_media_param do ) end - defp only_remote_param do + defp remote_param do Operation.parameter( - :only_remote, + :remote, :query, %Schema{allOf: [BooleanLike], default: false}, "Show only remote statuses?" diff --git a/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs index 066762748..cc409451c 100644 --- a/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/timeline_controller_test.exs @@ -131,22 +131,22 @@ test "filtering", %{conn: conn, user: user} do refute remote_activity.id in only_local_media_ids assert with_media.id in only_local_media_ids - only_remote_ids = + remote_ids = conn - |> get("/api/v1/timelines/home?only_remote=true") + |> get("/api/v1/timelines/home?remote=true") |> json_response_and_validate_schema(200) |> Enum.map(& &1["id"]) - refute local_activity.id in only_remote_ids - assert remote_activity.id in only_remote_ids - refute with_media.id in only_remote_ids + refute local_activity.id in remote_ids + assert remote_activity.id in remote_ids + refute with_media.id in remote_ids assert conn - |> get("/api/v1/timelines/home?only_remote=true&only_media=true") + |> get("/api/v1/timelines/home?remote=true&only_media=true") |> json_response_and_validate_schema(200) == [] assert conn - |> get("/api/v1/timelines/home?only_remote=true&local=true") + |> get("/api/v1/timelines/home?remote=true&local=true") |> json_response_and_validate_schema(200) == [] end end @@ -210,7 +210,7 @@ test "the public timeline", %{conn: conn} do assert [%{"id" => ^remote_id}] = conn - |> get("/api/v1/timelines/public?only_remote=true") + |> get("/api/v1/timelines/public?remote=true") |> json_response_and_validate_schema(:ok) with_media_id = with_media.id @@ -221,7 +221,7 @@ test "the public timeline", %{conn: conn} do |> json_response_and_validate_schema(:ok) assert conn - |> get("/api/v1/timelines/public?only_remote=true&only_media=true") + |> get("/api/v1/timelines/public?remote=true&only_media=true") |> json_response_and_validate_schema(:ok) == [] # does not contain repeats @@ -657,7 +657,7 @@ test "muted emotions", %{user: user, conn: conn} do ] = result end - test "filering", %{user: user, conn: conn} do + test "filtering", %{user: user, conn: conn} do {:ok, list} = Pleroma.List.create("name", user) local_user = insert(:user) @@ -699,18 +699,18 @@ test "filering", %{user: user, conn: conn} do assert with_media.id in only_local_media_ids refute remote_activity.id in only_local_media_ids - only_remote_ids = + remote_ids = conn - |> get("/api/v1/timelines/list/#{list.id}?only_remote=true") + |> get("/api/v1/timelines/list/#{list.id}?remote=true") |> json_response_and_validate_schema(200) |> Enum.map(& &1["id"]) - refute local_activity.id in only_remote_ids - refute with_media.id in only_remote_ids - assert remote_activity.id in only_remote_ids + refute local_activity.id in remote_ids + refute with_media.id in remote_ids + assert remote_activity.id in remote_ids assert conn - |> get("/api/v1/timelines/list/#{list.id}?only_remote=true&only_media=true") + |> get("/api/v1/timelines/list/#{list.id}?remote=true&only_media=true") |> json_response_and_validate_schema(200) == [] only_media_ids = @@ -724,9 +724,7 @@ test "filering", %{user: user, conn: conn} do refute remote_activity.id in only_media_ids assert conn - |> get( - "/api/v1/timelines/list/#{list.id}?only_media=true&local=true&only_remote=true" - ) + |> get("/api/v1/timelines/list/#{list.id}?only_media=true&local=true&remote=true") |> json_response_and_validate_schema(200) == [] end end @@ -777,7 +775,7 @@ test "hashtag timeline", %{conn: conn} do remote_ids = conn - |> get("/api/v1/timelines/tag/2hu?only_remote=true") + |> get("/api/v1/timelines/tag/2hu?remote=true") |> json_response_and_validate_schema(:ok) |> Enum.map(& &1["id"]) @@ -807,7 +805,7 @@ test "hashtag timeline", %{conn: conn} do ids = conn - |> get("/api/v1/timelines/tag/2hu?only_media=true&local=true&only_remote=true") + |> get("/api/v1/timelines/tag/2hu?only_media=true&local=true&remote=true") |> json_response_and_validate_schema(:ok) |> Enum.map(& &1["id"]) @@ -816,7 +814,7 @@ test "hashtag timeline", %{conn: conn} do refute remote_activity.id in ids assert conn - |> get("/api/v1/timelines/tag/2hu?only_media=true&only_remote=true") + |> get("/api/v1/timelines/tag/2hu?only_media=true&remote=true") |> json_response_and_validate_schema(:ok) == [] end