Implement power_level_content_override for client room creation

This commit is contained in:
Pim Kunis 2021-09-11 15:11:38 +02:00
parent ee8c821653
commit 8ab147190e
3 changed files with 115 additions and 22 deletions

View file

@ -207,7 +207,6 @@ defmodule Architex.RoomServer do
_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_3pid
case Repo.transaction(create_room_insert_events(room, account, request)) 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}}
@ -448,13 +447,15 @@ defmodule Architex.RoomServer do
preset: preset, preset: preset,
name: name, name: name,
topic: topic, topic: topic,
invite: invite invite: invite,
power_level_content_override: power_level_content_override
}) do }) do
events = events =
([ ([
Event.CreateRoom.new(room, account, room_version), Event.CreateRoom.new(room, account, room_version),
Event.Join.new(room, account), Event.Join.new(room, account),
Event.PowerLevels.new(room, account) Event.PowerLevels.create_room_new(room, account, power_level_content_override)
] ++ ] ++
room_creation_preset(account, preset, room) ++ room_creation_preset(account, preset, room) ++
[ [

View file

@ -37,30 +37,61 @@ end
defmodule Architex.Event.PowerLevels do defmodule Architex.Event.PowerLevels do
alias Architex.{Event, Account, Room} alias Architex.{Event, Account, Room}
alias ArchitexWeb.Client.Request.CreateRoom
alias ArchitexWeb.Client.Request.CreateRoom.PowerLevelContentOverride
@spec new(Room.t(), Account.t()) :: %Event{} @ban 50
def new(room, %Account{localpart: localpart} = sender) do @events_default 0
@invite 50
@kick 50
@redact 50
@state_default 50
@creator 50
@users_default 0
@notifications_room 50
@spec create_room_new(Room.t(), Account.t(), CreateRoom.plco_t()) :: %Event{}
def create_room_new(room, sender, nil) do
create_room_new(room, sender, %PowerLevelContentOverride{})
end
def create_room_new(room, %Account{localpart: localpart} = sender, %PowerLevelContentOverride{
ban: ban_override,
events: events_override,
events_default: events_default_override,
invite: invite_override,
kick: kick_override,
redact: redact_override,
state_default: state_default_override,
users: users_override,
users_default: users_default_override,
notifications: notifications_override
}) do
mxid = Architex.get_mxid(localpart) mxid = Architex.get_mxid(localpart)
users = %{mxid => @creator}
users = if users_override, do: Map.merge(users, users_override), else: users
notifications =
case notifications_override do
%{room: room} -> %{"room" => room}
_ -> %{"room" => @notifications_room}
end
%Event{ %Event{
Event.new(room, sender) Event.new(room, sender)
| type: "m.room.power_levels", | type: "m.room.power_levels",
state_key: "", state_key: "",
content: %{ content: %{
"ban" => 50, "ban" => ban_override || @ban,
"events" => %{}, "events" => events_override || %{},
"events_default" => 0, "events_default" => events_default_override || @events_default,
"invite" => 50, "invite" => invite_override || @invite,
"kick" => 50, "kick" => kick_override || @kick,
"redact" => 50, "redact" => redact_override || @redact,
"state_default" => 50, "state_default" => state_default_override || @state_default,
"users" => %{ "users" => users,
mxid => 50 "users_default" => users_default_override || @users_default,
}, "notifications" => notifications
"users_default" => 0,
"notifications" => %{
"room" => 50
}
} }
} }
end end

View file

@ -8,7 +8,25 @@ defmodule ArchitexWeb.Client.Request.CreateRoom do
topic: String.t() | nil, topic: String.t() | nil,
invite: list(String.t()) | nil, invite: list(String.t()) | nil,
room_version: String.t() | nil, room_version: String.t() | nil,
preset: String.t() | nil preset: String.t() | nil,
power_level_content_override: plco_t() | nil
}
@type plco_t :: %__MODULE__.PowerLevelContentOverride{
ban: integer() | nil,
events: %{optional(String.t()) => integer()} | nil,
events_default: integer() | nil,
invite: integer() | nil,
kick: integer() | nil,
redact: integer() | nil,
state_default: integer() | nil,
users: %{optional(String.t()) => integer()} | nil,
users_default: integer() | nil,
notifications: plco_n_t() | nil
}
@type plco_n_t :: %__MODULE__.PowerLevelContentOverride.Notifications{
room: integer() | nil
} }
@primary_key false @primary_key false
@ -20,9 +38,25 @@ defmodule ArchitexWeb.Client.Request.CreateRoom do
field :invite, {:array, :string} field :invite, {:array, :string}
field :room_version, :string field :room_version, :string
field :preset, :string field :preset, :string
embeds_one :power_level_content_override, PowerLevelContentOverride, primary_key: false do
field :ban, :integer
field :events, {:map, :integer}
field :events_default, :integer
field :invite, :integer
field :kick, :integer
field :redact, :integer
field :state_default, :integer
field :users, {:map, :integer}
field :users_default, :integer
embeds_one :notifications, Notifications, primary_key: false do
field :room, :integer
end
end
# TODO: unimplemented: # TODO: unimplemented:
# creation_content, initial_state, invite_3pid, initial_state, # creation_content, initial_state, invite_3pid, initial_state, is_direct
# is_direct, power_level_content_override
end end
def changeset(data, params) do def changeset(data, params) do
@ -36,6 +70,33 @@ defmodule ArchitexWeb.Client.Request.CreateRoom do
:room_version, :room_version,
:preset :preset
]) ])
|> cast_embed(:power_level_content_override,
with: &power_level_content_override_changeset/2,
required: false
)
|> validate_inclusion(:preset, ["private_chat", "public_chat", "trusted_private_chat"]) |> validate_inclusion(:preset, ["private_chat", "public_chat", "trusted_private_chat"])
end end
def power_level_content_override_changeset(data, params) do
data
|> cast(params, [
:ban,
:events,
:events_default,
:invite,
:kick,
:redact,
:state_default,
:users,
:users_default
])
|> cast_embed(:notifications,
with: &power_level_content_override_notifications_changeset/2,
required: false
)
end
def power_level_content_override_notifications_changeset(data, params) do
cast(data, params, [:room])
end
end end