Fix user query checks. HS>AS pushing now works.

This commit is contained in:
Kegan Dougal 2015-02-05 14:17:08 +00:00
parent 0613666d9c
commit c71456117d
2 changed files with 44 additions and 21 deletions

View file

@ -75,27 +75,23 @@ class ApplicationService(object):
def _matches_user(self, event): def _matches_user(self, event):
if (hasattr(event, "sender") and if (hasattr(event, "sender") and
self._matches_regex( self.is_interested_in_user(event.sender)):
event.sender, ApplicationService.NS_USERS)):
return True return True
# also check m.room.member state key # also check m.room.member state key
if (hasattr(event, "type") and event.type == EventTypes.Member if (hasattr(event, "type") and event.type == EventTypes.Member
and hasattr(event, "state_key") and hasattr(event, "state_key")
and self._matches_regex( and self.is_interested_in_user(event.state_key)):
event.state_key, ApplicationService.NS_USERS)):
return True return True
return False return False
def _matches_room_id(self, event): def _matches_room_id(self, event):
if hasattr(event, "room_id"): if hasattr(event, "room_id"):
return self._matches_regex( return self.is_interested_in_room(event.room_id)
event.room_id, ApplicationService.NS_ROOMS
)
return False return False
def _matches_aliases(self, event, alias_list): def _matches_aliases(self, event, alias_list):
for alias in alias_list: for alias in alias_list:
if self._matches_regex(alias, ApplicationService.NS_ALIASES): if self.is_interested_in_alias(alias):
return True return True
return False return False
@ -128,5 +124,14 @@ class ApplicationService(object):
elif restrict_to == ApplicationService.NS_USERS: elif restrict_to == ApplicationService.NS_USERS:
return self._matches_user(event) return self._matches_user(event)
def is_interested_in_user(self, user_id):
return self._matches_regex(user_id, ApplicationService.NS_USERS)
def is_interested_in_alias(self, alias):
return self._matches_regex(alias, ApplicationService.NS_ALIASES)
def is_interested_in_room(self, room_id):
return self._matches_regex(room_id, ApplicationService.NS_ROOMS)
def __str__(self): def __str__(self):
return "ApplicationService: %s" % (self.__dict__,) return "ApplicationService: %s" % (self.__dict__,)

View file

@ -15,6 +15,7 @@
from twisted.internet import defer from twisted.internet import defer
from synapse.api.constants import EventTypes
from synapse.api.errors import Codes, StoreError, SynapseError from synapse.api.errors import Codes, StoreError, SynapseError
from synapse.appservice import ApplicationService from synapse.appservice import ApplicationService
from synapse.appservice.api import ApplicationServiceApi from synapse.appservice.api import ApplicationServiceApi
@ -78,32 +79,31 @@ class ApplicationServicesHandler(object):
return # no services need notifying return # no services need notifying
# Do we know this user exists? If not, poke the user query API for # Do we know this user exists? If not, poke the user query API for
# all services which match that user regex. # all services which match that user regex. This needs to block as these
unknown_user = yield self._is_unknown_user(event.sender) # user queries need to be made BEFORE pushing the event.
if unknown_user: yield self._check_user_exists(event.sender)
yield self.query_user_exists(event) if event.type == EventTypes.Member:
yield self._check_user_exists(event.state_key)
# Fork off pushes to these services - XXX First cut, best effort # Fork off pushes to these services - XXX First cut, best effort
for service in services: for service in services:
self.appservice_api.push(service, event) self.appservice_api.push(service, event)
@defer.inlineCallbacks @defer.inlineCallbacks
def query_user_exists(self, event): def query_user_exists(self, user_id):
"""Check if an application services knows this event.sender exists. """Check if any application service knows this user_id exists.
Args: Args:
event: An event sent by the user to query user_id(str): The user to query if they exist on any AS.
Returns: Returns:
True if this user exists. True if this user exists on at least one application service.
""" """
# TODO Would be nice for this to accept a user ID instead of an event. user_query_services = yield self._get_services_for_user(
user_query_services = yield self._get_services_for_event( user_id=user_id
event=event,
restrict_to=ApplicationService.NS_USERS
) )
for user_service in user_query_services: for user_service in user_query_services:
is_known_user = yield self.appservice_api.query_user( is_known_user = yield self.appservice_api.query_user(
user_service, event.sender user_service, user_id
) )
if is_known_user: if is_known_user:
defer.returnValue(True) defer.returnValue(True)
@ -161,6 +161,16 @@ class ApplicationServicesHandler(object):
] ]
defer.returnValue(interested_list) defer.returnValue(interested_list)
@defer.inlineCallbacks
def _get_services_for_user(self, user_id):
services = yield self.store.get_app_services()
interested_list = [
s for s in services if (
s.is_interested_in_user(user_id)
)
]
defer.returnValue(interested_list)
@defer.inlineCallbacks @defer.inlineCallbacks
def _is_unknown_user(self, user_id): def _is_unknown_user(self, user_id):
user = UserID.from_string(user_id) user = UserID.from_string(user_id)
@ -173,5 +183,13 @@ class ApplicationServicesHandler(object):
user_info = yield self.store.get_user_by_id(user_id) user_info = yield self.store.get_user_by_id(user_id)
defer.returnValue(len(user_info) == 0) defer.returnValue(len(user_info) == 0)
@defer.inlineCallbacks
def _check_user_exists(self, user_id):
unknown_user = yield self._is_unknown_user(user_id)
if unknown_user:
exists = yield self.query_user_exists(user_id)
defer.returnValue(exists)
defer.returnValue(True)
def _generate_hs_token(self): def _generate_hs_token(self):
return stringutils.random_string(24) return stringutils.random_string(24)