1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Push.Subscription do
12 alias Pleroma.Web.OAuth.Token
13 alias Pleroma.Web.Push.Subscription
15 @type t :: %__MODULE__{}
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: %{})
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
31 defp alerts(%{data: %{alerts: alerts}}) do
32 alerts = Map.take(alerts, @supported_alert_types)
36 def enabled?(subscription, "follow_request") do
37 enabled?(subscription, "follow")
40 def enabled?(subscription, alert_type) do
41 get_in(subscription.data, ["alerts", alert_type])
50 keys: %{auth: key_auth, p256dh: key_p256dh}
54 Repo.insert(%Subscription{
58 key_auth: ensure_base64_urlsafe(key_auth),
59 key_p256dh: ensure_base64_urlsafe(key_p256dh),
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}
73 def update(user, token, params) do
74 with {:ok, subscription} <- get(user, token) do
76 |> change(data: alerts(params))
81 def delete(user, token) do
82 with {:ok, subscription} <- get(user, token),
83 do: Repo.delete(subscription)
86 def delete_if_exists(user, token) do
87 case get(user, token) do
88 {:error, _} -> {:ok, nil}
89 {:ok, sub} -> Repo.delete(sub)
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
98 |> String.replace("+", "-")
99 |> String.replace("/", "_")
100 |> String.replace("=", "")