mirror of
https://github.com/element-hq/synapse.git
synced 2024-12-19 01:21:09 +03:00
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.
This commit is contained in:
parent
2b77c8d50e
commit
de6d002d01
2 changed files with 58 additions and 2 deletions
|
@ -56,7 +56,7 @@ from synapse.replication.http.federation import (
|
||||||
)
|
)
|
||||||
from synapse.replication.http.membership import ReplicationUserJoinedLeftRoomRestServlet
|
from synapse.replication.http.membership import ReplicationUserJoinedLeftRoomRestServlet
|
||||||
from synapse.state import StateResolutionStore, resolve_events_with_store
|
from synapse.state import StateResolutionStore, resolve_events_with_store
|
||||||
from synapse.types import UserID, create_requester, get_domain_from_id
|
from synapse.types import UserID, get_domain_from_id, create_requester
|
||||||
from synapse.util import logcontext, unwrapFirstError
|
from synapse.util import logcontext, unwrapFirstError
|
||||||
from synapse.util.async_helpers import Linearizer
|
from synapse.util.async_helpers import Linearizer
|
||||||
from synapse.util.distributor import user_joined_room
|
from synapse.util.distributor import user_joined_room
|
||||||
|
@ -1409,8 +1409,37 @@ class FederationHandler(BaseHandler):
|
||||||
context = yield self.state_handler.compute_event_context(event)
|
context = yield self.state_handler.compute_event_context(event)
|
||||||
yield self.persist_events_and_notify([(event, context)])
|
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.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
|
@defer.inlineCallbacks
|
||||||
def do_remotely_reject_invite(self, target_hosts, room_id, user_id):
|
def do_remotely_reject_invite(self, target_hosts, room_id, user_id):
|
||||||
origin, event = yield self._make_and_verify_event(
|
origin, event = yield self._make_and_verify_event(
|
||||||
|
|
|
@ -29,7 +29,7 @@ import synapse.server
|
||||||
import synapse.types
|
import synapse.types
|
||||||
from synapse.api.constants import EventTypes, Membership
|
from synapse.api.constants import EventTypes, Membership
|
||||||
from synapse.api.errors import AuthError, Codes, SynapseError
|
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.async_helpers import Linearizer
|
||||||
from synapse.util.distributor import user_joined_room, user_left_room
|
from synapse.util.distributor import user_joined_room, user_left_room
|
||||||
|
|
||||||
|
@ -416,6 +416,7 @@ class RoomMemberHandler(object):
|
||||||
ret = yield self._remote_join(
|
ret = yield self._remote_join(
|
||||||
requester, remote_room_hosts, room_id, target, content
|
requester, remote_room_hosts, room_id, target, content
|
||||||
)
|
)
|
||||||
|
self._send_merged_user_invites(requester, room_id)
|
||||||
defer.returnValue(ret)
|
defer.returnValue(ret)
|
||||||
|
|
||||||
elif effective_membership_state == Membership.LEAVE:
|
elif effective_membership_state == Membership.LEAVE:
|
||||||
|
@ -450,8 +451,34 @@ class RoomMemberHandler(object):
|
||||||
prev_events_and_hashes=prev_events_and_hashes,
|
prev_events_and_hashes=prev_events_and_hashes,
|
||||||
content=content,
|
content=content,
|
||||||
)
|
)
|
||||||
|
self._send_merged_user_invites(requester, room_id)
|
||||||
defer.returnValue(res)
|
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
|
@defer.inlineCallbacks
|
||||||
def send_membership_event(
|
def send_membership_event(
|
||||||
self,
|
self,
|
||||||
|
|
Loading…
Reference in a new issue