architex/lib/matrix_server/device.ex

79 lines
2.2 KiB
Elixir
Raw Normal View History

defmodule MatrixServer.Device do
use Ecto.Schema
import Ecto.{Changeset, Query}
alias MatrixServer.{Account, Device, Repo}
2021-07-13 21:16:56 +00:00
alias MatrixServerWeb.API.Login
@primary_key false
schema "devices" do
field :device_id, :string, primary_key: true
2021-07-10 21:16:00 +00:00
field :access_token, :string, redact: true
field :display_name, :string
belongs_to :account, Account,
foreign_key: :localpart,
references: :localpart,
type: :string,
primary_key: true
end
def changeset(device, params \\ %{}) do
device
2021-06-27 15:28:28 +00:00
|> cast(params, [:display_name, :device_id])
|> validate_required([:localpart, :device_id])
|> unique_constraint([:localpart, :device_id], name: :devices_pkey)
end
2021-06-27 15:28:28 +00:00
def insert_new_access_token(repo, %{
device: %Device{localpart: localpart, device_id: device_id} = device
}) do
2021-06-27 15:28:28 +00:00
access_token = generate_access_token(localpart, device_id)
device
|> change(%{access_token: access_token})
|> repo.update()
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
2021-06-26 20:02:18 +00:00
time_string =
DateTime.utc_now()
|> DateTime.to_unix()
|> Integer.to_string()
"#{localpart}_#{time_string}"
end
2021-07-17 15:38:20 +00:00
def login(%Login{} = input, account) do
device_id = input.device_id || generate_device_id(account.localpart)
2021-07-13 21:16:56 +00:00
access_token = generate_access_token(account.localpart, device_id)
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-07-17 15:38:20 +00:00
if input.initial_device_display_name do
update(q, set: [display_name: ^input.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-07-17 15:38:20 +00:00
display_name: input.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: [:localpart, :device_id])
end
end