aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlain <lain@soykaf.club>2020-11-18 18:33:48 +0000
committerlain <lain@soykaf.club>2020-11-18 18:33:48 +0000
commitaae669d05ec737f7e895ed0d19f7b0eacf6cd005 (patch)
tree3d1a244162e92a99c1e28f5e2a4829de1eaadeec
parent6b32e1de234a84d0bdbbee5eeb5b2a543f24ce6b (diff)
parent1b63aa0b4f969a404cc354ae45852b551fed61b1 (diff)
downloadpleroma-aae669d05ec737f7e895ed0d19f7b0eacf6cd005.tar.gz
Merge branch 'fix/webpush-and-emojireact' into 'develop'
Push notifications: add pleroma:emoji_reaction, improve tests Closes #2185 See merge request pleroma/pleroma!3141
-rw-r--r--CHANGELOG.md5
-rw-r--r--docs/API/differences_in_mastoapi_responses.md12
-rw-r--r--lib/pleroma/web/api_spec/operations/subscription_operation.ex15
-rw-r--r--lib/pleroma/web/push/impl.ex12
-rw-r--r--lib/pleroma/web/push/subscription.ex3
-rw-r--r--test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs128
-rw-r--r--test/pleroma/web/push/impl_test.exs18
7 files changed, 158 insertions, 35 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 616f9deeb..1a9260793 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -42,6 +42,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
</details>
## Unreleased (Patch)
+
+### Changed
+
+- 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/docs/API/differences_in_mastoapi_responses.md b/docs/API/differences_in_mastoapi_responses.md
index 17999da55..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
@@ -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:
diff --git a/lib/pleroma/web/api_spec/operations/subscription_operation.ex b/lib/pleroma/web/api_spec/operations/subscription_operation.ex
index 775dd795d..67c7ea8f3 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 @@ defmodule Pleroma.Web.ApiSpec.SubscriptionOperation do
allOf: [BooleanLike],
nullable: true,
description: "Receive chat notifications?"
+ },
+ "pleroma:emoji_reaction": %Schema{
+ allOf: [BooleanLike],
+ nullable: true,
+ description: "Receive emoji reaction notifications?"
}
}
}
@@ -210,6 +215,16 @@ defmodule Pleroma.Web.ApiSpec.SubscriptionOperation 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?"
}
}
}
diff --git a/lib/pleroma/web/push/impl.ex b/lib/pleroma/web/push/impl.ex
index da535aa68..82152dffa 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}
@@ -150,6 +150,15 @@ defmodule Pleroma.Web.Push.Impl do
end
def format_body(
+ %{activity: %{data: %{"type" => "EmojiReact", "content" => content}}},
+ actor,
+ _object,
+ _mastodon_type
+ ) do
+ "@#{actor.nickname} reacted with #{content}"
+ end
+
+ def format_body(
%{activity: %{data: %{"type" => type}}} = notification,
actor,
_object,
@@ -179,6 +188,7 @@ defmodule Pleroma.Web.Push.Impl 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/lib/pleroma/web/push/subscription.ex b/lib/pleroma/web/push/subscription.ex
index 5b5aa0d59..749a573ba 100644
--- a/lib/pleroma/web/push/subscription.ex
+++ b/lib/pleroma/web/push/subscription.ex
@@ -25,7 +25,8 @@ defmodule Pleroma.Web.Push.Subscription do
timestamps()
end
- @supported_alert_types ~w[follow favourite mention reblog pleroma:chat_mention]a
+ # 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
alerts = Map.take(alerts, @supported_alert_types)
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..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,21 +45,77 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
end
end
- describe "creates push subscription" do
- test "returns error when push disabled ", %{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", %{
+ "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
- |> post("/api/v1/push/subscription", %{subscription: @sub})
+ |> 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
+ |> 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
|> post("/api/v1/push/subscription", %{
"data" => %{
- "alerts" => %{"mention" => true, "test" => true, "pleroma:chat_mention" => true}
+ "alerts" => %{
+ "mention" => true,
+ "favourite" => true,
+ "follow" => true,
+ "reblog" => true,
+ "pleroma:chat_mention" => true,
+ "pleroma:emoji_reaction" => true
+ }
},
"subscription" => @sub
})
@@ -68,7 +124,14 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
[subscription] = Pleroma.Repo.all(Subscription)
assert %{
- "alerts" => %{"mention" => true, "pleroma:chat_mention" => true},
+ "alerts" => %{
+ "mention" => true,
+ "favourite" => true,
+ "follow" => true,
+ "reblog" => true,
+ "pleroma:chat_mention" => true,
+ "pleroma:emoji_reaction" => true
+ },
"endpoint" => subscription.endpoint,
"id" => to_string(subscription.id),
"server_key" => @server_key
@@ -77,14 +140,6 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest 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
@@ -124,30 +179,47 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest 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}
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
|> 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
@@ -158,14 +230,6 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest 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
diff --git a/test/pleroma/web/push/impl_test.exs b/test/pleroma/web/push/impl_test.exs
index 7d8cc999a..2a4a8fd06 100644
--- a/test/pleroma/web/push/impl_test.exs
+++ b/test/pleroma/web/push/impl_test.exs
@@ -184,6 +184,24 @@ defmodule Pleroma.Web.Push.ImplTest 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 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")