Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[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", "Move"]
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}} = activity,
26 user: %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 access_token: subscription.token.token,
43 notification_id: notif.id,
44 notification_type: type,
45 icon: avatar_url,
46 preferred_locale: "en",
47 pleroma: %{
48 activity_id: notif.activity.id,
49 direct_conversation_id: direct_conversation_id
50 }
51 }
52 |> Map.merge(build_content(notif, actor, object))
53 |> Jason.encode!()
54 |> push_message(build_sub(subscription), gcm_api_key, subscription)
55 end
56 end
57
58 def perform(_) do
59 Logger.warn("Unknown notification type")
60 :error
61 end
62
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)
69 :ok
70
71 {:ok, %{status_code: code}} when 200 <= code and code < 300 ->
72 :ok
73
74 {:ok, %{status_code: code}} ->
75 Logger.error("Web Push Notification failed with code: #{code}")
76 :error
77
78 _ ->
79 Logger.error("Web Push Notification failed with unknown error")
80 :error
81 end
82 end
83
84 @doc "Gets user subscriptions"
85 def fetch_subsriptions(user_id) do
86 Subscription
87 |> where(user_id: ^user_id)
88 |> preload(:token)
89 |> Repo.all()
90 end
91
92 def build_sub(subscription) do
93 %{
94 keys: %{
95 p256dh: subscription.key_p256dh,
96 auth: subscription.key_auth
97 },
98 endpoint: subscription.endpoint
99 }
100 end
101
102 def build_content(
103 %{
104 activity: %{data: %{"directMessage" => true}},
105 user: %{notification_settings: %{privacy_option: true}}
106 },
107 actor,
108 _
109 ) do
110 %{title: "New Direct Message", body: "@#{actor.nickname}"}
111 end
112
113 def build_content(notif, actor, object) do
114 %{
115 title: format_title(notif),
116 body: format_body(notif, actor, object)
117 }
118 end
119
120 def format_body(
121 %{activity: %{data: %{"type" => "Create"}}},
122 actor,
123 %{data: %{"content" => content}}
124 ) do
125 "@#{actor.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}"
126 end
127
128 def format_body(
129 %{activity: %{data: %{"type" => "Announce"}}},
130 actor,
131 %{data: %{"content" => content}}
132 ) do
133 "@#{actor.nickname} repeated: #{Utils.scrub_html_and_truncate(content, 80)}"
134 end
135
136 def format_body(
137 %{activity: %{data: %{"type" => type}}},
138 actor,
139 _object
140 )
141 when type in ["Follow", "Like"] do
142 case type do
143 "Follow" -> "@#{actor.nickname} has followed you"
144 "Like" -> "@#{actor.nickname} has favorited your post"
145 end
146 end
147
148 def format_title(%{activity: %{data: %{"directMessage" => true}}}) do
149 "New Direct Message"
150 end
151
152 def format_title(%{activity: %{data: %{"type" => type}}}) do
153 case type do
154 "Create" -> "New Mention"
155 "Follow" -> "New Follower"
156 "Announce" -> "New Repeat"
157 "Like" -> "New Favorite"
158 end
159 end
160 end