aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorEgor Kislitsyn <egor@kislitsyn.com>2018-12-07 21:08:42 +0700
committerhref <href@random.sh>2018-12-14 13:05:46 +0100
commit324933a0ac8db810669375315d43fc9250a93a7b (patch)
tree9e68601152c5b88c0c936669544e50a3f9962e15 /lib
parent3b65f316069a7c754d29515ad33b852661fff5ef (diff)
downloadpleroma-324933a0ac8db810669375315d43fc9250a93a7b.tar.gz
improve push message format (compatibility with mastodon)
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/web/push/push.ex54
1 files changed, 25 insertions, 29 deletions
diff --git a/lib/pleroma/web/push/push.ex b/lib/pleroma/web/push/push.ex
index fb6605f30..4af3e159a 100644
--- a/lib/pleroma/web/push/push.ex
+++ b/lib/pleroma/web/push/push.ex
@@ -41,10 +41,10 @@ defmodule Pleroma.Web.Push do
)
when type in @types do
actor = User.get_cached_by_ap_id(notification.activity.data["actor"])
- body = notification |> format(actor) |> Jason.encode!()
Subscription
|> where(user_id: ^user_id)
+ |> preload(:token)
|> Repo.all()
|> Enum.each(fn record ->
subscription = %{
@@ -55,6 +55,16 @@ defmodule Pleroma.Web.Push do
endpoint: record.endpoint
}
+ body =
+ Jason.encode!(%{
+ title: format_title(notification),
+ body: format_body(notification, actor),
+ notification_id: notification.id,
+ icon: User.avatar_url(actor),
+ preferred_locale: "en",
+ access_token: record.token.token
+ })
+
case WebPushEncryption.send_web_push(body, subscription, @gcm_api_key) do
{:ok, %{status_code: code}} when 400 <= code and code < 500 ->
Logger.debug("Removing subscription record")
@@ -82,35 +92,21 @@ defmodule Pleroma.Web.Push do
{:noreply, state}
end
- def format(%{activity: %{data: %{"type" => "Create"}}}, actor) do
- %{
- title: "New Mention",
- body: "@#{actor.nickname} has mentiond you",
- icon: User.avatar_url(actor)
- }
- end
-
- def format(%{activity: %{data: %{"type" => "Follow"}}}, actor) do
- %{
- title: "New Follower",
- body: "@#{actor.nickname} has followed you",
- icon: User.avatar_url(actor)
- }
- end
-
- def format(%{activity: %{data: %{"type" => "Announce"}}}, actor) do
- %{
- title: "New Repeat",
- body: "@#{actor.nickname} has repeated your post",
- icon: User.avatar_url(actor)
- }
+ defp format_title(%{activity: %{data: %{"type" => type}}}) do
+ case type do
+ "Create" -> "New Mention"
+ "Follow" -> "New Follower"
+ "Announce" -> "New Repeat"
+ "Like" -> "New Favorite"
+ end
end
- def format(%{activity: %{data: %{"type" => "Like"}}}, actor) do
- %{
- title: "New Favorite",
- body: "@#{actor.nickname} has favorited your post",
- icon: User.avatar_url(actor)
- }
+ defp format_body(%{activity: %{data: %{"type" => type}}}, actor) do
+ case type do
+ "Create" -> "@#{actor.nickname} has mentiond you"
+ "Follow" -> "@#{actor.nickname} has followed you"
+ "Announce" -> "@#{actor.nickname} has repeated your post"
+ "Like" -> "@#{actor.nickname} has favorited your post"
+ end
end
end