From d23a42e29cdeb7c94bffbc5ec0cbd45ee99ffd4f Mon Sep 17 00:00:00 2001 From: Pim Kunis Date: Sat, 11 Sep 2021 17:38:25 +0200 Subject: [PATCH] Implement is_direct option for client room creation --- README.md | 2 +- lib/architex/room_server.ex | 15 ++++++++------- lib/architex/schema/event/generators.ex | 11 ++++++----- .../api_schemas/client/request/create_room.ex | 7 +++++-- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 8b1044f..317a3c7 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: Only with optional parameters name, topic, preset and invite. +- POST /_matrix/client/r0/createRoom: Except creation_content, initial_state, invite_3pid and initial_state. - 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 fe25ee4..c7a6c32 100644 --- a/lib/architex/room_server.ex +++ b/lib/architex/room_server.ex @@ -448,9 +448,9 @@ defmodule Architex.RoomServer do name: name, topic: topic, invite: invite, - power_level_content_override: power_level_content_override + power_level_content_override: power_level_content_override, + is_direct: is_direct }) do - events = ([ Event.CreateRoom.new(room, account, room_version), @@ -461,7 +461,7 @@ defmodule Architex.RoomServer do [ if(name, do: Event.Name.new(room, account, name)), if(topic, do: Event.Topic.new(room, account, topic)) - ] ++ room_creation_invite_events(account, invite, room)) + ] ++ room_creation_invite_events(account, invite, room, is_direct)) |> Enum.reject(&Kernel.is_nil/1) fn -> @@ -528,11 +528,12 @@ defmodule Architex.RoomServer do 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: [] + @spec room_creation_invite_events(Account.t(), [String.t()] | nil, Room.t(), boolean() | nil) :: + [%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)) + defp room_creation_invite_events(account, invite_user_ids, room, is_direct) do + Enum.map(invite_user_ids, &Event.Invite.new(room, account, &1, is_direct)) end # Finalize the event struct and insert it into the room's state using state resolution. diff --git a/lib/architex/schema/event/generators.ex b/lib/architex/schema/event/generators.ex index 24e40aa..4edb6fd 100644 --- a/lib/architex/schema/event/generators.ex +++ b/lib/architex/schema/event/generators.ex @@ -180,15 +180,16 @@ end defmodule Architex.Event.Invite do alias Architex.{Event, Account, Room} - @spec new(Room.t(), Account.t(), String.t()) :: %Event{} - def new(room, sender, user_id) do + @spec new(Room.t(), Account.t(), String.t(), boolean() | nil) :: %Event{} + def new(room, sender, user_id, is_direct \\ nil) do + content = %{"membership" => "invite"} + content = if is_direct != nil, do: Map.put(content, "is_direct", is_direct), else: content + %Event{ Event.new(room, sender) | type: "m.room.member", state_key: user_id, - content: %{ - "membership" => "invite" - } + 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 4a57ec3..f84ec95 100644 --- a/lib/architex_web/api_schemas/client/request/create_room.ex +++ b/lib/architex_web/api_schemas/client/request/create_room.ex @@ -9,6 +9,7 @@ defmodule ArchitexWeb.Client.Request.CreateRoom do invite: list(String.t()) | nil, room_version: String.t() | nil, preset: String.t() | nil, + is_direct: boolean() | nil, power_level_content_override: plco_t() | nil } @@ -38,6 +39,7 @@ defmodule ArchitexWeb.Client.Request.CreateRoom do field :invite, {:array, :string} field :room_version, :string field :preset, :string + field :is_direct, :boolean embeds_one :power_level_content_override, PowerLevelContentOverride, primary_key: false do field :ban, :integer @@ -56,7 +58,7 @@ defmodule ArchitexWeb.Client.Request.CreateRoom do end # TODO: unimplemented: - # creation_content, initial_state, invite_3pid, initial_state, is_direct + # creation_content, initial_state, invite_3pid, initial_state end def changeset(data, params) do @@ -68,7 +70,8 @@ defmodule ArchitexWeb.Client.Request.CreateRoom do :topic, :invite, :room_version, - :preset + :preset, + :is_direct ]) |> cast_embed(:power_level_content_override, with: &power_level_content_override_changeset/2,