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"
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(notif.activity.data["actor"])
32 type = Activity.mastodon_notification_type(notif.activity)
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)
36 user = User.get_cached_by_id(user_id)
37 direct_conversation_id = Activity.direct_conversation_id(activity, user)
39 for subscription <- fetch_subsriptions(user_id),
40 get_in(subscription.data, ["alerts", type]) do
42 access_token: subscription.token.token,
43 notification_id: notif.id,
44 notification_type: type,
46 preferred_locale: "en",
48 activity_id: notif.activity.id,
49 direct_conversation_id: direct_conversation_id
52 |> Map.merge(build_content(notif, actor, object))
54 |> push_message(build_sub(subscription), gcm_api_key, subscription)
59 Logger.warn("Unknown notification type")
63 @doc "Push message to web"
64 def push_message(body, sub, api_key, subscription) do
65 case WebPushEncryption.send_web_push(body, sub, api_key) do
66 {:ok, %{status_code: code}} when 400 <= code and code < 500 ->
67 Logger.debug("Removing subscription record")
68 Repo.delete!(subscription)
71 {:ok, %{status_code: code}} when 200 <= code and code < 300 ->
74 {:ok, %{status_code: code}} ->
75 Logger.error("Web Push Notification failed with code: #{code}")
79 Logger.error("Web Push Notification failed with unknown error")
84 @doc "Gets user subscriptions"
85 def fetch_subsriptions(user_id) do
87 |> where(user_id: ^user_id)
92 def build_sub(subscription) do
95 p256dh: subscription.key_p256dh,
96 auth: subscription.key_auth
98 endpoint: subscription.endpoint
104 activity: %{data: %{"directMessage" => true}},
105 user: %{notification_settings: %{privacy_option: true}}
110 %{title: "New Direct Message", body: "@#{actor.nickname}"}
113 def build_content(notif, actor, object) do
115 title: format_title(notif),
116 body: format_body(notif, actor, object)
121 %{activity: %{data: %{"type" => "Create"}}},
123 %{data: %{"content" => content}}
125 "@#{actor.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}"
129 %{activity: %{data: %{"type" => "Announce"}}},
131 %{data: %{"content" => content}}
133 "@#{actor.nickname} repeated: #{Utils.scrub_html_and_truncate(content, 80)}"
137 %{activity: %{data: %{"type" => type}}},
141 when type in ["Follow", "Like"] do
143 "Follow" -> "@#{actor.nickname} has followed you"
144 "Like" -> "@#{actor.nickname} has favorited your post"
148 def format_title(%{activity: %{data: %{"directMessage" => true}}}) do
152 def format_title(%{activity: %{data: %{"type" => type}}}) do
154 "Create" -> "New Mention"
155 "Follow" -> "New Follower"
156 "Announce" -> "New Repeat"
157 "Like" -> "New Favorite"