Add Push notifications backend support

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-05-06 12:27:04 +02:00
parent 4f6e203ced
commit 9f5e3a39ec
14 changed files with 321 additions and 26 deletions

View File

@@ -0,0 +1,49 @@
defmodule Mobilizon.Users.PushSubscription do
use Ecto.Schema
alias Mobilizon.Users.User
import Ecto.Changeset
schema "user_push_subscriptions" do
field(:digest, :string)
belongs_to(:user, User)
embeds_one :data, Data, on_replace: :delete do
field(:endpoint, :string)
embeds_one :keys, Keys, on_replace: :delete do
field(:auth, :string)
field(:p256dh, :string)
end
end
timestamps()
end
@doc false
def changeset(push_subscription, attrs) do
push_subscription
|> cast(attrs, [:user_id])
|> cast_embed(:data, with: &cast_data/2)
|> put_change(:digest, compute_digest(attrs.data))
|> validate_required([:digest, :user_id, :data])
end
defp cast_data(schema, attrs) do
schema
|> cast(attrs, [:endpoint])
|> cast_embed(:keys, with: &cast_keys/2)
|> validate_required([:endpoint, :keys])
end
defp cast_keys(schema, attrs) do
schema
|> cast(attrs, [:auth, :p256dh])
|> validate_required([:auth, :p256dh])
end
defp compute_digest(data) do
:sha256
|> :crypto.hash(data)
|> Base.encode16()
end
end