Implement inviting users during room creation

This commit is contained in:
Pim Kunis 2021-09-11 12:39:42 +02:00
parent d4b058e739
commit ee8c821653
3 changed files with 32 additions and 9 deletions

View file

@ -50,7 +50,7 @@ Here, implemented and some unimplemented features are listed.
- GET /_matrix/client/r0/login - GET /_matrix/client/r0/login
- POST /_matrix/client/r0/login: Only with password flow. - POST /_matrix/client/r0/login: Only with password flow.
- POST /_matrix/client/r0/register: Only with dummy flow. - POST /_matrix/client/r0/register: Only with dummy flow.
- POST /_matrix/client/r0/createRoom: Only with optional parameters name, topic and preset. - POST /_matrix/client/r0/createRoom: Only with optional parameters name, topic, preset and invite.
- GET /_matrix/client/r0/joined_rooms - GET /_matrix/client/r0/joined_rooms
- POST /_matrix/client/r0/rooms/{roomId}/invite - POST /_matrix/client/r0/rooms/{roomId}/invite
- POST /_matrix/client/r0/rooms/{roomId}/join: Except with third party invite. - POST /_matrix/client/r0/rooms/{roomId}/join: Except with third party invite.

View file

@ -75,8 +75,8 @@ defmodule Architex.RoomServer do
to the [Matrix documentation](https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-createroom). to the [Matrix documentation](https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-createroom).
""" """
@spec create_room(pid(), Account.t(), CreateRoom.t()) :: {:ok, String.t()} | {:error, atom()} @spec create_room(pid(), Account.t(), CreateRoom.t()) :: {:ok, String.t()} | {:error, atom()}
def create_room(pid, account, input) do def create_room(pid, account, request) do
GenServer.call(pid, {:create_room, account, input}) GenServer.call(pid, {:create_room, account, request})
end end
@doc """ @doc """
@ -203,12 +203,12 @@ defmodule Architex.RoomServer do
@impl true @impl true
def handle_call( def handle_call(
{:create_room, account, input}, {:create_room, account, request},
_from, _from,
%{room: %Room{id: room_id} = room} = state %{room: %Room{id: room_id} = room} = state
) do ) do
# TODO: power_level_content_override, initial_state, invite, invite_3pid # TODO: power_level_content_override, initial_state, invite_3pid
case Repo.transaction(create_room_insert_events(room, account, input)) do case Repo.transaction(create_room_insert_events(room, account, request)) do
{:ok, {state_set, room}} -> {:ok, {state_set, room}} ->
{:reply, {:ok, room_id}, %{state | state_set: state_set, room: room}} {:reply, {:ok, room_id}, %{state | state_set: state_set, room: room}}
@ -447,7 +447,8 @@ defmodule Architex.RoomServer do
room_version: room_version, room_version: room_version,
preset: preset, preset: preset,
name: name, name: name,
topic: topic topic: topic,
invite: invite
}) do }) do
events = events =
([ ([
@ -459,7 +460,7 @@ defmodule Architex.RoomServer do
[ [
if(name, do: Event.Name.new(room, account, name)), if(name, do: Event.Name.new(room, account, name)),
if(topic, do: Event.Topic.new(room, account, topic)) if(topic, do: Event.Topic.new(room, account, topic))
]) ] ++ room_creation_invite_events(account, invite, room))
|> Enum.reject(&Kernel.is_nil/1) |> Enum.reject(&Kernel.is_nil/1)
fn -> fn ->
@ -525,6 +526,14 @@ defmodule Architex.RoomServer do
] ]
end end
# Get the events for room creation for inviting other users.
@spec room_creation_invite_events(Account.t(), [String.t()] | nil, Room.t()) :: [%Event{}]
defp room_creation_invite_events(_, nil, _), do: []
defp room_creation_invite_events(account, invite_user_ids, room) do
Enum.map(invite_user_ids, &Event.Invite.new(room, account, &1))
end
# Finalize the event struct and insert it into the room's state using state resolution. # Finalize the event struct and insert it into the room's state using state resolution.
# The values that are automatically added are: # The values that are automatically added are:
# - Auth events # - Auth events

View file

@ -13,3 +13,17 @@ account
access_token: "sneed" access_token: "sneed"
) )
|> Repo.insert!() |> Repo.insert!()
account =
Repo.insert!(%Account{
localpart: "steamed",
password_hash: Bcrypt.hash_pwd_salt("hams")
})
account
|> Ecto.build_assoc(:devices,
id: "iPhone",
display_name: "My iPhone",
access_token: "hams"
)
|> Repo.insert!()