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