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