Merge branch 'features/add-credo-to-ci' into 'develop'
[akkoma] / lib / pleroma / web / push / subscription.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.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: Pleroma.FlakeId)
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 @supported_alert_types ~w[follow favourite mention reblog]
29
30 defp alerts(%{"data" => %{"alerts" => alerts}}) do
31 alerts = Map.take(alerts, @supported_alert_types)
32 %{"alerts" => alerts}
33 end
34
35 def create(
36 %User{} = user,
37 %Token{} = token,
38 %{
39 "subscription" => %{
40 "endpoint" => endpoint,
41 "keys" => %{"auth" => key_auth, "p256dh" => key_p256dh}
42 }
43 } = params
44 ) do
45 Repo.insert(%Subscription{
46 user_id: user.id,
47 token_id: token.id,
48 endpoint: endpoint,
49 key_auth: ensure_base64_urlsafe(key_auth),
50 key_p256dh: ensure_base64_urlsafe(key_p256dh),
51 data: alerts(params)
52 })
53 end
54
55 @doc "Gets subsciption by user & token"
56 @spec get(User.t(), Token.t()) :: {:ok, t()} | {:error, :not_found}
57 def get(%User{id: user_id}, %Token{id: token_id}) do
58 case Repo.get_by(Subscription, user_id: user_id, token_id: token_id) do
59 nil -> {:error, :not_found}
60 subscription -> {:ok, subscription}
61 end
62 end
63
64 def update(user, token, params) do
65 with {:ok, subscription} <- get(user, token) do
66 subscription
67 |> change(data: alerts(params))
68 |> Repo.update()
69 end
70 end
71
72 def delete(user, token) do
73 with {:ok, subscription} <- get(user, token),
74 do: Repo.delete(subscription)
75 end
76
77 def delete_if_exists(user, token) do
78 case get(user, token) do
79 {:error, _} -> {:ok, nil}
80 {:ok, sub} -> Repo.delete(sub)
81 end
82 end
83
84 # Some webpush clients (e.g. iOS Toot!) use an non urlsafe base64 as an encoding for the key.
85 # However, the web push rfs specify to use base64 urlsafe, and the `web_push_encryption` library
86 # we use requires the key to be properly encoded. So we just convert base64 to urlsafe base64.
87 defp ensure_base64_urlsafe(string) do
88 string
89 |> String.replace("+", "-")
90 |> String.replace("/", "_")
91 |> String.replace("=", "")
92 end
93 end