Merge branch 'revert/subscription-refactoring' 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"]
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
37 for subscription <- fetch_subsriptions(user_id),
38 get_in(subscription.data, ["alerts", type]) do
39 %{
40 title: format_title(notif),
41 access_token: subscription.token.token,
42 body: format_body(notif, actor, object),
43 notification_id: notif.id,
44 notification_type: type,
45 icon: avatar_url,
46 preferred_locale: "en",
47 pleroma: %{
48 activity_id: activity_id
49 }
50 }
51 |> Jason.encode!()
52 |> push_message(build_sub(subscription), gcm_api_key, subscription)
53 end
54 end
55
56 def perform(_) do
57 Logger.warn("Unknown notification type")
58 :error
59 end
60
61 @doc "Push message to web"
62 def push_message(body, sub, api_key, subscription) do
63 case WebPushEncryption.send_web_push(body, sub, api_key) do
64 {:ok, %{status_code: code}} when 400 <= code and code < 500 ->
65 Logger.debug("Removing subscription record")
66 Repo.delete!(subscription)
67 :ok
68
69 {:ok, %{status_code: code}} when 200 <= code and code < 300 ->
70 :ok
71
72 {:ok, %{status_code: code}} ->
73 Logger.error("Web Push Notification failed with code: #{code}")
74 :error
75
76 _ ->
77 Logger.error("Web Push Notification failed with unknown error")
78 :error
79 end
80 end
81
82 @doc "Gets user subscriptions"
83 def fetch_subsriptions(user_id) do
84 Subscription
85 |> where(user_id: ^user_id)
86 |> preload(:token)
87 |> Repo.all()
88 end
89
90 def build_sub(subscription) do
91 %{
92 keys: %{
93 p256dh: subscription.key_p256dh,
94 auth: subscription.key_auth
95 },
96 endpoint: subscription.endpoint
97 }
98 end
99
100 def format_body(
101 %{activity: %{data: %{"type" => "Create"}}},
102 actor,
103 %{data: %{"content" => content}}
104 ) do
105 "@#{actor.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}"
106 end
107
108 def format_body(
109 %{activity: %{data: %{"type" => "Announce"}}},
110 actor,
111 %{data: %{"content" => content}}
112 ) do
113 "@#{actor.nickname} repeated: #{Utils.scrub_html_and_truncate(content, 80)}"
114 end
115
116 def format_body(
117 %{activity: %{data: %{"type" => type}}},
118 actor,
119 _object
120 )
121 when type in ["Follow", "Like"] do
122 case type do
123 "Follow" -> "@#{actor.nickname} has followed you"
124 "Like" -> "@#{actor.nickname} has favorited your post"
125 end
126 end
127
128 def format_title(%{activity: %{data: %{"type" => type}}}) do
129 case type do
130 "Create" -> "New Mention"
131 "Follow" -> "New Follower"
132 "Announce" -> "New Repeat"
133 "Like" -> "New Favorite"
134 end
135 end
136 end