Implement client ban and unban endpoints

This commit is contained in:
Pim Kunis 2021-08-26 22:09:45 +02:00
parent bb7c3b07e9
commit 2088f0a869
5 changed files with 139 additions and 3 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, Kick} alias MatrixServerWeb.Client.Request.{CreateRoom, Kick, Ban}
@registry MatrixServer.RoomServer.Registry @registry MatrixServer.RoomServer.Registry
@supervisor MatrixServer.RoomServer.Supervisor @supervisor MatrixServer.RoomServer.Supervisor
@ -130,6 +130,22 @@ defmodule MatrixServer.RoomServer do
GenServer.call(pid, {:kick, account, request}) GenServer.call(pid, {:kick, account, request})
end end
@doc """
Ban a user from this room.
"""
@spec ban(pid(), Account.t(), Ban.t()) :: :ok | {:error, atom()}
def ban(pid, account, request) do
GenServer.call(pid, {:ban, account, request})
end
@doc """
Unban a user from this room.
"""
@spec unban(pid(), Account.t(), String.t()) :: :ok | {:error, atom()}
def unban(pid, account, user_id) do
GenServer.call(pid, {:unban, account, user_id})
end
### Implementation ### Implementation
@impl true @impl true
@ -268,7 +284,29 @@ defmodule MatrixServer.RoomServer do
end end
end end
@spec insert_single_event(Room.t(), t(), Event.t()) :: {:ok, t(), Room.t()} | {:error, atom()} def handle_call(
{:ban, account, %Kick{user_id: user_id, reason: reason}},
_from,
%{room: room, state_set: state_set} = state
) do
ban_event = Event.ban(room, account, user_id, reason)
case insert_single_event(room, state_set, ban_event) do
{:ok, {state_set, room}} -> {:reply, :ok, %{state | state_set: state_set, room: room}}
{:error, reason} -> {:reply, {:error, reason}, state}
end
end
def handle_call({:unban, account, user_id},_from,%{room: room, state_set: state_set} = state) do
unban_event = Event.unban(room, account, user_id)
case insert_single_event(room, state_set, unban_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

View file

@ -226,6 +226,31 @@ defmodule MatrixServer.Event do
} }
end end
@spec ban(Room.t(), Account.t(), String.t(), String.t() | nil) :: t()
def ban(room, sender, user_id, reason \\ nil) do
content = %{"membership" => "ban"}
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 unban(Room.t(), Account.t(), String.t()) :: t()
def unban(room, sender, user_id) do
%Event{
new(room, sender)
| type: "m.room.member",
state_key: user_id,
content: %{
"membership" => "leave"
}
}
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

@ -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, Kick} alias MatrixServerWeb.Client.Request.{CreateRoom, Kick, Ban}
alias Ecto.Changeset alias Ecto.Changeset
alias Plug.Conn alias Plug.Conn
@ -156,4 +156,55 @@ defmodule MatrixServerWeb.Client.RoomController do
{:error, :not_found} -> put_error(conn, :not_found, "Room not found.") {:error, :not_found} -> put_error(conn, :not_found, "Room not found.")
end end
end end
@doc """
Ban a user in the room.
Action for POST /_matrix/client/r0/rooms/{roomId}/ban.
"""
def ban(%Conn{assigns: %{account: account}} = conn, %{"room_id" => room_id} = params) do
with {:ok, request} <- Ban.parse(params),
{:ok, pid} <- RoomServer.get_room_server(room_id) do
case RoomServer.ban(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
@doc """
Unban a user from the room.
Action for POST /_matrix/client/r0/rooms/{roomId}/unban.
"""
def unban(%Conn{assigns: %{account: account}} = conn, %{
"room_id" => room_id,
"user_id" => user_id
}) do
case RoomServer.get_room_server(room_id) do
{:ok, pid} ->
case RoomServer.unban(pid, account, user_id) do
:ok ->
conn
|> send_resp(200, [])
|> halt()
{:error, _} ->
put_error(conn, :unknown)
end
{:error, :not_found} ->
put_error(conn, :not_found, "The given room was not found.")
end
end
def unban(conn, _), do: put_error(conn, :missing_param)
end end

View file

@ -0,0 +1,20 @@
defmodule MatrixServerWeb.Client.Request.Ban 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

@ -62,6 +62,8 @@ defmodule MatrixServerWeb.Router do
post "/join", RoomController, :join post "/join", RoomController, :join
post "/leave", RoomController, :leave post "/leave", RoomController, :leave
post "/kick", RoomController, :kick post "/kick", RoomController, :kick
post "/ban", RoomController, :ban
post "/unban", RoomController, :unban
end end
end end
end end