2016-01-06 23:26:29 -05:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2022-03-30 09:39:27 -04:00
|
|
|
# Copyright 2022 The Matrix.org Foundation C.I.C.
|
2015-02-03 09:44:16 -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.
|
2021-12-14 12:02:46 -05:00
|
|
|
|
2015-02-04 07:24:20 -05:00
|
|
|
import logging
|
2015-02-03 09:44:16 -05:00
|
|
|
import re
|
2021-12-02 10:30:05 -05:00
|
|
|
from enum import Enum
|
2023-02-06 07:49:06 -05:00
|
|
|
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Pattern, Sequence
|
2021-12-14 12:02:46 -05:00
|
|
|
|
|
|
|
import attr
|
|
|
|
from netaddr import IPSet
|
2015-02-03 09:44:16 -05:00
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.api.constants import EventTypes
|
2020-10-15 12:33:28 -04:00
|
|
|
from synapse.events import EventBase
|
2022-05-26 09:04:34 -04:00
|
|
|
from synapse.types import DeviceListUpdates, JsonDict, UserID
|
2020-10-29 12:58:16 -04:00
|
|
|
from synapse.util.caches.descriptors import _CacheContext, cached
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2020-08-28 07:54:27 -04:00
|
|
|
if TYPE_CHECKING:
|
2020-10-15 12:33:28 -04:00
|
|
|
from synapse.appservice.api import ApplicationServiceApi
|
2020-08-28 07:54:27 -04:00
|
|
|
from synapse.storage.databases.main import DataStore
|
|
|
|
|
2015-02-04 07:24:20 -05:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-11-28 11:17:29 -05:00
|
|
|
# Type for the `device_one_time_keys_count` field in an appservice transaction
|
2022-02-24 12:55:45 -05:00
|
|
|
# user ID -> {device ID -> {algorithm -> count}}
|
2022-11-28 11:17:29 -05:00
|
|
|
TransactionOneTimeKeysCount = Dict[str, Dict[str, Dict[str, int]]]
|
2022-02-24 12:55:45 -05:00
|
|
|
|
2022-04-22 11:03:46 -04:00
|
|
|
# Type for the `device_unused_fallback_key_types` field in an appservice transaction
|
2022-02-24 12:55:45 -05:00
|
|
|
# user ID -> {device ID -> [algorithm]}
|
|
|
|
TransactionUnusedFallbackKeys = Dict[str, Dict[str, List[str]]]
|
|
|
|
|
2015-02-03 09:44:16 -05:00
|
|
|
|
2021-12-02 10:30:05 -05:00
|
|
|
class ApplicationServiceState(Enum):
|
2015-03-06 10:12:24 -05:00
|
|
|
DOWN = "down"
|
|
|
|
UP = "up"
|
|
|
|
|
|
|
|
|
2021-12-14 12:02:46 -05:00
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
|
|
|
class Namespace:
|
|
|
|
exclusive: bool
|
|
|
|
regex: Pattern[str]
|
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class ApplicationService:
|
2015-02-03 09:44:16 -05:00
|
|
|
"""Defines an application service. This definition is mostly what is
|
|
|
|
provided to the /register AS API.
|
|
|
|
|
|
|
|
Provides methods to check if this service is "interested" in events.
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2015-02-03 09:44:16 -05:00
|
|
|
NS_USERS = "users"
|
|
|
|
NS_ALIASES = "aliases"
|
|
|
|
NS_ROOMS = "rooms"
|
|
|
|
# The ordering here is important as it is used to map database values (which
|
|
|
|
# are stored as ints representing the position in this list) to namespace
|
|
|
|
# values.
|
|
|
|
NS_LIST = [NS_USERS, NS_ALIASES, NS_ROOMS]
|
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-12-14 12:02:46 -05:00
|
|
|
token: str,
|
|
|
|
id: str,
|
|
|
|
sender: str,
|
|
|
|
url: Optional[str] = None,
|
|
|
|
namespaces: Optional[JsonDict] = None,
|
|
|
|
hs_token: Optional[str] = None,
|
|
|
|
protocols: Optional[Iterable[str]] = None,
|
|
|
|
rate_limited: bool = True,
|
|
|
|
ip_range_whitelist: Optional[IPSet] = None,
|
|
|
|
supports_ephemeral: bool = False,
|
2022-02-24 12:55:45 -05:00
|
|
|
msc3202_transaction_extensions: bool = False,
|
2019-06-20 05:32:02 -04:00
|
|
|
):
|
2015-02-03 09:44:16 -05:00
|
|
|
self.token = token
|
2019-10-31 13:32:25 -04:00
|
|
|
self.url = (
|
|
|
|
url.rstrip("/") if isinstance(url, str) else None
|
|
|
|
) # url must not end with a slash
|
2015-02-05 05:08:12 -05:00
|
|
|
self.hs_token = hs_token
|
2023-05-23 10:35:43 -04:00
|
|
|
# The full Matrix ID for this application service's sender.
|
2015-02-09 07:03:37 -05:00
|
|
|
self.sender = sender
|
2015-02-03 09:44:16 -05:00
|
|
|
self.namespaces = self._check_namespaces(namespaces)
|
2015-03-06 12:27:55 -05:00
|
|
|
self.id = id
|
2018-06-28 15:31:53 -04:00
|
|
|
self.ip_range_whitelist = ip_range_whitelist
|
2020-10-15 12:33:28 -04:00
|
|
|
self.supports_ephemeral = supports_ephemeral
|
2022-02-24 12:55:45 -05:00
|
|
|
self.msc3202_transaction_extensions = msc3202_transaction_extensions
|
2016-08-24 07:33:01 -04:00
|
|
|
|
2016-12-06 05:43:48 -05:00
|
|
|
if "|" in self.id:
|
|
|
|
raise Exception("application service ID cannot contain '|' character")
|
|
|
|
|
2016-08-24 07:33:01 -04:00
|
|
|
# .protocols is a publicly visible field
|
2016-08-18 09:56:02 -04:00
|
|
|
if protocols:
|
|
|
|
self.protocols = set(protocols)
|
|
|
|
else:
|
|
|
|
self.protocols = set()
|
2015-02-03 09:44:16 -05:00
|
|
|
|
2016-10-18 12:04:09 -04:00
|
|
|
self.rate_limited = rate_limited
|
|
|
|
|
2021-12-14 12:02:46 -05:00
|
|
|
def _check_namespaces(
|
|
|
|
self, namespaces: Optional[JsonDict]
|
|
|
|
) -> Dict[str, List[Namespace]]:
|
2015-02-03 09:44:16 -05:00
|
|
|
# Sanity check that it is of the form:
|
|
|
|
# {
|
2015-02-27 05:44:32 -05:00
|
|
|
# users: [ {regex: "[A-z]+.*", exclusive: true}, ...],
|
|
|
|
# aliases: [ {regex: "[A-z]+.*", exclusive: true}, ...],
|
|
|
|
# rooms: [ {regex: "[A-z]+.*", exclusive: true}, ...],
|
2015-02-03 09:44:16 -05:00
|
|
|
# }
|
2021-12-14 12:02:46 -05:00
|
|
|
if namespaces is None:
|
2015-03-31 06:35:45 -04:00
|
|
|
namespaces = {}
|
2015-02-03 09:44:16 -05:00
|
|
|
|
2021-12-14 12:02:46 -05:00
|
|
|
result: Dict[str, List[Namespace]] = {}
|
|
|
|
|
2015-02-03 09:44:16 -05:00
|
|
|
for ns in ApplicationService.NS_LIST:
|
2021-12-14 12:02:46 -05:00
|
|
|
result[ns] = []
|
|
|
|
|
2015-02-27 05:44:32 -05:00
|
|
|
if ns not in namespaces:
|
|
|
|
continue
|
|
|
|
|
2021-12-14 12:02:46 -05:00
|
|
|
if not isinstance(namespaces[ns], list):
|
2015-02-27 05:44:32 -05:00
|
|
|
raise ValueError("Bad namespace value for '%s'" % ns)
|
|
|
|
for regex_obj in namespaces[ns]:
|
|
|
|
if not isinstance(regex_obj, dict):
|
|
|
|
raise ValueError("Expected dict regex for ns '%s'" % ns)
|
2021-12-14 12:02:46 -05:00
|
|
|
exclusive = regex_obj.get("exclusive")
|
|
|
|
if not isinstance(exclusive, bool):
|
2019-06-20 05:32:02 -04:00
|
|
|
raise ValueError("Expected bool for 'exclusive' in ns '%s'" % ns)
|
2017-11-16 12:54:27 -05:00
|
|
|
|
2017-03-28 08:03:50 -04:00
|
|
|
regex = regex_obj.get("regex")
|
2021-12-14 12:02:46 -05:00
|
|
|
if not isinstance(regex, str):
|
2019-06-20 05:32:02 -04:00
|
|
|
raise ValueError("Expected string for 'regex' in ns '%s'" % ns)
|
2015-02-03 09:44:16 -05:00
|
|
|
|
2021-12-14 12:02:46 -05:00
|
|
|
# Pre-compile regex.
|
2022-05-26 09:04:34 -04:00
|
|
|
result[ns].append(Namespace(exclusive, re.compile(regex)))
|
2021-12-14 12:02:46 -05:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
def _matches_regex(
|
|
|
|
self, namespace_key: str, test_string: str
|
|
|
|
) -> Optional[Namespace]:
|
|
|
|
for namespace in self.namespaces[namespace_key]:
|
|
|
|
if namespace.regex.match(test_string):
|
|
|
|
return namespace
|
2017-03-28 08:20:15 -04:00
|
|
|
return None
|
2015-02-03 09:44:16 -05:00
|
|
|
|
2021-12-14 12:02:46 -05:00
|
|
|
def _is_exclusive(self, namespace_key: str, test_string: str) -> bool:
|
|
|
|
namespace = self._matches_regex(namespace_key, test_string)
|
|
|
|
if namespace:
|
|
|
|
return namespace.exclusive
|
2015-02-27 06:03:56 -05:00
|
|
|
return False
|
|
|
|
|
2020-10-29 12:58:16 -04:00
|
|
|
@cached(num_args=1, cache_context=True)
|
2022-03-03 13:14:09 -05:00
|
|
|
async def _matches_user_in_member_list(
|
2021-02-16 17:32:34 -05:00
|
|
|
self,
|
|
|
|
room_id: str,
|
|
|
|
store: "DataStore",
|
|
|
|
cache_context: _CacheContext,
|
2020-10-15 12:33:28 -04:00
|
|
|
) -> bool:
|
2022-03-03 13:14:09 -05:00
|
|
|
"""Check if this service is interested a room based upon its membership
|
2020-10-15 12:33:28 -04:00
|
|
|
|
|
|
|
Args:
|
|
|
|
room_id: The room to check.
|
|
|
|
store: The datastore to query.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if this service would like to know about this room.
|
|
|
|
"""
|
2022-10-27 14:29:23 -04:00
|
|
|
# We can use `get_local_users_in_room(...)` here because an application service
|
|
|
|
# can only be interested in local users of the server it's on (ignore any remote
|
|
|
|
# users that might match the user namespace regex).
|
|
|
|
#
|
|
|
|
# In the future, we can consider re-using
|
|
|
|
# `store.get_app_service_users_in_room` which is very similar to this
|
|
|
|
# function but has a slightly worse performance than this because we
|
|
|
|
# have an early escape-hatch if we find a single user that the
|
|
|
|
# appservice is interested in. The juice would be worth the squeeze if
|
|
|
|
# `store.get_app_service_users_in_room` was used in more places besides
|
|
|
|
# an experimental MSC. But for now we can avoid doing more work and
|
|
|
|
# barely using it later.
|
|
|
|
local_user_ids = await store.get_local_users_in_room(
|
2020-10-29 12:58:16 -04:00
|
|
|
room_id, on_invalidate=cache_context.invalidate
|
|
|
|
)
|
2016-08-17 12:20:50 -04:00
|
|
|
|
2015-02-11 05:36:08 -05:00
|
|
|
# check joined member events
|
2022-10-27 14:29:23 -04:00
|
|
|
for user_id in local_user_ids:
|
2015-05-22 06:01:28 -04:00
|
|
|
if self.is_interested_in_user(user_id):
|
2019-07-23 09:00:55 -04:00
|
|
|
return True
|
|
|
|
return False
|
2015-02-03 09:44:16 -05:00
|
|
|
|
2022-03-03 13:14:09 -05:00
|
|
|
def is_interested_in_user(
|
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
) -> bool:
|
|
|
|
"""
|
|
|
|
Returns whether the application is interested in a given user ID.
|
|
|
|
|
|
|
|
The appservice is considered to be interested in a user if either: the
|
|
|
|
user ID is in the appservice's user namespace, or if the user is the
|
|
|
|
appservice's configured sender_localpart.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id: The ID of the user to check.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if the application service is interested in the user, False if not.
|
|
|
|
"""
|
|
|
|
return (
|
2023-05-23 10:35:43 -04:00
|
|
|
# User is the appservice's configured sender_localpart user
|
2022-03-03 13:14:09 -05:00
|
|
|
user_id == self.sender
|
|
|
|
# User is in the appservice's user namespace
|
|
|
|
or self.is_user_in_namespace(user_id)
|
|
|
|
)
|
|
|
|
|
|
|
|
@cached(num_args=1, cache_context=True)
|
|
|
|
async def is_interested_in_room(
|
|
|
|
self,
|
|
|
|
room_id: str,
|
|
|
|
store: "DataStore",
|
|
|
|
cache_context: _CacheContext,
|
|
|
|
) -> bool:
|
|
|
|
"""
|
|
|
|
Returns whether the application service is interested in a given room ID.
|
|
|
|
|
|
|
|
The appservice is considered to be interested in the room if either: the ID or one
|
|
|
|
of the aliases of the room is in the appservice's room ID or alias namespace
|
|
|
|
respectively, or if one of the members of the room fall into the appservice's user
|
|
|
|
namespace.
|
2015-02-03 09:44:16 -05:00
|
|
|
|
2022-03-03 13:14:09 -05:00
|
|
|
Args:
|
|
|
|
room_id: The ID of the room to check.
|
|
|
|
store: The homeserver's datastore class.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if the application service is interested in the room, False if not.
|
|
|
|
"""
|
|
|
|
# Check if we have interest in this room ID
|
|
|
|
if self.is_room_id_in_namespace(room_id):
|
|
|
|
return True
|
|
|
|
|
|
|
|
# likewise with the room's aliases (if it has any)
|
2022-12-12 13:13:43 -05:00
|
|
|
alias_list = await store.get_aliases_for_room(
|
|
|
|
room_id, on_invalidate=cache_context.invalidate
|
|
|
|
)
|
2015-02-03 09:44:16 -05:00
|
|
|
for alias in alias_list:
|
2022-03-03 13:14:09 -05:00
|
|
|
if self.is_room_alias_in_namespace(alias):
|
2019-07-23 09:00:55 -04:00
|
|
|
return True
|
2022-02-07 13:26:42 -05:00
|
|
|
|
2022-03-03 13:14:09 -05:00
|
|
|
# And finally, perform an expensive check on whether any of the
|
|
|
|
# users in the room match the appservice's user namespace
|
|
|
|
return await self._matches_user_in_member_list(
|
|
|
|
room_id, store, on_invalidate=cache_context.invalidate
|
|
|
|
)
|
2015-02-03 09:44:16 -05:00
|
|
|
|
2022-03-03 13:14:09 -05:00
|
|
|
@cached(num_args=1, cache_context=True)
|
|
|
|
async def is_interested_in_event(
|
|
|
|
self,
|
|
|
|
event_id: str,
|
|
|
|
event: EventBase,
|
|
|
|
store: "DataStore",
|
|
|
|
cache_context: _CacheContext,
|
|
|
|
) -> bool:
|
2015-02-03 09:44:16 -05:00
|
|
|
"""Check if this service is interested in this event.
|
|
|
|
|
|
|
|
Args:
|
2022-03-03 13:14:09 -05:00
|
|
|
event_id: The ID of the event to check. This is purely used for simplifying the
|
|
|
|
caching of calls to this method.
|
2020-10-15 12:33:28 -04:00
|
|
|
event: The event to check.
|
|
|
|
store: The datastore to query.
|
|
|
|
|
2015-02-03 09:44:16 -05:00
|
|
|
Returns:
|
2022-03-03 13:14:09 -05:00
|
|
|
True if this service would like to know about this event, otherwise False.
|
2015-02-03 09:44:16 -05:00
|
|
|
"""
|
2022-03-03 13:14:09 -05:00
|
|
|
# Check if we're interested in this event's sender by namespace (or if they're the
|
|
|
|
# sender_localpart user)
|
|
|
|
if self.is_interested_in_user(event.sender):
|
2019-07-23 09:00:55 -04:00
|
|
|
return True
|
2016-08-17 12:20:50 -04:00
|
|
|
|
2022-03-03 13:14:09 -05:00
|
|
|
# additionally, if this is a membership event, perform the same checks on
|
|
|
|
# the user it references
|
|
|
|
if event.type == EventTypes.Member and self.is_interested_in_user(
|
|
|
|
event.state_key
|
|
|
|
):
|
2020-10-15 12:33:28 -04:00
|
|
|
return True
|
|
|
|
|
2022-03-03 13:14:09 -05:00
|
|
|
# This will check the datastore, so should be run last
|
|
|
|
if await self.is_interested_in_room(
|
|
|
|
event.room_id, store, on_invalidate=cache_context.invalidate
|
|
|
|
):
|
2019-07-23 09:00:55 -04:00
|
|
|
return True
|
2016-08-17 12:20:50 -04:00
|
|
|
|
2020-10-15 12:33:28 -04:00
|
|
|
return False
|
|
|
|
|
2022-03-03 13:14:09 -05:00
|
|
|
@cached(num_args=1, cache_context=True)
|
2020-10-15 12:33:28 -04:00
|
|
|
async def is_interested_in_presence(
|
2022-03-03 13:14:09 -05:00
|
|
|
self, user_id: UserID, store: "DataStore", cache_context: _CacheContext
|
2020-10-15 12:33:28 -04:00
|
|
|
) -> bool:
|
|
|
|
"""Check if this service is interested a user's presence
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_id: The user to check.
|
|
|
|
store: The datastore to query.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
True if this service would like to know about presence for this user.
|
|
|
|
"""
|
|
|
|
# Find all the rooms the sender is in
|
|
|
|
if self.is_interested_in_user(user_id.to_string()):
|
2019-07-23 09:00:55 -04:00
|
|
|
return True
|
2022-12-12 13:13:43 -05:00
|
|
|
room_ids = await store.get_rooms_for_user(
|
|
|
|
user_id.to_string(), on_invalidate=cache_context.invalidate
|
|
|
|
)
|
2016-08-17 12:20:50 -04:00
|
|
|
|
2020-10-15 12:33:28 -04:00
|
|
|
# Then find out if the appservice is interested in any of those rooms
|
|
|
|
for room_id in room_ids:
|
2022-03-03 13:14:09 -05:00
|
|
|
if await self.is_interested_in_room(
|
|
|
|
room_id, store, on_invalidate=cache_context.invalidate
|
|
|
|
):
|
2020-10-15 12:33:28 -04:00
|
|
|
return True
|
2019-07-23 09:00:55 -04:00
|
|
|
return False
|
2015-02-03 09:44:16 -05:00
|
|
|
|
2022-03-03 13:14:09 -05:00
|
|
|
def is_user_in_namespace(self, user_id: str) -> bool:
|
|
|
|
return bool(self._matches_regex(ApplicationService.NS_USERS, user_id))
|
2015-02-05 09:17:08 -05:00
|
|
|
|
2022-03-03 13:14:09 -05:00
|
|
|
def is_room_alias_in_namespace(self, alias: str) -> bool:
|
2021-12-14 12:02:46 -05:00
|
|
|
return bool(self._matches_regex(ApplicationService.NS_ALIASES, alias))
|
2015-02-05 09:17:08 -05:00
|
|
|
|
2022-03-03 13:14:09 -05:00
|
|
|
def is_room_id_in_namespace(self, room_id: str) -> bool:
|
2021-12-14 12:02:46 -05:00
|
|
|
return bool(self._matches_regex(ApplicationService.NS_ROOMS, room_id))
|
2015-02-05 09:17:08 -05:00
|
|
|
|
2020-10-15 12:33:28 -04:00
|
|
|
def is_exclusive_user(self, user_id: str) -> bool:
|
2015-04-01 09:05:24 -04:00
|
|
|
return (
|
|
|
|
self._is_exclusive(ApplicationService.NS_USERS, user_id)
|
|
|
|
or user_id == self.sender
|
|
|
|
)
|
2015-02-27 06:03:56 -05:00
|
|
|
|
2020-10-15 12:33:28 -04:00
|
|
|
def is_interested_in_protocol(self, protocol: str) -> bool:
|
2016-08-18 09:56:02 -04:00
|
|
|
return protocol in self.protocols
|
|
|
|
|
2020-10-15 12:33:28 -04:00
|
|
|
def is_exclusive_alias(self, alias: str) -> bool:
|
2015-02-27 06:03:56 -05:00
|
|
|
return self._is_exclusive(ApplicationService.NS_ALIASES, alias)
|
|
|
|
|
2020-10-15 12:33:28 -04:00
|
|
|
def is_exclusive_room(self, room_id: str) -> bool:
|
2015-02-27 06:03:56 -05:00
|
|
|
return self._is_exclusive(ApplicationService.NS_ROOMS, room_id)
|
|
|
|
|
2021-12-14 12:02:46 -05:00
|
|
|
def get_exclusive_user_regexes(self) -> List[Pattern[str]]:
|
2017-05-18 11:34:41 -04:00
|
|
|
"""Get the list of regexes used to determine if a user is exclusively
|
|
|
|
registered by the AS
|
|
|
|
"""
|
|
|
|
return [
|
2021-12-14 12:02:46 -05:00
|
|
|
namespace.regex
|
|
|
|
for namespace in self.namespaces[ApplicationService.NS_USERS]
|
|
|
|
if namespace.exclusive
|
2017-05-18 11:34:41 -04:00
|
|
|
]
|
|
|
|
|
2020-10-15 12:33:28 -04:00
|
|
|
def is_rate_limited(self) -> bool:
|
2016-10-18 12:04:09 -04:00
|
|
|
return self.rate_limited
|
|
|
|
|
2021-12-14 12:02:46 -05:00
|
|
|
def __str__(self) -> str:
|
2018-06-06 05:35:29 -04:00
|
|
|
# copy dictionary and redact token fields so they don't get logged
|
|
|
|
dict_copy = self.__dict__.copy()
|
|
|
|
dict_copy["token"] = "<redacted>"
|
|
|
|
dict_copy["hs_token"] = "<redacted>"
|
|
|
|
return "ApplicationService: %s" % (dict_copy,)
|
2020-10-15 12:33:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
class AppServiceTransaction:
|
|
|
|
"""Represents an application service transaction."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
service: ApplicationService,
|
|
|
|
id: int,
|
2023-02-06 07:49:06 -05:00
|
|
|
events: Sequence[EventBase],
|
2020-10-15 12:33:28 -04:00
|
|
|
ephemeral: List[JsonDict],
|
2022-02-01 09:13:38 -05:00
|
|
|
to_device_messages: List[JsonDict],
|
2022-11-28 11:17:29 -05:00
|
|
|
one_time_keys_count: TransactionOneTimeKeysCount,
|
2022-02-24 12:55:45 -05:00
|
|
|
unused_fallback_keys: TransactionUnusedFallbackKeys,
|
2022-03-30 09:39:27 -04:00
|
|
|
device_list_summary: DeviceListUpdates,
|
2020-10-15 12:33:28 -04:00
|
|
|
):
|
|
|
|
self.service = service
|
|
|
|
self.id = id
|
|
|
|
self.events = events
|
|
|
|
self.ephemeral = ephemeral
|
2022-02-01 09:13:38 -05:00
|
|
|
self.to_device_messages = to_device_messages
|
2022-11-28 11:17:29 -05:00
|
|
|
self.one_time_keys_count = one_time_keys_count
|
2022-02-24 12:55:45 -05:00
|
|
|
self.unused_fallback_keys = unused_fallback_keys
|
2022-03-30 09:39:27 -04:00
|
|
|
self.device_list_summary = device_list_summary
|
2020-10-15 12:33:28 -04:00
|
|
|
|
|
|
|
async def send(self, as_api: "ApplicationServiceApi") -> bool:
|
|
|
|
"""Sends this transaction using the provided AS API interface.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
as_api: The API to use to send.
|
|
|
|
Returns:
|
|
|
|
True if the transaction was sent.
|
|
|
|
"""
|
|
|
|
return await as_api.push_bulk(
|
|
|
|
service=self.service,
|
|
|
|
events=self.events,
|
|
|
|
ephemeral=self.ephemeral,
|
2022-02-01 09:13:38 -05:00
|
|
|
to_device_messages=self.to_device_messages,
|
2022-11-28 11:17:29 -05:00
|
|
|
one_time_keys_count=self.one_time_keys_count,
|
2022-02-24 12:55:45 -05:00
|
|
|
unused_fallback_keys=self.unused_fallback_keys,
|
2022-03-30 09:39:27 -04:00
|
|
|
device_list_summary=self.device_list_summary,
|
2020-10-15 12:33:28 -04:00
|
|
|
txn_id=self.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
async def complete(self, store: "DataStore") -> None:
|
|
|
|
"""Completes this transaction as successful.
|
|
|
|
|
|
|
|
Marks this transaction ID on the application service and removes the
|
|
|
|
transaction contents from the database.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
store: The database store to operate on.
|
|
|
|
"""
|
|
|
|
await store.complete_appservice_txn(service=self.service, txn_id=self.id)
|