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})
|
||||
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
|
||||
|
||||
@impl true
|
||||
|
@ -200,6 +208,30 @@ defmodule MatrixServer.RoomServer do
|
|||
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.
|
||||
@spec invite_insert_event(Room.t(), t(), Account.t(), String.t()) ::
|
||||
(() -> {:ok, t()} | {:error, atom()})
|
||||
|
|
|
@ -312,9 +312,6 @@ defmodule MatrixServer.StateResolution.Authorization do
|
|||
|> Repo.all()
|
||||
|> Enum.reduce(%{}, &update_state_set/2)
|
||||
|
||||
IO.inspect(event)
|
||||
IO.inspect(state_set)
|
||||
|
||||
authorized?(event, state_set)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue