1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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"
9 alias Pleroma.Notification
13 alias Pleroma.Web.Metadata.Utils
14 alias Pleroma.Web.Push.Subscription
19 defdelegate mastodon_notification_type(activity), to: Activity
21 @types ["Create", "Follow", "Announce", "Like", "Move"]
23 @doc "Performs sending notifications for user subscriptions"
24 @spec perform(Notification.t()) :: list(any) | :error
27 activity: %{data: %{"type" => activity_type}} = activity,
28 user: %User{id: user_id}
31 when activity_type in @types do
32 actor = User.get_cached_by_ap_id(notification.activity.data["actor"])
34 mastodon_type = mastodon_notification_type(notification.activity)
35 gcm_api_key = Application.get_env(:web_push_encryption, :gcm_api_key)
36 avatar_url = User.avatar_url(actor)
37 object = Object.normalize(activity)
38 user = User.get_cached_by_id(user_id)
39 direct_conversation_id = Activity.direct_conversation_id(activity, user)
41 for subscription <- fetch_subscriptions(user_id),
42 Subscription.enabled?(subscription, mastodon_type) do
44 access_token: subscription.token.token,
45 notification_id: notification.id,
46 notification_type: mastodon_type,
48 preferred_locale: "en",
50 activity_id: notification.activity.id,
51 direct_conversation_id: direct_conversation_id
54 |> Map.merge(build_content(notification, actor, object, mastodon_type))
56 |> push_message(build_sub(subscription), gcm_api_key, subscription)
62 Logger.warn("Unknown notification type")
63 {:error, :unknown_type}
66 @doc "Push message to web"
67 def push_message(body, sub, api_key, subscription) do
68 case WebPushEncryption.send_web_push(body, sub, api_key) do
69 {:ok, %{status_code: code}} when 400 <= code and code < 500 ->
70 Logger.debug("Removing subscription record")
71 Repo.delete!(subscription)
74 {:ok, %{status_code: code}} when 200 <= code and code < 300 ->
77 {:ok, %{status_code: code}} ->
78 Logger.error("Web Push Notification failed with code: #{code}")
82 Logger.error("Web Push Notification failed with unknown error")
87 @doc "Gets user subscriptions"
88 def fetch_subscriptions(user_id) do
90 |> where(user_id: ^user_id)
95 def build_sub(subscription) do
98 p256dh: subscription.key_p256dh,
99 auth: subscription.key_auth
101 endpoint: subscription.endpoint
105 def build_content(notification, actor, object, mastodon_type \\ nil)
109 activity: %{data: %{"directMessage" => true}},
110 user: %{notification_settings: %{privacy_option: true}}
116 %{title: "New Direct Message", body: "@#{actor.nickname}"}
119 def build_content(notification, actor, object, mastodon_type) do
120 mastodon_type = mastodon_type || mastodon_notification_type(notification.activity)
123 title: format_title(notification, mastodon_type),
124 body: format_body(notification, actor, object, mastodon_type)
128 def format_body(activity, actor, object, mastodon_type \\ nil)
131 %{activity: %{data: %{"type" => "Create"}}},
133 %{data: %{"content" => content}},
136 "@#{actor.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}"
140 %{activity: %{data: %{"type" => "Announce"}}},
142 %{data: %{"content" => content}},
145 "@#{actor.nickname} repeated: #{Utils.scrub_html_and_truncate(content, 80)}"
149 %{activity: %{data: %{"type" => type}}} = notification,
154 when type in ["Follow", "Like"] do
155 mastodon_type = mastodon_type || mastodon_notification_type(notification.activity)
157 case mastodon_type do
158 "follow" -> "@#{actor.nickname} has followed you"
159 "follow_request" -> "@#{actor.nickname} has requested to follow you"
160 "favourite" -> "@#{actor.nickname} has favorited your post"
164 def format_title(activity, mastodon_type \\ nil)
166 def format_title(%{activity: %{data: %{"directMessage" => true}}}, _mastodon_type) do
170 def format_title(%{activity: activity}, mastodon_type) do
171 mastodon_type = mastodon_type || mastodon_notification_type(activity)
173 case mastodon_type do
174 "mention" -> "New Mention"
175 "follow" -> "New Follower"
176 "follow_request" -> "New Follow Request"
177 "reblog" -> "New Repeat"
178 "favourite" -> "New Favorite"
179 type -> "New #{String.capitalize(type || "event")}"