Add endpoint for room creation
This commit is contained in:
parent
80c8d3a66b
commit
598af7a884
15 changed files with 679 additions and 551 deletions
28
lib/matrix_server_web/api/create_room.ex
Normal file
28
lib/matrix_server_web/api/create_room.ex
Normal file
|
@ -0,0 +1,28 @@
|
|||
defmodule MatrixServerWeb.API.CreateRoom do
|
||||
use Ecto.Schema
|
||||
|
||||
import Ecto.Changeset
|
||||
|
||||
alias Ecto.Changeset
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :visibility, :string
|
||||
field :room_alias_name, :string
|
||||
field :name, :string
|
||||
field :topic, :string
|
||||
field :invite, {:array, :string}
|
||||
field :room_version, :string
|
||||
# TODO: unimplemented:
|
||||
# creation_content, initial_state, invite_3pid, initial_state, preset,
|
||||
# is_direct, power_level_content_override
|
||||
end
|
||||
|
||||
def changeset(params) do
|
||||
%__MODULE__{}
|
||||
|> cast(params, [:visibility, :room_alias_name, :name, :topic, :invite, :room_version])
|
||||
end
|
||||
|
||||
def get_error(%Changeset{errors: [error | _]}), do: get_error(error)
|
||||
def get_error(_), do: :bad_json
|
||||
end
|
|
@ -4,6 +4,8 @@ defmodule MatrixServerWeb.API.Login do
|
|||
|
||||
import Ecto.Changeset
|
||||
|
||||
# TODO: Maybe use inline embedded schema here
|
||||
# https://hexdocs.pm/ecto/Ecto.Schema.html#embeds_one/3
|
||||
defmodule MatrixServerWeb.API.Login.Identifier do
|
||||
use Ecto.Schema
|
||||
|
||||
|
@ -15,9 +17,9 @@ defmodule MatrixServerWeb.API.Login do
|
|||
field :user, :string
|
||||
end
|
||||
|
||||
def changeset(identifier, attrs) do
|
||||
def changeset(identifier, params) do
|
||||
identifier
|
||||
|> cast(attrs, [:type, :user])
|
||||
|> cast(params, [:type, :user])
|
||||
|> validate_required([:type, :user])
|
||||
end
|
||||
end
|
||||
|
@ -33,9 +35,9 @@ defmodule MatrixServerWeb.API.Login do
|
|||
embeds_one :identifier, Identifier
|
||||
end
|
||||
|
||||
def changeset(attrs) do
|
||||
def changeset(params) do
|
||||
%__MODULE__{}
|
||||
|> cast(attrs, [:type, :password, :device_id, :initial_device_display_name])
|
||||
|> cast(params, [:type, :password, :device_id, :initial_device_display_name])
|
||||
|> cast_embed(:identifier, with: &Identifier.changeset/2, required: true)
|
||||
|> validate_required([:type, :password])
|
||||
end
|
||||
|
|
|
@ -2,7 +2,6 @@ defmodule MatrixServerWeb.API.Register do
|
|||
use Ecto.Schema
|
||||
|
||||
import Ecto.Changeset
|
||||
import MatrixServerWeb.Plug.Error
|
||||
|
||||
alias Ecto.Changeset
|
||||
|
||||
|
@ -27,12 +26,8 @@ defmodule MatrixServerWeb.API.Register do
|
|||
|> validate_required([:password, :username])
|
||||
end
|
||||
|
||||
def handle_error(conn, cs) do
|
||||
put_error(conn, get_register_error(cs))
|
||||
end
|
||||
|
||||
defp get_register_error(%Changeset{errors: [error | _]}), do: get_register_error(error)
|
||||
defp get_register_error({:localpart, {_, [{:constraint, :unique} | _]}}), do: :user_in_use
|
||||
defp get_register_error({:localpart, {_, [{:validation, _} | _]}}), do: :invalid_username
|
||||
defp get_register_error(_), do: :bad_json
|
||||
def get_error(%Changeset{errors: [error | _]}), do: get_error(error)
|
||||
def get_error({:localpart, {_, [{:constraint, :unique} | _]}}), do: :user_in_use
|
||||
def get_error({:localpart, {_, [{:validation, _} | _]}}), do: :invalid_username
|
||||
def get_error(_), do: :bad_json
|
||||
end
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
defmodule MatrixServerWeb.AuthController do
|
||||
use MatrixServerWeb, :controller
|
||||
|
||||
import MatrixServer
|
||||
import MatrixServerWeb.Plug.Error
|
||||
import Ecto.Changeset, only: [apply_changes: 1]
|
||||
import Ecto.Query
|
||||
import Ecto.Changeset
|
||||
|
||||
alias MatrixServer.{Repo, Account, Device}
|
||||
alias MatrixServer.{Repo, Account}
|
||||
alias MatrixServerWeb.API.{Register, Login}
|
||||
alias Ecto.Changeset
|
||||
|
||||
|
@ -19,13 +17,13 @@ defmodule MatrixServerWeb.AuthController do
|
|||
input =
|
||||
apply_changes(cs)
|
||||
|> Map.from_struct()
|
||||
|> update_map_entry(:initial_device_display_name, :display_name)
|
||||
|> update_map_entry(:username, :localpart)
|
||||
|> update_map_entry(:password, :password_hash, &Bcrypt.hash_pwd_salt/1)
|
||||
|> MatrixServer.maybe_update_map(:initial_device_display_name, :display_name)
|
||||
|> MatrixServer.maybe_update_map(:username, :localpart)
|
||||
|> MatrixServer.maybe_update_map(:password, :password_hash, &Bcrypt.hash_pwd_salt/1)
|
||||
|
||||
case Account.register(input) |> Repo.transaction() do
|
||||
{:ok, %{device_with_access_token: device}} ->
|
||||
data = %{user_id: get_mxid(device.localpart)}
|
||||
data = %{user_id: MatrixServer.get_mxid(device.localpart)}
|
||||
|
||||
data =
|
||||
if not input.inhibit_login do
|
||||
|
@ -41,7 +39,8 @@ defmodule MatrixServerWeb.AuthController do
|
|||
|> json(data)
|
||||
|
||||
{:error, _, cs, _} ->
|
||||
Register.handle_error(conn, cs)
|
||||
IO.inspect(cs)
|
||||
put_error(conn, Register.get_error(cs))
|
||||
end
|
||||
|
||||
_ ->
|
||||
|
@ -83,8 +82,8 @@ defmodule MatrixServerWeb.AuthController do
|
|||
input =
|
||||
apply_changes(cs)
|
||||
|> Map.from_struct()
|
||||
|> update_map_entry(:initial_device_display_name, :display_name)
|
||||
|> update_map_entry(:identifier, :localpart, fn
|
||||
|> MatrixServer.maybe_update_map(:initial_device_display_name, :display_name)
|
||||
|> MatrixServer.maybe_update_map(:identifier, :localpart, fn
|
||||
%{user: "@" <> rest} ->
|
||||
case String.split(rest) do
|
||||
[localpart, _] -> localpart
|
||||
|
@ -96,10 +95,10 @@ defmodule MatrixServerWeb.AuthController do
|
|||
user
|
||||
end)
|
||||
|
||||
case Repo.transaction(login_transaction(input)) do
|
||||
case Account.login(input) |> Repo.transaction() do
|
||||
{:ok, device} ->
|
||||
data = %{
|
||||
user_id: get_mxid(device.localpart),
|
||||
user_id: MatrixServer.get_mxid(device.localpart),
|
||||
access_token: device.access_token,
|
||||
device_id: device.device_id
|
||||
}
|
||||
|
@ -121,26 +120,4 @@ defmodule MatrixServerWeb.AuthController do
|
|||
# Other login types and identifiers are unsupported for now.
|
||||
put_error(conn, :unknown)
|
||||
end
|
||||
|
||||
defp login_transaction(%{localpart: localpart, password: password} = params) do
|
||||
fn repo ->
|
||||
case repo.one(from a in Account, where: a.localpart == ^localpart) do
|
||||
%Account{password_hash: hash} = account ->
|
||||
if Bcrypt.verify_pass(password, hash) do
|
||||
device_id = Map.get(params, :device_id, Device.generate_device_id(localpart))
|
||||
access_token = Device.generate_access_token(localpart, device_id)
|
||||
|
||||
case Device.login(account, device_id, access_token, params) do
|
||||
{:ok, device} -> device
|
||||
{:error, _cs} -> repo.rollback(:forbidden)
|
||||
end
|
||||
else
|
||||
repo.rollback(:forbidden)
|
||||
end
|
||||
|
||||
nil ->
|
||||
repo.rollback(:forbidden)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
25
lib/matrix_server_web/controllers/room_controller.ex
Normal file
25
lib/matrix_server_web/controllers/room_controller.ex
Normal file
|
@ -0,0 +1,25 @@
|
|||
defmodule MatrixServerWeb.RoomController do
|
||||
use MatrixServerWeb, :controller
|
||||
|
||||
import MatrixServerWeb.Plug.Error
|
||||
import Ecto.Changeset
|
||||
|
||||
alias MatrixServerWeb.API.{CreateRoom}
|
||||
alias Ecto.Changeset
|
||||
|
||||
def create(conn, params) do
|
||||
case CreateRoom.changeset(params) do
|
||||
%Changeset{valid?: true} = cs ->
|
||||
api_struct = apply_changes(cs)
|
||||
|
||||
MatrixServer.RoomServer.create_room(api_struct)
|
||||
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(%{})
|
||||
|
||||
_ ->
|
||||
put_error(conn, :bad_json)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -32,6 +32,7 @@ defmodule MatrixServerWeb.Router do
|
|||
get "/account/whoami", AccountController, :whoami
|
||||
post "/logout", AccountController, :logout
|
||||
post "/logout/all", AccountController, :logout_all
|
||||
post "/createRoom", RoomController, :create
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue