appservice.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. import re
  17. from typing import TYPE_CHECKING, Any, Dict, List, Optional, Pattern, Tuple, cast
  18. from synapse.appservice import (
  19. ApplicationService,
  20. ApplicationServiceState,
  21. AppServiceTransaction,
  22. TransactionOneTimeKeyCounts,
  23. TransactionUnusedFallbackKeys,
  24. )
  25. from synapse.config.appservice import load_appservices
  26. from synapse.events import EventBase
  27. from synapse.storage._base import db_to_json
  28. from synapse.storage.database import (
  29. DatabasePool,
  30. LoggingDatabaseConnection,
  31. LoggingTransaction,
  32. )
  33. from synapse.storage.databases.main.events_worker import EventsWorkerStore
  34. from synapse.storage.databases.main.roommember import RoomMemberWorkerStore
  35. from synapse.storage.types import Cursor
  36. from synapse.storage.util.sequence import build_sequence_generator
  37. from synapse.types import DeviceListUpdates, JsonDict
  38. from synapse.util import json_encoder
  39. from synapse.util.caches.descriptors import _CacheContext, cached
  40. if TYPE_CHECKING:
  41. from synapse.server import HomeServer
  42. logger = logging.getLogger(__name__)
  43. def _make_exclusive_regex(
  44. services_cache: List[ApplicationService],
  45. ) -> Optional[Pattern]:
  46. # We precompile a regex constructed from all the regexes that the AS's
  47. # have registered for exclusive users.
  48. exclusive_user_regexes = [
  49. regex.pattern
  50. for service in services_cache
  51. for regex in service.get_exclusive_user_regexes()
  52. ]
  53. if exclusive_user_regexes:
  54. exclusive_user_regex = "|".join("(" + r + ")" for r in exclusive_user_regexes)
  55. exclusive_user_pattern: Optional[Pattern] = re.compile(exclusive_user_regex)
  56. else:
  57. # We handle this case specially otherwise the constructed regex
  58. # will always match
  59. exclusive_user_pattern = None
  60. return exclusive_user_pattern
  61. class ApplicationServiceWorkerStore(RoomMemberWorkerStore):
  62. def __init__(
  63. self,
  64. database: DatabasePool,
  65. db_conn: LoggingDatabaseConnection,
  66. hs: "HomeServer",
  67. ):
  68. self.services_cache = load_appservices(
  69. hs.hostname, hs.config.appservice.app_service_config_files
  70. )
  71. self.exclusive_user_regex = _make_exclusive_regex(self.services_cache)
  72. def get_max_as_txn_id(txn: Cursor) -> int:
  73. logger.warning("Falling back to slow query, you should port to postgres")
  74. txn.execute(
  75. "SELECT COALESCE(max(txn_id), 0) FROM application_services_txns"
  76. )
  77. return cast(Tuple[int], txn.fetchone())[0]
  78. self._as_txn_seq_gen = build_sequence_generator(
  79. db_conn,
  80. database.engine,
  81. get_max_as_txn_id,
  82. "application_services_txn_id_seq",
  83. table="application_services_txns",
  84. id_column="txn_id",
  85. )
  86. super().__init__(database, db_conn, hs)
  87. def get_app_services(self) -> List[ApplicationService]:
  88. return self.services_cache
  89. def get_if_app_services_interested_in_user(self, user_id: str) -> bool:
  90. """Check if the user is one associated with an app service (exclusively)"""
  91. if self.exclusive_user_regex:
  92. return bool(self.exclusive_user_regex.match(user_id))
  93. else:
  94. return False
  95. def get_app_service_by_user_id(self, user_id: str) -> Optional[ApplicationService]:
  96. """Retrieve an application service from their user ID.
  97. All application services have associated with them a particular user ID.
  98. There is no distinguishing feature on the user ID which indicates it
  99. represents an application service. This function allows you to map from
  100. a user ID to an application service.
  101. Args:
  102. user_id: The user ID to see if it is an application service.
  103. Returns:
  104. The application service or None.
  105. """
  106. for service in self.services_cache:
  107. if service.sender == user_id:
  108. return service
  109. return None
  110. def get_app_service_by_token(self, token: str) -> Optional[ApplicationService]:
  111. """Get the application service with the given appservice token.
  112. Args:
  113. token: The application service token.
  114. Returns:
  115. The application service or None.
  116. """
  117. for service in self.services_cache:
  118. if service.token == token:
  119. return service
  120. return None
  121. def get_app_service_by_id(self, as_id: str) -> Optional[ApplicationService]:
  122. """Get the application service with the given appservice ID.
  123. Args:
  124. as_id: The application service ID.
  125. Returns:
  126. The application service or None.
  127. """
  128. for service in self.services_cache:
  129. if service.id == as_id:
  130. return service
  131. return None
  132. @cached(iterable=True, cache_context=True)
  133. async def get_app_service_users_in_room(
  134. self,
  135. room_id: str,
  136. app_service: "ApplicationService",
  137. cache_context: _CacheContext,
  138. ) -> List[str]:
  139. users_in_room = await self.get_users_in_room(
  140. room_id, on_invalidate=cache_context.invalidate
  141. )
  142. return list(filter(app_service.is_interested_in_user, users_in_room))
  143. class ApplicationServiceStore(ApplicationServiceWorkerStore):
  144. # This is currently empty due to there not being any AS storage functions
  145. # that can't be run on the workers. Since this may change in future, and
  146. # to keep consistency with the other stores, we keep this empty class for
  147. # now.
  148. pass
  149. class ApplicationServiceTransactionWorkerStore(
  150. ApplicationServiceWorkerStore, EventsWorkerStore
  151. ):
  152. async def get_appservices_by_state(
  153. self, state: ApplicationServiceState
  154. ) -> List[ApplicationService]:
  155. """Get a list of application services based on their state.
  156. Args:
  157. state: The state to filter on.
  158. Returns:
  159. A list of ApplicationServices, which may be empty.
  160. """
  161. results = await self.db_pool.simple_select_list(
  162. "application_services_state", {"state": state.value}, ["as_id"]
  163. )
  164. # NB: This assumes this class is linked with ApplicationServiceStore
  165. as_list = self.get_app_services()
  166. services = []
  167. for res in results:
  168. for service in as_list:
  169. if service.id == res["as_id"]:
  170. services.append(service)
  171. return services
  172. async def get_appservice_state(
  173. self, service: ApplicationService
  174. ) -> Optional[ApplicationServiceState]:
  175. """Get the application service state.
  176. Args:
  177. service: The service whose state to get.
  178. Returns:
  179. An ApplicationServiceState, or None if we have yet to attempt any
  180. transactions to the AS.
  181. """
  182. # if we have created transactions for this AS but not yet attempted to send
  183. # them, we will have a row in the table with state=NULL (recording the stream
  184. # positions we have processed up to).
  185. #
  186. # On the other hand, if we have yet to create any transactions for this AS at
  187. # all, then there will be no row for the AS.
  188. #
  189. # In either case, we return None to indicate "we don't yet know the state of
  190. # this AS".
  191. result = await self.db_pool.simple_select_one_onecol(
  192. "application_services_state",
  193. {"as_id": service.id},
  194. retcol="state",
  195. allow_none=True,
  196. desc="get_appservice_state",
  197. )
  198. if result:
  199. return ApplicationServiceState(result)
  200. return None
  201. async def set_appservice_state(
  202. self, service: ApplicationService, state: ApplicationServiceState
  203. ) -> None:
  204. """Set the application service state.
  205. Args:
  206. service: The service whose state to set.
  207. state: The connectivity state to apply.
  208. """
  209. await self.db_pool.simple_upsert(
  210. "application_services_state", {"as_id": service.id}, {"state": state.value}
  211. )
  212. async def create_appservice_txn(
  213. self,
  214. service: ApplicationService,
  215. events: List[EventBase],
  216. ephemeral: List[JsonDict],
  217. to_device_messages: List[JsonDict],
  218. one_time_key_counts: TransactionOneTimeKeyCounts,
  219. unused_fallback_keys: TransactionUnusedFallbackKeys,
  220. device_list_summary: DeviceListUpdates,
  221. ) -> AppServiceTransaction:
  222. """Atomically creates a new transaction for this application service
  223. with the given list of events. Ephemeral events are NOT persisted to the
  224. database and are not resent if a transaction is retried.
  225. Args:
  226. service: The service who the transaction is for.
  227. events: A list of persistent events to put in the transaction.
  228. ephemeral: A list of ephemeral events to put in the transaction.
  229. to_device_messages: A list of to-device messages to put in the transaction.
  230. one_time_key_counts: Counts of remaining one-time keys for relevant
  231. appservice devices in the transaction.
  232. unused_fallback_keys: Lists of unused fallback keys for relevant
  233. appservice devices in the transaction.
  234. device_list_summary: The device list summary to include in the transaction.
  235. Returns:
  236. A new transaction.
  237. """
  238. def _create_appservice_txn(txn: LoggingTransaction) -> AppServiceTransaction:
  239. new_txn_id = self._as_txn_seq_gen.get_next_id_txn(txn)
  240. # Insert new txn into txn table
  241. event_ids = json_encoder.encode([e.event_id for e in events])
  242. txn.execute(
  243. "INSERT INTO application_services_txns(as_id, txn_id, event_ids) "
  244. "VALUES(?,?,?)",
  245. (service.id, new_txn_id, event_ids),
  246. )
  247. return AppServiceTransaction(
  248. service=service,
  249. id=new_txn_id,
  250. events=events,
  251. ephemeral=ephemeral,
  252. to_device_messages=to_device_messages,
  253. one_time_key_counts=one_time_key_counts,
  254. unused_fallback_keys=unused_fallback_keys,
  255. device_list_summary=device_list_summary,
  256. )
  257. return await self.db_pool.runInteraction(
  258. "create_appservice_txn", _create_appservice_txn
  259. )
  260. async def complete_appservice_txn(
  261. self, txn_id: int, service: ApplicationService
  262. ) -> None:
  263. """Completes an application service transaction.
  264. Args:
  265. txn_id: The transaction ID being completed.
  266. service: The application service which was sent this transaction.
  267. """
  268. def _complete_appservice_txn(txn: LoggingTransaction) -> None:
  269. # Delete txn
  270. self.db_pool.simple_delete_txn(
  271. txn,
  272. "application_services_txns",
  273. {"txn_id": txn_id, "as_id": service.id},
  274. )
  275. await self.db_pool.runInteraction(
  276. "complete_appservice_txn", _complete_appservice_txn
  277. )
  278. async def get_oldest_unsent_txn(
  279. self, service: ApplicationService
  280. ) -> Optional[AppServiceTransaction]:
  281. """Get the oldest transaction which has not been sent for this service.
  282. Args:
  283. service: The app service to get the oldest txn.
  284. Returns:
  285. An AppServiceTransaction or None.
  286. """
  287. def _get_oldest_unsent_txn(
  288. txn: LoggingTransaction,
  289. ) -> Optional[Dict[str, Any]]:
  290. # Monotonically increasing txn ids, so just select the smallest
  291. # one in the txns table (we delete them when they are sent)
  292. txn.execute(
  293. "SELECT * FROM application_services_txns WHERE as_id=?"
  294. " ORDER BY txn_id ASC LIMIT 1",
  295. (service.id,),
  296. )
  297. rows = self.db_pool.cursor_to_dict(txn)
  298. if not rows:
  299. return None
  300. entry = rows[0]
  301. return entry
  302. entry = await self.db_pool.runInteraction(
  303. "get_oldest_unsent_appservice_txn", _get_oldest_unsent_txn
  304. )
  305. if not entry:
  306. return None
  307. event_ids = db_to_json(entry["event_ids"])
  308. events = await self.get_events_as_list(event_ids)
  309. # TODO: to-device messages, one-time key counts, device list summaries and unused
  310. # fallback keys are not yet populated for catch-up transactions.
  311. # We likely want to populate those for reliability.
  312. return AppServiceTransaction(
  313. service=service,
  314. id=entry["txn_id"],
  315. events=events,
  316. ephemeral=[],
  317. to_device_messages=[],
  318. one_time_key_counts={},
  319. unused_fallback_keys={},
  320. device_list_summary=DeviceListUpdates(),
  321. )
  322. async def set_appservice_last_pos(self, pos: int) -> None:
  323. def set_appservice_last_pos_txn(txn: LoggingTransaction) -> None:
  324. txn.execute(
  325. "UPDATE appservice_stream_position SET stream_ordering = ?", (pos,)
  326. )
  327. await self.db_pool.runInteraction(
  328. "set_appservice_last_pos", set_appservice_last_pos_txn
  329. )
  330. async def get_new_events_for_appservice(
  331. self, current_id: int, limit: int
  332. ) -> Tuple[int, List[EventBase]]:
  333. """Get all new events for an appservice"""
  334. def get_new_events_for_appservice_txn(
  335. txn: LoggingTransaction,
  336. ) -> Tuple[int, List[str]]:
  337. sql = (
  338. "SELECT e.stream_ordering, e.event_id"
  339. " FROM events AS e"
  340. " WHERE"
  341. " (SELECT stream_ordering FROM appservice_stream_position)"
  342. " < e.stream_ordering"
  343. " AND e.stream_ordering <= ?"
  344. " ORDER BY e.stream_ordering ASC"
  345. " LIMIT ?"
  346. )
  347. txn.execute(sql, (current_id, limit))
  348. rows = txn.fetchall()
  349. upper_bound = current_id
  350. if len(rows) == limit:
  351. upper_bound = rows[-1][0]
  352. return upper_bound, [row[1] for row in rows]
  353. upper_bound, event_ids = await self.db_pool.runInteraction(
  354. "get_new_events_for_appservice", get_new_events_for_appservice_txn
  355. )
  356. events = await self.get_events_as_list(event_ids, get_prev_content=True)
  357. return upper_bound, events
  358. async def get_type_stream_id_for_appservice(
  359. self, service: ApplicationService, type: str
  360. ) -> int:
  361. if type not in ("read_receipt", "presence", "to_device", "device_list"):
  362. raise ValueError(
  363. "Expected type to be a valid application stream id type, got %s"
  364. % (type,)
  365. )
  366. def get_type_stream_id_for_appservice_txn(txn: LoggingTransaction) -> int:
  367. stream_id_type = "%s_stream_id" % type
  368. txn.execute(
  369. # We do NOT want to escape `stream_id_type`.
  370. "SELECT %s FROM application_services_state WHERE as_id=?"
  371. % stream_id_type,
  372. (service.id,),
  373. )
  374. last_stream_id = txn.fetchone()
  375. if last_stream_id is None or last_stream_id[0] is None: # no row exists
  376. # Stream tokens always start from 1, to avoid foot guns around `0` being falsey.
  377. return 1
  378. else:
  379. return int(last_stream_id[0])
  380. return await self.db_pool.runInteraction(
  381. "get_type_stream_id_for_appservice", get_type_stream_id_for_appservice_txn
  382. )
  383. async def set_appservice_stream_type_pos(
  384. self, service: ApplicationService, stream_type: str, pos: Optional[int]
  385. ) -> None:
  386. if stream_type not in ("read_receipt", "presence", "to_device", "device_list"):
  387. raise ValueError(
  388. "Expected type to be a valid application stream id type, got %s"
  389. % (stream_type,)
  390. )
  391. # this may be the first time that we're recording any state for this AS, so
  392. # we don't yet know if a row for it exists; hence we have to upsert here.
  393. await self.db_pool.simple_upsert(
  394. table="application_services_state",
  395. keyvalues={"as_id": service.id},
  396. values={f"{stream_type}_stream_id": pos},
  397. # no need to lock when emulating upsert: as_id is a unique key
  398. lock=False,
  399. desc="set_appservice_stream_type_pos",
  400. )
  401. class ApplicationServiceTransactionStore(ApplicationServiceTransactionWorkerStore):
  402. # This is currently empty due to there not being any AS storage functions
  403. # that can't be run on the workers. Since this may change in future, and
  404. # to keep consistency with the other stores, we keep this empty class for
  405. # now.
  406. pass