architex/lib/matrix_server/schema/device.ex

70 lines
2 KiB
Elixir
Raw Normal View History

defmodule MatrixServer.Device do
use Ecto.Schema
import Ecto.{Changeset, Query}
alias MatrixServer.{Account, Device, Repo, DeviceTransaction}
2021-08-30 20:53:10 +00:00
alias MatrixServerWeb.Client.Request.Login
@type t :: %__MODULE__{
device_id: String.t(),
access_token: String.t(),
display_name: String.t(),
account_id: integer()
}
schema "devices" do
field :device_id, :string
2021-07-10 21:16:00 +00:00
field :access_token, :string, redact: true
field :display_name, :string
belongs_to :account, Account
has_many :device_transactions, DeviceTransaction
end
def changeset(device, params \\ %{}) do
device
2021-06-27 15:28:28 +00:00
|> cast(params, [:display_name, :device_id])
|> validate_required([:device_id])
|> unique_constraint([:device_id, :account_id], name: :devices_device_id_account_id_index)
end
2021-06-26 20:02:18 +00:00
2021-06-27 15:28:28 +00:00
def generate_access_token(localpart, device_id) do
Phoenix.Token.encrypt(MatrixServerWeb.Endpoint, "access_token", {localpart, device_id})
end
def generate_device_id(localpart) do
2021-07-13 15:08:07 +00:00
# TODO: use random string instead
"#{localpart}_#{System.os_time(:millisecond)}"
2021-06-26 20:02:18 +00:00
end
2021-08-30 20:53:10 +00:00
def login(
%Login{device_id: device_id, initial_device_display_name: initial_device_display_name},
%Account{localpart: localpart} = account
) do
device_id = device_id || generate_device_id(localpart)
access_token = generate_access_token(localpart, device_id)
2021-07-13 21:16:56 +00:00
update_query =
from(d in Device)
|> update(set: [access_token: ^access_token, device_id: ^device_id])
2021-07-13 21:16:56 +00:00
|> then(fn q ->
2021-08-30 20:53:10 +00:00
if initial_device_display_name do
update(q, set: [display_name: ^initial_device_display_name])
2021-07-13 21:16:56 +00:00
else
q
end
end)
2021-07-13 21:16:56 +00:00
device_params = %{
device_id: device_id,
2021-08-30 20:53:10 +00:00
display_name: initial_device_display_name
2021-07-13 21:16:56 +00:00
}
Ecto.build_assoc(account, :devices)
2021-07-13 21:16:56 +00:00
|> Device.changeset(device_params)
|> put_change(:access_token, access_token)
|> Repo.insert(on_conflict: update_query, conflict_target: [:account_id, :device_id])
end
end