Implement client join endpoint
This commit is contained in:
parent
941eb685b4
commit
658efa228f
4 changed files with 59 additions and 3 deletions
|
@ -107,6 +107,14 @@ defmodule MatrixServer.RoomServer do
|
||||||
GenServer.call(pid, {:invite, account, user_id})
|
GenServer.call(pid, {:invite, account, user_id})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Join a room.
|
||||||
|
"""
|
||||||
|
@spec join(pid(), Account.t()) :: {:ok, String.t()} | {:error, atom()}
|
||||||
|
def join(pid, account) do
|
||||||
|
GenServer.call(pid, {:join, account})
|
||||||
|
end
|
||||||
|
|
||||||
### Implementation
|
### Implementation
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
@ -200,6 +208,30 @@ defmodule MatrixServer.RoomServer do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handle_call({:join, account}, _from, %{room: %Room{id: room_id} = room, state_set: state_set} = state) do
|
||||||
|
case Repo.transaction(join_insert_event(room, state_set, account)) do
|
||||||
|
{:ok, state_set} -> {:reply, {:ok, room_id}, %{state | state_set: state_set}}
|
||||||
|
{:error, reason} -> {:reply, {:error, reason}, state}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Get a function that inserts a join event into the room for the given account.
|
||||||
|
@spec join_insert_event(Room.t(), t(), Account.t()) :: (-> {:ok, t()} | {:error, atom()})
|
||||||
|
defp join_insert_event(room, state_set, account) do
|
||||||
|
join_event = Event.join(room, account)
|
||||||
|
|
||||||
|
fn ->
|
||||||
|
case finalize_and_insert_event(join_event, state_set, room) do
|
||||||
|
{:ok, state_set, room} ->
|
||||||
|
_ = update_room_state_set(room, state_set)
|
||||||
|
state_set
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
Repo.rollback(reason)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Get a function that inserts an invite event into a room.
|
# Get a function that inserts an invite event into a room.
|
||||||
@spec invite_insert_event(Room.t(), t(), Account.t(), String.t()) ::
|
@spec invite_insert_event(Room.t(), t(), Account.t(), String.t()) ::
|
||||||
(() -> {:ok, t()} | {:error, atom()})
|
(() -> {:ok, t()} | {:error, atom()})
|
||||||
|
|
|
@ -312,9 +312,6 @@ defmodule MatrixServer.StateResolution.Authorization do
|
||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
|> Enum.reduce(%{}, &update_state_set/2)
|
|> Enum.reduce(%{}, &update_state_set/2)
|
||||||
|
|
||||||
IO.inspect(event)
|
|
||||||
IO.inspect(state_set)
|
|
||||||
|
|
||||||
authorized?(event, state_set)
|
authorized?(event, state_set)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -86,4 +86,30 @@ defmodule MatrixServerWeb.Client.RoomController do
|
||||||
end
|
end
|
||||||
|
|
||||||
def invite(conn, _), do: put_error(conn, :missing_param)
|
def invite(conn, _), do: put_error(conn, :missing_param)
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
This API starts a user participating in a particular room, if that user is allowed to participate in that room.
|
||||||
|
|
||||||
|
Action for POST /_matrix/client/r0/rooms/{roomId}/join.
|
||||||
|
TODO: third_party_signed
|
||||||
|
"""
|
||||||
|
def join(%Conn{assigns: %{account: account}} = conn, %{"room_id" => room_id}) do
|
||||||
|
case RoomServer.get_room_server(room_id) do
|
||||||
|
{:ok, pid} ->
|
||||||
|
case RoomServer.join(pid, account) do
|
||||||
|
{:ok, room_id} ->
|
||||||
|
conn
|
||||||
|
|> put_status(200)
|
||||||
|
|> json(%{room_id: room_id})
|
||||||
|
|
||||||
|
{:error, _} ->
|
||||||
|
put_error(conn, :unknown)
|
||||||
|
end
|
||||||
|
|
||||||
|
{:error, :not_found} ->
|
||||||
|
put_error(conn, :not_found, "The given room was not found.")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def join(conn, _), do: put_error(conn, :missing_param)
|
||||||
end
|
end
|
||||||
|
|
|
@ -59,6 +59,7 @@ defmodule MatrixServerWeb.Router do
|
||||||
|
|
||||||
scope "/rooms/:room_id" do
|
scope "/rooms/:room_id" do
|
||||||
post "/invite", RoomController, :invite
|
post "/invite", RoomController, :invite
|
||||||
|
post "/join", RoomController, :join
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue