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. # instead of the state_set state.
# Create custom type for this. # Create custom type for this.
serialized_state_set = serialized_state_set =
Enum.map(state_set, fn {{type, state_key}, event} -> Enum.map(state_set, fn {{type, state_key}, %Event{event_id: event_id}} ->
[type, state_key, event.event_id] [type, state_key, event_id]
end) end)
Repo.update!(change(room, state: serialized_state_set)) Repo.update!(change(room, state: serialized_state_set))
@ -473,7 +473,7 @@ defmodule MatrixServer.RoomServer do
state_set state_set
|> Map.take(state_pairs) |> Map.take(state_pairs)
|> Map.values() |> Map.values()
|> Enum.map(& &1.event_id) |> Enum.map(fn %Event{event_id: event_id} -> event_id end)
end end
# Get the auth events specific to m.room.member events. # 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. Return an multi to register a new user.
""" """
@spec register(Register.t()) :: Multi.t() @spec register(Register.t()) :: Multi.t()
def register(input) do def register(%Register{username: username, device_id: device_id, initial_device_display_name: initial_device_display_name, password: password}) do
localpart = input.username || MatrixServer.random_string(10, ?a..?z) localpart = username || MatrixServer.random_string(10, ?a..?z)
account_params = %{ account_params = %{
localpart: localpart, localpart: localpart,
password_hash: Bcrypt.hash_pwd_salt(input.password) password_hash: Bcrypt.hash_pwd_salt(password)
} }
Multi.new() Multi.new()
|> Multi.insert(:account, changeset(%Account{}, account_params)) |> Multi.insert(:account, changeset(%Account{}, account_params))
|> Multi.insert(:device, fn %{account: account} -> |> 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) access_token = Device.generate_access_token(localpart, device_id)
device_params = %{ device_params = %{
display_name: input.initial_device_display_name, display_name: initial_device_display_name,
device_id: device_id device_id: device_id
} }
@ -78,13 +78,13 @@ defmodule MatrixServer.Account do
Return a function to log a user in. Return a function to log a user in.
""" """
@spec login(Login.t()) :: (Ecto.Repo.t() -> {:error, any()} | {:ok, {Account.t(), Device.t()}}) @spec login(Login.t()) :: (Ecto.Repo.t() -> {:error, any()} | {:ok, {Account.t(), Device.t()}})
def login(input) do def login(%Login{password: password, identifier: %Login.Identifier{user: user}} = input) do
localpart = try_get_localpart(input.identifier.user) localpart = try_get_localpart(user)
fn repo -> fn repo ->
case repo.one(from a in Account, where: a.localpart == ^localpart) do case repo.one(from a in Account, where: a.localpart == ^localpart) do
%Account{password_hash: hash} = account -> %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 case Device.login(input, account) do
{:ok, device} -> {:ok, device} ->
{account, device} {account, device}

View file

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

View file

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

View file

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