First
[anni] / lib / pleroma / web / push / subscription.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Push.Subscription do
6   use Ecto.Schema
7
8   import Ecto.Changeset
9
10   alias Pleroma.Repo
11   alias Pleroma.User
12   alias Pleroma.Web.OAuth.Token
13   alias Pleroma.Web.Push.Subscription
14
15   @type t :: %__MODULE__{}
16
17   schema "push_subscriptions" do
18     belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
19     belongs_to(:token, Token)
20     field(:endpoint, :string)
21     field(:key_p256dh, :string)
22     field(:key_auth, :string)
23     field(:data, :map, default: %{})
24
25     timestamps()
26   end
27
28   # credo:disable-for-next-line Credo.Check.Readability.MaxLineLength
29   @supported_alert_types ~w[follow favourite mention reblog poll pleroma:chat_mention pleroma:emoji_reaction]a
30
31   defp alerts(%{data: %{alerts: alerts}}) do
32     alerts = Map.take(alerts, @supported_alert_types)
33     %{"alerts" => alerts}
34   end
35
36   def enabled?(subscription, "follow_request") do
37     enabled?(subscription, "follow")
38   end
39
40   def enabled?(subscription, alert_type) do
41     get_in(subscription.data, ["alerts", alert_type])
42   end
43
44   def create(
45         %User{} = user,
46         %Token{} = token,
47         %{
48           subscription: %{
49             endpoint: endpoint,
50             keys: %{auth: key_auth, p256dh: key_p256dh}
51           }
52         } = params
53       ) do
54     Repo.insert(%Subscription{
55       user_id: user.id,
56       token_id: token.id,
57       endpoint: endpoint,
58       key_auth: ensure_base64_urlsafe(key_auth),
59       key_p256dh: ensure_base64_urlsafe(key_p256dh),
60       data: alerts(params)
61     })
62   end
63
64   @doc "Gets subsciption by user & token"
65   @spec get(User.t(), Token.t()) :: {:ok, t()} | {:error, :not_found}
66   def get(%User{id: user_id}, %Token{id: token_id}) do
67     case Repo.get_by(Subscription, user_id: user_id, token_id: token_id) do
68       nil -> {:error, :not_found}
69       subscription -> {:ok, subscription}
70     end
71   end
72
73   def update(user, token, params) do
74     with {:ok, subscription} <- get(user, token) do
75       subscription
76       |> change(data: alerts(params))
77       |> Repo.update()
78     end
79   end
80
81   def delete(user, token) do
82     with {:ok, subscription} <- get(user, token),
83          do: Repo.delete(subscription)
84   end
85
86   def delete_if_exists(user, token) do
87     case get(user, token) do
88       {:error, _} -> {:ok, nil}
89       {:ok, sub} -> Repo.delete(sub)
90     end
91   end
92
93   # Some webpush clients (e.g. iOS Toot!) use an non urlsafe base64 as an encoding for the key.
94   # However, the web push rfs specify to use base64 urlsafe, and the `web_push_encryption` library
95   # we use requires the key to be properly encoded. So we just convert base64 to urlsafe base64.
96   defp ensure_base64_urlsafe(string) do
97     string
98     |> String.replace("+", "-")
99     |> String.replace("/", "_")
100     |> String.replace("=", "")
101   end
102 end