Improve room creation

Handle topics, and use arguments instead of initial_state objects
This commit is contained in:
Andrew Ferrazzutti 2022-04-28 01:39:20 -04:00
parent 2602e58734
commit ee8b0a2fca

View File

@ -42,8 +42,6 @@ from mautrix.types import (
EventType,
FileInfo,
ImageInfo,
JoinRule,
JoinRulesStateEventContent,
LocationMessageEventContent,
MediaInfo,
MediaMessageEventContent,
@ -55,6 +53,7 @@ from mautrix.types import (
RoomNameStateEventContent,
RoomTopicStateEventContent,
RelationType,
RoomCreatePreset,
RoomID,
StateEvent,
StateEventContent,
@ -826,6 +825,7 @@ class Portal(DBPortal, BasePortal):
# NOTE Must do this to find the other member of the DM, since the channel ID != the member's ID!
await self._update_participants(source, info.participantInfo)
name: str | None = None
description: str | None = None
initial_state = [
{
"type": str(StateBridge),
@ -841,26 +841,15 @@ class Portal(DBPortal, BasePortal):
]
if self.is_open:
initial_state.extend((
{
"type": str(EventType.ROOM_JOIN_RULES),
"content": JoinRulesStateEventContent(join_rule=JoinRule.PUBLIC).serialize(),
},
{
"type": "m.room.guest_access",
"content": {"guest_access": "forbidden"},
},
))
preset = RoomCreatePreset.PUBLIC
# TODO Find whether perms apply to any non-direct channel, or just open ones
user_power_levels = await self._get_mapped_participant_power_levels(info.participantInfo.participants)
# NOTE Giving the bot a +1 power level if necessary so it can demote non-puppet admins
user_power_levels[self.main_intent.mxid] = max(100, 1 + FROM_PERM_MAP[OpenChannelUserPerm.OWNER])
initial_state.append(
{
"type": str(EventType.ROOM_POWER_LEVELS),
"content": PowerLevelStateEventContent(users=user_power_levels).serialize()
}
)
power_level_override = PowerLevelStateEventContent(users=user_power_levels)
else:
preset = RoomCreatePreset.PRIVATE
power_level_override = None
invites = []
if self.config["bridge.encryption.default"] and self.matrix.e2ee:
@ -878,6 +867,7 @@ class Portal(DBPortal, BasePortal):
if self.encrypted or not self.is_direct:
name = self.name
description = self.description
initial_state.append(
{
"type": str(EventType.ROOM_AVATAR),
@ -892,11 +882,14 @@ class Portal(DBPortal, BasePortal):
if not self.config["bridge.federate_rooms"]:
creation_content["m.federate"] = False
self.mxid = await self.main_intent.create_room(
preset=preset,
name=name,
topic=description,
is_direct=self.is_direct,
initial_state=initial_state,
invitees=invites,
creation_content=creation_content,
power_level_override=power_level_override,
)
if not self.mxid:
raise Exception("Failed to create room: no mxid returned")