Change code for idiomatic Elixir

This commit is contained in:
Pim Kunis 2021-08-30 22:53:10 +02:00
parent 91a06aff1b
commit cf9f53e446
5 changed files with 24 additions and 20 deletions

View file

@ -397,8 +397,8 @@ defmodule MatrixServer.RoomServer do
# instead of the state_set state.
# Create custom type for this.
serialized_state_set =
Enum.map(state_set, fn {{type, state_key}, event} ->
[type, state_key, event.event_id]
Enum.map(state_set, fn {{type, state_key}, %Event{event_id: event_id}} ->
[type, state_key, event_id]
end)
Repo.update!(change(room, state: serialized_state_set))
@ -473,7 +473,7 @@ defmodule MatrixServer.RoomServer do
state_set
|> Map.take(state_pairs)
|> Map.values()
|> Enum.map(& &1.event_id)
|> Enum.map(fn %Event{event_id: event_id} -> event_id end)
end
# Get the auth events specific to m.room.member events.

View file

@ -50,22 +50,22 @@ defmodule MatrixServer.Account do
Return an multi to register a new user.
"""
@spec register(Register.t()) :: Multi.t()
def register(input) do
localpart = input.username || MatrixServer.random_string(10, ?a..?z)
def register(%Register{username: username, device_id: device_id, initial_device_display_name: initial_device_display_name, password: password}) do
localpart = username || MatrixServer.random_string(10, ?a..?z)
account_params = %{
localpart: localpart,
password_hash: Bcrypt.hash_pwd_salt(input.password)
password_hash: Bcrypt.hash_pwd_salt(password)
}
Multi.new()
|> Multi.insert(:account, changeset(%Account{}, account_params))
|> Multi.insert(:device, fn %{account: account} ->
device_id = input.device_id || Device.generate_device_id(account.localpart)
device_id = device_id || Device.generate_device_id(account.localpart)
access_token = Device.generate_access_token(localpart, device_id)
device_params = %{
display_name: input.initial_device_display_name,
display_name: initial_device_display_name,
device_id: device_id
}
@ -78,13 +78,13 @@ defmodule MatrixServer.Account do
Return a function to log a user in.
"""
@spec login(Login.t()) :: (Ecto.Repo.t() -> {:error, any()} | {:ok, {Account.t(), Device.t()}})
def login(input) do
localpart = try_get_localpart(input.identifier.user)
def login(%Login{password: password, identifier: %Login.Identifier{user: user}} = input) do
localpart = try_get_localpart(user)
fn repo ->
case repo.one(from a in Account, where: a.localpart == ^localpart) do
%Account{password_hash: hash} = account ->
if Bcrypt.verify_pass(input.password, hash) do
if Bcrypt.verify_pass(password, hash) do
case Device.login(input, account) do
{:ok, device} ->
{account, device}

View file

@ -4,6 +4,7 @@ defmodule MatrixServer.Device do
import Ecto.{Changeset, Query}
alias MatrixServer.{Account, Device, Repo}
alias MatrixServerWeb.Client.Request.Login
@type t :: %__MODULE__{
device_id: String.t(),
@ -36,16 +37,19 @@ defmodule MatrixServer.Device do
"#{localpart}_#{System.os_time(:millisecond)}"
end
def login(input, %Account{localpart: localpart} = account) do
device_id = input.device_id || generate_device_id(localpart)
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)
update_query =
from(d in Device)
|> update(set: [access_token: ^access_token, device_id: ^device_id])
|> then(fn q ->
if input.initial_device_display_name do
update(q, set: [display_name: ^input.initial_device_display_name])
if initial_device_display_name do
update(q, set: [display_name: ^initial_device_display_name])
else
q
end
@ -53,7 +57,7 @@ defmodule MatrixServer.Device do
device_params = %{
device_id: device_id,
display_name: input.initial_device_display_name
display_name: initial_device_display_name
}
Ecto.build_assoc(account, :devices)

View file

@ -26,8 +26,8 @@ defmodule MatrixServer.Room do
cast(room, params, [:visibility])
end
def create_changeset(%CreateRoom{} = input) do
visibility = input.visibility || :public
def create_changeset(%CreateRoom{visibility: visibility}) do
visibility = visibility || :public
%Room{id: generate_room_id()}
|> changeset(%{visibility: visibility})

View file

@ -18,7 +18,7 @@ defmodule MatrixServerWeb.Client.RegisterController do
def register(conn, %{"auth" => %{"type" => @register_type}} = params) do
case Register.changeset(params) do
%Changeset{valid?: true} = cs ->
input = apply_changes(cs)
%Register{inhibit_login: inhibit_login} = input = apply_changes(cs)
case Account.register(input) |> Repo.transaction() do
{:ok,
@ -29,7 +29,7 @@ defmodule MatrixServerWeb.Client.RegisterController do
data = %{user_id: MatrixServer.get_mxid(localpart)}
data =
if not input.inhibit_login do
if not inhibit_login do
data
|> Map.put(:device_id, device_id)
|> Map.put(:access_token, access_token)