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
1 changed files with 11 additions and 18 deletions

View File

@ -42,8 +42,6 @@ from mautrix.types import (
EventType, EventType,
FileInfo, FileInfo,
ImageInfo, ImageInfo,
JoinRule,
JoinRulesStateEventContent,
LocationMessageEventContent, LocationMessageEventContent,
MediaInfo, MediaInfo,
MediaMessageEventContent, MediaMessageEventContent,
@ -55,6 +53,7 @@ from mautrix.types import (
RoomNameStateEventContent, RoomNameStateEventContent,
RoomTopicStateEventContent, RoomTopicStateEventContent,
RelationType, RelationType,
RoomCreatePreset,
RoomID, RoomID,
StateEvent, StateEvent,
StateEventContent, 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! # 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) await self._update_participants(source, info.participantInfo)
name: str | None = None name: str | None = None
description: str | None = None
initial_state = [ initial_state = [
{ {
"type": str(StateBridge), "type": str(StateBridge),
@ -841,26 +841,15 @@ class Portal(DBPortal, BasePortal):
] ]
if self.is_open: if self.is_open:
initial_state.extend(( preset = RoomCreatePreset.PUBLIC
{
"type": str(EventType.ROOM_JOIN_RULES),
"content": JoinRulesStateEventContent(join_rule=JoinRule.PUBLIC).serialize(),
},
{
"type": "m.room.guest_access",
"content": {"guest_access": "forbidden"},
},
))
# TODO Find whether perms apply to any non-direct channel, or just open ones # 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) 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 # 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]) user_power_levels[self.main_intent.mxid] = max(100, 1 + FROM_PERM_MAP[OpenChannelUserPerm.OWNER])
initial_state.append( power_level_override = PowerLevelStateEventContent(users=user_power_levels)
{ else:
"type": str(EventType.ROOM_POWER_LEVELS), preset = RoomCreatePreset.PRIVATE
"content": PowerLevelStateEventContent(users=user_power_levels).serialize() power_level_override = None
}
)
invites = [] invites = []
if self.config["bridge.encryption.default"] and self.matrix.e2ee: 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: if self.encrypted or not self.is_direct:
name = self.name name = self.name
description = self.description
initial_state.append( initial_state.append(
{ {
"type": str(EventType.ROOM_AVATAR), "type": str(EventType.ROOM_AVATAR),
@ -892,11 +882,14 @@ class Portal(DBPortal, BasePortal):
if not self.config["bridge.federate_rooms"]: if not self.config["bridge.federate_rooms"]:
creation_content["m.federate"] = False creation_content["m.federate"] = False
self.mxid = await self.main_intent.create_room( self.mxid = await self.main_intent.create_room(
preset=preset,
name=name, name=name,
topic=description,
is_direct=self.is_direct, is_direct=self.is_direct,
initial_state=initial_state, initial_state=initial_state,
invitees=invites, invitees=invites,
creation_content=creation_content, creation_content=creation_content,
power_level_override=power_level_override,
) )
if not self.mxid: if not self.mxid:
raise Exception("Failed to create room: no mxid returned") raise Exception("Failed to create room: no mxid returned")