2015-01-27 15:50:28 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-07 04:26:29 +00:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2015-01-27 15:50:28 +00:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
2015-05-22 13:45:46 +00:00
|
|
|
from synapse.api.constants import EventTypes
|
2016-08-17 10:12:29 +00:00
|
|
|
from synapse.util.metrics import Measure
|
2016-08-18 10:54:41 +00:00
|
|
|
from synapse.util.logcontext import preserve_fn
|
2016-08-18 16:19:55 +00:00
|
|
|
from synapse.types import ThirdPartyEntityKind
|
2015-01-27 15:50:28 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2015-03-09 17:01:19 +00:00
|
|
|
def log_failure(failure):
|
2015-03-16 10:16:59 +00:00
|
|
|
logger.error(
|
|
|
|
"Application Services Failure",
|
|
|
|
exc_info=(
|
|
|
|
failure.type,
|
|
|
|
failure.value,
|
|
|
|
failure.getTracebackObject()
|
|
|
|
)
|
|
|
|
)
|
2015-03-09 17:01:19 +00:00
|
|
|
|
2016-08-18 15:26:19 +00:00
|
|
|
|
2016-08-18 15:09:50 +00:00
|
|
|
def _is_valid_3pentity_result(r, field):
|
2016-08-18 14:40:41 +00:00
|
|
|
if not isinstance(r, dict):
|
|
|
|
return False
|
|
|
|
|
2016-08-18 15:09:50 +00:00
|
|
|
for k in (field, "protocol"):
|
2016-08-18 14:40:41 +00:00
|
|
|
if k not in r:
|
|
|
|
return False
|
|
|
|
if not isinstance(r[k], str):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if "fields" not in r:
|
|
|
|
return False
|
|
|
|
fields = r["fields"]
|
|
|
|
if not isinstance(fields, dict):
|
|
|
|
return False
|
|
|
|
for k in fields.keys():
|
|
|
|
if not isinstance(fields[k], str):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2015-03-09 17:01:19 +00:00
|
|
|
|
2016-08-18 15:26:19 +00:00
|
|
|
|
2015-02-04 12:24:20 +00:00
|
|
|
class ApplicationServicesHandler(object):
|
2015-01-27 15:50:28 +00:00
|
|
|
|
2016-05-31 12:53:48 +00:00
|
|
|
def __init__(self, hs):
|
2015-02-04 12:24:20 +00:00
|
|
|
self.store = hs.get_datastore()
|
2016-05-31 12:53:48 +00:00
|
|
|
self.is_mine_id = hs.is_mine_id
|
|
|
|
self.appservice_api = hs.get_application_service_api()
|
|
|
|
self.scheduler = hs.get_application_service_scheduler()
|
2015-03-09 17:01:19 +00:00
|
|
|
self.started_scheduler = False
|
2016-08-17 10:12:29 +00:00
|
|
|
self.clock = hs.get_clock()
|
2015-01-27 15:50:28 +00:00
|
|
|
|
2015-02-04 16:44:53 +00:00
|
|
|
@defer.inlineCallbacks
|
2016-08-18 10:54:41 +00:00
|
|
|
def notify_interested_services(self, current_id):
|
2015-01-27 16:53:59 +00:00
|
|
|
"""Notifies (pushes) all application services interested in this event.
|
|
|
|
|
|
|
|
Pushing is done asynchronously, so this method won't block for any
|
|
|
|
prolonged length of time.
|
|
|
|
|
|
|
|
Args:
|
2016-08-18 10:54:41 +00:00
|
|
|
current_id(int): The current maximum ID.
|
2015-01-27 16:53:59 +00:00
|
|
|
"""
|
2016-08-18 10:54:41 +00:00
|
|
|
services = yield self.store.get_app_services()
|
|
|
|
if not services:
|
|
|
|
return
|
|
|
|
|
2016-08-17 10:12:29 +00:00
|
|
|
with Measure(self.clock, "notify_interested_services"):
|
2016-08-18 10:54:41 +00:00
|
|
|
upper_bound = current_id
|
|
|
|
limit = 100
|
|
|
|
while True:
|
|
|
|
upper_bound, events = yield self.store.get_new_events_for_appservice(
|
|
|
|
upper_bound, limit
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.info("Current_id: %r, upper_bound: %r", current_id, upper_bound)
|
|
|
|
|
|
|
|
if not events:
|
|
|
|
break
|
|
|
|
|
|
|
|
for event in events:
|
|
|
|
# Gather interested services
|
|
|
|
services = yield self._get_services_for_event(event)
|
|
|
|
if len(services) == 0:
|
|
|
|
continue # no services need notifying
|
|
|
|
|
|
|
|
# Do we know this user exists? If not, poke the user query API for
|
|
|
|
# all services which match that user regex. This needs to block as
|
|
|
|
# these user queries need to be made BEFORE pushing the event.
|
|
|
|
yield self._check_user_exists(event.sender)
|
|
|
|
if event.type == EventTypes.Member:
|
|
|
|
yield self._check_user_exists(event.state_key)
|
|
|
|
|
|
|
|
if not self.started_scheduler:
|
|
|
|
self.scheduler.start().addErrback(log_failure)
|
|
|
|
self.started_scheduler = True
|
|
|
|
|
|
|
|
# Fork off pushes to these services
|
|
|
|
for service in services:
|
|
|
|
preserve_fn(self.scheduler.submit_event_for_as)(service, event)
|
|
|
|
|
|
|
|
yield self.store.set_appservice_last_pos(upper_bound)
|
|
|
|
|
|
|
|
if len(events) < limit:
|
|
|
|
break
|
2015-02-05 10:08:12 +00:00
|
|
|
|
2015-02-05 13:19:46 +00:00
|
|
|
@defer.inlineCallbacks
|
2015-02-05 14:17:08 +00:00
|
|
|
def query_user_exists(self, user_id):
|
|
|
|
"""Check if any application service knows this user_id exists.
|
2015-02-05 13:19:46 +00:00
|
|
|
|
|
|
|
Args:
|
2015-02-05 14:17:08 +00:00
|
|
|
user_id(str): The user to query if they exist on any AS.
|
2015-02-05 13:19:46 +00:00
|
|
|
Returns:
|
2015-02-05 14:17:08 +00:00
|
|
|
True if this user exists on at least one application service.
|
2015-02-05 13:19:46 +00:00
|
|
|
"""
|
2015-02-05 14:17:08 +00:00
|
|
|
user_query_services = yield self._get_services_for_user(
|
|
|
|
user_id=user_id
|
2015-02-05 13:19:46 +00:00
|
|
|
)
|
|
|
|
for user_service in user_query_services:
|
|
|
|
is_known_user = yield self.appservice_api.query_user(
|
2015-02-05 14:17:08 +00:00
|
|
|
user_service, user_id
|
2015-02-05 13:19:46 +00:00
|
|
|
)
|
|
|
|
if is_known_user:
|
|
|
|
defer.returnValue(True)
|
|
|
|
defer.returnValue(False)
|
2015-02-05 11:47:11 +00:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def query_room_alias_exists(self, room_alias):
|
|
|
|
"""Check if an application service knows this room alias exists.
|
|
|
|
|
|
|
|
Args:
|
2015-02-05 13:19:46 +00:00
|
|
|
room_alias(RoomAlias): The room alias to query.
|
2015-02-05 11:47:11 +00:00
|
|
|
Returns:
|
|
|
|
namedtuple: with keys "room_id" and "servers" or None if no
|
|
|
|
association can be found.
|
|
|
|
"""
|
2015-02-09 15:01:28 +00:00
|
|
|
room_alias_str = room_alias.to_string()
|
2016-08-17 16:20:50 +00:00
|
|
|
services = yield self.store.get_app_services()
|
|
|
|
alias_query_services = [
|
|
|
|
s for s in services if (
|
|
|
|
s.is_interested_in_alias(room_alias_str)
|
|
|
|
)
|
|
|
|
]
|
2015-02-05 11:47:11 +00:00
|
|
|
for alias_service in alias_query_services:
|
|
|
|
is_known_alias = yield self.appservice_api.query_alias(
|
2015-02-09 15:01:28 +00:00
|
|
|
alias_service, room_alias_str
|
2015-02-05 11:47:11 +00:00
|
|
|
)
|
|
|
|
if is_known_alias:
|
|
|
|
# the alias exists now so don't query more ASes.
|
|
|
|
result = yield self.store.get_association_from_room_alias(
|
|
|
|
room_alias
|
|
|
|
)
|
|
|
|
defer.returnValue(result)
|
|
|
|
|
2016-08-17 12:15:06 +00:00
|
|
|
@defer.inlineCallbacks
|
2016-08-18 16:19:55 +00:00
|
|
|
def query_3pe(self, kind, protocol, fields):
|
2016-08-17 12:15:06 +00:00
|
|
|
services = yield self._get_services_for_3pn(protocol)
|
|
|
|
|
2016-08-18 15:58:25 +00:00
|
|
|
results = yield defer.DeferredList([
|
2016-08-18 16:19:55 +00:00
|
|
|
self.appservice_api.query_3pe(service, kind, protocol, fields)
|
2016-08-18 15:58:25 +00:00
|
|
|
for service in services
|
|
|
|
], consumeErrors=True)
|
2016-08-18 14:40:41 +00:00
|
|
|
|
2016-08-18 16:19:55 +00:00
|
|
|
required_field = (
|
|
|
|
"userid" if kind == ThirdPartyEntityKind.USER else
|
|
|
|
"alias" if kind == ThirdPartyEntityKind.LOCATION else
|
|
|
|
None
|
|
|
|
)
|
2016-08-18 15:09:50 +00:00
|
|
|
|
|
|
|
ret = []
|
|
|
|
for (success, result) in results:
|
|
|
|
if not success:
|
|
|
|
continue
|
|
|
|
if not isinstance(result, list):
|
|
|
|
continue
|
|
|
|
for r in result:
|
2016-08-18 16:19:55 +00:00
|
|
|
if _is_valid_3pentity_result(r, field=required_field):
|
2016-08-18 14:49:55 +00:00
|
|
|
ret.append(r)
|
|
|
|
else:
|
|
|
|
logger.warn("Application service returned an " +
|
|
|
|
"invalid result %r", r)
|
2016-08-17 12:15:06 +00:00
|
|
|
|
2016-08-18 14:40:41 +00:00
|
|
|
defer.returnValue(ret)
|
2016-08-17 12:15:06 +00:00
|
|
|
|
2015-02-05 11:47:11 +00:00
|
|
|
@defer.inlineCallbacks
|
2016-08-17 16:20:50 +00:00
|
|
|
def _get_services_for_event(self, event):
|
2015-02-05 11:47:11 +00:00
|
|
|
"""Retrieve a list of application services interested in this event.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
event(Event): The event to check. Can be None if alias_list is not.
|
|
|
|
Returns:
|
|
|
|
list<ApplicationService>: A list of services interested in this
|
|
|
|
event based on the service regex.
|
|
|
|
"""
|
|
|
|
services = yield self.store.get_app_services()
|
|
|
|
interested_list = [
|
|
|
|
s for s in services if (
|
2016-08-17 16:20:50 +00:00
|
|
|
yield s.is_interested(event, self.store)
|
2015-02-05 11:47:11 +00:00
|
|
|
)
|
|
|
|
]
|
|
|
|
defer.returnValue(interested_list)
|
|
|
|
|
2015-02-05 14:17:08 +00:00
|
|
|
@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)
|
|
|
|
|
2016-08-17 12:15:06 +00:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _get_services_for_3pn(self, protocol):
|
|
|
|
services = yield self.store.get_app_services()
|
2016-08-18 13:56:02 +00:00
|
|
|
interested_list = [
|
|
|
|
s for s in services if s.is_interested_in_protocol(protocol)
|
|
|
|
]
|
|
|
|
defer.returnValue(interested_list)
|
2016-08-17 12:15:06 +00:00
|
|
|
|
2015-02-05 11:25:32 +00:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _is_unknown_user(self, user_id):
|
2016-05-31 12:53:48 +00:00
|
|
|
if not self.is_mine_id(user_id):
|
2015-02-05 11:25:32 +00:00
|
|
|
# we don't know if they are unknown or not since it isn't one of our
|
|
|
|
# users. We can't poke ASes.
|
|
|
|
defer.returnValue(False)
|
|
|
|
return
|
|
|
|
|
|
|
|
user_info = yield self.store.get_user_by_id(user_id)
|
2015-06-17 16:26:03 +00:00
|
|
|
if user_info:
|
2015-04-01 13:05:24 +00:00
|
|
|
defer.returnValue(False)
|
|
|
|
return
|
|
|
|
|
|
|
|
# user not found; could be the AS though, so check.
|
|
|
|
services = yield self.store.get_app_services()
|
|
|
|
service_list = [s for s in services if s.sender == user_id]
|
|
|
|
defer.returnValue(len(service_list) == 0)
|
2015-02-05 11:25:32 +00:00
|
|
|
|
2015-02-05 14:17:08 +00:00
|
|
|
@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)
|