Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commands: implement sync-space #85

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.vim
docker-requirements.txt
linkedinmatrix.db*
linkedin.db

# Test caches so I don't get banned from LinkedIn
convocache.json
Expand Down
3 changes: 2 additions & 1 deletion linkedin_matrix/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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