1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Push.Impl do
6 @moduledoc "The module represents implementation push web notification"
10 alias Pleroma.Activity
12 alias Pleroma.Web.Push.Subscription
13 alias Pleroma.Web.Metadata.Utils
14 alias Pleroma.Notification
19 @types ["Create", "Follow", "Announce", "Like"]
21 @doc "Performs sending notifications for user subscriptions"
22 @spec perform_send(Notification.t()) :: list(any)
23 def perform_send(%{activity: %{data: %{"type" => activity_type}, id: activity_id}, user_id: user_id} = notif)
24 when activity_type in @types do
25 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
27 type = Activity.mastodon_notification_type(notif.activity)
28 gcm_api_key = Application.get_env(:web_push_encryption, :gcm_api_key)
29 avatar_url = User.avatar_url(actor)
31 for subscription <- fetch_subsriptions(user_id),
32 get_in(subscription.data, ["alerts", type]) do
34 title: format_title(notif),
35 access_token: subscription.token.token,
36 body: format_body(notif, actor),
37 notification_id: notif.id,
38 notification_type: type,
40 preferred_locale: "en",
42 activity_id: activity_id
46 |> push_message(build_sub(subscription), gcm_api_key, subscription)
50 def perform_send(_) do
51 Logger.warn("Unknown notification type")
55 @doc "Push message to web"
56 def push_message(body, sub, api_key, subscription) do
57 case WebPushEncryption.send_web_push(body, sub, api_key) do
58 {:ok, %{status_code: code}} when 400 <= code and code < 500 ->
59 Logger.debug("Removing subscription record")
60 Repo.delete!(subscription)
63 {:ok, %{status_code: code}} when 200 <= code and code < 300 ->
66 {:ok, %{status_code: code}} ->
67 Logger.error("Web Push Notification failed with code: #{code}")
71 Logger.error("Web Push Notification failed with unknown error")
76 @doc "Gets user subscriptions"
77 def fetch_subsriptions(user_id) do
79 |> where(user_id: ^user_id)
84 def build_sub(subscription) do
87 p256dh: subscription.key_p256dh,
88 auth: subscription.key_auth
90 endpoint: subscription.endpoint
95 %{activity: %{data: %{"type" => "Create", "object" => %{"content" => content}}}},
98 "@#{actor.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}"
102 %{activity: %{data: %{"type" => "Announce", "object" => activity_id}}},
105 %Activity{data: %{"object" => %{"id" => object_id}}} = Activity.get_by_ap_id(activity_id)
106 %Object{data: %{"content" => content}} = Object.get_by_ap_id(object_id)
108 "@#{actor.nickname} repeated: #{Utils.scrub_html_and_truncate(content, 80)}"
112 %{activity: %{data: %{"type" => type}}},
115 when type in ["Follow", "Like"] do
117 "Follow" -> "@#{actor.nickname} has followed you"
118 "Like" -> "@#{actor.nickname} has favorited your post"
122 def format_title(%{activity: %{data: %{"type" => type}}}) do
124 "Create" -> "New Mention"
125 "Follow" -> "New Follower"
126 "Announce" -> "New Repeat"
127 "Like" -> "New Favorite"