__init__.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2018 New Vector Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from synapse.storage.devices import DeviceStore
  17. from .appservice import (
  18. ApplicationServiceStore, ApplicationServiceTransactionStore
  19. )
  20. from .directory import DirectoryStore
  21. from .events import EventsStore
  22. from .presence import PresenceStore, UserPresenceState
  23. from .profile import ProfileStore
  24. from .registration import RegistrationStore
  25. from .room import RoomStore
  26. from .roommember import RoomMemberStore
  27. from .stream import StreamStore
  28. from .transactions import TransactionStore
  29. from .keys import KeyStore
  30. from .event_federation import EventFederationStore
  31. from .pusher import PusherStore
  32. from .push_rule import PushRuleStore
  33. from .media_repository import MediaRepositoryStore
  34. from .rejections import RejectionsStore
  35. from .event_push_actions import EventPushActionsStore
  36. from .deviceinbox import DeviceInboxStore
  37. from .group_server import GroupServerStore
  38. from .state import StateStore
  39. from .signatures import SignatureStore
  40. from .filtering import FilteringStore
  41. from .end_to_end_keys import EndToEndKeyStore
  42. from .receipts import ReceiptsStore
  43. from .search import SearchStore
  44. from .tags import TagsStore
  45. from .account_data import AccountDataStore
  46. from .openid import OpenIdStore
  47. from .client_ips import ClientIpStore
  48. from .user_directory import UserDirectoryStore
  49. from .util.id_generators import IdGenerator, StreamIdGenerator, ChainedIdGenerator
  50. from .engines import PostgresEngine
  51. from synapse.api.constants import PresenceState
  52. from synapse.util.caches.stream_change_cache import StreamChangeCache
  53. import logging
  54. logger = logging.getLogger(__name__)
  55. class DataStore(RoomMemberStore, RoomStore,
  56. RegistrationStore, StreamStore, ProfileStore,
  57. PresenceStore, TransactionStore,
  58. DirectoryStore, KeyStore, StateStore, SignatureStore,
  59. ApplicationServiceStore,
  60. EventFederationStore,
  61. MediaRepositoryStore,
  62. RejectionsStore,
  63. FilteringStore,
  64. PusherStore,
  65. PushRuleStore,
  66. ApplicationServiceTransactionStore,
  67. EventsStore,
  68. ReceiptsStore,
  69. EndToEndKeyStore,
  70. SearchStore,
  71. TagsStore,
  72. AccountDataStore,
  73. EventPushActionsStore,
  74. OpenIdStore,
  75. ClientIpStore,
  76. DeviceStore,
  77. DeviceInboxStore,
  78. UserDirectoryStore,
  79. GroupServerStore,
  80. ):
  81. def __init__(self, db_conn, hs):
  82. self.hs = hs
  83. self._clock = hs.get_clock()
  84. self.database_engine = hs.database_engine
  85. self._stream_id_gen = StreamIdGenerator(
  86. db_conn, "events", "stream_ordering",
  87. extra_tables=[("local_invites", "stream_id")]
  88. )
  89. self._backfill_id_gen = StreamIdGenerator(
  90. db_conn, "events", "stream_ordering", step=-1,
  91. extra_tables=[("ex_outlier_stream", "event_stream_ordering")]
  92. )
  93. self._presence_id_gen = StreamIdGenerator(
  94. db_conn, "presence_stream", "stream_id"
  95. )
  96. self._device_inbox_id_gen = StreamIdGenerator(
  97. db_conn, "device_max_stream_id", "stream_id"
  98. )
  99. self._public_room_id_gen = StreamIdGenerator(
  100. db_conn, "public_room_list_stream", "stream_id"
  101. )
  102. self._device_list_id_gen = StreamIdGenerator(
  103. db_conn, "device_lists_stream", "stream_id",
  104. )
  105. self._transaction_id_gen = IdGenerator(db_conn, "sent_transactions", "id")
  106. self._access_tokens_id_gen = IdGenerator(db_conn, "access_tokens", "id")
  107. self._event_reports_id_gen = IdGenerator(db_conn, "event_reports", "id")
  108. self._push_rule_id_gen = IdGenerator(db_conn, "push_rules", "id")
  109. self._push_rules_enable_id_gen = IdGenerator(db_conn, "push_rules_enable", "id")
  110. self._push_rules_stream_id_gen = ChainedIdGenerator(
  111. self._stream_id_gen, db_conn, "push_rules_stream", "stream_id"
  112. )
  113. self._pushers_id_gen = StreamIdGenerator(
  114. db_conn, "pushers", "id",
  115. extra_tables=[("deleted_pushers", "stream_id")],
  116. )
  117. self._group_updates_id_gen = StreamIdGenerator(
  118. db_conn, "local_group_updates", "stream_id",
  119. )
  120. if isinstance(self.database_engine, PostgresEngine):
  121. self._cache_id_gen = StreamIdGenerator(
  122. db_conn, "cache_invalidation_stream", "stream_id",
  123. )
  124. else:
  125. self._cache_id_gen = None
  126. self._presence_on_startup = self._get_active_presence(db_conn)
  127. presence_cache_prefill, min_presence_val = self._get_cache_dict(
  128. db_conn, "presence_stream",
  129. entity_column="user_id",
  130. stream_column="stream_id",
  131. max_value=self._presence_id_gen.get_current_token(),
  132. )
  133. self.presence_stream_cache = StreamChangeCache(
  134. "PresenceStreamChangeCache", min_presence_val,
  135. prefilled_cache=presence_cache_prefill
  136. )
  137. max_device_inbox_id = self._device_inbox_id_gen.get_current_token()
  138. device_inbox_prefill, min_device_inbox_id = self._get_cache_dict(
  139. db_conn, "device_inbox",
  140. entity_column="user_id",
  141. stream_column="stream_id",
  142. max_value=max_device_inbox_id,
  143. limit=1000,
  144. )
  145. self._device_inbox_stream_cache = StreamChangeCache(
  146. "DeviceInboxStreamChangeCache", min_device_inbox_id,
  147. prefilled_cache=device_inbox_prefill,
  148. )
  149. # The federation outbox and the local device inbox uses the same
  150. # stream_id generator.
  151. device_outbox_prefill, min_device_outbox_id = self._get_cache_dict(
  152. db_conn, "device_federation_outbox",
  153. entity_column="destination",
  154. stream_column="stream_id",
  155. max_value=max_device_inbox_id,
  156. limit=1000,
  157. )
  158. self._device_federation_outbox_stream_cache = StreamChangeCache(
  159. "DeviceFederationOutboxStreamChangeCache", min_device_outbox_id,
  160. prefilled_cache=device_outbox_prefill,
  161. )
  162. device_list_max = self._device_list_id_gen.get_current_token()
  163. self._device_list_stream_cache = StreamChangeCache(
  164. "DeviceListStreamChangeCache", device_list_max,
  165. )
  166. self._device_list_federation_stream_cache = StreamChangeCache(
  167. "DeviceListFederationStreamChangeCache", device_list_max,
  168. )
  169. events_max = self._stream_id_gen.get_current_token()
  170. curr_state_delta_prefill, min_curr_state_delta_id = self._get_cache_dict(
  171. db_conn, "current_state_delta_stream",
  172. entity_column="room_id",
  173. stream_column="stream_id",
  174. max_value=events_max, # As we share the stream id with events token
  175. limit=1000,
  176. )
  177. self._curr_state_delta_stream_cache = StreamChangeCache(
  178. "_curr_state_delta_stream_cache", min_curr_state_delta_id,
  179. prefilled_cache=curr_state_delta_prefill,
  180. )
  181. _group_updates_prefill, min_group_updates_id = self._get_cache_dict(
  182. db_conn, "local_group_updates",
  183. entity_column="user_id",
  184. stream_column="stream_id",
  185. max_value=self._group_updates_id_gen.get_current_token(),
  186. limit=1000,
  187. )
  188. self._group_updates_stream_cache = StreamChangeCache(
  189. "_group_updates_stream_cache", min_group_updates_id,
  190. prefilled_cache=_group_updates_prefill,
  191. )
  192. self._stream_order_on_start = self.get_room_max_stream_ordering()
  193. self._min_stream_order_on_start = self.get_room_min_stream_ordering()
  194. super(DataStore, self).__init__(db_conn, hs)
  195. def take_presence_startup_info(self):
  196. active_on_startup = self._presence_on_startup
  197. self._presence_on_startup = None
  198. return active_on_startup
  199. def _get_active_presence(self, db_conn):
  200. """Fetch non-offline presence from the database so that we can register
  201. the appropriate time outs.
  202. """
  203. sql = (
  204. "SELECT user_id, state, last_active_ts, last_federation_update_ts,"
  205. " last_user_sync_ts, status_msg, currently_active FROM presence_stream"
  206. " WHERE state != ?"
  207. )
  208. sql = self.database_engine.convert_param_style(sql)
  209. txn = db_conn.cursor()
  210. txn.execute(sql, (PresenceState.OFFLINE,))
  211. rows = self.cursor_to_dict(txn)
  212. txn.close()
  213. for row in rows:
  214. row["currently_active"] = bool(row["currently_active"])
  215. return [UserPresenceState(**row) for row in rows]
  216. def count_daily_users(self):
  217. """
  218. Counts the number of users who used this homeserver in the last 24 hours.
  219. """
  220. def _count_users(txn):
  221. yesterday = int(self._clock.time_msec()) - (1000 * 60 * 60 * 24)
  222. sql = """
  223. SELECT COALESCE(count(*), 0) FROM (
  224. SELECT user_id FROM user_ips
  225. WHERE last_seen > ?
  226. GROUP BY user_id
  227. ) u
  228. """
  229. txn.execute(sql, (yesterday,))
  230. count, = txn.fetchone()
  231. return count
  232. return self.runInteraction("count_users", _count_users)
  233. def count_r30_users(self):
  234. """
  235. Counts the number of 30 day retained users, defined as:-
  236. * Users who have created their accounts more than 30 days ago
  237. * Where last seen at most 30 days ago
  238. * Where account creation and last_seen are > 30 days apart
  239. Returns counts globaly for a given user as well as breaking
  240. by platform
  241. """
  242. def _count_r30_users(txn):
  243. thirty_days_in_secs = 86400 * 30
  244. now = int(self._clock.time())
  245. thirty_days_ago_in_secs = now - thirty_days_in_secs
  246. sql = """
  247. SELECT platform, COALESCE(count(*), 0) FROM (
  248. SELECT
  249. users.name, platform, users.creation_ts * 1000,
  250. MAX(uip.last_seen)
  251. FROM users
  252. INNER JOIN (
  253. SELECT
  254. user_id,
  255. last_seen,
  256. CASE
  257. WHEN user_agent LIKE '%%Android%%' THEN 'android'
  258. WHEN user_agent LIKE '%%iOS%%' THEN 'ios'
  259. WHEN user_agent LIKE '%%Electron%%' THEN 'electron'
  260. WHEN user_agent LIKE '%%Mozilla%%' THEN 'web'
  261. WHEN user_agent LIKE '%%Gecko%%' THEN 'web'
  262. ELSE 'unknown'
  263. END
  264. AS platform
  265. FROM user_ips
  266. ) uip
  267. ON users.name = uip.user_id
  268. AND users.appservice_id is NULL
  269. AND users.creation_ts < ?
  270. AND uip.last_seen/1000 > ?
  271. AND (uip.last_seen/1000) - users.creation_ts > 86400 * 30
  272. GROUP BY users.name, platform, users.creation_ts
  273. ) u GROUP BY platform
  274. """
  275. results = {}
  276. txn.execute(sql, (thirty_days_ago_in_secs,
  277. thirty_days_ago_in_secs))
  278. for row in txn:
  279. if row[0] is 'unknown':
  280. pass
  281. results[row[0]] = row[1]
  282. sql = """
  283. SELECT COALESCE(count(*), 0) FROM (
  284. SELECT users.name, users.creation_ts * 1000,
  285. MAX(uip.last_seen)
  286. FROM users
  287. INNER JOIN (
  288. SELECT
  289. user_id,
  290. last_seen
  291. FROM user_ips
  292. ) uip
  293. ON users.name = uip.user_id
  294. AND appservice_id is NULL
  295. AND users.creation_ts < ?
  296. AND uip.last_seen/1000 > ?
  297. AND (uip.last_seen/1000) - users.creation_ts > 86400 * 30
  298. GROUP BY users.name, users.creation_ts
  299. ) u
  300. """
  301. txn.execute(sql, (thirty_days_ago_in_secs,
  302. thirty_days_ago_in_secs))
  303. count, = txn.fetchone()
  304. results['all'] = count
  305. return results
  306. return self.runInteraction("count_r30_users", _count_r30_users)
  307. def get_users(self):
  308. """Function to reterive a list of users in users table.
  309. Args:
  310. Returns:
  311. defer.Deferred: resolves to list[dict[str, Any]]
  312. """
  313. return self._simple_select_list(
  314. table="users",
  315. keyvalues={},
  316. retcols=[
  317. "name",
  318. "password_hash",
  319. "is_guest",
  320. "admin"
  321. ],
  322. desc="get_users",
  323. )
  324. def get_users_paginate(self, order, start, limit):
  325. """Function to reterive a paginated list of users from
  326. users list. This will return a json object, which contains
  327. list of users and the total number of users in users table.
  328. Args:
  329. order (str): column name to order the select by this column
  330. start (int): start number to begin the query from
  331. limit (int): number of rows to reterive
  332. Returns:
  333. defer.Deferred: resolves to json object {list[dict[str, Any]], count}
  334. """
  335. is_guest = 0
  336. i_start = (int)(start)
  337. i_limit = (int)(limit)
  338. return self.get_user_list_paginate(
  339. table="users",
  340. keyvalues={
  341. "is_guest": is_guest
  342. },
  343. pagevalues=[
  344. order,
  345. i_limit,
  346. i_start
  347. ],
  348. retcols=[
  349. "name",
  350. "password_hash",
  351. "is_guest",
  352. "admin"
  353. ],
  354. desc="get_users_paginate",
  355. )
  356. def search_users(self, term):
  357. """Function to search users list for one or more users with
  358. the matched term.
  359. Args:
  360. term (str): search term
  361. col (str): column to query term should be matched to
  362. Returns:
  363. defer.Deferred: resolves to list[dict[str, Any]]
  364. """
  365. return self._simple_search_list(
  366. table="users",
  367. term=term,
  368. col="name",
  369. retcols=[
  370. "name",
  371. "password_hash",
  372. "is_guest",
  373. "admin"
  374. ],
  375. desc="search_users",
  376. )
  377. def are_all_users_on_domain(txn, database_engine, domain):
  378. sql = database_engine.convert_param_style(
  379. "SELECT COUNT(*) FROM users WHERE name NOT LIKE ?"
  380. )
  381. pat = "%:" + domain
  382. txn.execute(sql, (pat,))
  383. num_not_matching = txn.fetchall()[0][0]
  384. if num_not_matching == 0:
  385. return True
  386. return False