From 3ce11b830ee69d8557146fce6d3507337298259d Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 13 Nov 2020 17:01:53 -0600 Subject: [PATCH 01/14] Add capability for emoji reaction push notifications --- lib/pleroma/web/push/impl.ex | 12 +++++++++++- test/pleroma/web/push/impl_test.exs | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/push/impl.ex b/lib/pleroma/web/push/impl.ex index da535aa68..f91cb1d40 100644 --- a/lib/pleroma/web/push/impl.ex +++ b/lib/pleroma/web/push/impl.ex @@ -16,7 +16,7 @@ defmodule Pleroma.Web.Push.Impl do require Logger import Ecto.Query - @types ["Create", "Follow", "Announce", "Like", "Move"] + @types ["Create", "Follow", "Announce", "Like", "Move", "EmojiReact"] @doc "Performs sending notifications for user subscriptions" @spec perform(Notification.t()) :: list(any) | :error | {:error, :unknown_type} @@ -149,6 +149,15 @@ def format_body( "@#{actor.nickname} repeated: #{Utils.scrub_html_and_truncate(content, 80)}" end + def format_body( + %{activity: %{data: %{"type" => "EmojiReact", "content" => content}}}, + actor, + _object, + _mastodon_type + ) do + "@#{actor.nickname} has reacted with #{content}" + end + def format_body( %{activity: %{data: %{"type" => type}}} = notification, actor, @@ -179,6 +188,7 @@ def format_title(%{type: type}, mastodon_type) do "reblog" -> "New Repeat" "favourite" -> "New Favorite" "pleroma:chat_mention" -> "New Chat Message" + "pleroma:emoji_reaction" -> "New Reaction" type -> "New #{String.capitalize(type || "event")}" end end diff --git a/test/pleroma/web/push/impl_test.exs b/test/pleroma/web/push/impl_test.exs index 7d8cc999a..2575c76d6 100644 --- a/test/pleroma/web/push/impl_test.exs +++ b/test/pleroma/web/push/impl_test.exs @@ -184,6 +184,24 @@ test "renders title and body for like activity" do "New Favorite" end + test "renders title and body for pleroma:emoji_reaction activity" do + user = insert(:user, nickname: "Bob") + + {:ok, activity} = + CommonAPI.post(user, %{ + status: "This post is a really good post!" + }) + + {:ok, activity} = CommonAPI.react_with_emoji(activity.id, user, "👍") + object = Object.normalize(activity) + + assert Impl.format_body(%{activity: activity, type: "pleroma:emoji_reaction"}, user, object) == + "@Bob has reacted with 👍" + + assert Impl.format_title(%{activity: activity, type: "pleroma:emoji_reaction"}) == + "New Reaction" + end + test "renders title for create activity with direct visibility" do user = insert(:user, nickname: "Bob") From 83ec2f1384611c385e79ddfd23566ddc173a8a4a Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Tue, 17 Nov 2020 18:33:40 +0000 Subject: [PATCH 02/14] Allow subscribing for pleroma:emoji_reaction push notifications --- lib/pleroma/web/push/subscription.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/web/push/subscription.ex b/lib/pleroma/web/push/subscription.ex index 5b5aa0d59..95425b986 100644 --- a/lib/pleroma/web/push/subscription.ex +++ b/lib/pleroma/web/push/subscription.ex @@ -25,7 +25,7 @@ defmodule Pleroma.Web.Push.Subscription do timestamps() end - @supported_alert_types ~w[follow favourite mention reblog pleroma:chat_mention]a + @supported_alert_types ~w[follow favourite mention reblog pleroma:chat_mention pleroma:emoji_reaction]a defp alerts(%{data: %{alerts: alerts}}) do alerts = Map.take(alerts, @supported_alert_types) From 1433d3c59c2053244b9f570516a80e949f5fb10d Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Tue, 17 Nov 2020 19:05:36 +0000 Subject: [PATCH 03/14] Document the API extensions for push subscriptions --- docs/API/differences_in_mastoapi_responses.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/API/differences_in_mastoapi_responses.md b/docs/API/differences_in_mastoapi_responses.md index 17999da55..4f0fe86e5 100644 --- a/docs/API/differences_in_mastoapi_responses.md +++ b/docs/API/differences_in_mastoapi_responses.md @@ -261,6 +261,16 @@ Has theses additional parameters (which are the same as in Pleroma-API): - `pleroma.metadata.post_formats`: A list of the allowed post format types - `vapid_public_key`: The public key needed for push messages +## Push Subscription + +`POST /api/v1/push/subscription` +`PUT /api/v1/push/subscription` + +Permits these additional alert types: + +- pleroma:chat_mention +- pleroma:emoji_reaction + ## Markers Has these additional fields under the `pleroma` object: From 80e21903d4abd3cd87694c3faaeff3a6afd2c8dc Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Tue, 17 Nov 2020 19:06:30 +0000 Subject: [PATCH 04/14] Spelling --- docs/API/differences_in_mastoapi_responses.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/API/differences_in_mastoapi_responses.md b/docs/API/differences_in_mastoapi_responses.md index 4f0fe86e5..843496482 100644 --- a/docs/API/differences_in_mastoapi_responses.md +++ b/docs/API/differences_in_mastoapi_responses.md @@ -233,7 +233,7 @@ Post here request with `grant_type=refresh_token` to obtain new access token. Re `POST /api/v1/accounts` -Has theses additional parameters (which are the same as in Pleroma-API): +Has these additional parameters (which are the same as in Pleroma-API): - `fullname`: optional - `bio`: optional From 67a6abd071fd4e9f62c032fe952b65b957140bc5 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Tue, 17 Nov 2020 19:15:11 +0000 Subject: [PATCH 05/14] Update OpenAPI spec/schema and test to verify support for pleroma:emoji_reaction subscriptions --- .../api_spec/operations/subscription_operation.ex | 5 +++++ .../controllers/subscription_controller_test.exs | 13 +++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/api_spec/operations/subscription_operation.ex b/lib/pleroma/web/api_spec/operations/subscription_operation.ex index 775dd795d..77e70005b 100644 --- a/lib/pleroma/web/api_spec/operations/subscription_operation.ex +++ b/lib/pleroma/web/api_spec/operations/subscription_operation.ex @@ -146,6 +146,11 @@ defp create_request do allOf: [BooleanLike], nullable: true, description: "Receive chat notifications?" + }, + "pleroma:emoji_reaction": %Schema{ + allOf: [BooleanLike], + nullable: true, + description: "Receive emoji reaction notifications?" } } } diff --git a/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs index d36bb1ae8..6e9369b3f 100644 --- a/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs @@ -59,7 +59,12 @@ test "successful creation", %{conn: conn} do conn |> post("/api/v1/push/subscription", %{ "data" => %{ - "alerts" => %{"mention" => true, "test" => true, "pleroma:chat_mention" => true} + "alerts" => %{ + "mention" => true, + "test" => true, + "pleroma:chat_mention" => true, + "pleroma:emoji_reaction" => true + } }, "subscription" => @sub }) @@ -68,7 +73,11 @@ test "successful creation", %{conn: conn} do [subscription] = Pleroma.Repo.all(Subscription) assert %{ - "alerts" => %{"mention" => true, "pleroma:chat_mention" => true}, + "alerts" => %{ + "mention" => true, + "pleroma:chat_mention" => true, + "pleroma:emoji_reaction" => true + }, "endpoint" => subscription.endpoint, "id" => to_string(subscription.id), "server_key" => @server_key From 5d0bc5e028c94ab1c7ebf746b5d01af1fbef396f Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Tue, 17 Nov 2020 20:21:48 +0000 Subject: [PATCH 06/14] Support both pleroma:chat_mention and pleroma:emoji_reaction for /api/v1/push/subscription --- CHANGELOG.md | 6 ++++++ .../web/api_spec/operations/subscription_operation.ex | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe1114c02..c6cb30176 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## Unreleased (Patch) + +### Changed + +- API: Empty parameter values for integer parameters are now ignored in non-strict validaton mode. +- Fix ability to update Pleroma Chat push notifications with PUT /api/v1/push/subscription and alert type pleroma:chat_mention + ### Fixed - Config generation: rename `Pleroma.Upload.Filter.ExifTool` to `Pleroma.Upload.Filter.Exiftool`. diff --git a/lib/pleroma/web/api_spec/operations/subscription_operation.ex b/lib/pleroma/web/api_spec/operations/subscription_operation.ex index 77e70005b..67c7ea8f3 100644 --- a/lib/pleroma/web/api_spec/operations/subscription_operation.ex +++ b/lib/pleroma/web/api_spec/operations/subscription_operation.ex @@ -215,6 +215,16 @@ defp update_request do allOf: [BooleanLike], nullable: true, description: "Receive poll notifications?" + }, + "pleroma:chat_mention": %Schema{ + allOf: [BooleanLike], + nullable: true, + description: "Receive chat notifications?" + }, + "pleroma:emoji_reaction": %Schema{ + allOf: [BooleanLike], + nullable: true, + description: "Receive emoji reaction notifications?" } } } From 499faa82f6e9bc400b059a7fd3e5a910eebe1a58 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Tue, 17 Nov 2020 19:51:37 +0000 Subject: [PATCH 07/14] Synchronize reaction notification text with PleromaFE's style --- lib/pleroma/web/push/impl.ex | 2 +- test/pleroma/web/push/impl_test.exs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/push/impl.ex b/lib/pleroma/web/push/impl.ex index f91cb1d40..82152dffa 100644 --- a/lib/pleroma/web/push/impl.ex +++ b/lib/pleroma/web/push/impl.ex @@ -155,7 +155,7 @@ def format_body( _object, _mastodon_type ) do - "@#{actor.nickname} has reacted with #{content}" + "@#{actor.nickname} reacted with #{content}" end def format_body( diff --git a/test/pleroma/web/push/impl_test.exs b/test/pleroma/web/push/impl_test.exs index 2575c76d6..2a4a8fd06 100644 --- a/test/pleroma/web/push/impl_test.exs +++ b/test/pleroma/web/push/impl_test.exs @@ -196,7 +196,7 @@ test "renders title and body for pleroma:emoji_reaction activity" do object = Object.normalize(activity) assert Impl.format_body(%{activity: activity, type: "pleroma:emoji_reaction"}, user, object) == - "@Bob has reacted with 👍" + "@Bob reacted with 👍" assert Impl.format_title(%{activity: activity, type: "pleroma:emoji_reaction"}) == "New Reaction" From 30f140e5702d59ecf8123ddb0959a7a3aefcd3f8 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Tue, 17 Nov 2020 20:14:38 +0000 Subject: [PATCH 08/14] Ensure all supported push notification subscription alert types are tested --- .../subscription_controller_test.exs | 52 ++++++++++++++++--- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs index 6e9369b3f..379260965 100644 --- a/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs @@ -61,9 +61,12 @@ test "successful creation", %{conn: conn} do "data" => %{ "alerts" => %{ "mention" => true, - "test" => true, + "favourite" => true, + "follow" => true, + "reblog" => true, "pleroma:chat_mention" => true, - "pleroma:emoji_reaction" => true + "pleroma:emoji_reaction" => true, + "test" => true } }, "subscription" => @sub @@ -75,6 +78,9 @@ test "successful creation", %{conn: conn} do assert %{ "alerts" => %{ "mention" => true, + "favourite" => true, + "follow" => true, + "reblog" => true, "pleroma:chat_mention" => true, "pleroma:emoji_reaction" => true }, @@ -133,7 +139,16 @@ test "returns a user subsciption", %{conn: conn, user: user, token: token} do insert(:push_subscription, user: user, token: token, - data: %{"alerts" => %{"mention" => true}} + data: %{ + "alerts" => %{ + "mention" => true, + "favourite" => true, + "follow" => true, + "reblog" => true, + "pleroma:chat_mention" => true, + "pleroma:emoji_reaction" => true + } + } ) %{conn: conn, user: user, token: token, subscription: subscription} @@ -142,7 +157,16 @@ test "returns a user subsciption", %{conn: conn, user: user, token: token} do test "returns error when push disabled ", %{conn: conn} do assert_error_when_disable_push do conn - |> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}}) + |> put("/api/v1/push/subscription", %{ + data: %{ + "mention" => false, + "favourite" => false, + "follow" => false, + "reblog" => false, + "pleroma:chat_mention" => false, + "pleroma:emoji_reaction" => false + } + }) |> json_response_and_validate_schema(403) end end @@ -151,12 +175,28 @@ test "returns updated subsciption", %{conn: conn, subscription: subscription} do res = conn |> put("/api/v1/push/subscription", %{ - data: %{"alerts" => %{"mention" => false, "follow" => true}} + data: %{ + "alerts" => %{ + "mention" => false, + "favourite" => false, + "follow" => false, + "reblog" => false, + "pleroma:chat_mention" => false, + "pleroma:emoji_reaction" => false + } + } }) |> json_response_and_validate_schema(200) expect = %{ - "alerts" => %{"follow" => true, "mention" => false}, + "alerts" => %{ + "mention" => false, + "favourite" => false, + "follow" => false, + "reblog" => false, + "pleroma:chat_mention" => false, + "pleroma:emoji_reaction" => false + }, "endpoint" => "https://example.com/example/1234", "id" => to_string(subscription.id), "server_key" => @server_key From ff7a4b6aa2b7a9018074652261d9e0d5dcf6602b Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Tue, 17 Nov 2020 20:18:51 +0000 Subject: [PATCH 09/14] Test that we ignore invalid subscription alert types separately. --- .../subscription_controller_test.exs | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs index 379260965..9e021a2b6 100644 --- a/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs @@ -54,6 +54,26 @@ test "returns error when push disabled ", %{conn: conn} do end end + test "ignores unsupported types", %{conn: conn} do + result = + conn + |> post("/api/v1/push/subscription", %{ + "data" => %{ + "alerts" => %{ + "fake_unsupported_type" => true + } + }, + "subscription" => @sub + }) + |> json_response_and_validate_schema(200) + + refute %{ + "alerts" => %{ + "fake_unsupported_type" => true + } + } == result + end + test "successful creation", %{conn: conn} do result = conn @@ -65,8 +85,7 @@ test "successful creation", %{conn: conn} do "follow" => true, "reblog" => true, "pleroma:chat_mention" => true, - "pleroma:emoji_reaction" => true, - "test" => true + "pleroma:emoji_reaction" => true } }, "subscription" => @sub From ccddedb504e5f03655c128edb074671b1f815f24 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Tue, 17 Nov 2020 20:33:30 +0000 Subject: [PATCH 10/14] Credo --- lib/pleroma/web/push/subscription.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/pleroma/web/push/subscription.ex b/lib/pleroma/web/push/subscription.ex index 95425b986..749a573ba 100644 --- a/lib/pleroma/web/push/subscription.ex +++ b/lib/pleroma/web/push/subscription.ex @@ -25,6 +25,7 @@ defmodule Pleroma.Web.Push.Subscription do timestamps() end + # credo:disable-for-next-line Credo.Check.Readability.MaxLineLength @supported_alert_types ~w[follow favourite mention reblog pleroma:chat_mention pleroma:emoji_reaction]a defp alerts(%{data: %{alerts: alerts}}) do From d9732fb7d318a7a56f2b82478b13fd5d694eb630 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Tue, 17 Nov 2020 21:34:18 +0000 Subject: [PATCH 11/14] Fix incorrect test description --- .../mastodon_api/controllers/subscription_controller_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs index 9e021a2b6..955cd9912 100644 --- a/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs @@ -46,7 +46,7 @@ defmacro assert_error_when_disable_push(do: yield) do end describe "creates push subscription" do - test "returns error when push disabled ", %{conn: conn} do + test "does not return unsupported types", %{conn: conn} do assert_error_when_disable_push do conn |> post("/api/v1/push/subscription", %{subscription: @sub}) From 3eaa5335c97e0b2697a692614d73897092df0d58 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Tue, 17 Nov 2020 21:37:17 +0000 Subject: [PATCH 12/14] Revert adding extra alert types here --- .../controllers/subscription_controller_test.exs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs index 955cd9912..dd2f9a86e 100644 --- a/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs @@ -176,16 +176,7 @@ test "returns a user subsciption", %{conn: conn, user: user, token: token} do test "returns error when push disabled ", %{conn: conn} do assert_error_when_disable_push do conn - |> put("/api/v1/push/subscription", %{ - data: %{ - "mention" => false, - "favourite" => false, - "follow" => false, - "reblog" => false, - "pleroma:chat_mention" => false, - "pleroma:emoji_reaction" => false - } - }) + |> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}}) |> json_response_and_validate_schema(403) end end From 415481a4d9c9d6527513b0460aad5863cadedb97 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Tue, 17 Nov 2020 22:18:22 +0000 Subject: [PATCH 13/14] Add test for POST when push is disabled Also group together the tests verifiying failure when disabled --- .../subscription_controller_test.exs | 59 ++++++++++--------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs index dd2f9a86e..5ef39bdb2 100644 --- a/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs @@ -45,15 +45,44 @@ defmacro assert_error_when_disable_push(do: yield) do end end - describe "creates push subscription" do - test "does not return unsupported types", %{conn: conn} do + describe "when disabled" do + test "POST returns error", %{conn: conn} do assert_error_when_disable_push do conn - |> post("/api/v1/push/subscription", %{subscription: @sub}) + |> post("/api/v1/push/subscription", %{ + "data" => %{"alerts" => %{"mention" => true}}, + "subscription" => @sub + }) |> json_response_and_validate_schema(403) end end + test "GET returns error", %{conn: conn} do + assert_error_when_disable_push do + conn + |> get("/api/v1/push/subscription", %{}) + |> json_response_and_validate_schema(403) + end + end + + test "PUT returns error", %{conn: conn} do + assert_error_when_disable_push do + conn + |> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}}) + |> json_response_and_validate_schema(403) + end + end + + test "DELETE returns error", %{conn: conn} do + assert_error_when_disable_push do + conn + |> delete("/api/v1/push/subscription", %{}) + |> json_response_and_validate_schema(403) + end + end + end + + describe "creates push subscription" do test "ignores unsupported types", %{conn: conn} do result = conn @@ -111,14 +140,6 @@ test "successful creation", %{conn: conn} do end describe "gets a user subscription" do - test "returns error when push disabled ", %{conn: conn} do - assert_error_when_disable_push do - conn - |> get("/api/v1/push/subscription", %{}) - |> json_response_and_validate_schema(403) - end - end - test "returns error when user hasn't subscription", %{conn: conn} do res = conn @@ -173,14 +194,6 @@ test "returns a user subsciption", %{conn: conn, user: user, token: token} do %{conn: conn, user: user, token: token, subscription: subscription} end - test "returns error when push disabled ", %{conn: conn} do - assert_error_when_disable_push do - conn - |> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}}) - |> json_response_and_validate_schema(403) - end - end - test "returns updated subsciption", %{conn: conn, subscription: subscription} do res = conn @@ -217,14 +230,6 @@ test "returns updated subsciption", %{conn: conn, subscription: subscription} do end describe "deletes the user subscription" do - test "returns error when push disabled ", %{conn: conn} do - assert_error_when_disable_push do - conn - |> delete("/api/v1/push/subscription", %{}) - |> json_response_and_validate_schema(403) - end - end - test "returns error when user hasn't subscription", %{conn: conn} do res = conn From 1b63aa0b4f969a404cc354ae45852b551fed61b1 Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 18 Nov 2020 18:27:30 +0000 Subject: [PATCH 14/14] Apply 1 suggestion(s) to 1 file(s) --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6cb30176..d0c9ac616 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Changed -- API: Empty parameter values for integer parameters are now ignored in non-strict validaton mode. - Fix ability to update Pleroma Chat push notifications with PUT /api/v1/push/subscription and alert type pleroma:chat_mention ### Fixed