From 64a4c147b1836be8af0c87b23073ed82bb9cf67c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Mon, 6 Dec 2021 18:00:58 +0100 Subject: [PATCH 1/3] MastoAPI: accept notify param in follow request MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../web/api_spec/operations/account_operation.ex | 6 ++++++ lib/pleroma/web/mastodon_api/mastodon_api.ex | 12 +++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/api_spec/operations/account_operation.ex b/lib/pleroma/web/api_spec/operations/account_operation.ex index 54e5ebc76..cb978c775 100644 --- a/lib/pleroma/web/api_spec/operations/account_operation.ex +++ b/lib/pleroma/web/api_spec/operations/account_operation.ex @@ -226,6 +226,12 @@ def follow_operation do type: :boolean, description: "Receive this account's reblogs in home timeline? Defaults to true.", default: true + }, + notify: %Schema{ + type: :boolean, + description: + "Receive notifications for all statuses posted by the account? Defaults to false.", + default: false } } }, diff --git a/lib/pleroma/web/mastodon_api/mastodon_api.ex b/lib/pleroma/web/mastodon_api/mastodon_api.ex index 71479550e..fb713d47c 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api.ex @@ -24,6 +24,7 @@ def follow(follower, followed, params \\ %{}) do with {:ok, follower, _followed, _} <- result do options = cast_params(params) set_reblogs_visibility(options[:reblogs], result) + set_subscription(options[:notify], result) {:ok, follower} end end @@ -36,6 +37,14 @@ defp set_reblogs_visibility(_, {:ok, follower, followed, _}) do CommonAPI.show_reblogs(follower, followed) end + defp set_subscription(true, {:ok, follower, followed, _}) do + User.subscribe(follower, followed) + end + + defp set_subscription(_, {:ok, follower, followed, _}) do + User.unsubscribe(follower, followed) + end + @spec get_followers(User.t(), map()) :: list(User.t()) def get_followers(user, params \\ %{}) do user @@ -73,7 +82,8 @@ defp cast_params(params) do exclude_visibilities: {:array, :string}, reblogs: :boolean, with_muted: :boolean, - account_ap_id: :string + account_ap_id: :string, + notify: :boolean } changeset = cast({%{}, param_types}, params, Map.keys(param_types)) From 3892bd353b68aff51fd596239de43fb320616eac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Mon, 6 Dec 2021 21:13:14 +0100 Subject: [PATCH 2/3] Add test for following with subscription MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../controllers/account_controller_test.exs | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs index a92a58224..70c6b152e 100644 --- a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs @@ -922,6 +922,27 @@ test "following with reblogs" do |> json_response_and_validate_schema(200) end + test "following with subscription and unsubscribing when notify is nil" do + %{conn: conn} = oauth_access(["follow"]) + followed = insert(:user) + + ret_conn = + conn + |> put_req_header("content-type", "application/json") + |> post("/api/v1/accounts/#{followed.id}/follow", %{notify: true}) + + assert %{"id" => _id, "subscribing" => true} = + json_response_and_validate_schema(ret_conn, 200) + + ret_conn = + conn + |> put_req_header("content-type", "application/json") + |> post("/api/v1/accounts/#{followed.id}/follow") + + assert %{"id" => _id, "subscribing" => false} = + json_response_and_validate_schema(ret_conn, 200) + end + test "following / unfollowing errors", %{user: user, conn: conn} do # self follow conn_res = post(conn, "/api/v1/accounts/#{user.id}/follow") From c96e52b88c47371de1cc4ed70786baf20008a811 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Mon, 6 Dec 2021 21:23:34 +0100 Subject: [PATCH 3/3] Add 'notifying' to relationship for compatibility with Mastodon MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- docs/development/API/pleroma_api.md | 2 ++ .../api_spec/operations/account_operation.ex | 3 +++ lib/pleroma/web/api_spec/schemas/account.ex | 3 ++- .../api_spec/schemas/account_relationship.ex | 6 ++++-- lib/pleroma/web/api_spec/schemas/status.ex | 3 ++- lib/pleroma/web/mastodon_api/mastodon_api.ex | 4 +++- .../web/mastodon_api/views/account_view.ex | 19 +++++++++++-------- .../controllers/account_controller_test.exs | 4 ++-- .../mastodon_api/views/account_view_test.exs | 2 ++ 9 files changed, 31 insertions(+), 15 deletions(-) diff --git a/docs/development/API/pleroma_api.md b/docs/development/API/pleroma_api.md index 8f6422da0..74a1ad206 100644 --- a/docs/development/API/pleroma_api.md +++ b/docs/development/API/pleroma_api.md @@ -159,6 +159,7 @@ See [Admin-API](admin_api.md) "muting": false, "muting_notifications": false, "subscribing": true, + "notifying": true, "requested": false, "domain_blocking": false, "showing_reblogs": true, @@ -183,6 +184,7 @@ See [Admin-API](admin_api.md) "muting": false, "muting_notifications": false, "subscribing": false, + "notifying": false, "requested": false, "domain_blocking": false, "showing_reblogs": true, diff --git a/lib/pleroma/web/api_spec/operations/account_operation.ex b/lib/pleroma/web/api_spec/operations/account_operation.ex index cb978c775..4fe5a3c03 100644 --- a/lib/pleroma/web/api_spec/operations/account_operation.ex +++ b/lib/pleroma/web/api_spec/operations/account_operation.ex @@ -694,6 +694,7 @@ defp array_of_relationships do "requested" => false, "domain_blocking" => false, "subscribing" => false, + "notifying" => false, "endorsed" => true }, %{ @@ -708,6 +709,7 @@ defp array_of_relationships do "requested" => true, "domain_blocking" => false, "subscribing" => false, + "notifying" => false, "endorsed" => false }, %{ @@ -722,6 +724,7 @@ defp array_of_relationships do "requested" => false, "domain_blocking" => true, "subscribing" => true, + "notifying" => true, "endorsed" => false } ] diff --git a/lib/pleroma/web/api_spec/schemas/account.ex b/lib/pleroma/web/api_spec/schemas/account.ex index bd7143ab9..ad1a85544 100644 --- a/lib/pleroma/web/api_spec/schemas/account.ex +++ b/lib/pleroma/web/api_spec/schemas/account.ex @@ -196,7 +196,8 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do "muting_notifications" => false, "requested" => false, "showing_reblogs" => true, - "subscribing" => false + "subscribing" => false, + "notifying" => false }, "settings_store" => %{ "pleroma-fe" => %{} diff --git a/lib/pleroma/web/api_spec/schemas/account_relationship.ex b/lib/pleroma/web/api_spec/schemas/account_relationship.ex index 16b73ebb4..b4f6d25b0 100644 --- a/lib/pleroma/web/api_spec/schemas/account_relationship.ex +++ b/lib/pleroma/web/api_spec/schemas/account_relationship.ex @@ -24,7 +24,8 @@ defmodule Pleroma.Web.ApiSpec.Schemas.AccountRelationship do muting_notifications: %Schema{type: :boolean}, requested: %Schema{type: :boolean}, showing_reblogs: %Schema{type: :boolean}, - subscribing: %Schema{type: :boolean} + subscribing: %Schema{type: :boolean}, + notifying: %Schema{type: :boolean} }, example: %{ "blocked_by" => false, @@ -38,7 +39,8 @@ defmodule Pleroma.Web.ApiSpec.Schemas.AccountRelationship do "muting_notifications" => false, "requested" => false, "showing_reblogs" => true, - "subscribing" => false + "subscribing" => false, + "notifying" => false } }) end diff --git a/lib/pleroma/web/api_spec/schemas/status.ex b/lib/pleroma/web/api_spec/schemas/status.ex index 3d042dc19..0bf3312d1 100644 --- a/lib/pleroma/web/api_spec/schemas/status.ex +++ b/lib/pleroma/web/api_spec/schemas/status.ex @@ -284,7 +284,8 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Status do "muting_notifications" => false, "requested" => false, "showing_reblogs" => true, - "subscribing" => false + "subscribing" => false, + "notifying" => false }, "skip_thread_containment" => false, "tags" => [] diff --git a/lib/pleroma/web/mastodon_api/mastodon_api.ex b/lib/pleroma/web/mastodon_api/mastodon_api.ex index fb713d47c..23846b36a 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api.ex @@ -41,10 +41,12 @@ defp set_subscription(true, {:ok, follower, followed, _}) do User.subscribe(follower, followed) end - defp set_subscription(_, {:ok, follower, followed, _}) do + defp set_subscription(false, {:ok, follower, followed, _}) do User.unsubscribe(follower, followed) end + defp set_subscription(_, _), do: {:ok, nil} + @spec get_followers(User.t(), map()) :: list(User.t()) def get_followers(user, params \\ %{}) do user diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index 9e9de33f6..25752cf56 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -101,6 +101,15 @@ def render( User.following?(target, reading_user) end + subscribing = + UserRelationship.exists?( + user_relationships, + :inverse_subscription, + target, + reading_user, + &User.subscribed_to?(&2, &1) + ) + # NOTE: adjust UserRelationship.view_relationships_option/2 on new relation-related flags %{ id: to_string(target.id), @@ -138,14 +147,8 @@ def render( target, &User.muted_notifications?(&1, &2) ), - subscribing: - UserRelationship.exists?( - user_relationships, - :inverse_subscription, - target, - reading_user, - &User.subscribed_to?(&2, &1) - ), + subscribing: subscribing, + notifying: subscribing, requested: follow_state == :follow_pending, domain_blocking: User.blocks_domain?(reading_user, target), showing_reblogs: diff --git a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs index 70c6b152e..581944b8a 100644 --- a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs @@ -922,7 +922,7 @@ test "following with reblogs" do |> json_response_and_validate_schema(200) end - test "following with subscription and unsubscribing when notify is nil" do + test "following with subscription and unsubscribing" do %{conn: conn} = oauth_access(["follow"]) followed = insert(:user) @@ -937,7 +937,7 @@ test "following with subscription and unsubscribing when notify is nil" do ret_conn = conn |> put_req_header("content-type", "application/json") - |> post("/api/v1/accounts/#{followed.id}/follow") + |> post("/api/v1/accounts/#{followed.id}/follow", %{notify: false}) assert %{"id" => _id, "subscribing" => false} = json_response_and_validate_schema(ret_conn, 200) diff --git a/test/pleroma/web/mastodon_api/views/account_view_test.exs b/test/pleroma/web/mastodon_api/views/account_view_test.exs index 60881756d..aeddd6b4c 100644 --- a/test/pleroma/web/mastodon_api/views/account_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/account_view_test.exs @@ -268,6 +268,7 @@ defp test_relationship_rendering(user, other_user, expected_result) do muting: false, muting_notifications: false, subscribing: false, + notifying: false, requested: false, domain_blocking: false, showing_reblogs: true, @@ -293,6 +294,7 @@ test "represent a relationship for the following and followed user" do muting: true, muting_notifications: true, subscribing: true, + notifying: true, showing_reblogs: false, id: to_string(other_user.id) }