Skip to content

Commit

Permalink
spaces: implement basic spaces support
Browse files Browse the repository at this point in the history
Added a new field to the user in the the database pointing to their
space
  • Loading branch information
sumnerevans committed Sep 13, 2022
1 parent fdf2fe8 commit e62020d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 12 deletions.
1 change: 1 addition & 0 deletions linkedin_matrix/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def do_update(self, helper: ConfigUpdateHelper):
copy("bridge.displayname_template")
copy("bridge.double_puppet_allow_discovery")
copy("bridge.double_puppet_server_map")
copy("bridge.enable_space_per_user")
copy("bridge.federate_rooms")
copy("bridge.initial_chat_sync")
copy("bridge.invite_own_puppet_to_pm")
Expand Down
2 changes: 2 additions & 0 deletions linkedin_matrix/db/upgrade/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
v03_add_topic_to_portal,
v04_add_portal_meta_set,
v05_add_index_to_reaction,
v06_add_space_mxid_to_user,
)

__all__ = (
Expand All @@ -16,4 +17,5 @@
"v03_add_topic_to_portal",
"v04_add_portal_meta_set",
"v05_add_index_to_reaction",
"v06_add_space_mxid_to_user",
)
15 changes: 15 additions & 0 deletions linkedin_matrix/db/upgrade/v06_add_space_mxid_to_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from asyncpg import Connection

from . import upgrade_table


@upgrade_table.register(description="Add space MXID to User")
async def upgrade_v6(conn: Connection):
create_table_queries = [
"""
ALTER TABLE "user" ADD COLUMN space_mxid TEXT
""",
]

for query in create_table_queries:
await conn.execute(query)
14 changes: 12 additions & 2 deletions linkedin_matrix/db/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ class User(Model):
mxid: UserID
li_member_urn: Optional[URN]
notice_room: Optional[RoomID]
space_mxid: Optional[RoomID]

client: Optional[LinkedInMessaging]

_table_name = "user"
_field_list = ["mxid", "li_member_urn", "client_pickle", "notice_room"]
_field_list = [
"mxid",
"li_member_urn",
"client_pickle",
"notice_room",
"space_mxid",
]

@property
def _client_pickle(self) -> Optional[bytes]:
Expand Down Expand Up @@ -63,6 +70,7 @@ async def insert(self):
self.li_member_urn.id_str() if self.li_member_urn else None,
self._client_pickle,
self.notice_room,
self.space_mxid,
)

async def delete(self):
Expand All @@ -73,7 +81,8 @@ async def save(self):
UPDATE "user"
SET li_member_urn=$2,
client_pickle=$3,
notice_room=$4
notice_room=$4,
space_mxid=$5
WHERE mxid=$1
"""
await self.db.execute(
Expand All @@ -82,4 +91,5 @@ async def save(self):
self.li_member_urn.id_str() if self.li_member_urn else None,
self._client_pickle,
self.notice_room,
self.space_mxid,
)
10 changes: 3 additions & 7 deletions linkedin_matrix/example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,9 @@ bridge:
# Localpart template of MXIDs for LinkedIn users.
# {userid} is replaced with the user ID of the LinkedIn user.
username_template: "linkedin_{userid}"
# Localpart template for per-user room grouping community IDs.
# The bridge will create these communities and add all of the specific user's portals to the community.
# {localpart} is the MXID localpart and {server} is the MXID server part of the user.
# (Note that, by default, non-admins might not have your homeserver's permission to create
# communities. You should set `enable_group_creation: true` in homeserver.yaml to fix this.)
# `linkedin_{localpart}={server}` is a good value.
community_template: null
# Whether or not to enable creating a space per user and inviting the user
# (as well as all of the puppets) to that space.
enable_space_per_user: true
# Displayname template for LinkedIn users.
# {displayname} is replaced with the display name of the LinkedIn user
# as defined below in displayname_preference.
Expand Down
37 changes: 34 additions & 3 deletions linkedin_matrix/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

from mautrix.bridge import BaseUser, async_getter_lock
from mautrix.errors import MNotFound
from mautrix.types import PushActionType, PushRuleKind, PushRuleScope, RoomID, UserID
from mautrix.types import EventType, PushActionType, PushRuleKind, PushRuleScope, RoomID, UserID
from mautrix.util.bridge_state import BridgeState, BridgeStateEvent
from mautrix.util.opt_prometheus import Gauge, Summary, async_time
from mautrix.util.opt_prometheus import async_time, Gauge, Summary
from mautrix.util.simple_lock import SimpleLock

from . import portal as po, puppet as pu
Expand Down Expand Up @@ -60,10 +60,12 @@ def __init__(
li_member_urn: Optional[URN] = None,
client: Optional[LinkedInMessaging] = None,
notice_room: Optional[RoomID] = None,
space_mxid: Optional[RoomID] = None,
):
super().__init__(mxid, li_member_urn, notice_room, client)
super().__init__(mxid, li_member_urn, notice_room, space_mxid, client)
BaseUser.__init__(self)
self.notice_room = notice_room
self.space_mxid = space_mxid
self._notice_room_lock = asyncio.Lock()
self._notice_send_lock = asyncio.Lock()

Expand Down Expand Up @@ -267,6 +269,8 @@ async def post_login(self):
except Exception:
self.user_profile_cache = None
self.log.exception("Failed to automatically enable custom puppet")

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

Expand Down Expand Up @@ -298,6 +302,33 @@ async def logout(self):

# endregion

# Spaces support

async def _create_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}")

# endregion

# region Thread Syncing

async def get_direct_chats(self) -> dict[UserID, list[RoomID]]:
Expand Down

0 comments on commit e62020d

Please sign in to comment.