Implement client kick endpoint

This commit is contained in:
Pim Kunis 2021-08-26 14:32:24 +02:00
parent 07d3326c07
commit bb7c3b07e9
7 changed files with 127 additions and 17 deletions

View file

@ -15,7 +15,7 @@ defmodule MatrixServer.RoomServer do
alias MatrixServer.{Repo, Room, Event, StateResolution, Account, JoinedRoom} alias MatrixServer.{Repo, Room, Event, StateResolution, Account, JoinedRoom}
alias MatrixServer.StateResolution.Authorization alias MatrixServer.StateResolution.Authorization
alias MatrixServerWeb.Client.Request.CreateRoom alias MatrixServerWeb.Client.Request.{CreateRoom, Kick}
@registry MatrixServer.RoomServer.Registry @registry MatrixServer.RoomServer.Registry
@supervisor MatrixServer.RoomServer.Supervisor @supervisor MatrixServer.RoomServer.Supervisor
@ -122,6 +122,14 @@ defmodule MatrixServer.RoomServer do
GenServer.call(pid, {:leave, account}) GenServer.call(pid, {:leave, account})
end end
@doc """
Kick a user from this room.
"""
@spec kick(pid(), Account.t(), Kick.t()) :: :ok | {:error, atom()}
def kick(pid, account, request) do
GenServer.call(pid, {:kick, account, request})
end
### Implementation ### Implementation
@impl true @impl true
@ -149,9 +157,14 @@ defmodule MatrixServer.RoomServer do
) do ) do
# TODO: power_level_content_override, initial_state, invite, invite_3pid # TODO: power_level_content_override, initial_state, invite, invite_3pid
case Repo.transaction(create_room_insert_events(room, account, input)) do case Repo.transaction(create_room_insert_events(room, account, input)) do
{:ok, state_set} -> {:reply, {:ok, room_id}, %{state | state_set: state_set}} {:ok, {state_set, room}} ->
{:error, reason} -> {:reply, {:error, reason}, state} {:reply, {:ok, room_id}, %{state | state_set: state_set, room: room}}
_ -> {:reply, {:error, :unknown}, state}
{:error, reason} ->
{:reply, {:error, reason}, state}
_ ->
{:reply, {:error, :unknown}, state}
end end
end end
@ -212,7 +225,7 @@ defmodule MatrixServer.RoomServer do
invite_event = Event.invite(room, account, user_id) invite_event = Event.invite(room, account, user_id)
case insert_single_event(room, state_set, invite_event) do case insert_single_event(room, state_set, invite_event) do
{:ok, state_set} -> {:reply, :ok, %{state | state_set: state_set}} {:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}}
{:error, reason} -> {:reply, {:error, reason}, state} {:error, reason} -> {:reply, {:error, reason}, state}
end end
end end
@ -225,8 +238,11 @@ defmodule MatrixServer.RoomServer do
join_event = Event.join(room, account) join_event = Event.join(room, account)
case insert_single_event(room, state_set, join_event) do case insert_single_event(room, state_set, join_event) do
{:ok, state_set} -> {:reply, {:ok, room_id}, %{state | state_set: state_set}} {:ok, {state_set, room}} ->
{:error, reason} -> {:reply, {:error, reason}, state} {:reply, {:ok, room_id}, %{state | state_set: state_set, room: room}}
{:error, reason} ->
{:reply, {:error, reason}, state}
end end
end end
@ -234,18 +250,31 @@ defmodule MatrixServer.RoomServer do
leave_event = Event.leave(room, account) leave_event = Event.leave(room, account)
case insert_single_event(room, state_set, leave_event) do case insert_single_event(room, state_set, leave_event) do
{:ok, state_set} -> {:reply, :ok, %{state | state_set: state_set}} {:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}}
{:error, reason} -> {:reply, {:error, reason}, state} {:error, reason} -> {:reply, {:error, reason}, state}
end end
end end
@spec insert_single_event(Room.t(), t(), Event.t()) :: {:ok, t()} | {:error, atom()} def handle_call(
{:kick, account, %Kick{user_id: user_id, reason: reason}},
_from,
%{room: room, state_set: state_set} = state
) do
kick_event = Event.kick(room, account, user_id, reason)
case insert_single_event(room, state_set, kick_event) do
{:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}}
{:error, reason} -> {:reply, {:error, reason}, state}
end
end
@spec insert_single_event(Room.t(), t(), Event.t()) :: {:ok, t(), Room.t()} | {:error, atom()}
defp insert_single_event(room, state_set, event) do defp insert_single_event(room, state_set, event) do
Repo.transaction(fn -> Repo.transaction(fn ->
case finalize_and_insert_event(event, state_set, room) do case finalize_and_insert_event(event, state_set, room) do
{:ok, state_set, room} -> {:ok, state_set, room} ->
_ = update_room_state_set(room, state_set) _ = update_room_state_set(room, state_set)
state_set {state_set, room}
{:error, reason} -> {:error, reason} ->
Repo.rollback(reason) Repo.rollback(reason)
@ -255,7 +284,7 @@ defmodule MatrixServer.RoomServer do
# Get a function that inserts all events for room creation. # Get a function that inserts all events for room creation.
@spec create_room_insert_events(Room.t(), Account.t(), CreateRoom.t()) :: @spec create_room_insert_events(Room.t(), Account.t(), CreateRoom.t()) ::
(() -> {:ok, t()} | {:error, atom()}) (() -> {:ok, t(), Room.t()} | {:error, atom()})
defp create_room_insert_events(room, account, %CreateRoom{ defp create_room_insert_events(room, account, %CreateRoom{
room_version: room_version, room_version: room_version,
preset: preset, preset: preset,
@ -290,7 +319,7 @@ defmodule MatrixServer.RoomServer do
{state_set, room} -> {state_set, room} ->
_ = update_room_state_set(room, state_set) _ = update_room_state_set(room, state_set)
state_set {state_set, room}
end end
end end
end end
@ -298,6 +327,9 @@ defmodule MatrixServer.RoomServer do
# Update the given room in the database with the given state set. # Update the given room in the database with the given state set.
@spec update_room_state_set(Room.t(), t()) :: Room.t() @spec update_room_state_set(Room.t(), t()) :: Room.t()
defp update_room_state_set(room, state_set) do defp update_room_state_set(room, state_set) do
# TODO: We might as well hold state in the Room struct,
# instead of the state_set state.
# 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} ->
[type, state_key, event.event_id] [type, state_key, event.event_id]

View file

@ -213,6 +213,19 @@ defmodule MatrixServer.Event do
} }
end end
@spec kick(Room.t(), Account.t(), String.t(), String.t() | nil) :: t()
def kick(room, sender, user_id, reason \\ nil) do
content = %{"membership" => "leave"}
content = if reason, do: Map.put(content, "reason", reason), else: content
%Event{
new(room, sender)
| type: "m.room.member",
state_key: user_id,
content: content
}
end
@spec is_control_event(t()) :: boolean() @spec is_control_event(t()) :: boolean()
def is_control_event(%Event{type: "m.room.power_levels", state_key: ""}), do: true def is_control_event(%Event{type: "m.room.power_levels", state_key: ""}), do: true
def is_control_event(%Event{type: "m.room.join_rules", state_key: ""}), do: true def is_control_event(%Event{type: "m.room.join_rules", state_key: ""}), do: true

View file

@ -305,7 +305,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)
authorized?(event, state_set) authorized?(event, state_set)
end end
end end

View file

@ -6,7 +6,7 @@ defmodule MatrixServerWeb.Client.RoomController do
alias MatrixServer.{Repo, Room, RoomServer} alias MatrixServer.{Repo, Room, RoomServer}
alias MatrixServer.Types.UserId alias MatrixServer.Types.UserId
alias MatrixServerWeb.Client.Request.CreateRoom alias MatrixServerWeb.Client.Request.{CreateRoom, Kick}
alias Ecto.Changeset alias Ecto.Changeset
alias Plug.Conn alias Plug.Conn
@ -111,8 +111,6 @@ defmodule MatrixServerWeb.Client.RoomController do
end end
end end
def join(conn, _), do: put_error(conn, :missing_param)
@doc """ @doc """
This API stops a user participating in a particular room. This API stops a user participating in a particular room.
@ -136,5 +134,26 @@ defmodule MatrixServerWeb.Client.RoomController do
end end
end end
def leave(conn, _), do: put_error(conn, :missing_param) @doc """
Kick a user from the room.
Action for POST /_matrix/client/r0/rooms/{roomId}/kick.
"""
def kick(%Conn{assigns: %{account: account}} = conn, %{"room_id" => room_id} = params) do
with {:ok, request} <- Kick.parse(params),
{:ok, pid} <- RoomServer.get_room_server(room_id) do
case RoomServer.kick(pid, account, request) do
:ok ->
conn
|> send_resp(200, [])
|> halt()
{:error, _} ->
put_error(conn, :unknown)
end
else
{:error, %Ecto.Changeset{}} -> put_error(conn, :bad_json)
{:error, :not_found} -> put_error(conn, :not_found, "Room not found.")
end
end
end end

View file

@ -0,0 +1,20 @@
defmodule MatrixServerWeb.Client.Request.Kick do
use MatrixServerWeb.Request
@type t :: %__MODULE__{
user_id: String.t(),
reason: String.t() | nil
}
@primary_key false
embedded_schema do
field :user_id, :string
field :reason, :string
end
def changeset(data, params) do
data
|> cast(params, [:user_id, :reason])
|> validate_required([:user_id])
end
end

View file

@ -0,0 +1,26 @@
defmodule MatrixServerWeb.Request do
import Ecto.Changeset
alias Ecto.Changeset
@spec parse(module(), map()) :: {:ok, struct()} | {:error, Changeset.t()}
def parse(module, params) do
case apply(module, :changeset, [struct(module), params]) do
%Ecto.Changeset{valid?: true} = cs -> {:ok, apply_changes(cs)}
cs -> {:error, cs}
end
end
defmacro __using__(_opts) do
quote do
use Ecto.Schema
import Ecto.Changeset
@spec parse(map()) :: {:ok, struct()} | {:error, Changeset.t()}
def parse(params) do
MatrixServerWeb.Request.parse(__MODULE__, params)
end
end
end
end

View file

@ -61,6 +61,7 @@ defmodule MatrixServerWeb.Router do
post "/invite", RoomController, :invite post "/invite", RoomController, :invite
post "/join", RoomController, :join post "/join", RoomController, :join
post "/leave", RoomController, :leave post "/leave", RoomController, :leave
post "/kick", RoomController, :kick
end end
end end
end end