Skip to content
This repository was archived by the owner on Apr 19, 2025. It is now read-only.

Commit ad13888

Browse files
committed
commands: implement sync-space
Closes #83 Signed-off-by: Sumner Evans <[email protected]>
1 parent 12f42ab commit ad13888

File tree

6 files changed

+82
-4
lines changed

6 files changed

+82
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
.vim
44
docker-requirements.txt
55
linkedinmatrix.db*
6+
linkedin.db
67

78
# Test caches so I don't get banned from LinkedIn
89
convocache.json
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from .auth import SECTION_AUTH, login
2+
from .spaces import SECTION_SPACES
23

3-
__all__ = ("SECTION_AUTH", "login")
4+
__all__ = ("SECTION_AUTH", "SECTION_SPACES", "login")

linkedin_matrix/commands/spaces.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import logging
2+
3+
from mautrix.bridge.commands import HelpSection, command_handler
4+
from mautrix.types import EventType
5+
6+
from ..portal import Portal
7+
from ..puppet import Puppet
8+
from .typehint import CommandEvent
9+
10+
SECTION_SPACES = HelpSection("Miscellaneous", 30, "")
11+
12+
13+
@command_handler(
14+
needs_auth=True,
15+
management_only=False,
16+
help_section=SECTION_SPACES,
17+
help_text="Synchronize your personal filtering space",
18+
)
19+
async def sync_space(evt: CommandEvent):
20+
if not evt.bridge.config["bridge.space_support.enable"]:
21+
await evt.reply("Spaces are not enabled on this instance of the bridge")
22+
return
23+
24+
await evt.sender.create_or_update_space()
25+
26+
if not evt.sender.space_mxid:
27+
await evt.reply("Failed to create or update space")
28+
return
29+
30+
async for portal in Portal.all():
31+
if not portal.mxid:
32+
logging.debug(f"Portal {portal} has no mxid")
33+
continue
34+
if portal.li_receiver_urn != evt.sender.li_member_urn:
35+
logging.debug(f"Portal {portal} does not belong to {evt.sender}")
36+
continue
37+
38+
logging.debug(f"Adding chat {portal.mxid} to user's space ({evt.sender.space_mxid})")
39+
try:
40+
await evt.bridge.az.intent.send_state_event(
41+
evt.sender.space_mxid,
42+
EventType.SPACE_CHILD,
43+
{"via": [evt.bridge.config["homeserver.domain"]], "suggested": True},
44+
state_key=str(portal.mxid),
45+
)
46+
except Exception:
47+
logging.warning(
48+
f"Failed to add chat {portal.mxid} to user's space ({evt.sender.space_mxid})"
49+
)
50+
51+
if not portal.li_is_group_chat:
52+
logging.debug(f"Adding puppet {portal.li_other_user_urn} to user's space")
53+
puppet = await Puppet.get_by_li_member_urn(portal.li_other_user_urn, create=False)
54+
if not puppet:
55+
continue
56+
try:
57+
await puppet.intent.ensure_joined(evt.sender.space_mxid)
58+
except Exception as e:
59+
logging.warning(
60+
f"Failed to join {puppet.mxid} to user's space ({evt.sender.space_mxid}): {e}"
61+
)
62+
63+
await evt.reply("Synced space")

linkedin_matrix/db/puppet.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ def _from_row(cls, row: Record | None) -> Puppet | None:
5858
li_member_urn=URN(li_member_urn),
5959
)
6060

61+
@classmethod
62+
async def all(cls) -> list["Puppet"]:
63+
query = Puppet.select_constructor()
64+
rows = await cls.db.fetch(query)
65+
return [cast(Puppet, cls._from_row(row)) for row in rows if row]
66+
6167
@classmethod
6268
async def get_by_li_member_urn(cls, li_member_urn: URN) -> Puppet | None:
6369
query = Puppet.select_constructor("li_member_urn=$1")

linkedin_matrix/portal.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,6 @@ async def _create_matrix_room(
631631
{"via": [self.config["homeserver.domain"]], "suggested": True},
632632
state_key=str(self.mxid),
633633
)
634-
await self.az.intent.invite_user(source.space_mxid, source.mxid)
635634
except Exception:
636635
self.log.warning(f"Failed to add chat {self.mxid} to user's space")
637636

linkedin_matrix/user.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ async def post_login(self):
289289
self.user_profile_cache = None
290290
self.log.exception("Failed to automatically enable custom puppet")
291291

292-
await self._create_or_update_space()
292+
await self.create_or_update_space()
293293
await self.sync_threads()
294294
self.start_listen()
295295

@@ -326,7 +326,7 @@ async def logout(self):
326326

327327
# Spaces support
328328

329-
async def _create_or_update_space(self):
329+
async def create_or_update_space(self):
330330
if not self.config["bridge.space_support.enable"]:
331331
return
332332

@@ -365,6 +365,14 @@ async def _create_or_update_space(self):
365365
except Exception:
366366
self.log.warning(f"Failed to add bridge bot to new space {room}")
367367

368+
# Ensure that the user is invited and joined to the space.
369+
try:
370+
puppet = await pu.Puppet.get_by_custom_mxid(self.mxid)
371+
if puppet and puppet.is_real_user:
372+
await puppet.intent.ensure_joined(self.space_mxid)
373+
except Exception:
374+
self.log.warning(f"Failed to add user to the space {self.space_mxid}")
375+
368376
# endregion
369377

370378
# region Thread Syncing

0 commit comments

Comments
 (0)