improve push message format (compatibility with mastodon)

This commit is contained in:
Egor Kislitsyn 2018-12-07 21:08:42 +07:00 committed by href
parent 3b65f31606
commit 324933a0ac
No known key found for this signature in database
GPG key ID: EE8296C1A152C325

View file

@ -41,10 +41,10 @@ def handle_cast(
) )
when type in @types do when type in @types do
actor = User.get_cached_by_ap_id(notification.activity.data["actor"]) actor = User.get_cached_by_ap_id(notification.activity.data["actor"])
body = notification |> format(actor) |> Jason.encode!()
Subscription Subscription
|> where(user_id: ^user_id) |> where(user_id: ^user_id)
|> preload(:token)
|> Repo.all() |> Repo.all()
|> Enum.each(fn record -> |> Enum.each(fn record ->
subscription = %{ subscription = %{
@ -55,6 +55,16 @@ def handle_cast(
endpoint: record.endpoint 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 case WebPushEncryption.send_web_push(body, subscription, @gcm_api_key) do
{:ok, %{status_code: code}} when 400 <= code and code < 500 -> {:ok, %{status_code: code}} when 400 <= code and code < 500 ->
Logger.debug("Removing subscription record") Logger.debug("Removing subscription record")
@ -82,35 +92,21 @@ def handle_cast({:send, _}, state) do
{:noreply, state} {:noreply, state}
end end
def format(%{activity: %{data: %{"type" => "Create"}}}, actor) do defp format_title(%{activity: %{data: %{"type" => type}}}) do
%{ case type do
title: "New Mention", "Create" -> "New Mention"
body: "@#{actor.nickname} has mentiond you", "Follow" -> "New Follower"
icon: User.avatar_url(actor) "Announce" -> "New Repeat"
} "Like" -> "New Favorite"
end
end end
def format(%{activity: %{data: %{"type" => "Follow"}}}, actor) do defp format_body(%{activity: %{data: %{"type" => type}}}, actor) do
%{ case type do
title: "New Follower", "Create" -> "@#{actor.nickname} has mentiond you"
body: "@#{actor.nickname} has followed you", "Follow" -> "@#{actor.nickname} has followed you"
icon: User.avatar_url(actor) "Announce" -> "@#{actor.nickname} has repeated your post"
} "Like" -> "@#{actor.nickname} has favorited your post"
end end
def format(%{activity: %{data: %{"type" => "Announce"}}}, actor) do
%{
title: "New Repeat",
body: "@#{actor.nickname} has repeated your post",
icon: User.avatar_url(actor)
}
end
def format(%{activity: %{data: %{"type" => "Like"}}}, actor) do
%{
title: "New Favorite",
body: "@#{actor.nickname} has favorited your post",
icon: User.avatar_url(actor)
}
end end
end end