From 8dfd770fd00f357fbb38350e615c82a8260d51f9 Mon Sep 17 00:00:00 2001 From: Pim Kunis Date: Sat, 11 Sep 2021 18:23:51 +0200 Subject: [PATCH] Implement creation_content option for client room creation --- README.md | 2 +- lib/architex/room_server.ex | 5 ++-- lib/architex/schema/event/generators.ex | 25 ++++++++++++++----- .../api_schemas/client/request/create_room.ex | 8 +++--- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 317a3c7..5290f5c 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ Here, implemented and some unimplemented features are listed. - GET /_matrix/client/r0/login - POST /_matrix/client/r0/login: Only with password flow. - POST /_matrix/client/r0/register: Only with dummy flow. -- POST /_matrix/client/r0/createRoom: Except creation_content, initial_state, invite_3pid and initial_state. +- POST /_matrix/client/r0/createRoom: Except with options invite_3pid, initial_state and room_alias_name. - GET /_matrix/client/r0/joined_rooms - POST /_matrix/client/r0/rooms/{roomId}/invite - POST /_matrix/client/r0/rooms/{roomId}/join: Except with third party invite. diff --git a/lib/architex/room_server.ex b/lib/architex/room_server.ex index c7a6c32..c74a8a0 100644 --- a/lib/architex/room_server.ex +++ b/lib/architex/room_server.ex @@ -449,11 +449,12 @@ defmodule Architex.RoomServer do topic: topic, invite: invite, power_level_content_override: power_level_content_override, - is_direct: is_direct + is_direct: is_direct, + creation_content: creation_content }) do events = ([ - Event.CreateRoom.new(room, account, room_version), + Event.CreateRoom.new(room, account, room_version, creation_content), Event.Join.new(room, account), Event.PowerLevels.create_room_new(room, account, power_level_content_override) ] ++ diff --git a/lib/architex/schema/event/generators.ex b/lib/architex/schema/event/generators.ex index 4edb6fd..37bb9c6 100644 --- a/lib/architex/schema/event/generators.ex +++ b/lib/architex/schema/event/generators.ex @@ -19,18 +19,31 @@ end defmodule Architex.Event.CreateRoom do alias Architex.{Event, Account, Room} - @spec new(Room.t(), Account.t(), String.t()) :: %Event{} - def new(room, %Account{localpart: localpart} = creator, room_version) do + @clobber_content_keys ["creator", "room_version"] + + @spec new(Room.t(), Account.t(), String.t(), %{optional(String.t()) => any()} | nil) :: %Event{} + def new(room, %Account{localpart: localpart} = creator, room_version, creation_content) do mxid = Architex.get_mxid(localpart) + content = %{ + "creator" => mxid, + "room_version" => room_version || Architex.default_room_version() + } + + content = + if creation_content do + creation_content + |> Map.drop(@clobber_content_keys) + |> Map.merge(content) + else + content + end + %Event{ Event.new(room, creator) | type: "m.room.create", state_key: "", - content: %{ - "creator" => mxid, - "room_version" => room_version || Architex.default_room_version() - } + content: content } end end diff --git a/lib/architex_web/api_schemas/client/request/create_room.ex b/lib/architex_web/api_schemas/client/request/create_room.ex index f84ec95..95a4f85 100644 --- a/lib/architex_web/api_schemas/client/request/create_room.ex +++ b/lib/architex_web/api_schemas/client/request/create_room.ex @@ -10,6 +10,7 @@ defmodule ArchitexWeb.Client.Request.CreateRoom do room_version: String.t() | nil, preset: String.t() | nil, is_direct: boolean() | nil, + creation_content: %{optional(String.t()) => any()} | nil, power_level_content_override: plco_t() | nil } @@ -40,6 +41,7 @@ defmodule ArchitexWeb.Client.Request.CreateRoom do field :room_version, :string field :preset, :string field :is_direct, :boolean + field :creation_content, :map embeds_one :power_level_content_override, PowerLevelContentOverride, primary_key: false do field :ban, :integer @@ -57,8 +59,7 @@ defmodule ArchitexWeb.Client.Request.CreateRoom do end end - # TODO: unimplemented: - # creation_content, initial_state, invite_3pid, initial_state + # TODO: unimplemented: invite_3pid, initial_state, room_alias_name end def changeset(data, params) do @@ -71,7 +72,8 @@ defmodule ArchitexWeb.Client.Request.CreateRoom do :invite, :room_version, :preset, - :is_direct + :is_direct, + :creation_content ]) |> cast_embed(:power_level_content_override, with: &power_level_content_override_changeset/2,