Skip to content

Commit

Permalink
spaces: improve spaces support
Browse files Browse the repository at this point in the history
  • Loading branch information
sumnerevans committed Sep 13, 2022
1 parent e62020d commit 3d4afcd
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 21 deletions.
59 changes: 59 additions & 0 deletions linkedin_matrix/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,18 @@ async def _update_participants(
if puppet.li_member_urn != self.li_receiver_urn or puppet.is_real_user:
await puppet.intent_for(self).ensure_joined(self.mxid, bot=self.main_intent)

if source.space_mxid:
try:
await self.az.intent.invite_user(
source.space_mxid, puppet.custom_mxid or puppet.mxid
)
await puppet.intent.join_room_by_id(source.space_mxid)
except Exception as e:
self.log.warning(
f"Failed to invite and join puppet {puppet.li_member_urn} to "
f"space {source.space_mxid}: {e}"
)

return changed

# endregion
Expand Down Expand Up @@ -592,6 +604,18 @@ async def _create_matrix_room(
except Exception:
self.log.warning(f"Failed to add bridge bot to new private chat {self.mxid}")

if source.space_mxid:
try:
await self.az.intent.send_state_event(
source.space_mxid,
EventType.SPACE_CHILD,
{"via": [self.config["homeserver.domain"]], "suggested": True},
state_key=str(self.mxid),
)
await self.az.intent.invite_user(source.space_mxid, source.mxid)
except Exception:
self.log.warning(f"Failed to add chat {self.mxid} to user's space")

await self.save()
self.log.debug(f"Matrix room created: {self.mxid}")
self.by_mxid[self.mxid] = self
Expand Down Expand Up @@ -627,6 +651,25 @@ async def _create_matrix_room(
levels.events_default = 50
await self.main_intent.set_power_levels(self.mxid, levels)

puppet = await p.Puppet.get_by_custom_mxid(source.mxid)
if puppet:
try:
did_join = await puppet.intent.join_room_by_id(self.mxid)
if did_join:
await source.update_direct_chats(
{self.main_intent.mxid: [self.mxid]}
)
if source.space_mxid:
await self.az.intent.invite_user(
source.space_mxid, puppet.custom_mxid
)
await puppet.intent.join_room_by_id(source.space_mxid)
except MatrixError:
self.log.debug(
"Failed to join custom puppet into newly created portal",
exc_info=True,
)

await self._update_participants(source, conversation)

try:
Expand All @@ -652,6 +695,14 @@ async def _update_matrix_room(
)
if puppet and puppet.is_real_user:
await puppet.intent.ensure_joined(self.mxid)

if source.space_mxid and self.mxid:
await self.az.intent.send_state_event(
source.space_mxid,
EventType.SPACE_CHILD,
{"via": [self.config["homeserver.domain"]], "suggested": True},
state_key=str(self.mxid),
)
await self.update_info(source, conversation)

@property
Expand Down Expand Up @@ -850,6 +901,14 @@ async def handle_matrix_leave(self, user: "u.User"):
f"{user.mxid} was the recipient of this portal. " "Cleaning up and deleting..."
)
await self.cleanup_and_delete()

if user.space_mxid:
await self.az.intent.send_state_event(
user.space_mxid,
EventType.SPACE_CHILD,
{},
state_key=str(self.mxid),
)
else:
self.log.debug(f"{user.mxid} left portal to {self.li_other_user_urn}")

Expand Down
60 changes: 39 additions & 21 deletions linkedin_matrix/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ async def post_login(self):
self.user_profile_cache = None
self.log.exception("Failed to automatically enable custom puppet")

await self._create_space()
await self._create_or_update_space()
await self.sync_threads()
self.start_listen()

Expand Down Expand Up @@ -304,28 +304,46 @@ async def logout(self):

# Spaces support

async def _create_space(self):
async def _create_or_update_space(self):
if not self.config["bridge.enable_space_per_user"]:
return
self.log.debug(f"Creating space for {self.li_member_urn}, inviting {self.mxid}")
room = await self.az.intent.create_room(
is_direct=False,
invitees=[self.mxid],
creation_content={"type": "m.space"},
initial_state=[
{
"type": str(EventType.ROOM_AVATAR),
"content": {"avatar_url": self.config["appservice.bot_avatar"]},
},
],
)
self.space_mxid = room
await self.save()
self.log.debug(f"Created space {room}")
try:
await self.az.intent.ensure_joined(room)
except Exception:
self.log.warning(f"Failed to add bridge bot to new space {room}")

avatar_state_event_content = {"url": self.config["appservice.bot_avatar"]}
name_state_event_content = {"name": "LinkedIn"} # TODO template

if self.space_mxid:
await self.az.intent.send_state_event(
self.space_mxid, EventType.ROOM_AVATAR, avatar_state_event_content
)
await self.az.intent.send_state_event(
self.space_mxid, EventType.ROOM_NAME, name_state_event_content
)
else:
self.log.debug(
f"Creating space for {self.li_member_urn}, inviting {self.mxid}"
)
room = await self.az.intent.create_room(
is_direct=False,
invitees=[self.mxid],
creation_content={"type": "m.space"},
initial_state=[
{
"type": str(EventType.ROOM_NAME),
"content": name_state_event_content,
},
{
"type": str(EventType.ROOM_AVATAR),
"content": avatar_state_event_content,
},
],
)
self.space_mxid = room
await self.save()
self.log.debug(f"Created space {room}")
try:
await self.az.intent.ensure_joined(room)
except Exception:
self.log.warning(f"Failed to add bridge bot to new space {room}")

# endregion

Expand Down

0 comments on commit 3d4afcd

Please sign in to comment.