room.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket 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. from twisted.internet import defer
  16. from synapse.api.errors import StoreError
  17. from synapse.storage._base import SQLBaseStore
  18. from synapse.storage.search import SearchStore
  19. from synapse.util.caches.descriptors import cached, cachedInlineCallbacks
  20. import collections
  21. import logging
  22. import simplejson as json
  23. import re
  24. logger = logging.getLogger(__name__)
  25. OpsLevel = collections.namedtuple(
  26. "OpsLevel",
  27. ("ban_level", "kick_level", "redact_level",)
  28. )
  29. RatelimitOverride = collections.namedtuple(
  30. "RatelimitOverride",
  31. ("messages_per_second", "burst_count",)
  32. )
  33. class RoomWorkerStore(SQLBaseStore):
  34. def get_public_room_ids(self):
  35. return self._simple_select_onecol(
  36. table="rooms",
  37. keyvalues={
  38. "is_public": True,
  39. },
  40. retcol="room_id",
  41. desc="get_public_room_ids",
  42. )
  43. @cached(num_args=2, max_entries=100)
  44. def get_public_room_ids_at_stream_id(self, stream_id, network_tuple):
  45. """Get pulbic rooms for a particular list, or across all lists.
  46. Args:
  47. stream_id (int)
  48. network_tuple (ThirdPartyInstanceID): The list to use (None, None)
  49. means the main list, None means all lsits.
  50. """
  51. return self.runInteraction(
  52. "get_public_room_ids_at_stream_id",
  53. self.get_public_room_ids_at_stream_id_txn,
  54. stream_id, network_tuple=network_tuple
  55. )
  56. def get_public_room_ids_at_stream_id_txn(self, txn, stream_id,
  57. network_tuple):
  58. return {
  59. rm
  60. for rm, vis in self.get_published_at_stream_id_txn(
  61. txn, stream_id, network_tuple=network_tuple
  62. ).items()
  63. if vis
  64. }
  65. def get_published_at_stream_id_txn(self, txn, stream_id, network_tuple):
  66. if network_tuple:
  67. # We want to get from a particular list. No aggregation required.
  68. sql = ("""
  69. SELECT room_id, visibility FROM public_room_list_stream
  70. INNER JOIN (
  71. SELECT room_id, max(stream_id) AS stream_id
  72. FROM public_room_list_stream
  73. WHERE stream_id <= ? %s
  74. GROUP BY room_id
  75. ) grouped USING (room_id, stream_id)
  76. """)
  77. if network_tuple.appservice_id is not None:
  78. txn.execute(
  79. sql % ("AND appservice_id = ? AND network_id = ?",),
  80. (stream_id, network_tuple.appservice_id, network_tuple.network_id,)
  81. )
  82. else:
  83. txn.execute(
  84. sql % ("AND appservice_id IS NULL",),
  85. (stream_id,)
  86. )
  87. return dict(txn)
  88. else:
  89. # We want to get from all lists, so we need to aggregate the results
  90. logger.info("Executing full list")
  91. sql = ("""
  92. SELECT room_id, visibility
  93. FROM public_room_list_stream
  94. INNER JOIN (
  95. SELECT
  96. room_id, max(stream_id) AS stream_id, appservice_id,
  97. network_id
  98. FROM public_room_list_stream
  99. WHERE stream_id <= ?
  100. GROUP BY room_id, appservice_id, network_id
  101. ) grouped USING (room_id, stream_id)
  102. """)
  103. txn.execute(
  104. sql,
  105. (stream_id,)
  106. )
  107. results = {}
  108. # A room is visible if its visible on any list.
  109. for room_id, visibility in txn:
  110. results[room_id] = bool(visibility) or results.get(room_id, False)
  111. return results
  112. def get_public_room_changes(self, prev_stream_id, new_stream_id,
  113. network_tuple):
  114. def get_public_room_changes_txn(txn):
  115. then_rooms = self.get_public_room_ids_at_stream_id_txn(
  116. txn, prev_stream_id, network_tuple
  117. )
  118. now_rooms_dict = self.get_published_at_stream_id_txn(
  119. txn, new_stream_id, network_tuple
  120. )
  121. now_rooms_visible = set(
  122. rm for rm, vis in now_rooms_dict.items() if vis
  123. )
  124. now_rooms_not_visible = set(
  125. rm for rm, vis in now_rooms_dict.items() if not vis
  126. )
  127. newly_visible = now_rooms_visible - then_rooms
  128. newly_unpublished = now_rooms_not_visible & then_rooms
  129. return newly_visible, newly_unpublished
  130. return self.runInteraction(
  131. "get_public_room_changes", get_public_room_changes_txn
  132. )
  133. @cached(max_entries=10000)
  134. def is_room_blocked(self, room_id):
  135. return self._simple_select_one_onecol(
  136. table="blocked_rooms",
  137. keyvalues={
  138. "room_id": room_id,
  139. },
  140. retcol="1",
  141. allow_none=True,
  142. desc="is_room_blocked",
  143. )
  144. class RoomStore(RoomWorkerStore, SearchStore):
  145. @defer.inlineCallbacks
  146. def store_room(self, room_id, room_creator_user_id, is_public):
  147. """Stores a room.
  148. Args:
  149. room_id (str): The desired room ID, can be None.
  150. room_creator_user_id (str): The user ID of the room creator.
  151. is_public (bool): True to indicate that this room should appear in
  152. public room lists.
  153. Raises:
  154. StoreError if the room could not be stored.
  155. """
  156. try:
  157. def store_room_txn(txn, next_id):
  158. self._simple_insert_txn(
  159. txn,
  160. "rooms",
  161. {
  162. "room_id": room_id,
  163. "creator": room_creator_user_id,
  164. "is_public": is_public,
  165. },
  166. )
  167. if is_public:
  168. self._simple_insert_txn(
  169. txn,
  170. table="public_room_list_stream",
  171. values={
  172. "stream_id": next_id,
  173. "room_id": room_id,
  174. "visibility": is_public,
  175. }
  176. )
  177. with self._public_room_id_gen.get_next() as next_id:
  178. yield self.runInteraction(
  179. "store_room_txn",
  180. store_room_txn, next_id,
  181. )
  182. except Exception as e:
  183. logger.error("store_room with room_id=%s failed: %s", room_id, e)
  184. raise StoreError(500, "Problem creating room.")
  185. def get_room(self, room_id):
  186. """Retrieve a room.
  187. Args:
  188. room_id (str): The ID of the room to retrieve.
  189. Returns:
  190. A namedtuple containing the room information, or an empty list.
  191. """
  192. return self._simple_select_one(
  193. table="rooms",
  194. keyvalues={"room_id": room_id},
  195. retcols=("room_id", "is_public", "creator"),
  196. desc="get_room",
  197. allow_none=True,
  198. )
  199. @defer.inlineCallbacks
  200. def set_room_is_public(self, room_id, is_public):
  201. def set_room_is_public_txn(txn, next_id):
  202. self._simple_update_one_txn(
  203. txn,
  204. table="rooms",
  205. keyvalues={"room_id": room_id},
  206. updatevalues={"is_public": is_public},
  207. )
  208. entries = self._simple_select_list_txn(
  209. txn,
  210. table="public_room_list_stream",
  211. keyvalues={
  212. "room_id": room_id,
  213. "appservice_id": None,
  214. "network_id": None,
  215. },
  216. retcols=("stream_id", "visibility"),
  217. )
  218. entries.sort(key=lambda r: r["stream_id"])
  219. add_to_stream = True
  220. if entries:
  221. add_to_stream = bool(entries[-1]["visibility"]) != is_public
  222. if add_to_stream:
  223. self._simple_insert_txn(
  224. txn,
  225. table="public_room_list_stream",
  226. values={
  227. "stream_id": next_id,
  228. "room_id": room_id,
  229. "visibility": is_public,
  230. "appservice_id": None,
  231. "network_id": None,
  232. }
  233. )
  234. with self._public_room_id_gen.get_next() as next_id:
  235. yield self.runInteraction(
  236. "set_room_is_public",
  237. set_room_is_public_txn, next_id,
  238. )
  239. self.hs.get_notifier().on_new_replication_data()
  240. @defer.inlineCallbacks
  241. def set_room_is_public_appservice(self, room_id, appservice_id, network_id,
  242. is_public):
  243. """Edit the appservice/network specific public room list.
  244. Each appservice can have a number of published room lists associated
  245. with them, keyed off of an appservice defined `network_id`, which
  246. basically represents a single instance of a bridge to a third party
  247. network.
  248. Args:
  249. room_id (str)
  250. appservice_id (str)
  251. network_id (str)
  252. is_public (bool): Whether to publish or unpublish the room from the
  253. list.
  254. """
  255. def set_room_is_public_appservice_txn(txn, next_id):
  256. if is_public:
  257. try:
  258. self._simple_insert_txn(
  259. txn,
  260. table="appservice_room_list",
  261. values={
  262. "appservice_id": appservice_id,
  263. "network_id": network_id,
  264. "room_id": room_id
  265. },
  266. )
  267. except self.database_engine.module.IntegrityError:
  268. # We've already inserted, nothing to do.
  269. return
  270. else:
  271. self._simple_delete_txn(
  272. txn,
  273. table="appservice_room_list",
  274. keyvalues={
  275. "appservice_id": appservice_id,
  276. "network_id": network_id,
  277. "room_id": room_id
  278. },
  279. )
  280. entries = self._simple_select_list_txn(
  281. txn,
  282. table="public_room_list_stream",
  283. keyvalues={
  284. "room_id": room_id,
  285. "appservice_id": appservice_id,
  286. "network_id": network_id,
  287. },
  288. retcols=("stream_id", "visibility"),
  289. )
  290. entries.sort(key=lambda r: r["stream_id"])
  291. add_to_stream = True
  292. if entries:
  293. add_to_stream = bool(entries[-1]["visibility"]) != is_public
  294. if add_to_stream:
  295. self._simple_insert_txn(
  296. txn,
  297. table="public_room_list_stream",
  298. values={
  299. "stream_id": next_id,
  300. "room_id": room_id,
  301. "visibility": is_public,
  302. "appservice_id": appservice_id,
  303. "network_id": network_id,
  304. }
  305. )
  306. with self._public_room_id_gen.get_next() as next_id:
  307. yield self.runInteraction(
  308. "set_room_is_public_appservice",
  309. set_room_is_public_appservice_txn, next_id,
  310. )
  311. self.hs.get_notifier().on_new_replication_data()
  312. def get_room_count(self):
  313. """Retrieve a list of all rooms
  314. """
  315. def f(txn):
  316. sql = "SELECT count(*) FROM rooms"
  317. txn.execute(sql)
  318. row = txn.fetchone()
  319. return row[0] or 0
  320. return self.runInteraction(
  321. "get_rooms", f
  322. )
  323. def _store_room_topic_txn(self, txn, event):
  324. if hasattr(event, "content") and "topic" in event.content:
  325. self._simple_insert_txn(
  326. txn,
  327. "topics",
  328. {
  329. "event_id": event.event_id,
  330. "room_id": event.room_id,
  331. "topic": event.content["topic"],
  332. },
  333. )
  334. self.store_event_search_txn(
  335. txn, event, "content.topic", event.content["topic"],
  336. )
  337. def _store_room_name_txn(self, txn, event):
  338. if hasattr(event, "content") and "name" in event.content:
  339. self._simple_insert_txn(
  340. txn,
  341. "room_names",
  342. {
  343. "event_id": event.event_id,
  344. "room_id": event.room_id,
  345. "name": event.content["name"],
  346. }
  347. )
  348. self.store_event_search_txn(
  349. txn, event, "content.name", event.content["name"],
  350. )
  351. def _store_room_message_txn(self, txn, event):
  352. if hasattr(event, "content") and "body" in event.content:
  353. self.store_event_search_txn(
  354. txn, event, "content.body", event.content["body"],
  355. )
  356. def _store_history_visibility_txn(self, txn, event):
  357. self._store_content_index_txn(txn, event, "history_visibility")
  358. def _store_guest_access_txn(self, txn, event):
  359. self._store_content_index_txn(txn, event, "guest_access")
  360. def _store_content_index_txn(self, txn, event, key):
  361. if hasattr(event, "content") and key in event.content:
  362. sql = (
  363. "INSERT INTO %(key)s"
  364. " (event_id, room_id, %(key)s)"
  365. " VALUES (?, ?, ?)" % {"key": key}
  366. )
  367. txn.execute(sql, (
  368. event.event_id,
  369. event.room_id,
  370. event.content[key]
  371. ))
  372. def add_event_report(self, room_id, event_id, user_id, reason, content,
  373. received_ts):
  374. next_id = self._event_reports_id_gen.get_next()
  375. return self._simple_insert(
  376. table="event_reports",
  377. values={
  378. "id": next_id,
  379. "received_ts": received_ts,
  380. "room_id": room_id,
  381. "event_id": event_id,
  382. "user_id": user_id,
  383. "reason": reason,
  384. "content": json.dumps(content),
  385. },
  386. desc="add_event_report"
  387. )
  388. def get_current_public_room_stream_id(self):
  389. return self._public_room_id_gen.get_current_token()
  390. def get_all_new_public_rooms(self, prev_id, current_id, limit):
  391. def get_all_new_public_rooms(txn):
  392. sql = ("""
  393. SELECT stream_id, room_id, visibility, appservice_id, network_id
  394. FROM public_room_list_stream
  395. WHERE stream_id > ? AND stream_id <= ?
  396. ORDER BY stream_id ASC
  397. LIMIT ?
  398. """)
  399. txn.execute(sql, (prev_id, current_id, limit,))
  400. return txn.fetchall()
  401. if prev_id == current_id:
  402. return defer.succeed([])
  403. return self.runInteraction(
  404. "get_all_new_public_rooms", get_all_new_public_rooms
  405. )
  406. @cachedInlineCallbacks(max_entries=10000)
  407. def get_ratelimit_for_user(self, user_id):
  408. """Check if there are any overrides for ratelimiting for the given
  409. user
  410. Args:
  411. user_id (str)
  412. Returns:
  413. RatelimitOverride if there is an override, else None. If the contents
  414. of RatelimitOverride are None or 0 then ratelimitng has been
  415. disabled for that user entirely.
  416. """
  417. row = yield self._simple_select_one(
  418. table="ratelimit_override",
  419. keyvalues={"user_id": user_id},
  420. retcols=("messages_per_second", "burst_count"),
  421. allow_none=True,
  422. desc="get_ratelimit_for_user",
  423. )
  424. if row:
  425. defer.returnValue(RatelimitOverride(
  426. messages_per_second=row["messages_per_second"],
  427. burst_count=row["burst_count"],
  428. ))
  429. else:
  430. defer.returnValue(None)
  431. @defer.inlineCallbacks
  432. def block_room(self, room_id, user_id):
  433. yield self._simple_insert(
  434. table="blocked_rooms",
  435. values={
  436. "room_id": room_id,
  437. "user_id": user_id,
  438. },
  439. desc="block_room",
  440. )
  441. yield self.runInteraction(
  442. "block_room_invalidation",
  443. self._invalidate_cache_and_stream,
  444. self.is_room_blocked, (room_id,),
  445. )
  446. def get_media_mxcs_in_room(self, room_id):
  447. """Retrieves all the local and remote media MXC URIs in a given room
  448. Args:
  449. room_id (str)
  450. Returns:
  451. The local and remote media as a lists of tuples where the key is
  452. the hostname and the value is the media ID.
  453. """
  454. def _get_media_mxcs_in_room_txn(txn):
  455. local_mxcs, remote_mxcs = self._get_media_mxcs_in_room_txn(txn, room_id)
  456. local_media_mxcs = []
  457. remote_media_mxcs = []
  458. # Convert the IDs to MXC URIs
  459. for media_id in local_mxcs:
  460. local_media_mxcs.append("mxc://%s/%s" % (self.hostname, media_id))
  461. for hostname, media_id in remote_mxcs:
  462. remote_media_mxcs.append("mxc://%s/%s" % (hostname, media_id))
  463. return local_media_mxcs, remote_media_mxcs
  464. return self.runInteraction("get_media_ids_in_room", _get_media_mxcs_in_room_txn)
  465. def quarantine_media_ids_in_room(self, room_id, quarantined_by):
  466. """For a room loops through all events with media and quarantines
  467. the associated media
  468. """
  469. def _quarantine_media_in_room_txn(txn):
  470. local_mxcs, remote_mxcs = self._get_media_mxcs_in_room_txn(txn, room_id)
  471. total_media_quarantined = 0
  472. # Now update all the tables to set the quarantined_by flag
  473. txn.executemany("""
  474. UPDATE local_media_repository
  475. SET quarantined_by = ?
  476. WHERE media_id = ?
  477. """, ((quarantined_by, media_id) for media_id in local_mxcs))
  478. txn.executemany(
  479. """
  480. UPDATE remote_media_cache
  481. SET quarantined_by = ?
  482. WHERE media_origin = ? AND media_id = ?
  483. """,
  484. (
  485. (quarantined_by, origin, media_id)
  486. for origin, media_id in remote_mxcs
  487. )
  488. )
  489. total_media_quarantined += len(local_mxcs)
  490. total_media_quarantined += len(remote_mxcs)
  491. return total_media_quarantined
  492. return self.runInteraction(
  493. "quarantine_media_in_room",
  494. _quarantine_media_in_room_txn,
  495. )
  496. def _get_media_mxcs_in_room_txn(self, txn, room_id):
  497. """Retrieves all the local and remote media MXC URIs in a given room
  498. Args:
  499. txn (cursor)
  500. room_id (str)
  501. Returns:
  502. The local and remote media as a lists of tuples where the key is
  503. the hostname and the value is the media ID.
  504. """
  505. mxc_re = re.compile("^mxc://([^/]+)/([^/#?]+)")
  506. next_token = self.get_current_events_token() + 1
  507. local_media_mxcs = []
  508. remote_media_mxcs = []
  509. while next_token:
  510. sql = """
  511. SELECT stream_ordering, content FROM events
  512. WHERE room_id = ?
  513. AND stream_ordering < ?
  514. AND contains_url = ? AND outlier = ?
  515. ORDER BY stream_ordering DESC
  516. LIMIT ?
  517. """
  518. txn.execute(sql, (room_id, next_token, True, False, 100))
  519. next_token = None
  520. for stream_ordering, content_json in txn:
  521. next_token = stream_ordering
  522. content = json.loads(content_json)
  523. content_url = content.get("url")
  524. thumbnail_url = content.get("info", {}).get("thumbnail_url")
  525. for url in (content_url, thumbnail_url):
  526. if not url:
  527. continue
  528. matches = mxc_re.match(url)
  529. if matches:
  530. hostname = matches.group(1)
  531. media_id = matches.group(2)
  532. if hostname == self.hostname:
  533. local_media_mxcs.append(media_id)
  534. else:
  535. remote_media_mxcs.append((hostname, media_id))
  536. return local_media_mxcs, remote_media_mxcs