Skip to content

Commit

Permalink
commands: implement sync-space
Browse files Browse the repository at this point in the history
Closes #83

Signed-off-by: Sumner Evans <sumner@beeper.com>
  • Loading branch information
sumnerevans committed Mar 8, 2024
1 parent 12f42ab commit b7ffff0
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 4 deletions.
Binary file added linkedin.db
Binary file not shown.
3 changes: 2 additions & 1 deletion linkedin_matrix/commands/__init__.py
@@ -1,3 +1,4 @@
from .auth import SECTION_AUTH, login
from .spaces import SECTION_SPACES

__all__ = ("SECTION_AUTH", "login")
__all__ = ("SECTION_AUTH", "SECTION_SPACES", "login")
63 changes: 63 additions & 0 deletions linkedin_matrix/commands/spaces.py
@@ -0,0 +1,63 @@
import logging

from mautrix.bridge.commands import HelpSection, command_handler
from mautrix.types import EventType

from ..portal import Portal
from ..puppet import Puppet
from .typehint import CommandEvent

SECTION_SPACES = HelpSection("Miscellaneous", 30, "")


@command_handler(
needs_auth=True,
management_only=False,
help_section=SECTION_SPACES,
help_text="Synchronize your personal filtering space",
)
async def sync_space(evt: CommandEvent):
if not evt.bridge.config["bridge.space_support.enable"]:
await evt.reply("Spaces are not enabled on this instance of the bridge")
return

await evt.sender.create_or_update_space()

if not evt.sender.space_mxid:
await evt.reply("Failed to create or update space")
return

async for portal in Portal.all():
if not portal.mxid:
logging.debug(f"Portal {portal} has no mxid")
continue
if portal.li_receiver_urn != evt.sender.li_member_urn:
logging.debug(f"Portal {portal} does not belong to {evt.sender}")
continue

logging.debug(f"Adding chat {portal.mxid} to user's space ({evt.sender.space_mxid})")
try:
await evt.bridge.az.intent.send_state_event(
evt.sender.space_mxid,
EventType.SPACE_CHILD,
{"via": [evt.bridge.config["homeserver.domain"]], "suggested": True},
state_key=str(portal.mxid),
)
except Exception:
logging.warning(
f"Failed to add chat {portal.mxid} to user's space ({evt.sender.space_mxid})"
)

if not portal.li_is_group_chat:
logging.debug(f"Adding puppet {portal.li_other_user_urn} to user's space")
puppet = await Puppet.get_by_li_member_urn(portal.li_other_user_urn, create=False)
if not puppet:
continue
try:
await puppet.intent.ensure_joined(evt.sender.space_mxid)
except Exception as e:
logging.warning(
f"Failed to join {puppet.mxid} to user's space ({evt.sender.space_mxid}): {e}"
)

await evt.reply("Synced space")
6 changes: 6 additions & 0 deletions linkedin_matrix/db/puppet.py
Expand Up @@ -58,6 +58,12 @@ def _from_row(cls, row: Record | None) -> Puppet | None:
li_member_urn=URN(li_member_urn),
)

@classmethod
async def all(cls) -> list["Puppet"]:
query = Puppet.select_constructor()
rows = await cls.db.fetch(query)
return [cast(Puppet, cls._from_row(row)) for row in rows if row]

@classmethod
async def get_by_li_member_urn(cls, li_member_urn: URN) -> Puppet | None:
query = Puppet.select_constructor("li_member_urn=$1")
Expand Down
1 change: 0 additions & 1 deletion linkedin_matrix/portal.py
Expand Up @@ -631,7 +631,6 @@ async def _create_matrix_room(
{"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")

Expand Down
12 changes: 10 additions & 2 deletions linkedin_matrix/user.py
Expand Up @@ -289,7 +289,7 @@ async def post_login(self):
self.user_profile_cache = None
self.log.exception("Failed to automatically enable custom puppet")

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

Expand Down Expand Up @@ -326,7 +326,7 @@ async def logout(self):

# Spaces support

async def _create_or_update_space(self):
async def create_or_update_space(self):
if not self.config["bridge.space_support.enable"]:
return

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

# Ensure that the user is invited and joined to the space.
try:
puppet = await pu.Puppet.get_by_custom_mxid(self.mxid)
if puppet and puppet.is_real_user:
await puppet.intent.ensure_joined(self.space_mxid)
except Exception:
self.log.warning(f"Failed to add user to the space {self.space_mxid}")

# endregion

# region Thread Syncing
Expand Down

0 comments on commit b7ffff0

Please sign in to comment.