diff options
author | Travis Ralston <travpc@gmail.com> | 2018-11-20 13:19:37 -0700 |
---|---|---|
committer | Travis Ralston <travpc@gmail.com> | 2018-11-21 13:03:35 -0700 |
commit | ce1b3936826870909f237c3016367eebdcf078df (patch) | |
tree | 9ab899ad5ba85c0d1799880250c5c61a14c380c3 /synapse/handlers | |
parent | Remove riot.im from the list of trusted Identity Servers in the default confi... (diff) | |
download | synapse-ce1b3936826870909f237c3016367eebdcf078df.tar.xz |
Proof of concept for auto-accepting invites
This is for demonstration purposes only. In practice this would actually look up the right profile and use the right thing, not to mention be in a more reasonable location.
Diffstat (limited to 'synapse/handlers')
-rw-r--r-- | synapse/handlers/federation.py | 31 | ||||
-rw-r--r-- | synapse/handlers/room_member.py | 29 |
2 files changed, 58 insertions, 2 deletions
diff --git a/synapse/handlers/federation.py b/synapse/handlers/federation.py index a3bb864bb2..51741d9095 100644 --- a/synapse/handlers/federation.py +++ b/synapse/handlers/federation.py @@ -54,7 +54,7 @@ from synapse.replication.http.federation import ( ) from synapse.replication.http.membership import ReplicationUserJoinedLeftRoomRestServlet from synapse.state import StateResolutionStore, resolve_events_with_store -from synapse.types import UserID, get_domain_from_id +from synapse.types import UserID, get_domain_from_id, create_requester from synapse.util import logcontext, unwrapFirstError from synapse.util.async_helpers import Linearizer from synapse.util.distributor import user_joined_room @@ -1300,9 +1300,38 @@ class FederationHandler(BaseHandler): context = yield self.state_handler.compute_event_context(event) yield self.persist_events_and_notify([(event, context)]) + sender = UserID.from_string(event.sender) + target = UserID.from_string(event.state_key) + if (sender.localpart == target.localpart): + logcontext.run_in_background( + self._auto_accept_invite, + sender, target, event.room_id, + ) + defer.returnValue(event) @defer.inlineCallbacks + def _auto_accept_invite(self, sender, target, room_id): + joined = False + for attempt in range(0, 10): + try: + yield self.hs.get_room_member_handler().update_membership( + requester=create_requester(target.to_string()), + target=target, + room_id=room_id, + action="join", + ) + joined = True + break + except Exception: + # We're going to retry, but we should log the error + logger.exception("Error auto-accepting invite on attempt %d" % attempt) + yield self.hs.get_clock().sleep(1) + if not joined: + logger.error("Giving up on trying to auto-accept invite: too many attempts") + + + @defer.inlineCallbacks def do_remotely_reject_invite(self, target_hosts, room_id, user_id): origin, event = yield self._make_and_verify_event( target_hosts, diff --git a/synapse/handlers/room_member.py b/synapse/handlers/room_member.py index 07fd3e82fc..219ed78c33 100644 --- a/synapse/handlers/room_member.py +++ b/synapse/handlers/room_member.py @@ -29,7 +29,7 @@ import synapse.server import synapse.types from synapse.api.constants import EventTypes, Membership from synapse.api.errors import AuthError, Codes, SynapseError -from synapse.types import RoomID, UserID +from synapse.types import RoomID, UserID, RoomAlias from synapse.util.async_helpers import Linearizer from synapse.util.distributor import user_joined_room, user_left_room @@ -416,6 +416,7 @@ class RoomMemberHandler(object): ret = yield self._remote_join( requester, remote_room_hosts, room_id, target, content ) + self._send_merged_user_invites(requester, room_id) defer.returnValue(ret) elif effective_membership_state == Membership.LEAVE: @@ -450,9 +451,35 @@ class RoomMemberHandler(object): prev_events_and_hashes=prev_events_and_hashes, content=content, ) + self._send_merged_user_invites(requester, room_id) defer.returnValue(res) @defer.inlineCallbacks + def _send_merged_user_invites(self, requester, room_id): + profile_alias = "#_profile_" + requester.user.localpart + ":" + self.hs.hostname + profile_alias = RoomAlias.from_string(profile_alias) + profile_room_id, remote_room_hosts = yield self.lookup_room_alias(profile_alias) + if profile_room_id: + linked_accounts = yield self.state_handler.get_current_state( + room_id=profile_room_id.to_string(), + event_type="m.linked_accounts", + state_key="", + ) + if not linked_accounts or not linked_accounts.content['all_children']: + return + for child_id in linked_accounts.content['all_children']: + child = UserID.from_string(child_id) + if self.hs.is_mine(child) or child_id == requester.user.to_string(): + # TODO: Handle auto-invite for local users (not a priority) + continue + self.update_membership( + requester=requester, + target=child, + room_id=room_id, + action="invite", + ) + + @defer.inlineCallbacks def send_membership_event( self, requester, |