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