2016-01-06 23:26:29 -05:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2015-01-27 10:50:28 -05: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 02:09:20 -04:00
|
|
|
import logging
|
2021-09-20 08:56:23 -04:00
|
|
|
from typing import TYPE_CHECKING, Collection, Dict, Iterable, List, Optional, Union
|
2015-01-27 10:50:28 -05:00
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from prometheus_client import Counter
|
|
|
|
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
2018-01-15 13:20:30 -05:00
|
|
|
import synapse
|
2015-05-22 09:45:46 -04:00
|
|
|
from synapse.api.constants import EventTypes
|
2020-10-15 12:33:28 -04:00
|
|
|
from synapse.appservice import ApplicationService
|
|
|
|
from synapse.events import EventBase
|
|
|
|
from synapse.handlers.presence import format_user_presence_state
|
2019-07-03 10:07:04 -04:00
|
|
|
from synapse.logging.context import make_deferred_yieldable, run_in_background
|
2018-08-07 14:09:48 -04:00
|
|
|
from synapse.metrics import (
|
|
|
|
event_processing_loop_counter,
|
|
|
|
event_processing_loop_room_count,
|
|
|
|
)
|
2020-10-26 05:30:19 -04:00
|
|
|
from synapse.metrics.background_process_metrics import (
|
|
|
|
run_as_background_process,
|
|
|
|
wrap_as_background_process,
|
|
|
|
)
|
2020-10-28 11:12:21 -04:00
|
|
|
from synapse.storage.databases.main.directory import RoomAliasMapping
|
2022-03-30 09:39:27 -04:00
|
|
|
from synapse.types import (
|
|
|
|
DeviceListUpdates,
|
|
|
|
JsonDict,
|
|
|
|
RoomAlias,
|
|
|
|
RoomStreamToken,
|
|
|
|
UserID,
|
|
|
|
)
|
2021-11-03 12:51:00 -04:00
|
|
|
from synapse.util.async_helpers import Linearizer
|
2016-08-17 06:12:29 -04:00
|
|
|
from synapse.util.metrics import Measure
|
2015-01-27 10:50:28 -05:00
|
|
|
|
2020-10-28 11:12:21 -04:00
|
|
|
if TYPE_CHECKING:
|
2021-03-23 07:12:48 -04:00
|
|
|
from synapse.server import HomeServer
|
2020-10-28 11:12:21 -04:00
|
|
|
|
2015-01-27 10:50:28 -05:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2018-05-21 20:47:37 -04:00
|
|
|
events_processed_counter = Counter("synapse_handlers_appservice_events_processed", "")
|
2018-01-15 13:20:30 -05:00
|
|
|
|
2015-01-27 10:50:28 -05:00
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class ApplicationServicesHandler:
|
2020-10-28 11:12:21 -04:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2016-05-31 08:53:48 -04: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 13:01:19 -04:00
|
|
|
self.started_scheduler = False
|
2016-08-17 06:12:29 -04:00
|
|
|
self.clock = hs.get_clock()
|
2021-09-23 07:13:34 -04:00
|
|
|
self.notify_appservices = hs.config.appservice.notify_appservices
|
2020-10-15 12:33:28 -04:00
|
|
|
self.event_sources = hs.get_event_sources()
|
2022-02-01 09:13:38 -05:00
|
|
|
self._msc2409_to_device_messages_enabled = (
|
|
|
|
hs.config.experimental.msc2409_to_device_messages_enabled
|
|
|
|
)
|
2022-03-30 09:39:27 -04:00
|
|
|
self._msc3202_transaction_extensions_enabled = (
|
|
|
|
hs.config.experimental.msc3202_transaction_extensions
|
|
|
|
)
|
2016-08-18 09:59:55 -04:00
|
|
|
|
|
|
|
self.current_max = 0
|
|
|
|
self.is_processing = False
|
2015-01-27 10:50:28 -05:00
|
|
|
|
2021-11-03 12:51:00 -04:00
|
|
|
self._ephemeral_events_linearizer = Linearizer(
|
|
|
|
name="appservice_ephemeral_events"
|
|
|
|
)
|
|
|
|
|
2021-09-20 08:56:23 -04:00
|
|
|
def notify_interested_services(self, max_token: RoomStreamToken) -> None:
|
2015-01-27 11:53:59 -05: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.
|
|
|
|
"""
|
2020-10-14 08:27:51 -04:00
|
|
|
# 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
|
|
|
|
|
2016-10-06 04:43:32 -04:00
|
|
|
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:
|
2016-08-18 06:54:41 -04:00
|
|
|
return
|
|
|
|
|
2020-10-26 05:30:19 -04:00
|
|
|
# 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")
|
2021-09-20 08:56:23 -04:00
|
|
|
async def _notify_interested_services(self, max_token: RoomStreamToken) -> None:
|
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
|
2021-06-07 10:42:05 -04:00
|
|
|
upper_bound = -1
|
|
|
|
while upper_bound < self.current_max:
|
2019-10-31 11:43:24 -04:00
|
|
|
(
|
|
|
|
upper_bound,
|
|
|
|
events,
|
2020-07-06 07:40:35 -04:00
|
|
|
) = await self.store.get_new_events_for_appservice(
|
2017-11-02 15:49:43 -04:00
|
|
|
self.current_max, limit
|
2016-08-18 09:59:55 -04:00
|
|
|
)
|
|
|
|
|
2021-07-16 13:22:36 -04:00
|
|
|
events_by_room: 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)
|
|
|
|
|
2021-09-20 08:56:23 -04:00
|
|
|
async def handle_event(event: EventBase) -> None:
|
2016-08-18 09:59:55 -04:00
|
|
|
# Gather interested services
|
2020-07-06 07:40:35 -04:00
|
|
|
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.
|
2020-07-06 07:40:35 -04:00
|
|
|
await self._check_user_exists(event.sender)
|
2016-08-18 09:59:55 -04:00
|
|
|
if event.type == EventTypes.Member:
|
2020-07-06 07:40:35 -04:00
|
|
|
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
|
|
|
|
2021-09-20 08:56:23 -04:00
|
|
|
async def start_scheduler() -> None:
|
2020-07-30 07:27:39 -04:00
|
|
|
try:
|
2021-09-20 08:56:23 -04:00
|
|
|
await self.scheduler.start()
|
2020-07-30 07:27:39 -04:00
|
|
|
except Exception:
|
|
|
|
logger.error("Application Services Failure")
|
2018-10-08 09:06:19 -04:00
|
|
|
|
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:
|
2022-02-01 09:13:38 -05:00
|
|
|
self.scheduler.enqueue_for_appservice(
|
|
|
|
service, events=[event]
|
|
|
|
)
|
2016-08-18 09:59:55 -04:00
|
|
|
|
2020-06-30 11:58:06 -04:00
|
|
|
now = self.clock.time_msec()
|
2020-07-06 07:40:35 -04:00
|
|
|
ts = await self.store.get_received_ts(event.event_id)
|
2021-09-10 12:03:18 -04:00
|
|
|
assert ts is not None
|
|
|
|
|
2020-06-30 11:58:06 -04:00
|
|
|
synapse.metrics.event_processing_lag_by_event.labels(
|
|
|
|
"appservice_sender"
|
2020-07-01 10:23:58 -04:00
|
|
|
).observe((now - ts) / 1000)
|
2020-06-30 11:58:06 -04:00
|
|
|
|
2021-09-20 08:56:23 -04:00
|
|
|
async def handle_room_events(events: Iterable[EventBase]) -> None:
|
2018-04-11 04:56:00 -04:00
|
|
|
for event in events:
|
2020-07-06 07:40:35 -04:00
|
|
|
await handle_event(event)
|
2018-04-11 04:56:00 -04:00
|
|
|
|
2020-07-06 07:40:35 -04:00
|
|
|
await make_deferred_yieldable(
|
2019-06-20 05:32:02 -04:00
|
|
|
defer.gatherResults(
|
|
|
|
[
|
|
|
|
run_in_background(handle_room_events, evs)
|
2020-06-15 07:03:36 -04:00
|
|
|
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
|
|
|
|
2020-07-06 07:40:35 -04:00
|
|
|
await self.store.set_appservice_last_pos(upper_bound)
|
2018-04-11 06:07:51 -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)
|
|
|
|
)
|
2018-08-07 14:09:48 -04:00
|
|
|
|
|
|
|
event_processing_loop_counter.labels("appservice_sender").inc()
|
|
|
|
|
2021-06-07 10:42:05 -04:00
|
|
|
if events:
|
|
|
|
now = self.clock.time_msec()
|
|
|
|
ts = await self.store.get_received_ts(events[-1].event_id)
|
2021-09-10 12:03:18 -04:00
|
|
|
assert ts is not None
|
2021-06-07 10:42:05 -04:00
|
|
|
|
|
|
|
synapse.metrics.event_processing_lag.labels(
|
|
|
|
"appservice_sender"
|
|
|
|
).set(now - ts)
|
|
|
|
synapse.metrics.event_processing_last_ts.labels(
|
|
|
|
"appservice_sender"
|
|
|
|
).set(ts)
|
2016-08-18 09:59:55 -04:00
|
|
|
finally:
|
|
|
|
self.is_processing = False
|
2015-02-05 05:08:12 -05:00
|
|
|
|
2020-10-26 05:30:19 -04:00
|
|
|
def notify_interested_services_ephemeral(
|
|
|
|
self,
|
|
|
|
stream_key: str,
|
2021-11-02 06:39:02 -04:00
|
|
|
new_token: Union[int, RoomStreamToken],
|
2021-11-09 09:31:15 -05:00
|
|
|
users: Collection[Union[str, UserID]],
|
2021-09-20 08:56:23 -04:00
|
|
|
) -> None:
|
2021-10-21 12:42:25 -04:00
|
|
|
"""
|
|
|
|
This is called by the notifier in the background when an ephemeral event is handled
|
|
|
|
by the homeserver.
|
2020-10-15 12:33:28 -04:00
|
|
|
|
2021-10-21 12:42:25 -04:00
|
|
|
This will determine which appservices are interested in the event, and submit them.
|
2020-10-15 12:33:28 -04:00
|
|
|
|
|
|
|
Args:
|
|
|
|
stream_key: The stream the event came from.
|
2021-10-21 12:42:25 -04:00
|
|
|
|
2022-03-30 09:39:27 -04:00
|
|
|
`stream_key` can be "typing_key", "receipt_key", "presence_key",
|
|
|
|
"to_device_key" or "device_list_key". Any other value for `stream_key`
|
|
|
|
will cause this function to return early.
|
2021-10-21 12:42:25 -04:00
|
|
|
|
|
|
|
Ephemeral events will only be pushed to appservices that have opted into
|
2021-11-09 09:31:15 -05:00
|
|
|
receiving them by setting `push_ephemeral` to true in their registration
|
|
|
|
file. Note that while MSC2409 is experimental, this option is called
|
|
|
|
`de.sorunome.msc2409.push_ephemeral`.
|
2021-10-21 12:42:25 -04:00
|
|
|
|
|
|
|
Appservices will only receive ephemeral events that fall within their
|
|
|
|
registered user and room namespaces.
|
|
|
|
|
2021-11-02 06:39:02 -04:00
|
|
|
new_token: The stream token of the event.
|
2021-10-21 12:42:25 -04:00
|
|
|
users: The users that should be informed of the new event, if any.
|
2020-10-15 12:33:28 -04:00
|
|
|
"""
|
2020-10-26 05:30:19 -04:00
|
|
|
if not self.notify_appservices:
|
|
|
|
return
|
|
|
|
|
2022-02-01 09:13:38 -05:00
|
|
|
# Notify appservices of updates in ephemeral event streams.
|
|
|
|
# Only the following streams are currently supported.
|
|
|
|
# FIXME: We should use constants for these values.
|
|
|
|
if stream_key not in (
|
|
|
|
"typing_key",
|
|
|
|
"receipt_key",
|
|
|
|
"presence_key",
|
|
|
|
"to_device_key",
|
2022-03-30 09:39:27 -04:00
|
|
|
"device_list_key",
|
2022-02-01 09:13:38 -05:00
|
|
|
):
|
2020-10-26 05:30:19 -04:00
|
|
|
return
|
|
|
|
|
2021-11-02 06:39:02 -04:00
|
|
|
# Assert that new_token is an integer (and not a RoomStreamToken).
|
|
|
|
# All of the supported streams that this function handles use an
|
|
|
|
# integer to track progress (rather than a RoomStreamToken - a
|
|
|
|
# vector clock implementation) as they don't support multiple
|
|
|
|
# stream writers.
|
|
|
|
#
|
|
|
|
# As a result, we simply assert that new_token is an integer.
|
|
|
|
# If we do end up needing to pass a RoomStreamToken down here
|
|
|
|
# in the future, using RoomStreamToken.stream (the minimum stream
|
|
|
|
# position) to convert to an ascending integer value should work.
|
|
|
|
# Additional context: https://github.com/matrix-org/synapse/pull/11137
|
|
|
|
assert isinstance(new_token, int)
|
|
|
|
|
2022-02-01 09:13:38 -05:00
|
|
|
# Ignore to-device messages if the feature flag is not enabled
|
|
|
|
if (
|
|
|
|
stream_key == "to_device_key"
|
|
|
|
and not self._msc2409_to_device_messages_enabled
|
|
|
|
):
|
|
|
|
return
|
|
|
|
|
2022-03-30 09:39:27 -04:00
|
|
|
# Ignore device lists if the feature flag is not enabled
|
|
|
|
if (
|
|
|
|
stream_key == "device_list_key"
|
|
|
|
and not self._msc3202_transaction_extensions_enabled
|
|
|
|
):
|
|
|
|
return
|
|
|
|
|
2021-11-09 09:31:15 -05:00
|
|
|
# Check whether there are any appservices which have registered to receive
|
|
|
|
# ephemeral events.
|
|
|
|
#
|
|
|
|
# Note that whether these events are actually relevant to these appservices
|
|
|
|
# is decided later on.
|
2022-03-30 09:39:27 -04:00
|
|
|
services = self.store.get_app_services()
|
2020-10-15 12:33:28 -04:00
|
|
|
services = [
|
|
|
|
service
|
2022-03-30 09:39:27 -04:00
|
|
|
for service in services
|
|
|
|
# Different stream keys require different support booleans
|
|
|
|
if (
|
|
|
|
stream_key
|
|
|
|
in (
|
|
|
|
"typing_key",
|
|
|
|
"receipt_key",
|
|
|
|
"presence_key",
|
|
|
|
"to_device_key",
|
|
|
|
)
|
|
|
|
and service.supports_ephemeral
|
|
|
|
)
|
|
|
|
or (
|
|
|
|
stream_key == "device_list_key"
|
|
|
|
and service.msc3202_transaction_extensions
|
|
|
|
)
|
2020-10-15 12:33:28 -04:00
|
|
|
]
|
2020-10-26 05:30:19 -04:00
|
|
|
if not services:
|
2021-11-09 09:31:15 -05:00
|
|
|
# Bail out early if none of the target appservices have explicitly registered
|
|
|
|
# to receive these ephemeral events.
|
2020-10-15 12:33:28 -04:00
|
|
|
return
|
2020-10-26 05:30:19 -04:00
|
|
|
|
|
|
|
# We only start a new background process if necessary rather than
|
|
|
|
# optimistically (to cut down on overhead).
|
|
|
|
self._notify_interested_services_ephemeral(
|
2021-11-09 09:31:15 -05:00
|
|
|
services, stream_key, new_token, users
|
2020-10-26 05:30:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
@wrap_as_background_process("notify_interested_services_ephemeral")
|
|
|
|
async def _notify_interested_services_ephemeral(
|
|
|
|
self,
|
|
|
|
services: List[ApplicationService],
|
|
|
|
stream_key: str,
|
2021-11-02 06:39:02 -04:00
|
|
|
new_token: int,
|
2020-10-26 05:30:19 -04:00
|
|
|
users: Collection[Union[str, UserID]],
|
2021-09-20 08:56:23 -04:00
|
|
|
) -> None:
|
2021-11-09 09:31:15 -05:00
|
|
|
logger.debug("Checking interested services for %s", stream_key)
|
2020-10-15 12:33:28 -04:00
|
|
|
with Measure(self.clock, "notify_interested_services_ephemeral"):
|
|
|
|
for service in services:
|
2021-11-02 06:39:02 -04:00
|
|
|
if stream_key == "typing_key":
|
2022-02-01 09:13:38 -05:00
|
|
|
# Note that we don't persist the token (via set_appservice_stream_type_pos)
|
2021-10-21 12:42:25 -04:00
|
|
|
# for typing_key due to performance reasons and due to their highly
|
|
|
|
# ephemeral nature.
|
|
|
|
#
|
|
|
|
# Instead we simply grab the latest typing updates in _handle_typing
|
|
|
|
# and, if they apply to this application service, send it off.
|
2020-10-15 12:33:28 -04:00
|
|
|
events = await self._handle_typing(service, new_token)
|
|
|
|
if events:
|
2022-02-01 09:13:38 -05:00
|
|
|
self.scheduler.enqueue_for_appservice(service, ephemeral=events)
|
2021-11-03 12:51:00 -04:00
|
|
|
continue
|
2021-10-21 12:42:25 -04:00
|
|
|
|
2021-11-03 12:51:00 -04:00
|
|
|
# Since we read/update the stream position for this AS/stream
|
2022-04-05 10:43:52 -04:00
|
|
|
async with self._ephemeral_events_linearizer.queue(
|
|
|
|
(service.id, stream_key)
|
2021-11-03 12:51:00 -04:00
|
|
|
):
|
|
|
|
if stream_key == "receipt_key":
|
|
|
|
events = await self._handle_receipts(service, new_token)
|
2022-02-01 09:13:38 -05:00
|
|
|
self.scheduler.enqueue_for_appservice(service, ephemeral=events)
|
2021-11-03 12:51:00 -04:00
|
|
|
|
|
|
|
# Persist the latest handled stream token for this appservice
|
2022-02-01 09:13:38 -05:00
|
|
|
await self.store.set_appservice_stream_type_pos(
|
2021-11-03 12:51:00 -04:00
|
|
|
service, "read_receipt", new_token
|
|
|
|
)
|
2021-10-21 12:42:25 -04:00
|
|
|
|
2021-11-03 12:51:00 -04:00
|
|
|
elif stream_key == "presence_key":
|
|
|
|
events = await self._handle_presence(service, users, new_token)
|
2022-02-01 09:13:38 -05:00
|
|
|
self.scheduler.enqueue_for_appservice(service, ephemeral=events)
|
2021-10-21 12:42:25 -04:00
|
|
|
|
2021-11-03 12:51:00 -04:00
|
|
|
# Persist the latest handled stream token for this appservice
|
2022-02-01 09:13:38 -05:00
|
|
|
await self.store.set_appservice_stream_type_pos(
|
2021-11-03 12:51:00 -04:00
|
|
|
service, "presence", new_token
|
|
|
|
)
|
2020-10-15 12:33:28 -04:00
|
|
|
|
2022-02-01 09:13:38 -05:00
|
|
|
elif stream_key == "to_device_key":
|
|
|
|
# Retrieve a list of to-device message events, as well as the
|
|
|
|
# maximum stream token of the messages we were able to retrieve.
|
|
|
|
to_device_messages = await self._get_to_device_messages(
|
|
|
|
service, new_token, users
|
|
|
|
)
|
|
|
|
self.scheduler.enqueue_for_appservice(
|
|
|
|
service, to_device_messages=to_device_messages
|
|
|
|
)
|
|
|
|
|
|
|
|
# Persist the latest handled stream token for this appservice
|
|
|
|
await self.store.set_appservice_stream_type_pos(
|
|
|
|
service, "to_device", new_token
|
|
|
|
)
|
|
|
|
|
2022-03-30 09:39:27 -04:00
|
|
|
elif stream_key == "device_list_key":
|
|
|
|
device_list_summary = await self._get_device_list_summary(
|
|
|
|
service, new_token
|
|
|
|
)
|
|
|
|
if device_list_summary:
|
|
|
|
self.scheduler.enqueue_for_appservice(
|
|
|
|
service, device_list_summary=device_list_summary
|
|
|
|
)
|
|
|
|
|
|
|
|
# Persist the latest handled stream token for this appservice
|
|
|
|
await self.store.set_appservice_stream_type_pos(
|
|
|
|
service, "device_list", new_token
|
|
|
|
)
|
|
|
|
|
2020-10-28 11:12:21 -04:00
|
|
|
async def _handle_typing(
|
|
|
|
self, service: ApplicationService, new_token: int
|
|
|
|
) -> List[JsonDict]:
|
2021-10-21 12:42:25 -04:00
|
|
|
"""
|
|
|
|
Return the typing events since the given stream token that the given application
|
|
|
|
service should receive.
|
|
|
|
|
|
|
|
First fetch all typing events between the given typing stream token (non-inclusive)
|
|
|
|
and the latest typing event stream token (inclusive). Then return only those typing
|
|
|
|
events that the given application service may be interested in.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
service: The application service to check for which events it should receive.
|
|
|
|
new_token: A typing event stream token.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of JSON dictionaries containing data derived from the typing events that
|
|
|
|
should be sent to the given application service.
|
|
|
|
"""
|
2021-09-21 13:34:26 -04:00
|
|
|
typing_source = self.event_sources.sources.typing
|
2020-10-15 12:33:28 -04:00
|
|
|
# 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
|
2021-10-21 12:42:25 -04:00
|
|
|
# token in the DB and instead fetch the latest typing event
|
2020-10-15 12:33:28 -04:00
|
|
|
# for appservices.
|
2021-10-21 12:42:25 -04:00
|
|
|
# TODO: It'd likely be more efficient to simply fetch the
|
|
|
|
# typing event with the given 'new_token' stream token and
|
|
|
|
# check if the given service was interested, rather than
|
|
|
|
# iterating over all typing events and only grabbing the
|
|
|
|
# latest few.
|
2020-10-15 12:33:28 -04:00
|
|
|
from_key=new_token - 1,
|
|
|
|
)
|
|
|
|
return typing
|
|
|
|
|
2021-11-03 12:51:00 -04:00
|
|
|
async def _handle_receipts(
|
2022-04-26 15:14:21 -04:00
|
|
|
self, service: ApplicationService, new_token: int
|
2021-11-03 12:51:00 -04:00
|
|
|
) -> List[JsonDict]:
|
2021-10-21 12:42:25 -04:00
|
|
|
"""
|
|
|
|
Return the latest read receipts that the given application service should receive.
|
|
|
|
|
|
|
|
First fetch all read receipts between the last receipt stream token that this
|
|
|
|
application service should have previously received (non-inclusive) and the
|
|
|
|
latest read receipt stream token (inclusive). Then from that set, return only
|
|
|
|
those read receipts that the given application service may be interested in.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
service: The application service to check for which events it should receive.
|
2021-11-09 09:31:15 -05:00
|
|
|
new_token: A receipts event stream token. Purely used to double-check that the
|
|
|
|
from_token we pull from the database isn't greater than or equal to this
|
|
|
|
token. Prevents accidentally duplicating work.
|
2021-10-21 12:42:25 -04:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of JSON dictionaries containing data derived from the read receipts that
|
|
|
|
should be sent to the given application service.
|
|
|
|
"""
|
2020-10-15 12:33:28 -04:00
|
|
|
from_key = await self.store.get_type_stream_id_for_appservice(
|
|
|
|
service, "read_receipt"
|
|
|
|
)
|
2021-11-03 12:51:00 -04:00
|
|
|
if new_token is not None and new_token <= from_key:
|
|
|
|
logger.debug(
|
|
|
|
"Rejecting token lower than or equal to stored: %s" % (new_token,)
|
|
|
|
)
|
|
|
|
return []
|
|
|
|
|
2021-09-21 13:34:26 -04:00
|
|
|
receipts_source = self.event_sources.sources.receipt
|
2020-10-15 12:33:28 -04:00
|
|
|
receipts, _ = await receipts_source.get_new_events_as(
|
2022-04-26 15:14:21 -04:00
|
|
|
service=service, from_key=from_key, to_key=new_token
|
2020-10-15 12:33:28 -04:00
|
|
|
)
|
|
|
|
return receipts
|
|
|
|
|
|
|
|
async def _handle_presence(
|
2021-11-03 12:51:00 -04:00
|
|
|
self,
|
|
|
|
service: ApplicationService,
|
|
|
|
users: Collection[Union[str, UserID]],
|
|
|
|
new_token: Optional[int],
|
2020-10-28 11:12:21 -04:00
|
|
|
) -> List[JsonDict]:
|
2021-10-21 12:42:25 -04:00
|
|
|
"""
|
|
|
|
Return the latest presence updates that the given application service should receive.
|
|
|
|
|
|
|
|
First, filter the given users list to those that the application service is
|
|
|
|
interested in. Then retrieve the latest presence updates since the
|
|
|
|
the last-known previously received presence stream token for the given
|
|
|
|
application service. Return those presence updates.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
service: The application service that ephemeral events are being sent to.
|
|
|
|
users: The users that should receive the presence update.
|
2021-11-09 09:31:15 -05:00
|
|
|
new_token: A presence update stream token. Purely used to double-check that the
|
|
|
|
from_token we pull from the database isn't greater than or equal to this
|
|
|
|
token. Prevents accidentally duplicating work.
|
2021-10-21 12:42:25 -04:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of json dictionaries containing data derived from the presence events
|
|
|
|
that should be sent to the given application service.
|
|
|
|
"""
|
2021-07-16 13:22:36 -04:00
|
|
|
events: List[JsonDict] = []
|
2021-09-21 13:34:26 -04:00
|
|
|
presence_source = self.event_sources.sources.presence
|
2020-10-15 12:33:28 -04:00
|
|
|
from_key = await self.store.get_type_stream_id_for_appservice(
|
|
|
|
service, "presence"
|
|
|
|
)
|
2021-11-03 12:51:00 -04:00
|
|
|
if new_token is not None and new_token <= from_key:
|
|
|
|
logger.debug(
|
|
|
|
"Rejecting token lower than or equal to stored: %s" % (new_token,)
|
|
|
|
)
|
|
|
|
return []
|
|
|
|
|
2020-10-15 12:33:28 -04:00
|
|
|
for user in users:
|
2020-10-26 05:30:19 -04:00
|
|
|
if isinstance(user, str):
|
|
|
|
user = UserID.from_string(user)
|
|
|
|
|
2020-10-15 12:33:28 -04:00
|
|
|
interested = await service.is_interested_in_presence(user, self.store)
|
|
|
|
if not interested:
|
|
|
|
continue
|
2021-10-21 12:42:25 -04:00
|
|
|
|
2020-10-15 12:33:28 -04:00
|
|
|
presence_events, _ = await presence_source.get_new_events(
|
2021-02-16 17:32:34 -05:00
|
|
|
user=user,
|
|
|
|
from_key=from_key,
|
2020-10-15 12:33:28 -04:00
|
|
|
)
|
|
|
|
time_now = self.clock.time_msec()
|
2020-10-26 09:19:07 -04:00
|
|
|
events.extend(
|
2020-10-15 12:33:28 -04:00
|
|
|
{
|
|
|
|
"type": "m.presence",
|
|
|
|
"sender": event.user_id,
|
|
|
|
"content": format_user_presence_state(
|
|
|
|
event, time_now, include_user_id=False
|
|
|
|
),
|
|
|
|
}
|
|
|
|
for event in presence_events
|
2020-10-26 09:19:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
return events
|
2020-10-15 12:33:28 -04:00
|
|
|
|
2022-02-01 09:13:38 -05:00
|
|
|
async def _get_to_device_messages(
|
|
|
|
self,
|
|
|
|
service: ApplicationService,
|
|
|
|
new_token: int,
|
|
|
|
users: Collection[Union[str, UserID]],
|
|
|
|
) -> List[JsonDict]:
|
|
|
|
"""
|
|
|
|
Given an application service, determine which events it should receive
|
|
|
|
from those between the last-recorded to-device message stream token for this
|
|
|
|
appservice and the given stream token.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
service: The application service to check for which events it should receive.
|
|
|
|
new_token: The latest to-device event stream token.
|
|
|
|
users: The users to be notified for the new to-device messages
|
|
|
|
(ie, the recipients of the messages).
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A list of JSON dictionaries containing data derived from the to-device events
|
|
|
|
that should be sent to the given application service.
|
|
|
|
"""
|
|
|
|
# Get the stream token that this application service has processed up until
|
|
|
|
from_key = await self.store.get_type_stream_id_for_appservice(
|
|
|
|
service, "to_device"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Filter out users that this appservice is not interested in
|
|
|
|
users_appservice_is_interested_in: List[str] = []
|
|
|
|
for user in users:
|
|
|
|
# FIXME: We should do this farther up the call stack. We currently repeat
|
|
|
|
# this operation in _handle_presence.
|
|
|
|
if isinstance(user, UserID):
|
|
|
|
user = user.to_string()
|
|
|
|
|
|
|
|
if service.is_interested_in_user(user):
|
|
|
|
users_appservice_is_interested_in.append(user)
|
|
|
|
|
|
|
|
if not users_appservice_is_interested_in:
|
|
|
|
# Return early if the AS was not interested in any of these users
|
|
|
|
return []
|
|
|
|
|
|
|
|
# Retrieve the to-device messages for each user
|
|
|
|
recipient_device_to_messages = await self.store.get_messages_for_user_devices(
|
|
|
|
users_appservice_is_interested_in,
|
|
|
|
from_key,
|
|
|
|
new_token,
|
|
|
|
)
|
|
|
|
|
|
|
|
# According to MSC2409, we'll need to add 'to_user_id' and 'to_device_id' fields
|
|
|
|
# to the event JSON so that the application service will know which user/device
|
|
|
|
# combination this messages was intended for.
|
|
|
|
#
|
|
|
|
# So we mangle this dict into a flat list of to-device messages with the relevant
|
|
|
|
# user ID and device ID embedded inside each message dict.
|
|
|
|
message_payload: List[JsonDict] = []
|
|
|
|
for (
|
|
|
|
user_id,
|
|
|
|
device_id,
|
|
|
|
), messages in recipient_device_to_messages.items():
|
|
|
|
for message_json in messages:
|
|
|
|
# Remove 'message_id' from the to-device message, as it's an internal ID
|
|
|
|
message_json.pop("message_id", None)
|
|
|
|
|
|
|
|
message_payload.append(
|
|
|
|
{
|
|
|
|
"to_user_id": user_id,
|
|
|
|
"to_device_id": device_id,
|
|
|
|
**message_json,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return message_payload
|
|
|
|
|
2022-03-30 09:39:27 -04:00
|
|
|
async def _get_device_list_summary(
|
|
|
|
self,
|
|
|
|
appservice: ApplicationService,
|
|
|
|
new_key: int,
|
|
|
|
) -> DeviceListUpdates:
|
|
|
|
"""
|
|
|
|
Retrieve a list of users who have changed their device lists.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
appservice: The application service to retrieve device list changes for.
|
|
|
|
new_key: The stream key of the device list change that triggered this method call.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A set of device list updates, comprised of users that the appservices needs to:
|
|
|
|
* resync the device list of, and
|
|
|
|
* stop tracking the device list of.
|
|
|
|
"""
|
|
|
|
# Fetch the last successfully processed device list update stream ID
|
|
|
|
# for this appservice.
|
|
|
|
from_key = await self.store.get_type_stream_id_for_appservice(
|
|
|
|
appservice, "device_list"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Fetch the users who have modified their device list since then.
|
|
|
|
users_with_changed_device_lists = (
|
|
|
|
await self.store.get_users_whose_devices_changed(from_key, to_key=new_key)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Filter out any users the application service is not interested in
|
|
|
|
#
|
|
|
|
# For each user who changed their device list, we want to check whether this
|
|
|
|
# appservice would be interested in the change.
|
|
|
|
filtered_users_with_changed_device_lists = {
|
|
|
|
user_id
|
|
|
|
for user_id in users_with_changed_device_lists
|
|
|
|
if await self._is_appservice_interested_in_device_lists_of_user(
|
|
|
|
appservice, user_id
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create a summary of "changed" and "left" users.
|
|
|
|
# TODO: Calculate "left" users.
|
|
|
|
device_list_summary = DeviceListUpdates(
|
|
|
|
changed=filtered_users_with_changed_device_lists
|
|
|
|
)
|
|
|
|
|
|
|
|
return device_list_summary
|
|
|
|
|
|
|
|
async def _is_appservice_interested_in_device_lists_of_user(
|
|
|
|
self,
|
|
|
|
appservice: ApplicationService,
|
|
|
|
user_id: str,
|
|
|
|
) -> bool:
|
|
|
|
"""
|
|
|
|
Returns whether a given application service is interested in the device list
|
|
|
|
updates of a given user.
|
|
|
|
|
|
|
|
The application service is interested in the user's device list updates if any
|
|
|
|
of the following are true:
|
|
|
|
* The user is the appservice's sender localpart user.
|
|
|
|
* The user is in the appservice's user namespace.
|
|
|
|
* At least one member of one room that the user is a part of is in the
|
|
|
|
appservice's user namespace.
|
|
|
|
* The appservice is explicitly (via room ID or alias) interested in at
|
|
|
|
least one room that the user is in.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
appservice: The application service to gauge interest of.
|
|
|
|
user_id: The ID of the user whose device list interest is in question.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if the application service is interested in the user's device lists, False
|
|
|
|
otherwise.
|
|
|
|
"""
|
|
|
|
# This method checks against both the sender localpart user as well as if the
|
|
|
|
# user is in the appservice's user namespace.
|
|
|
|
if appservice.is_interested_in_user(user_id):
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Determine whether any of the rooms the user is in justifies sending this
|
|
|
|
# device list update to the application service.
|
|
|
|
room_ids = await self.store.get_rooms_for_user(user_id)
|
|
|
|
for room_id in room_ids:
|
|
|
|
# This method covers checking room members for appservice interest as well as
|
|
|
|
# room ID and alias checks.
|
|
|
|
if await appservice.is_interested_in_room(room_id, self.store):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2020-10-28 11:12:21 -04:00
|
|
|
async def query_user_exists(self, user_id: str) -> bool:
|
2015-02-05 09:17:08 -05:00
|
|
|
"""Check if any application service knows this user_id exists.
|
2015-02-05 08:19:46 -05:00
|
|
|
|
|
|
|
Args:
|
2020-10-28 11:12:21 -04:00
|
|
|
user_id: The user to query if they exist on any AS.
|
2015-02-05 08:19:46 -05:00
|
|
|
Returns:
|
2015-02-05 09:17:08 -05:00
|
|
|
True if this user exists on at least one application service.
|
2015-02-05 08:19:46 -05:00
|
|
|
"""
|
2020-07-06 07:40:35 -04:00
|
|
|
user_query_services = self._get_services_for_user(user_id=user_id)
|
2015-02-05 08:19:46 -05:00
|
|
|
for user_service in user_query_services:
|
2020-07-06 07:40:35 -04:00
|
|
|
is_known_user = await self.appservice_api.query_user(user_service, user_id)
|
2015-02-05 08:19:46 -05:00
|
|
|
if is_known_user:
|
2019-07-23 09:00:55 -04:00
|
|
|
return True
|
|
|
|
return False
|
2015-02-05 06:47:11 -05:00
|
|
|
|
2020-10-28 11:12:21 -04:00
|
|
|
async def query_room_alias_exists(
|
|
|
|
self, room_alias: RoomAlias
|
|
|
|
) -> Optional[RoomAliasMapping]:
|
2015-02-05 06:47:11 -05:00
|
|
|
"""Check if an application service knows this room alias exists.
|
|
|
|
|
|
|
|
Args:
|
2020-10-28 11:12:21 -04:00
|
|
|
room_alias: The room alias to query.
|
2021-12-30 13:47:12 -05:00
|
|
|
|
2015-02-05 06:47:11 -05:00
|
|
|
Returns:
|
2021-12-30 13:47:12 -05:00
|
|
|
RoomAliasMapping or None if no association can be found.
|
2015-02-05 06:47:11 -05:00
|
|
|
"""
|
2015-02-09 10:01:28 -05:00
|
|
|
room_alias_str = room_alias.to_string()
|
2016-10-06 04:43:32 -04:00
|
|
|
services = self.store.get_app_services()
|
2016-08-17 12:20:50 -04:00
|
|
|
alias_query_services = [
|
2022-03-03 13:14:09 -05:00
|
|
|
s for s in services if (s.is_room_alias_in_namespace(room_alias_str))
|
2016-08-17 12:20:50 -04:00
|
|
|
]
|
2015-02-05 06:47:11 -05:00
|
|
|
for alias_service in alias_query_services:
|
2020-07-06 07:40:35 -04:00
|
|
|
is_known_alias = await self.appservice_api.query_alias(
|
2015-02-09 10:01:28 -05:00
|
|
|
alias_service, room_alias_str
|
2015-02-05 06:47:11 -05:00
|
|
|
)
|
|
|
|
if is_known_alias:
|
|
|
|
# the alias exists now so don't query more ASes.
|
2020-10-28 11:12:21 -04:00
|
|
|
return await self.store.get_association_from_room_alias(room_alias)
|
|
|
|
|
|
|
|
return None
|
2015-02-05 06:47:11 -05:00
|
|
|
|
2020-10-28 11:12:21 -04:00
|
|
|
async def query_3pe(
|
|
|
|
self, kind: str, protocol: str, fields: Dict[bytes, List[bytes]]
|
|
|
|
) -> List[JsonDict]:
|
2020-07-06 07:40:35 -04:00
|
|
|
services = self._get_services_for_3pn(protocol)
|
2016-08-17 08:15:06 -04:00
|
|
|
|
2020-07-06 07:40:35 -04:00
|
|
|
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,
|
2018-04-27 06:29:27 -04:00
|
|
|
)
|
2019-06-20 05:32:02 -04:00
|
|
|
)
|
2016-08-18 10:40:41 -04:00
|
|
|
|
2016-08-18 11:09:50 -04:00
|
|
|
ret = []
|
|
|
|
for (success, result) in results:
|
2016-08-18 12:33:56 -04:00
|
|
|
if success:
|
|
|
|
ret.extend(result)
|
2016-08-17 08:15:06 -04:00
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return ret
|
2016-08-17 08:15:06 -04:00
|
|
|
|
2020-10-28 11:12:21 -04:00
|
|
|
async def get_3pe_protocols(
|
|
|
|
self, only_protocol: Optional[str] = None
|
|
|
|
) -> Dict[str, JsonDict]:
|
2016-10-06 04:43:32 -04:00
|
|
|
services = self.store.get_app_services()
|
2021-07-16 13:22:36 -04:00
|
|
|
protocols: Dict[str, List[JsonDict]] = {}
|
2016-09-08 12:43:53 -04:00
|
|
|
|
|
|
|
# Collect up all the individual protocol responses out of the ASes
|
2016-08-24 07:33:01 -04:00
|
|
|
for s in services:
|
2016-08-24 08:01:53 -04:00
|
|
|
for p in s.protocols:
|
2016-09-09 08:25:02 -04:00
|
|
|
if only_protocol is not None and p != only_protocol:
|
|
|
|
continue
|
|
|
|
|
2016-09-08 12:43:53 -04:00
|
|
|
if p not in protocols:
|
|
|
|
protocols[p] = []
|
2016-09-09 10:07:04 -04:00
|
|
|
|
2020-07-06 07:40:35 -04:00
|
|
|
info = await self.appservice_api.get_3pe_protocol(s, p)
|
2016-09-09 10:07:04 -04:00
|
|
|
|
|
|
|
if info is not None:
|
|
|
|
protocols[p].append(info)
|
2016-09-08 12:43:53 -04:00
|
|
|
|
2020-10-28 11:12:21 -04:00
|
|
|
def _merge_instances(infos: List[JsonDict]) -> JsonDict:
|
2016-09-08 12:43:53 -04:00
|
|
|
# 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
|
2016-09-08 12:43:53 -04:00
|
|
|
combined = dict(infos[0])
|
2016-09-09 08:10:36 -04:00
|
|
|
combined["instances"] = list(combined["instances"])
|
2016-09-08 12:43:53 -04:00
|
|
|
|
|
|
|
for info in infos[1:]:
|
|
|
|
combined["instances"].extend(info["instances"])
|
|
|
|
|
|
|
|
return combined
|
|
|
|
|
2021-08-05 08:22:14 -04:00
|
|
|
return {
|
|
|
|
p: _merge_instances(protocols[p]) for p in protocols.keys() if protocols[p]
|
|
|
|
}
|
2016-08-24 08:01:53 -04:00
|
|
|
|
2020-10-28 11:12:21 -04:00
|
|
|
async def _get_services_for_event(
|
|
|
|
self, event: EventBase
|
|
|
|
) -> List[ApplicationService]:
|
2015-02-05 06:47:11 -05:00
|
|
|
"""Retrieve a list of application services interested in this event.
|
|
|
|
|
|
|
|
Args:
|
2022-02-07 13:26:42 -05:00
|
|
|
event: The event to check.
|
2015-02-05 06:47:11 -05:00
|
|
|
Returns:
|
2020-10-28 11:12:21 -04:00
|
|
|
A list of services interested in this event based on the service regex.
|
2015-02-05 06:47:11 -05:00
|
|
|
"""
|
2016-10-06 04:43:32 -04:00
|
|
|
services = self.store.get_app_services()
|
2018-04-28 07:04:40 -04: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:
|
2022-03-03 13:14:09 -05:00
|
|
|
if await s.is_interested_in_event(event.event_id, event, self.store):
|
2018-04-28 07:04:40 -04:00
|
|
|
interested_list.append(s)
|
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return interested_list
|
2015-02-05 06:47:11 -05:00
|
|
|
|
2020-10-28 11:12:21 -04:00
|
|
|
def _get_services_for_user(self, user_id: str) -> List[ApplicationService]:
|
2016-10-06 04:43:32 -04:00
|
|
|
services = self.store.get_app_services()
|
2020-10-28 11:12:21 -04:00
|
|
|
return [s for s in services if (s.is_interested_in_user(user_id))]
|
2015-02-05 09:17:08 -05:00
|
|
|
|
2020-10-28 11:12:21 -04:00
|
|
|
def _get_services_for_3pn(self, protocol: str) -> List[ApplicationService]:
|
2016-10-06 04:43:32 -04:00
|
|
|
services = self.store.get_app_services()
|
2020-10-28 11:12:21 -04:00
|
|
|
return [s for s in services if s.is_interested_in_protocol(protocol)]
|
2016-08-17 08:15:06 -04:00
|
|
|
|
2020-10-28 11:12:21 -04:00
|
|
|
async def _is_unknown_user(self, user_id: str) -> bool:
|
2016-05-31 08:53:48 -04:00
|
|
|
if not self.is_mine_id(user_id):
|
2015-02-05 06:25:32 -05: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 09:00:55 -04:00
|
|
|
return False
|
2015-02-05 06:25:32 -05:00
|
|
|
|
2020-07-06 07:40:35 -04:00
|
|
|
user_info = await self.store.get_user_by_id(user_id)
|
2015-06-17 12:26:03 -04:00
|
|
|
if user_info:
|
2019-07-23 09:00:55 -04:00
|
|
|
return False
|
2015-04-01 09:05:24 -04:00
|
|
|
|
|
|
|
# user not found; could be the AS though, so check.
|
2016-10-06 04:43:32 -04:00
|
|
|
services = self.store.get_app_services()
|
2015-04-01 09:05:24 -04:00
|
|
|
service_list = [s for s in services if s.sender == user_id]
|
2019-07-23 09:00:55 -04:00
|
|
|
return len(service_list) == 0
|
2015-02-05 06:25:32 -05:00
|
|
|
|
2020-10-28 11:12:21 -04:00
|
|
|
async def _check_user_exists(self, user_id: str) -> bool:
|
2020-07-06 07:40:35 -04:00
|
|
|
unknown_user = await self._is_unknown_user(user_id)
|
2015-02-05 09:17:08 -05:00
|
|
|
if unknown_user:
|
2020-10-28 11:12:21 -04:00
|
|
|
return await self.query_user_exists(user_id)
|
2019-07-23 09:00:55 -04:00
|
|
|
return True
|