2015-01-27 16:50:28 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-07 05:26:29 +01:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2015-01-27 16:50:28 +01: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.
|
|
|
|
|
2018-07-09 08:09:20 +02:00
|
|
|
import logging
|
2015-01-27 16:50:28 +01:00
|
|
|
|
2018-05-31 11:03:47 +02:00
|
|
|
from six import itervalues
|
|
|
|
|
2018-07-09 08:09:20 +02:00
|
|
|
from prometheus_client import Counter
|
|
|
|
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
2018-01-15 19:20:30 +01:00
|
|
|
import synapse
|
2015-05-22 15:45:46 +02:00
|
|
|
from synapse.api.constants import EventTypes
|
2019-07-03 16:07:04 +02:00
|
|
|
from synapse.logging.context import make_deferred_yieldable, run_in_background
|
2018-08-07 20:09:48 +02:00
|
|
|
from synapse.metrics import (
|
|
|
|
event_processing_loop_counter,
|
|
|
|
event_processing_loop_room_count,
|
|
|
|
)
|
2018-07-24 01:57:48 +02:00
|
|
|
from synapse.metrics.background_process_metrics import run_as_background_process
|
2018-10-08 15:06:19 +02:00
|
|
|
from synapse.util import log_failure
|
2016-08-17 12:12:29 +02:00
|
|
|
from synapse.util.metrics import Measure
|
2015-01-27 16:50:28 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2018-05-22 02:47:37 +02:00
|
|
|
events_processed_counter = Counter("synapse_handlers_appservice_events_processed", "")
|
2018-01-15 19:20:30 +01:00
|
|
|
|
2015-01-27 16:50:28 +01:00
|
|
|
|
2015-02-04 13:24:20 +01:00
|
|
|
class ApplicationServicesHandler(object):
|
2016-05-31 14:53:48 +02:00
|
|
|
def __init__(self, hs):
|
2015-02-04 13:24:20 +01:00
|
|
|
self.store = hs.get_datastore()
|
2016-05-31 14:53:48 +02: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 18:01:19 +01:00
|
|
|
self.started_scheduler = False
|
2016-08-17 12:12:29 +02:00
|
|
|
self.clock = hs.get_clock()
|
2016-08-18 15:59:55 +02:00
|
|
|
self.notify_appservices = hs.config.notify_appservices
|
|
|
|
|
|
|
|
self.current_max = 0
|
|
|
|
self.is_processing = False
|
2015-01-27 16:50:28 +01:00
|
|
|
|
2015-02-04 17:44:53 +01:00
|
|
|
@defer.inlineCallbacks
|
2016-08-18 12:54:41 +02:00
|
|
|
def notify_interested_services(self, current_id):
|
2015-01-27 17:53:59 +01: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 12:54:41 +02:00
|
|
|
current_id(int): The current maximum ID.
|
2015-01-27 17:53:59 +01:00
|
|
|
"""
|
2016-10-06 10:43:32 +02:00
|
|
|
services = self.store.get_app_services()
|
2016-08-18 15:59:55 +02:00
|
|
|
if not services or not self.notify_appservices:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.current_max = max(self.current_max, current_id)
|
|
|
|
if self.is_processing:
|
2016-08-18 12:54:41 +02:00
|
|
|
return
|
|
|
|
|
2016-08-17 12:12:29 +02:00
|
|
|
with Measure(self.clock, "notify_interested_services"):
|
2016-08-18 15:59:55 +02:00
|
|
|
self.is_processing = True
|
|
|
|
try:
|
|
|
|
limit = 100
|
|
|
|
while True:
|
2019-10-31 16:43:24 +01:00
|
|
|
(
|
|
|
|
upper_bound,
|
|
|
|
events,
|
|
|
|
) = yield self.store.get_new_events_for_appservice(
|
2017-11-02 20:49:43 +01:00
|
|
|
self.current_max, limit
|
2016-08-18 15:59:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if not events:
|
|
|
|
break
|
|
|
|
|
2018-04-11 10:56:00 +02:00
|
|
|
events_by_room = {}
|
2016-08-18 15:59:55 +02:00
|
|
|
for event in events:
|
2018-04-11 10:56:00 +02:00
|
|
|
events_by_room.setdefault(event.room_id, []).append(event)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def handle_event(event):
|
2016-08-18 15:59:55 +02:00
|
|
|
# Gather interested services
|
|
|
|
services = yield self._get_services_for_event(event)
|
|
|
|
if len(services) == 0:
|
2018-04-11 10:56:00 +02:00
|
|
|
return # no services need notifying
|
2016-08-18 15:59:55 +02:00
|
|
|
|
|
|
|
# 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:
|
2019-06-20 11:32:02 +02:00
|
|
|
|
2018-07-24 01:57:48 +02:00
|
|
|
def start_scheduler():
|
2018-10-08 14:26:54 +02:00
|
|
|
return self.scheduler.start().addErrback(
|
2019-06-20 11:32:02 +02:00
|
|
|
log_failure, "Application Services Failure"
|
2018-10-08 14:26:54 +02:00
|
|
|
)
|
2018-10-08 15:06:19 +02:00
|
|
|
|
2018-07-24 01:57:48 +02:00
|
|
|
run_as_background_process("as_scheduler", start_scheduler)
|
2016-08-18 15:59:55 +02:00
|
|
|
self.started_scheduler = True
|
|
|
|
|
|
|
|
# Fork off pushes to these services
|
|
|
|
for service in services:
|
2018-04-27 13:09:47 +02:00
|
|
|
self.scheduler.submit_event_for_as(service, event)
|
2016-08-18 15:59:55 +02:00
|
|
|
|
2018-04-11 10:56:00 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def handle_room_events(events):
|
|
|
|
for event in events:
|
|
|
|
yield handle_event(event)
|
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
yield make_deferred_yieldable(
|
|
|
|
defer.gatherResults(
|
|
|
|
[
|
|
|
|
run_in_background(handle_room_events, evs)
|
|
|
|
for evs in itervalues(events_by_room)
|
|
|
|
],
|
|
|
|
consumeErrors=True,
|
|
|
|
)
|
|
|
|
)
|
2018-04-11 10:56:00 +02:00
|
|
|
|
2016-08-18 15:59:55 +02:00
|
|
|
yield self.store.set_appservice_last_pos(upper_bound)
|
2018-04-11 12:07:51 +02:00
|
|
|
|
2018-04-12 12:18:19 +02:00
|
|
|
now = self.clock.time_msec()
|
|
|
|
ts = yield self.store.get_received_ts(events[-1].event_id)
|
|
|
|
|
2018-05-23 00:32:57 +02:00
|
|
|
synapse.metrics.event_processing_positions.labels(
|
2019-06-20 11:32:02 +02:00
|
|
|
"appservice_sender"
|
|
|
|
).set(upper_bound)
|
2018-04-11 12:52:19 +02:00
|
|
|
|
2018-05-22 02:47:37 +02:00
|
|
|
events_processed_counter.inc(len(events))
|
2018-04-11 12:52:19 +02:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
event_processing_loop_room_count.labels("appservice_sender").inc(
|
|
|
|
len(events_by_room)
|
|
|
|
)
|
2018-08-07 20:09:48 +02:00
|
|
|
|
|
|
|
event_processing_loop_counter.labels("appservice_sender").inc()
|
|
|
|
|
2018-05-23 00:32:57 +02:00
|
|
|
synapse.metrics.event_processing_lag.labels(
|
2019-06-20 11:32:02 +02:00
|
|
|
"appservice_sender"
|
|
|
|
).set(now - ts)
|
2018-05-23 00:32:57 +02:00
|
|
|
synapse.metrics.event_processing_last_ts.labels(
|
2019-06-20 11:32:02 +02:00
|
|
|
"appservice_sender"
|
|
|
|
).set(ts)
|
2016-08-18 15:59:55 +02:00
|
|
|
finally:
|
|
|
|
self.is_processing = False
|
2015-02-05 11:08:12 +01:00
|
|
|
|
2015-02-05 14:19:46 +01:00
|
|
|
@defer.inlineCallbacks
|
2015-02-05 15:17:08 +01:00
|
|
|
def query_user_exists(self, user_id):
|
|
|
|
"""Check if any application service knows this user_id exists.
|
2015-02-05 14:19:46 +01:00
|
|
|
|
|
|
|
Args:
|
2015-02-05 15:17:08 +01:00
|
|
|
user_id(str): The user to query if they exist on any AS.
|
2015-02-05 14:19:46 +01:00
|
|
|
Returns:
|
2015-02-05 15:17:08 +01:00
|
|
|
True if this user exists on at least one application service.
|
2015-02-05 14:19:46 +01:00
|
|
|
"""
|
2019-06-20 11:32:02 +02:00
|
|
|
user_query_services = yield self._get_services_for_user(user_id=user_id)
|
2015-02-05 14:19:46 +01:00
|
|
|
for user_service in user_query_services:
|
2019-06-20 11:32:02 +02:00
|
|
|
is_known_user = yield self.appservice_api.query_user(user_service, user_id)
|
2015-02-05 14:19:46 +01:00
|
|
|
if is_known_user:
|
2019-07-23 15:00:55 +02:00
|
|
|
return True
|
|
|
|
return False
|
2015-02-05 12:47:11 +01: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 14:19:46 +01:00
|
|
|
room_alias(RoomAlias): The room alias to query.
|
2015-02-05 12:47:11 +01:00
|
|
|
Returns:
|
|
|
|
namedtuple: with keys "room_id" and "servers" or None if no
|
|
|
|
association can be found.
|
|
|
|
"""
|
2015-02-09 16:01:28 +01:00
|
|
|
room_alias_str = room_alias.to_string()
|
2016-10-06 10:43:32 +02:00
|
|
|
services = self.store.get_app_services()
|
2016-08-17 18:20:50 +02:00
|
|
|
alias_query_services = [
|
2019-06-20 11:32:02 +02:00
|
|
|
s for s in services if (s.is_interested_in_alias(room_alias_str))
|
2016-08-17 18:20:50 +02:00
|
|
|
]
|
2015-02-05 12:47:11 +01:00
|
|
|
for alias_service in alias_query_services:
|
|
|
|
is_known_alias = yield self.appservice_api.query_alias(
|
2015-02-09 16:01:28 +01:00
|
|
|
alias_service, room_alias_str
|
2015-02-05 12:47:11 +01:00
|
|
|
)
|
|
|
|
if is_known_alias:
|
|
|
|
# the alias exists now so don't query more ASes.
|
2019-06-20 11:32:02 +02:00
|
|
|
result = yield self.store.get_association_from_room_alias(room_alias)
|
2019-07-23 15:00:55 +02:00
|
|
|
return result
|
2015-02-05 12:47:11 +01:00
|
|
|
|
2016-08-17 14:15:06 +02:00
|
|
|
@defer.inlineCallbacks
|
2016-08-18 18:19:55 +02:00
|
|
|
def query_3pe(self, kind, protocol, fields):
|
2016-08-17 14:15:06 +02:00
|
|
|
services = yield self._get_services_for_3pn(protocol)
|
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
results = yield make_deferred_yieldable(
|
|
|
|
defer.DeferredList(
|
|
|
|
[
|
|
|
|
run_in_background(
|
|
|
|
self.appservice_api.query_3pe, service, kind, protocol, fields
|
|
|
|
)
|
|
|
|
for service in services
|
|
|
|
],
|
|
|
|
consumeErrors=True,
|
2018-04-27 12:29:27 +02:00
|
|
|
)
|
2019-06-20 11:32:02 +02:00
|
|
|
)
|
2016-08-18 16:40:41 +02:00
|
|
|
|
2016-08-18 17:09:50 +02:00
|
|
|
ret = []
|
|
|
|
for (success, result) in results:
|
2016-08-18 18:33:56 +02:00
|
|
|
if success:
|
|
|
|
ret.extend(result)
|
2016-08-17 14:15:06 +02:00
|
|
|
|
2019-07-23 15:00:55 +02:00
|
|
|
return ret
|
2016-08-17 14:15:06 +02:00
|
|
|
|
2016-08-24 13:33:01 +02:00
|
|
|
@defer.inlineCallbacks
|
2016-09-09 14:25:02 +02:00
|
|
|
def get_3pe_protocols(self, only_protocol=None):
|
2016-10-06 10:43:32 +02:00
|
|
|
services = self.store.get_app_services()
|
2016-08-24 14:01:53 +02:00
|
|
|
protocols = {}
|
2016-09-08 18:43:53 +02:00
|
|
|
|
|
|
|
# Collect up all the individual protocol responses out of the ASes
|
2016-08-24 13:33:01 +02:00
|
|
|
for s in services:
|
2016-08-24 14:01:53 +02:00
|
|
|
for p in s.protocols:
|
2016-09-09 14:25:02 +02:00
|
|
|
if only_protocol is not None and p != only_protocol:
|
|
|
|
continue
|
|
|
|
|
2016-09-08 18:43:53 +02:00
|
|
|
if p not in protocols:
|
|
|
|
protocols[p] = []
|
2016-09-09 16:07:04 +02:00
|
|
|
|
|
|
|
info = yield self.appservice_api.get_3pe_protocol(s, p)
|
|
|
|
|
|
|
|
if info is not None:
|
|
|
|
protocols[p].append(info)
|
2016-09-08 18:43:53 +02:00
|
|
|
|
|
|
|
def _merge_instances(infos):
|
2016-09-09 15:54:16 +02:00
|
|
|
if not infos:
|
2016-09-08 18:43:53 +02:00
|
|
|
return {}
|
|
|
|
|
|
|
|
# Merge the 'instances' lists of multiple results, but just take
|
|
|
|
# the other fields from the first as they ought to be identical
|
2016-09-09 15:54:16 +02:00
|
|
|
# copy the result so as not to corrupt the cached one
|
2016-09-08 18:43:53 +02:00
|
|
|
combined = dict(infos[0])
|
2016-09-09 14:10:36 +02:00
|
|
|
combined["instances"] = list(combined["instances"])
|
2016-09-08 18:43:53 +02:00
|
|
|
|
|
|
|
for info in infos[1:]:
|
|
|
|
combined["instances"].extend(info["instances"])
|
|
|
|
|
|
|
|
return combined
|
|
|
|
|
|
|
|
for p in protocols.keys():
|
|
|
|
protocols[p] = _merge_instances(protocols[p])
|
2016-08-24 14:01:53 +02:00
|
|
|
|
2019-07-23 15:00:55 +02:00
|
|
|
return protocols
|
2016-08-24 13:33:01 +02:00
|
|
|
|
2015-02-05 12:47:11 +01:00
|
|
|
@defer.inlineCallbacks
|
2016-08-17 18:20:50 +02:00
|
|
|
def _get_services_for_event(self, event):
|
2015-02-05 12:47:11 +01: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.
|
|
|
|
"""
|
2016-10-06 10:43:32 +02:00
|
|
|
services = self.store.get_app_services()
|
2018-04-28 13:04:40 +02:00
|
|
|
|
|
|
|
# we can't use a list comprehension here. Since python 3, list
|
|
|
|
# comprehensions use a generator internally. This means you can't yield
|
|
|
|
# inside of a list comprehension anymore.
|
|
|
|
interested_list = []
|
|
|
|
for s in services:
|
|
|
|
if (yield s.is_interested(event, self.store)):
|
|
|
|
interested_list.append(s)
|
|
|
|
|
2019-07-23 15:00:55 +02:00
|
|
|
return interested_list
|
2015-02-05 12:47:11 +01:00
|
|
|
|
2015-02-05 15:17:08 +01:00
|
|
|
def _get_services_for_user(self, user_id):
|
2016-10-06 10:43:32 +02:00
|
|
|
services = self.store.get_app_services()
|
2019-06-20 11:32:02 +02:00
|
|
|
interested_list = [s for s in services if (s.is_interested_in_user(user_id))]
|
2016-10-06 10:43:32 +02:00
|
|
|
return defer.succeed(interested_list)
|
2015-02-05 15:17:08 +01:00
|
|
|
|
2016-08-17 14:15:06 +02:00
|
|
|
def _get_services_for_3pn(self, protocol):
|
2016-10-06 10:43:32 +02:00
|
|
|
services = self.store.get_app_services()
|
2019-06-20 11:32:02 +02:00
|
|
|
interested_list = [s for s in services if s.is_interested_in_protocol(protocol)]
|
2016-10-06 10:43:32 +02:00
|
|
|
return defer.succeed(interested_list)
|
2016-08-17 14:15:06 +02:00
|
|
|
|
2015-02-05 12:25:32 +01:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _is_unknown_user(self, user_id):
|
2016-05-31 14:53:48 +02:00
|
|
|
if not self.is_mine_id(user_id):
|
2015-02-05 12:25:32 +01:00
|
|
|
# we don't know if they are unknown or not since it isn't one of our
|
|
|
|
# users. We can't poke ASes.
|
2019-07-23 15:00:55 +02:00
|
|
|
return False
|
2015-02-05 12:25:32 +01:00
|
|
|
|
|
|
|
user_info = yield self.store.get_user_by_id(user_id)
|
2015-06-17 18:26:03 +02:00
|
|
|
if user_info:
|
2019-07-23 15:00:55 +02:00
|
|
|
return False
|
2015-04-01 15:05:24 +02:00
|
|
|
|
|
|
|
# user not found; could be the AS though, so check.
|
2016-10-06 10:43:32 +02:00
|
|
|
services = self.store.get_app_services()
|
2015-04-01 15:05:24 +02:00
|
|
|
service_list = [s for s in services if s.sender == user_id]
|
2019-07-23 15:00:55 +02:00
|
|
|
return len(service_list) == 0
|
2015-02-05 12:25:32 +01:00
|
|
|
|
2015-02-05 15:17:08 +01: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)
|
2019-07-23 15:00:55 +02:00
|
|
|
return exists
|
|
|
|
return True
|