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 @types ["Create", "Follow", "Announce", "Like", "Move"]
21 @doc "Performs sending notifications for user subscriptions"
22 @spec perform(Notification.t()) :: list(any) | :error
25 activity: %{data: %{"type" => activity_type}} = activity,
26 user: %User{id: user_id}
29 when activity_type in @types do
30 actor = User.get_cached_by_ap_id(notification.activity.data["actor"])
32 mastodon_type = notification.type
33 gcm_api_key = Application.get_env(:web_push_encryption, :gcm_api_key)
34 avatar_url = User.avatar_url(actor)
35 object = Object.normalize(activity, false)
36 user = User.get_cached_by_id(user_id)
37 direct_conversation_id = Activity.direct_conversation_id(activity, user)
39 for subscription <- fetch_subscriptions(user_id),
40 Subscription.enabled?(subscription, mastodon_type) do
42 access_token: subscription.token.token,
43 notification_id: notification.id,
44 notification_type: mastodon_type,
46 preferred_locale: "en",
48 activity_id: notification.activity.id,
49 direct_conversation_id: direct_conversation_id
52 |> Map.merge(build_content(notification, actor, object, mastodon_type))
54 |> push_message(build_sub(subscription), gcm_api_key, subscription)
60 Logger.warn("Unknown notification type")
61 {:error, :unknown_type}
64 @doc "Push message to web"
65 def push_message(body, sub, api_key, subscription) do
66 case WebPushEncryption.send_web_push(body, sub, api_key) do
67 {:ok, %{status_code: code}} when 400 <= code and code < 500 ->
68 Logger.debug("Removing subscription record")
69 Repo.delete!(subscription)
72 {:ok, %{status_code: code}} when 200 <= code and code < 300 ->
75 {:ok, %{status_code: code}} ->
76 Logger.error("Web Push Notification failed with code: #{code}")
80 Logger.error("Web Push Notification failed with unknown error")
85 @doc "Gets user subscriptions"
86 def fetch_subscriptions(user_id) do
88 |> where(user_id: ^user_id)
93 def build_sub(subscription) do
96 p256dh: subscription.key_p256dh,
97 auth: subscription.key_auth
99 endpoint: subscription.endpoint
103 def build_content(notification, actor, object, mastodon_type \\ nil)
107 user: %{notification_settings: %{privacy_option: true}}
113 %{body: format_title(notification, mastodon_type)}
116 def build_content(notification, actor, object, mastodon_type) do
117 mastodon_type = mastodon_type || notification.type
120 title: format_title(notification, mastodon_type),
121 body: format_body(notification, actor, object, mastodon_type)
125 def format_body(activity, actor, object, mastodon_type \\ nil)
127 def format_body(_activity, actor, %{data: %{"type" => "ChatMessage", "content" => content}}, _) do
129 nil -> "@#{actor.nickname}: (Attachment)"
130 content -> "@#{actor.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}"
135 %{activity: %{data: %{"type" => "Create"}}},
137 %{data: %{"content" => content}},
140 "@#{actor.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}"
144 %{activity: %{data: %{"type" => "Announce"}}},
146 %{data: %{"content" => content}},
149 "@#{actor.nickname} repeated: #{Utils.scrub_html_and_truncate(content, 80)}"
153 %{activity: %{data: %{"type" => type}}} = notification,
158 when type in ["Follow", "Like"] do
159 mastodon_type = mastodon_type || notification.type
161 case mastodon_type do
162 "follow" -> "@#{actor.nickname} has followed you"
163 "follow_request" -> "@#{actor.nickname} has requested to follow you"
164 "favourite" -> "@#{actor.nickname} has favorited your post"
168 def format_title(activity, mastodon_type \\ nil)
170 def format_title(%{activity: %{data: %{"directMessage" => true}}}, _mastodon_type) do
174 def format_title(%{type: type}, mastodon_type) do
175 case mastodon_type || type do
176 "mention" -> "New Mention"
177 "follow" -> "New Follower"
178 "follow_request" -> "New Follow Request"
179 "reblog" -> "New Repeat"
180 "favourite" -> "New Favorite"
181 "pleroma:chat_mention" -> "New Chat Message"
182 type -> "New #{String.capitalize(type || "event")}"