3de7af7086ca8f1c7814529defa538d6fd1d0da5
[akkoma] / lib / pleroma / web / push / impl.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Push.Impl do
6 @moduledoc "The module represents implementation push web notification"
7
8 alias Pleroma.Activity
9 alias Pleroma.Notification
10 alias Pleroma.Object
11 alias Pleroma.Repo
12 alias Pleroma.User
13 alias Pleroma.Web.Metadata.Utils
14 alias Pleroma.Web.Push.Subscription
15
16 require Logger
17 import Ecto.Query
18
19 @types ["Create", "Follow", "Announce", "Like"]
20
21 @doc "Performs sending notifications for user subscriptions"
22 @spec perform(Notification.t()) :: list(any) | :error
23 def perform(
24 %{
25 activity: %{data: %{"type" => activity_type}, id: activity_id} = activity,
26 user_id: user_id
27 } = notif
28 )
29 when activity_type in @types do
30 actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
31
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)
38
39 for subscription <- fetch_subsriptions(user_id),
40 get_in(subscription.data, ["alerts", type]) do
41 %{
42 title: format_title(notif),
43 access_token: subscription.token.token,
44 body: format_body(notif, actor, object),
45 notification_id: notif.id,
46 notification_type: type,
47 icon: avatar_url,
48 preferred_locale: "en",
49 pleroma: %{
50 activity_id: activity_id,
51 direct_conversation_id: direct_conversation_id
52 }
53 }
54 |> Jason.encode!()
55 |> push_message(build_sub(subscription), gcm_api_key, subscription)
56 end
57 end
58
59 def perform(_) do
60 Logger.warn("Unknown notification type")
61 :error
62 end
63
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)
70 :ok
71
72 {:ok, %{status_code: code}} when 200 <= code and code < 300 ->
73 :ok
74
75 {:ok, %{status_code: code}} ->
76 Logger.error("Web Push Notification failed with code: #{code}")
77 :error
78
79 _ ->
80 Logger.error("Web Push Notification failed with unknown error")
81 :error
82 end
83 end
84
85 @doc "Gets user subscriptions"
86 def fetch_subsriptions(user_id) do
87 Subscription
88 |> where(user_id: ^user_id)
89 |> preload(:token)
90 |> Repo.all()
91 end
92
93 def build_sub(subscription) do
94 %{
95 keys: %{
96 p256dh: subscription.key_p256dh,
97 auth: subscription.key_auth
98 },
99 endpoint: subscription.endpoint
100 }
101 end
102
103 def format_body(
104 %{activity: %{data: %{"type" => "Create"}}},
105 actor,
106 %{data: %{"content" => content}}
107 ) do
108 "@#{actor.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}"
109 end
110
111 def format_body(
112 %{activity: %{data: %{"type" => "Announce"}}},
113 actor,
114 %{data: %{"content" => content}}
115 ) do
116 "@#{actor.nickname} repeated: #{Utils.scrub_html_and_truncate(content, 80)}"
117 end
118
119 def format_body(
120 %{activity: %{data: %{"type" => type}}},
121 actor,
122 _object
123 )
124 when type in ["Follow", "Like"] do
125 case type do
126 "Follow" -> "@#{actor.nickname} has followed you"
127 "Like" -> "@#{actor.nickname} has favorited your post"
128 end
129 end
130
131 def format_title(%{activity: %{data: %{"directMessage" => true}}}) do
132 "New Direct Message"
133 end
134
135 def format_title(%{activity: %{data: %{"type" => type}}}) do
136 case type do
137 "Create" -> "New Mention"
138 "Follow" -> "New Follower"
139 "Announce" -> "New Repeat"
140 "Like" -> "New Favorite"
141 end
142 end
143 end