synapse-product/synapse/handlers/appservice.py

454 lines
17 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2016-01-06 23:26:29 -05:00
# Copyright 2015, 2016 OpenMarket Ltd
#
# 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 02:09:20 -04:00
import logging
from typing import Dict, List, Optional, Union
2018-07-09 02:09:20 -04:00
from prometheus_client import Counter
from twisted.internet import defer
import synapse
2015-05-22 09:45:46 -04:00
from synapse.api.constants import EventTypes
from synapse.appservice import ApplicationService
from synapse.events import EventBase
from synapse.handlers.presence import format_user_presence_state
from synapse.logging.context import make_deferred_yieldable, run_in_background
from synapse.metrics import (
event_processing_loop_counter,
event_processing_loop_room_count,
)
from synapse.metrics.background_process_metrics import (
run_as_background_process,
wrap_as_background_process,
)
from synapse.types import Collection, JsonDict, RoomStreamToken, UserID
2016-08-17 06:12:29 -04:00
from synapse.util.metrics import Measure
logger = logging.getLogger(__name__)
2018-05-21 20:47:37 -04:00
events_processed_counter = Counter("synapse_handlers_appservice_events_processed", "")
2020-09-04 06:54:56 -04:00
class ApplicationServicesHandler:
def __init__(self, hs):
self.store = hs.get_datastore()
self.is_mine_id = hs.is_mine_id
self.appservice_api = hs.get_application_service_api()
self.scheduler = hs.get_application_service_scheduler()
self.started_scheduler = False
2016-08-17 06:12:29 -04:00
self.clock = hs.get_clock()
2016-08-18 09:59:55 -04:00
self.notify_appservices = hs.config.notify_appservices
self.event_sources = hs.get_event_sources()
2016-08-18 09:59:55 -04:00
self.current_max = 0
self.is_processing = False
def notify_interested_services(self, max_token: RoomStreamToken):
"""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.
"""
# We just use the minimum stream ordering and ignore the vector clock
# component. This is safe to do as long as we *always* ignore the vector
# clock components.
current_id = max_token.stream
services = self.store.get_app_services()
2016-08-18 09:59:55 -04:00
if not services or not self.notify_appservices:
return
self.current_max = max(self.current_max, current_id)
if self.is_processing:
return
# We only start a new background process if necessary rather than
# optimistically (to cut down on overhead).
self._notify_interested_services(max_token)
@wrap_as_background_process("notify_interested_services")
async def _notify_interested_services(self, max_token: RoomStreamToken):
2016-08-17 06:12:29 -04:00
with Measure(self.clock, "notify_interested_services"):
2016-08-18 09:59:55 -04:00
self.is_processing = True
try:
limit = 100
while True:
(
upper_bound,
events,
) = await self.store.get_new_events_for_appservice(
self.current_max, limit
2016-08-18 09:59:55 -04:00
)
if not events:
break
events_by_room = {} # type: Dict[str, List[EventBase]]
2016-08-18 09:59:55 -04:00
for event in events:
2018-04-11 04:56:00 -04:00
events_by_room.setdefault(event.room_id, []).append(event)
async def handle_event(event):
2016-08-18 09:59:55 -04:00
# Gather interested services
services = await self._get_services_for_event(event)
2016-08-18 09:59:55 -04:00
if len(services) == 0:
2018-04-11 04:56:00 -04:00
return # no services need notifying
2016-08-18 09:59:55 -04: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.
await self._check_user_exists(event.sender)
2016-08-18 09:59:55 -04:00
if event.type == EventTypes.Member:
await self._check_user_exists(event.state_key)
2016-08-18 09:59:55 -04:00
if not self.started_scheduler:
2019-06-20 05:32:02 -04:00
2020-07-30 07:27:39 -04:00
async def start_scheduler():
try:
return await self.scheduler.start()
2020-07-30 07:27:39 -04:00
except Exception:
logger.error("Application Services Failure")
2018-07-23 19:57:48 -04:00
run_as_background_process("as_scheduler", start_scheduler)
2016-08-18 09:59:55 -04:00
self.started_scheduler = True
# Fork off pushes to these services
for service in services:
self.scheduler.submit_event_for_as(service, event)
2016-08-18 09:59:55 -04:00
now = self.clock.time_msec()
ts = await self.store.get_received_ts(event.event_id)
synapse.metrics.event_processing_lag_by_event.labels(
"appservice_sender"
).observe((now - ts) / 1000)
async def handle_room_events(events):
2018-04-11 04:56:00 -04:00
for event in events:
await handle_event(event)
2018-04-11 04:56:00 -04:00
await make_deferred_yieldable(
2019-06-20 05:32:02 -04:00
defer.gatherResults(
[
run_in_background(handle_room_events, evs)
for evs in events_by_room.values()
2019-06-20 05:32:02 -04:00
],
consumeErrors=True,
)
)
2018-04-11 04:56:00 -04:00
await self.store.set_appservice_last_pos(upper_bound)
2018-04-12 06:18:19 -04:00
now = self.clock.time_msec()
ts = await self.store.get_received_ts(events[-1].event_id)
2018-04-12 06:18:19 -04:00
2018-05-22 18:32:57 -04:00
synapse.metrics.event_processing_positions.labels(
2019-06-20 05:32:02 -04:00
"appservice_sender"
).set(upper_bound)
2018-04-11 06:52:19 -04:00
2018-05-21 20:47:37 -04:00
events_processed_counter.inc(len(events))
2018-04-11 06:52:19 -04:00
2019-06-20 05:32:02 -04:00
event_processing_loop_room_count.labels("appservice_sender").inc(
len(events_by_room)
)
event_processing_loop_counter.labels("appservice_sender").inc()
2018-05-22 18:32:57 -04:00
synapse.metrics.event_processing_lag.labels(
2019-06-20 05:32:02 -04:00
"appservice_sender"
).set(now - ts)
2018-05-22 18:32:57 -04:00
synapse.metrics.event_processing_last_ts.labels(
2019-06-20 05:32:02 -04:00
"appservice_sender"
).set(ts)
2016-08-18 09:59:55 -04:00
finally:
self.is_processing = False
def notify_interested_services_ephemeral(
self,
stream_key: str,
new_token: Optional[int],
users: Collection[Union[str, UserID]] = [],
):
"""This is called by the notifier in the background
when a ephemeral event handled by the homeserver.
This will determine which appservices
are interested in the event, and submit them.
Events will only be pushed to appservices
that have opted into ephemeral events
Args:
stream_key: The stream the event came from.
new_token: The latest stream token
users: The user(s) involved with the event.
"""
if not self.notify_appservices:
return
if stream_key not in ("typing_key", "receipt_key", "presence_key"):
return
services = [
service
for service in self.store.get_app_services()
if service.supports_ephemeral
]
if not services:
return
# We only start a new background process if necessary rather than
# optimistically (to cut down on overhead).
self._notify_interested_services_ephemeral(
services, stream_key, new_token, users
)
@wrap_as_background_process("notify_interested_services_ephemeral")
async def _notify_interested_services_ephemeral(
self,
services: List[ApplicationService],
stream_key: str,
new_token: Optional[int],
users: Collection[Union[str, UserID]],
):
logger.info("Checking interested services for %s" % (stream_key))
with Measure(self.clock, "notify_interested_services_ephemeral"):
for service in services:
# Only handle typing if we have the latest token
if stream_key == "typing_key" and new_token is not None:
events = await self._handle_typing(service, new_token)
if events:
self.scheduler.submit_ephemeral_events_for_as(service, events)
# We don't persist the token for typing_key for performance reasons
elif stream_key == "receipt_key":
events = await self._handle_receipts(service)
if events:
self.scheduler.submit_ephemeral_events_for_as(service, events)
await self.store.set_type_stream_id_for_appservice(
service, "read_receipt", new_token
)
elif stream_key == "presence_key":
events = await self._handle_presence(service, users)
if events:
self.scheduler.submit_ephemeral_events_for_as(service, events)
await self.store.set_type_stream_id_for_appservice(
service, "presence", new_token
)
async def _handle_typing(self, service: ApplicationService, new_token: int):
typing_source = self.event_sources.sources["typing"]
# Get the typing events from just before current
typing, _ = await typing_source.get_new_events_as(
service=service,
# For performance reasons, we don't persist the previous
# token in the DB and instead fetch the latest typing information
# for appservices.
from_key=new_token - 1,
)
return typing
async def _handle_receipts(self, service: ApplicationService):
from_key = await self.store.get_type_stream_id_for_appservice(
service, "read_receipt"
)
receipts_source = self.event_sources.sources["receipt"]
receipts, _ = await receipts_source.get_new_events_as(
service=service, from_key=from_key
)
return receipts
async def _handle_presence(
self, service: ApplicationService, users: Collection[Union[str, UserID]]
):
events = [] # type: List[JsonDict]
presence_source = self.event_sources.sources["presence"]
from_key = await self.store.get_type_stream_id_for_appservice(
service, "presence"
)
for user in users:
if isinstance(user, str):
user = UserID.from_string(user)
interested = await service.is_interested_in_presence(user, self.store)
if not interested:
continue
presence_events, _ = await presence_source.get_new_events(
user=user, service=service, from_key=from_key,
)
time_now = self.clock.time_msec()
events.extend(
{
"type": "m.presence",
"sender": event.user_id,
"content": format_user_presence_state(
event, time_now, include_user_id=False
),
}
for event in presence_events
)
return events
async def query_user_exists(self, user_id):
"""Check if any application service knows this user_id exists.
Args:
user_id(str): The user to query if they exist on any AS.
Returns:
True if this user exists on at least one application service.
"""
user_query_services = self._get_services_for_user(user_id=user_id)
for user_service in user_query_services:
is_known_user = await self.appservice_api.query_user(user_service, user_id)
if is_known_user:
return True
return False
async def query_room_alias_exists(self, room_alias):
"""Check if an application service knows this room alias exists.
Args:
room_alias(RoomAlias): The room alias to query.
Returns:
namedtuple: with keys "room_id" and "servers" or None if no
association can be found.
"""
room_alias_str = room_alias.to_string()
services = self.store.get_app_services()
2016-08-17 12:20:50 -04:00
alias_query_services = [
2019-06-20 05:32:02 -04:00
s for s in services if (s.is_interested_in_alias(room_alias_str))
2016-08-17 12:20:50 -04:00
]
for alias_service in alias_query_services:
is_known_alias = await self.appservice_api.query_alias(
alias_service, room_alias_str
)
if is_known_alias:
# the alias exists now so don't query more ASes.
result = await self.store.get_association_from_room_alias(room_alias)
return result
async def query_3pe(self, kind, protocol, fields):
services = self._get_services_for_3pn(protocol)
results = await make_deferred_yieldable(
2019-06-20 05:32:02 -04:00
defer.DeferredList(
[
run_in_background(
self.appservice_api.query_3pe, service, kind, protocol, fields
)
for service in services
],
consumeErrors=True,
)
2019-06-20 05:32:02 -04:00
)
ret = []
for (success, result) in results:
if success:
ret.extend(result)
return ret
async def get_3pe_protocols(self, only_protocol=None):
services = self.store.get_app_services()
protocols = {} # type: Dict[str, List[JsonDict]]
# Collect up all the individual protocol responses out of the ASes
for s in services:
for p in s.protocols:
if only_protocol is not None and p != only_protocol:
continue
if p not in protocols:
protocols[p] = []
info = await self.appservice_api.get_3pe_protocol(s, p)
if info is not None:
protocols[p].append(info)
def _merge_instances(infos):
2016-09-09 09:54:16 -04:00
if not infos:
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 09:54:16 -04:00
# copy the result so as not to corrupt the cached one
combined = dict(infos[0])
combined["instances"] = list(combined["instances"])
for info in infos[1:]:
combined["instances"].extend(info["instances"])
return combined
for p in protocols.keys():
protocols[p] = _merge_instances(protocols[p])
return protocols
async def _get_services_for_event(self, event):
"""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 = self.store.get_app_services()
# 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 await s.is_interested(event, self.store):
interested_list.append(s)
return interested_list
def _get_services_for_user(self, user_id):
services = self.store.get_app_services()
2019-06-20 05:32:02 -04:00
interested_list = [s for s in services if (s.is_interested_in_user(user_id))]
return interested_list
def _get_services_for_3pn(self, protocol):
services = self.store.get_app_services()
2019-06-20 05:32:02 -04:00
interested_list = [s for s in services if s.is_interested_in_protocol(protocol)]
return interested_list
async def _is_unknown_user(self, user_id):
if not self.is_mine_id(user_id):
# we don't know if they are unknown or not since it isn't one of our
# users. We can't poke ASes.
return False
user_info = await self.store.get_user_by_id(user_id)
if user_info:
return False
# user not found; could be the AS though, so check.
services = self.store.get_app_services()
service_list = [s for s in services if s.sender == user_id]
return len(service_list) == 0
async def _check_user_exists(self, user_id):
unknown_user = await self._is_unknown_user(user_id)
if unknown_user:
exists = await self.query_user_exists(user_id)
return exists
return True