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