room.py 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2019 The Matrix.org Foundation C.I.C.
  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. import collections
  17. import logging
  18. import re
  19. from abc import abstractmethod
  20. from enum import Enum
  21. from typing import Any, Dict, List, Optional, Tuple
  22. from synapse.api.constants import EventTypes
  23. from synapse.api.errors import StoreError
  24. from synapse.api.room_versions import RoomVersion, RoomVersions
  25. from synapse.storage._base import SQLBaseStore, db_to_json
  26. from synapse.storage.database import DatabasePool, LoggingTransaction
  27. from synapse.storage.databases.main.search import SearchStore
  28. from synapse.types import JsonDict, ThirdPartyInstanceID
  29. from synapse.util import json_encoder
  30. from synapse.util.caches.descriptors import cached
  31. logger = logging.getLogger(__name__)
  32. RatelimitOverride = collections.namedtuple(
  33. "RatelimitOverride", ("messages_per_second", "burst_count")
  34. )
  35. class RoomSortOrder(Enum):
  36. """
  37. Enum to define the sorting method used when returning rooms with get_rooms_paginate
  38. NAME = sort rooms alphabetically by name
  39. JOINED_MEMBERS = sort rooms by membership size, highest to lowest
  40. """
  41. # ALPHABETICAL and SIZE are deprecated.
  42. # ALPHABETICAL is the same as NAME.
  43. ALPHABETICAL = "alphabetical"
  44. # SIZE is the same as JOINED_MEMBERS.
  45. SIZE = "size"
  46. NAME = "name"
  47. CANONICAL_ALIAS = "canonical_alias"
  48. JOINED_MEMBERS = "joined_members"
  49. JOINED_LOCAL_MEMBERS = "joined_local_members"
  50. VERSION = "version"
  51. CREATOR = "creator"
  52. ENCRYPTION = "encryption"
  53. FEDERATABLE = "federatable"
  54. PUBLIC = "public"
  55. JOIN_RULES = "join_rules"
  56. GUEST_ACCESS = "guest_access"
  57. HISTORY_VISIBILITY = "history_visibility"
  58. STATE_EVENTS = "state_events"
  59. class RoomWorkerStore(SQLBaseStore):
  60. def __init__(self, database: DatabasePool, db_conn, hs):
  61. super().__init__(database, db_conn, hs)
  62. self.config = hs.config
  63. async def get_room(self, room_id: str) -> dict:
  64. """Retrieve a room.
  65. Args:
  66. room_id: The ID of the room to retrieve.
  67. Returns:
  68. A dict containing the room information, or None if the room is unknown.
  69. """
  70. return await self.db_pool.simple_select_one(
  71. table="rooms",
  72. keyvalues={"room_id": room_id},
  73. retcols=("room_id", "is_public", "creator"),
  74. desc="get_room",
  75. allow_none=True,
  76. )
  77. async def get_room_with_stats(self, room_id: str) -> Optional[Dict[str, Any]]:
  78. """Retrieve room with statistics.
  79. Args:
  80. room_id: The ID of the room to retrieve.
  81. Returns:
  82. A dict containing the room information, or None if the room is unknown.
  83. """
  84. def get_room_with_stats_txn(txn, room_id):
  85. sql = """
  86. SELECT room_id, state.name, state.canonical_alias, curr.joined_members,
  87. curr.local_users_in_room AS joined_local_members, rooms.room_version AS version,
  88. rooms.creator, state.encryption, state.is_federatable AS federatable,
  89. rooms.is_public AS public, state.join_rules, state.guest_access,
  90. state.history_visibility, curr.current_state_events AS state_events,
  91. state.avatar, state.topic
  92. FROM rooms
  93. LEFT JOIN room_stats_state state USING (room_id)
  94. LEFT JOIN room_stats_current curr USING (room_id)
  95. WHERE room_id = ?
  96. """
  97. txn.execute(sql, [room_id])
  98. # Catch error if sql returns empty result to return "None" instead of an error
  99. try:
  100. res = self.db_pool.cursor_to_dict(txn)[0]
  101. except IndexError:
  102. return None
  103. res["federatable"] = bool(res["federatable"])
  104. res["public"] = bool(res["public"])
  105. return res
  106. return await self.db_pool.runInteraction(
  107. "get_room_with_stats", get_room_with_stats_txn, room_id
  108. )
  109. async def get_public_room_ids(self) -> List[str]:
  110. return await self.db_pool.simple_select_onecol(
  111. table="rooms",
  112. keyvalues={"is_public": True},
  113. retcol="room_id",
  114. desc="get_public_room_ids",
  115. )
  116. async def count_public_rooms(
  117. self,
  118. network_tuple: Optional[ThirdPartyInstanceID],
  119. ignore_non_federatable: bool,
  120. ) -> int:
  121. """Counts the number of public rooms as tracked in the room_stats_current
  122. and room_stats_state table.
  123. Args:
  124. network_tuple
  125. ignore_non_federatable: If true filters out non-federatable rooms
  126. """
  127. def _count_public_rooms_txn(txn):
  128. query_args = []
  129. if network_tuple:
  130. if network_tuple.appservice_id:
  131. published_sql = """
  132. SELECT room_id from appservice_room_list
  133. WHERE appservice_id = ? AND network_id = ?
  134. """
  135. query_args.append(network_tuple.appservice_id)
  136. query_args.append(network_tuple.network_id)
  137. else:
  138. published_sql = """
  139. SELECT room_id FROM rooms WHERE is_public
  140. """
  141. else:
  142. published_sql = """
  143. SELECT room_id FROM rooms WHERE is_public
  144. UNION SELECT room_id from appservice_room_list
  145. """
  146. sql = """
  147. SELECT
  148. COALESCE(COUNT(*), 0)
  149. FROM (
  150. %(published_sql)s
  151. ) published
  152. INNER JOIN room_stats_state USING (room_id)
  153. INNER JOIN room_stats_current USING (room_id)
  154. WHERE
  155. (
  156. join_rules = 'public' OR history_visibility = 'world_readable'
  157. )
  158. AND joined_members > 0
  159. """ % {
  160. "published_sql": published_sql
  161. }
  162. txn.execute(sql, query_args)
  163. return txn.fetchone()[0]
  164. return await self.db_pool.runInteraction(
  165. "count_public_rooms", _count_public_rooms_txn
  166. )
  167. async def get_room_count(self) -> int:
  168. """Retrieve the total number of rooms.
  169. """
  170. def f(txn):
  171. sql = "SELECT count(*) FROM rooms"
  172. txn.execute(sql)
  173. row = txn.fetchone()
  174. return row[0] or 0
  175. return await self.db_pool.runInteraction("get_rooms", f)
  176. async def get_largest_public_rooms(
  177. self,
  178. network_tuple: Optional[ThirdPartyInstanceID],
  179. search_filter: Optional[dict],
  180. limit: Optional[int],
  181. bounds: Optional[Tuple[int, str]],
  182. forwards: bool,
  183. ignore_non_federatable: bool = False,
  184. ):
  185. """Gets the largest public rooms (where largest is in terms of joined
  186. members, as tracked in the statistics table).
  187. Args:
  188. network_tuple
  189. search_filter
  190. limit: Maxmimum number of rows to return, unlimited otherwise.
  191. bounds: An uppoer or lower bound to apply to result set if given,
  192. consists of a joined member count and room_id (these are
  193. excluded from result set).
  194. forwards: true iff going forwards, going backwards otherwise
  195. ignore_non_federatable: If true filters out non-federatable rooms.
  196. Returns:
  197. Rooms in order: biggest number of joined users first.
  198. We then arbitrarily use the room_id as a tie breaker.
  199. """
  200. where_clauses = []
  201. query_args = []
  202. if network_tuple:
  203. if network_tuple.appservice_id:
  204. published_sql = """
  205. SELECT room_id from appservice_room_list
  206. WHERE appservice_id = ? AND network_id = ?
  207. """
  208. query_args.append(network_tuple.appservice_id)
  209. query_args.append(network_tuple.network_id)
  210. else:
  211. published_sql = """
  212. SELECT room_id FROM rooms WHERE is_public
  213. """
  214. else:
  215. published_sql = """
  216. SELECT room_id FROM rooms WHERE is_public
  217. UNION SELECT room_id from appservice_room_list
  218. """
  219. # Work out the bounds if we're given them, these bounds look slightly
  220. # odd, but are designed to help query planner use indices by pulling
  221. # out a common bound.
  222. if bounds:
  223. last_joined_members, last_room_id = bounds
  224. if forwards:
  225. where_clauses.append(
  226. """
  227. joined_members <= ? AND (
  228. joined_members < ? OR room_id < ?
  229. )
  230. """
  231. )
  232. else:
  233. where_clauses.append(
  234. """
  235. joined_members >= ? AND (
  236. joined_members > ? OR room_id > ?
  237. )
  238. """
  239. )
  240. query_args += [last_joined_members, last_joined_members, last_room_id]
  241. if ignore_non_federatable:
  242. where_clauses.append("is_federatable")
  243. if search_filter and search_filter.get("generic_search_term", None):
  244. search_term = "%" + search_filter["generic_search_term"] + "%"
  245. where_clauses.append(
  246. """
  247. (
  248. LOWER(name) LIKE ?
  249. OR LOWER(topic) LIKE ?
  250. OR LOWER(canonical_alias) LIKE ?
  251. )
  252. """
  253. )
  254. query_args += [
  255. search_term.lower(),
  256. search_term.lower(),
  257. search_term.lower(),
  258. ]
  259. where_clause = ""
  260. if where_clauses:
  261. where_clause = " AND " + " AND ".join(where_clauses)
  262. sql = """
  263. SELECT
  264. room_id, name, topic, canonical_alias, joined_members,
  265. avatar, history_visibility, joined_members, guest_access
  266. FROM (
  267. %(published_sql)s
  268. ) published
  269. INNER JOIN room_stats_state USING (room_id)
  270. INNER JOIN room_stats_current USING (room_id)
  271. WHERE
  272. (
  273. join_rules = 'public' OR history_visibility = 'world_readable'
  274. )
  275. AND joined_members > 0
  276. %(where_clause)s
  277. ORDER BY joined_members %(dir)s, room_id %(dir)s
  278. """ % {
  279. "published_sql": published_sql,
  280. "where_clause": where_clause,
  281. "dir": "DESC" if forwards else "ASC",
  282. }
  283. if limit is not None:
  284. query_args.append(limit)
  285. sql += """
  286. LIMIT ?
  287. """
  288. def _get_largest_public_rooms_txn(txn):
  289. txn.execute(sql, query_args)
  290. results = self.db_pool.cursor_to_dict(txn)
  291. if not forwards:
  292. results.reverse()
  293. return results
  294. ret_val = await self.db_pool.runInteraction(
  295. "get_largest_public_rooms", _get_largest_public_rooms_txn
  296. )
  297. return ret_val
  298. @cached(max_entries=10000)
  299. async def is_room_blocked(self, room_id: str) -> Optional[bool]:
  300. return await self.db_pool.simple_select_one_onecol(
  301. table="blocked_rooms",
  302. keyvalues={"room_id": room_id},
  303. retcol="1",
  304. allow_none=True,
  305. desc="is_room_blocked",
  306. )
  307. async def get_rooms_paginate(
  308. self,
  309. start: int,
  310. limit: int,
  311. order_by: RoomSortOrder,
  312. reverse_order: bool,
  313. search_term: Optional[str],
  314. ) -> Tuple[List[Dict[str, Any]], int]:
  315. """Function to retrieve a paginated list of rooms as json.
  316. Args:
  317. start: offset in the list
  318. limit: maximum amount of rooms to retrieve
  319. order_by: the sort order of the returned list
  320. reverse_order: whether to reverse the room list
  321. search_term: a string to filter room names by
  322. Returns:
  323. A list of room dicts and an integer representing the total number of
  324. rooms that exist given this query
  325. """
  326. # Filter room names by a string
  327. where_statement = ""
  328. if search_term:
  329. where_statement = "WHERE state.name LIKE ?"
  330. # Our postgres db driver converts ? -> %s in SQL strings as that's the
  331. # placeholder for postgres.
  332. # HOWEVER, if you put a % into your SQL then everything goes wibbly.
  333. # To get around this, we're going to surround search_term with %'s
  334. # before giving it to the database in python instead
  335. search_term = "%" + search_term + "%"
  336. # Set ordering
  337. if RoomSortOrder(order_by) == RoomSortOrder.SIZE:
  338. # Deprecated in favour of RoomSortOrder.JOINED_MEMBERS
  339. order_by_column = "curr.joined_members"
  340. order_by_asc = False
  341. elif RoomSortOrder(order_by) == RoomSortOrder.ALPHABETICAL:
  342. # Deprecated in favour of RoomSortOrder.NAME
  343. order_by_column = "state.name"
  344. order_by_asc = True
  345. elif RoomSortOrder(order_by) == RoomSortOrder.NAME:
  346. order_by_column = "state.name"
  347. order_by_asc = True
  348. elif RoomSortOrder(order_by) == RoomSortOrder.CANONICAL_ALIAS:
  349. order_by_column = "state.canonical_alias"
  350. order_by_asc = True
  351. elif RoomSortOrder(order_by) == RoomSortOrder.JOINED_MEMBERS:
  352. order_by_column = "curr.joined_members"
  353. order_by_asc = False
  354. elif RoomSortOrder(order_by) == RoomSortOrder.JOINED_LOCAL_MEMBERS:
  355. order_by_column = "curr.local_users_in_room"
  356. order_by_asc = False
  357. elif RoomSortOrder(order_by) == RoomSortOrder.VERSION:
  358. order_by_column = "rooms.room_version"
  359. order_by_asc = False
  360. elif RoomSortOrder(order_by) == RoomSortOrder.CREATOR:
  361. order_by_column = "rooms.creator"
  362. order_by_asc = True
  363. elif RoomSortOrder(order_by) == RoomSortOrder.ENCRYPTION:
  364. order_by_column = "state.encryption"
  365. order_by_asc = True
  366. elif RoomSortOrder(order_by) == RoomSortOrder.FEDERATABLE:
  367. order_by_column = "state.is_federatable"
  368. order_by_asc = True
  369. elif RoomSortOrder(order_by) == RoomSortOrder.PUBLIC:
  370. order_by_column = "rooms.is_public"
  371. order_by_asc = True
  372. elif RoomSortOrder(order_by) == RoomSortOrder.JOIN_RULES:
  373. order_by_column = "state.join_rules"
  374. order_by_asc = True
  375. elif RoomSortOrder(order_by) == RoomSortOrder.GUEST_ACCESS:
  376. order_by_column = "state.guest_access"
  377. order_by_asc = True
  378. elif RoomSortOrder(order_by) == RoomSortOrder.HISTORY_VISIBILITY:
  379. order_by_column = "state.history_visibility"
  380. order_by_asc = True
  381. elif RoomSortOrder(order_by) == RoomSortOrder.STATE_EVENTS:
  382. order_by_column = "curr.current_state_events"
  383. order_by_asc = False
  384. else:
  385. raise StoreError(
  386. 500, "Incorrect value for order_by provided: %s" % order_by
  387. )
  388. # Whether to return the list in reverse order
  389. if reverse_order:
  390. # Flip the boolean
  391. order_by_asc = not order_by_asc
  392. # Create one query for getting the limited number of events that the user asked
  393. # for, and another query for getting the total number of events that could be
  394. # returned. Thus allowing us to see if there are more events to paginate through
  395. info_sql = """
  396. SELECT state.room_id, state.name, state.canonical_alias, curr.joined_members,
  397. curr.local_users_in_room, rooms.room_version, rooms.creator,
  398. state.encryption, state.is_federatable, rooms.is_public, state.join_rules,
  399. state.guest_access, state.history_visibility, curr.current_state_events
  400. FROM room_stats_state state
  401. INNER JOIN room_stats_current curr USING (room_id)
  402. INNER JOIN rooms USING (room_id)
  403. %s
  404. ORDER BY %s %s
  405. LIMIT ?
  406. OFFSET ?
  407. """ % (
  408. where_statement,
  409. order_by_column,
  410. "ASC" if order_by_asc else "DESC",
  411. )
  412. # Use a nested SELECT statement as SQL can't count(*) with an OFFSET
  413. count_sql = """
  414. SELECT count(*) FROM (
  415. SELECT room_id FROM room_stats_state state
  416. %s
  417. ) AS get_room_ids
  418. """ % (
  419. where_statement,
  420. )
  421. def _get_rooms_paginate_txn(txn):
  422. # Execute the data query
  423. sql_values = (limit, start)
  424. if search_term:
  425. # Add the search term into the WHERE clause
  426. sql_values = (search_term,) + sql_values
  427. txn.execute(info_sql, sql_values)
  428. # Refactor room query data into a structured dictionary
  429. rooms = []
  430. for room in txn:
  431. rooms.append(
  432. {
  433. "room_id": room[0],
  434. "name": room[1],
  435. "canonical_alias": room[2],
  436. "joined_members": room[3],
  437. "joined_local_members": room[4],
  438. "version": room[5],
  439. "creator": room[6],
  440. "encryption": room[7],
  441. "federatable": room[8],
  442. "public": room[9],
  443. "join_rules": room[10],
  444. "guest_access": room[11],
  445. "history_visibility": room[12],
  446. "state_events": room[13],
  447. }
  448. )
  449. # Execute the count query
  450. # Add the search term into the WHERE clause if present
  451. sql_values = (search_term,) if search_term else ()
  452. txn.execute(count_sql, sql_values)
  453. room_count = txn.fetchone()
  454. return rooms, room_count[0]
  455. return await self.db_pool.runInteraction(
  456. "get_rooms_paginate", _get_rooms_paginate_txn,
  457. )
  458. @cached(max_entries=10000)
  459. async def get_ratelimit_for_user(self, user_id):
  460. """Check if there are any overrides for ratelimiting for the given
  461. user
  462. Args:
  463. user_id (str)
  464. Returns:
  465. RatelimitOverride if there is an override, else None. If the contents
  466. of RatelimitOverride are None or 0 then ratelimitng has been
  467. disabled for that user entirely.
  468. """
  469. row = await self.db_pool.simple_select_one(
  470. table="ratelimit_override",
  471. keyvalues={"user_id": user_id},
  472. retcols=("messages_per_second", "burst_count"),
  473. allow_none=True,
  474. desc="get_ratelimit_for_user",
  475. )
  476. if row:
  477. return RatelimitOverride(
  478. messages_per_second=row["messages_per_second"],
  479. burst_count=row["burst_count"],
  480. )
  481. else:
  482. return None
  483. @cached()
  484. async def get_retention_policy_for_room(self, room_id):
  485. """Get the retention policy for a given room.
  486. If no retention policy has been found for this room, returns a policy defined
  487. by the configured default policy (which has None as both the 'min_lifetime' and
  488. the 'max_lifetime' if no default policy has been defined in the server's
  489. configuration).
  490. Args:
  491. room_id (str): The ID of the room to get the retention policy of.
  492. Returns:
  493. dict[int, int]: "min_lifetime" and "max_lifetime" for this room.
  494. """
  495. def get_retention_policy_for_room_txn(txn):
  496. txn.execute(
  497. """
  498. SELECT min_lifetime, max_lifetime FROM room_retention
  499. INNER JOIN current_state_events USING (event_id, room_id)
  500. WHERE room_id = ?;
  501. """,
  502. (room_id,),
  503. )
  504. return self.db_pool.cursor_to_dict(txn)
  505. ret = await self.db_pool.runInteraction(
  506. "get_retention_policy_for_room", get_retention_policy_for_room_txn,
  507. )
  508. # If we don't know this room ID, ret will be None, in this case return the default
  509. # policy.
  510. if not ret:
  511. return {
  512. "min_lifetime": self.config.retention_default_min_lifetime,
  513. "max_lifetime": self.config.retention_default_max_lifetime,
  514. }
  515. row = ret[0]
  516. # If one of the room's policy's attributes isn't defined, use the matching
  517. # attribute from the default policy.
  518. # The default values will be None if no default policy has been defined, or if one
  519. # of the attributes is missing from the default policy.
  520. if row["min_lifetime"] is None:
  521. row["min_lifetime"] = self.config.retention_default_min_lifetime
  522. if row["max_lifetime"] is None:
  523. row["max_lifetime"] = self.config.retention_default_max_lifetime
  524. return row
  525. async def get_media_mxcs_in_room(self, room_id: str) -> Tuple[List[str], List[str]]:
  526. """Retrieves all the local and remote media MXC URIs in a given room
  527. Args:
  528. room_id
  529. Returns:
  530. The local and remote media as a lists of the media IDs.
  531. """
  532. def _get_media_mxcs_in_room_txn(txn):
  533. local_mxcs, remote_mxcs = self._get_media_mxcs_in_room_txn(txn, room_id)
  534. local_media_mxcs = []
  535. remote_media_mxcs = []
  536. # Convert the IDs to MXC URIs
  537. for media_id in local_mxcs:
  538. local_media_mxcs.append("mxc://%s/%s" % (self.hs.hostname, media_id))
  539. for hostname, media_id in remote_mxcs:
  540. remote_media_mxcs.append("mxc://%s/%s" % (hostname, media_id))
  541. return local_media_mxcs, remote_media_mxcs
  542. return await self.db_pool.runInteraction(
  543. "get_media_ids_in_room", _get_media_mxcs_in_room_txn
  544. )
  545. async def quarantine_media_ids_in_room(
  546. self, room_id: str, quarantined_by: str
  547. ) -> int:
  548. """For a room loops through all events with media and quarantines
  549. the associated media
  550. """
  551. logger.info("Quarantining media in room: %s", room_id)
  552. def _quarantine_media_in_room_txn(txn):
  553. local_mxcs, remote_mxcs = self._get_media_mxcs_in_room_txn(txn, room_id)
  554. return self._quarantine_media_txn(
  555. txn, local_mxcs, remote_mxcs, quarantined_by
  556. )
  557. return await self.db_pool.runInteraction(
  558. "quarantine_media_in_room", _quarantine_media_in_room_txn
  559. )
  560. def _get_media_mxcs_in_room_txn(self, txn, room_id):
  561. """Retrieves all the local and remote media MXC URIs in a given room
  562. Args:
  563. txn (cursor)
  564. room_id (str)
  565. Returns:
  566. The local and remote media as a lists of tuples where the key is
  567. the hostname and the value is the media ID.
  568. """
  569. mxc_re = re.compile("^mxc://([^/]+)/([^/#?]+)")
  570. sql = """
  571. SELECT stream_ordering, json FROM events
  572. JOIN event_json USING (room_id, event_id)
  573. WHERE room_id = ?
  574. %(where_clause)s
  575. AND contains_url = ? AND outlier = ?
  576. ORDER BY stream_ordering DESC
  577. LIMIT ?
  578. """
  579. txn.execute(sql % {"where_clause": ""}, (room_id, True, False, 100))
  580. local_media_mxcs = []
  581. remote_media_mxcs = []
  582. while True:
  583. next_token = None
  584. for stream_ordering, content_json in txn:
  585. next_token = stream_ordering
  586. event_json = db_to_json(content_json)
  587. content = event_json["content"]
  588. content_url = content.get("url")
  589. thumbnail_url = content.get("info", {}).get("thumbnail_url")
  590. for url in (content_url, thumbnail_url):
  591. if not url:
  592. continue
  593. matches = mxc_re.match(url)
  594. if matches:
  595. hostname = matches.group(1)
  596. media_id = matches.group(2)
  597. if hostname == self.hs.hostname:
  598. local_media_mxcs.append(media_id)
  599. else:
  600. remote_media_mxcs.append((hostname, media_id))
  601. if next_token is None:
  602. # We've gone through the whole room, so we're finished.
  603. break
  604. txn.execute(
  605. sql % {"where_clause": "AND stream_ordering < ?"},
  606. (room_id, next_token, True, False, 100),
  607. )
  608. return local_media_mxcs, remote_media_mxcs
  609. async def quarantine_media_by_id(
  610. self, server_name: str, media_id: str, quarantined_by: str,
  611. ) -> int:
  612. """quarantines a single local or remote media id
  613. Args:
  614. server_name: The name of the server that holds this media
  615. media_id: The ID of the media to be quarantined
  616. quarantined_by: The user ID that initiated the quarantine request
  617. """
  618. logger.info("Quarantining media: %s/%s", server_name, media_id)
  619. is_local = server_name == self.config.server_name
  620. def _quarantine_media_by_id_txn(txn):
  621. local_mxcs = [media_id] if is_local else []
  622. remote_mxcs = [(server_name, media_id)] if not is_local else []
  623. return self._quarantine_media_txn(
  624. txn, local_mxcs, remote_mxcs, quarantined_by
  625. )
  626. return await self.db_pool.runInteraction(
  627. "quarantine_media_by_user", _quarantine_media_by_id_txn
  628. )
  629. async def quarantine_media_ids_by_user(
  630. self, user_id: str, quarantined_by: str
  631. ) -> int:
  632. """quarantines all local media associated with a single user
  633. Args:
  634. user_id: The ID of the user to quarantine media of
  635. quarantined_by: The ID of the user who made the quarantine request
  636. """
  637. def _quarantine_media_by_user_txn(txn):
  638. local_media_ids = self._get_media_ids_by_user_txn(txn, user_id)
  639. return self._quarantine_media_txn(txn, local_media_ids, [], quarantined_by)
  640. return await self.db_pool.runInteraction(
  641. "quarantine_media_by_user", _quarantine_media_by_user_txn
  642. )
  643. def _get_media_ids_by_user_txn(self, txn, user_id: str, filter_quarantined=True):
  644. """Retrieves local media IDs by a given user
  645. Args:
  646. txn (cursor)
  647. user_id: The ID of the user to retrieve media IDs of
  648. Returns:
  649. The local and remote media as a lists of tuples where the key is
  650. the hostname and the value is the media ID.
  651. """
  652. # Local media
  653. sql = """
  654. SELECT media_id
  655. FROM local_media_repository
  656. WHERE user_id = ?
  657. """
  658. if filter_quarantined:
  659. sql += "AND quarantined_by IS NULL"
  660. txn.execute(sql, (user_id,))
  661. local_media_ids = [row[0] for row in txn]
  662. # TODO: Figure out all remote media a user has referenced in a message
  663. return local_media_ids
  664. def _quarantine_media_txn(
  665. self,
  666. txn,
  667. local_mxcs: List[str],
  668. remote_mxcs: List[Tuple[str, str]],
  669. quarantined_by: str,
  670. ) -> int:
  671. """Quarantine local and remote media items
  672. Args:
  673. txn (cursor)
  674. local_mxcs: A list of local mxc URLs
  675. remote_mxcs: A list of (remote server, media id) tuples representing
  676. remote mxc URLs
  677. quarantined_by: The ID of the user who initiated the quarantine request
  678. Returns:
  679. The total number of media items quarantined
  680. """
  681. # Update all the tables to set the quarantined_by flag
  682. txn.executemany(
  683. """
  684. UPDATE local_media_repository
  685. SET quarantined_by = ?
  686. WHERE media_id = ? AND safe_from_quarantine = ?
  687. """,
  688. ((quarantined_by, media_id, False) for media_id in local_mxcs),
  689. )
  690. # Note that a rowcount of -1 can be used to indicate no rows were affected.
  691. total_media_quarantined = txn.rowcount if txn.rowcount > 0 else 0
  692. txn.executemany(
  693. """
  694. UPDATE remote_media_cache
  695. SET quarantined_by = ?
  696. WHERE media_origin = ? AND media_id = ?
  697. """,
  698. ((quarantined_by, origin, media_id) for origin, media_id in remote_mxcs),
  699. )
  700. total_media_quarantined += txn.rowcount if txn.rowcount > 0 else 0
  701. return total_media_quarantined
  702. async def get_all_new_public_rooms(
  703. self, instance_name: str, last_id: int, current_id: int, limit: int
  704. ) -> Tuple[List[Tuple[int, tuple]], int, bool]:
  705. """Get updates for public rooms replication stream.
  706. Args:
  707. instance_name: The writer we want to fetch updates from. Unused
  708. here since there is only ever one writer.
  709. last_id: The token to fetch updates from. Exclusive.
  710. current_id: The token to fetch updates up to. Inclusive.
  711. limit: The requested limit for the number of rows to return. The
  712. function may return more or fewer rows.
  713. Returns:
  714. A tuple consisting of: the updates, a token to use to fetch
  715. subsequent updates, and whether we returned fewer rows than exists
  716. between the requested tokens due to the limit.
  717. The token returned can be used in a subsequent call to this
  718. function to get further updatees.
  719. The updates are a list of 2-tuples of stream ID and the row data
  720. """
  721. if last_id == current_id:
  722. return [], current_id, False
  723. def get_all_new_public_rooms(txn):
  724. sql = """
  725. SELECT stream_id, room_id, visibility, appservice_id, network_id
  726. FROM public_room_list_stream
  727. WHERE stream_id > ? AND stream_id <= ?
  728. ORDER BY stream_id ASC
  729. LIMIT ?
  730. """
  731. txn.execute(sql, (last_id, current_id, limit))
  732. updates = [(row[0], row[1:]) for row in txn]
  733. limited = False
  734. upto_token = current_id
  735. if len(updates) >= limit:
  736. upto_token = updates[-1][0]
  737. limited = True
  738. return updates, upto_token, limited
  739. return await self.db_pool.runInteraction(
  740. "get_all_new_public_rooms", get_all_new_public_rooms
  741. )
  742. async def get_rooms_for_retention_period_in_range(
  743. self, min_ms: Optional[int], max_ms: Optional[int], include_null: bool = False
  744. ) -> Dict[str, dict]:
  745. """Retrieves all of the rooms within the given retention range.
  746. Optionally includes the rooms which don't have a retention policy.
  747. Args:
  748. min_ms: Duration in milliseconds that define the lower limit of
  749. the range to handle (exclusive). If None, doesn't set a lower limit.
  750. max_ms: Duration in milliseconds that define the upper limit of
  751. the range to handle (inclusive). If None, doesn't set an upper limit.
  752. include_null: Whether to include rooms which retention policy is NULL
  753. in the returned set.
  754. Returns:
  755. The rooms within this range, along with their retention
  756. policy. The key is "room_id", and maps to a dict describing the retention
  757. policy associated with this room ID. The keys for this nested dict are
  758. "min_lifetime" (int|None), and "max_lifetime" (int|None).
  759. """
  760. def get_rooms_for_retention_period_in_range_txn(txn):
  761. range_conditions = []
  762. args = []
  763. if min_ms is not None:
  764. range_conditions.append("max_lifetime > ?")
  765. args.append(min_ms)
  766. if max_ms is not None:
  767. range_conditions.append("max_lifetime <= ?")
  768. args.append(max_ms)
  769. # Do a first query which will retrieve the rooms that have a retention policy
  770. # in their current state.
  771. sql = """
  772. SELECT room_id, min_lifetime, max_lifetime FROM room_retention
  773. INNER JOIN current_state_events USING (event_id, room_id)
  774. """
  775. if len(range_conditions):
  776. sql += " WHERE (" + " AND ".join(range_conditions) + ")"
  777. if include_null:
  778. sql += " OR max_lifetime IS NULL"
  779. txn.execute(sql, args)
  780. rows = self.db_pool.cursor_to_dict(txn)
  781. rooms_dict = {}
  782. for row in rows:
  783. rooms_dict[row["room_id"]] = {
  784. "min_lifetime": row["min_lifetime"],
  785. "max_lifetime": row["max_lifetime"],
  786. }
  787. if include_null:
  788. # If required, do a second query that retrieves all of the rooms we know
  789. # of so we can handle rooms with no retention policy.
  790. sql = "SELECT DISTINCT room_id FROM current_state_events"
  791. txn.execute(sql)
  792. rows = self.db_pool.cursor_to_dict(txn)
  793. # If a room isn't already in the dict (i.e. it doesn't have a retention
  794. # policy in its state), add it with a null policy.
  795. for row in rows:
  796. if row["room_id"] not in rooms_dict:
  797. rooms_dict[row["room_id"]] = {
  798. "min_lifetime": None,
  799. "max_lifetime": None,
  800. }
  801. return rooms_dict
  802. return await self.db_pool.runInteraction(
  803. "get_rooms_for_retention_period_in_range",
  804. get_rooms_for_retention_period_in_range_txn,
  805. )
  806. class RoomBackgroundUpdateStore(SQLBaseStore):
  807. REMOVE_TOMESTONED_ROOMS_BG_UPDATE = "remove_tombstoned_rooms_from_directory"
  808. ADD_ROOMS_ROOM_VERSION_COLUMN = "add_rooms_room_version_column"
  809. def __init__(self, database: DatabasePool, db_conn, hs):
  810. super().__init__(database, db_conn, hs)
  811. self.config = hs.config
  812. self.db_pool.updates.register_background_update_handler(
  813. "insert_room_retention", self._background_insert_retention,
  814. )
  815. self.db_pool.updates.register_background_update_handler(
  816. self.REMOVE_TOMESTONED_ROOMS_BG_UPDATE,
  817. self._remove_tombstoned_rooms_from_directory,
  818. )
  819. self.db_pool.updates.register_background_update_handler(
  820. self.ADD_ROOMS_ROOM_VERSION_COLUMN,
  821. self._background_add_rooms_room_version_column,
  822. )
  823. async def _background_insert_retention(self, progress, batch_size):
  824. """Retrieves a list of all rooms within a range and inserts an entry for each of
  825. them into the room_retention table.
  826. NULLs the property's columns if missing from the retention event in the room's
  827. state (or NULLs all of them if there's no retention event in the room's state),
  828. so that we fall back to the server's retention policy.
  829. """
  830. last_room = progress.get("room_id", "")
  831. def _background_insert_retention_txn(txn):
  832. txn.execute(
  833. """
  834. SELECT state.room_id, state.event_id, events.json
  835. FROM current_state_events as state
  836. LEFT JOIN event_json AS events ON (state.event_id = events.event_id)
  837. WHERE state.room_id > ? AND state.type = '%s'
  838. ORDER BY state.room_id ASC
  839. LIMIT ?;
  840. """
  841. % EventTypes.Retention,
  842. (last_room, batch_size),
  843. )
  844. rows = self.db_pool.cursor_to_dict(txn)
  845. if not rows:
  846. return True
  847. for row in rows:
  848. if not row["json"]:
  849. retention_policy = {}
  850. else:
  851. ev = db_to_json(row["json"])
  852. retention_policy = ev["content"]
  853. self.db_pool.simple_insert_txn(
  854. txn=txn,
  855. table="room_retention",
  856. values={
  857. "room_id": row["room_id"],
  858. "event_id": row["event_id"],
  859. "min_lifetime": retention_policy.get("min_lifetime"),
  860. "max_lifetime": retention_policy.get("max_lifetime"),
  861. },
  862. )
  863. logger.info("Inserted %d rows into room_retention", len(rows))
  864. self.db_pool.updates._background_update_progress_txn(
  865. txn, "insert_room_retention", {"room_id": rows[-1]["room_id"]}
  866. )
  867. if batch_size > len(rows):
  868. return True
  869. else:
  870. return False
  871. end = await self.db_pool.runInteraction(
  872. "insert_room_retention", _background_insert_retention_txn,
  873. )
  874. if end:
  875. await self.db_pool.updates._end_background_update("insert_room_retention")
  876. return batch_size
  877. async def _background_add_rooms_room_version_column(
  878. self, progress: dict, batch_size: int
  879. ):
  880. """Background update to go and add room version inforamtion to `rooms`
  881. table from `current_state_events` table.
  882. """
  883. last_room_id = progress.get("room_id", "")
  884. def _background_add_rooms_room_version_column_txn(txn: LoggingTransaction):
  885. sql = """
  886. SELECT room_id, json FROM current_state_events
  887. INNER JOIN event_json USING (room_id, event_id)
  888. WHERE room_id > ? AND type = 'm.room.create' AND state_key = ''
  889. ORDER BY room_id
  890. LIMIT ?
  891. """
  892. txn.execute(sql, (last_room_id, batch_size))
  893. updates = []
  894. for room_id, event_json in txn:
  895. event_dict = db_to_json(event_json)
  896. room_version_id = event_dict.get("content", {}).get(
  897. "room_version", RoomVersions.V1.identifier
  898. )
  899. creator = event_dict.get("content").get("creator")
  900. updates.append((room_id, creator, room_version_id))
  901. if not updates:
  902. return True
  903. new_last_room_id = ""
  904. for room_id, creator, room_version_id in updates:
  905. # We upsert here just in case we don't already have a row,
  906. # mainly for paranoia as much badness would happen if we don't
  907. # insert the row and then try and get the room version for the
  908. # room.
  909. self.db_pool.simple_upsert_txn(
  910. txn,
  911. table="rooms",
  912. keyvalues={"room_id": room_id},
  913. values={"room_version": room_version_id},
  914. insertion_values={"is_public": False, "creator": creator},
  915. )
  916. new_last_room_id = room_id
  917. self.db_pool.updates._background_update_progress_txn(
  918. txn, self.ADD_ROOMS_ROOM_VERSION_COLUMN, {"room_id": new_last_room_id}
  919. )
  920. return False
  921. end = await self.db_pool.runInteraction(
  922. "_background_add_rooms_room_version_column",
  923. _background_add_rooms_room_version_column_txn,
  924. )
  925. if end:
  926. await self.db_pool.updates._end_background_update(
  927. self.ADD_ROOMS_ROOM_VERSION_COLUMN
  928. )
  929. return batch_size
  930. async def _remove_tombstoned_rooms_from_directory(
  931. self, progress, batch_size
  932. ) -> int:
  933. """Removes any rooms with tombstone events from the room directory
  934. Nowadays this is handled by the room upgrade handler, but we may have some
  935. that got left behind
  936. """
  937. last_room = progress.get("room_id", "")
  938. def _get_rooms(txn):
  939. txn.execute(
  940. """
  941. SELECT room_id
  942. FROM rooms r
  943. INNER JOIN current_state_events cse USING (room_id)
  944. WHERE room_id > ? AND r.is_public
  945. AND cse.type = '%s' AND cse.state_key = ''
  946. ORDER BY room_id ASC
  947. LIMIT ?;
  948. """
  949. % EventTypes.Tombstone,
  950. (last_room, batch_size),
  951. )
  952. return [row[0] for row in txn]
  953. rooms = await self.db_pool.runInteraction(
  954. "get_tombstoned_directory_rooms", _get_rooms
  955. )
  956. if not rooms:
  957. await self.db_pool.updates._end_background_update(
  958. self.REMOVE_TOMESTONED_ROOMS_BG_UPDATE
  959. )
  960. return 0
  961. for room_id in rooms:
  962. logger.info("Removing tombstoned room %s from the directory", room_id)
  963. await self.set_room_is_public(room_id, False)
  964. await self.db_pool.updates._background_update_progress(
  965. self.REMOVE_TOMESTONED_ROOMS_BG_UPDATE, {"room_id": rooms[-1]}
  966. )
  967. return len(rooms)
  968. @abstractmethod
  969. def set_room_is_public(self, room_id, is_public):
  970. # this will need to be implemented if a background update is performed with
  971. # existing (tombstoned, public) rooms in the database.
  972. #
  973. # It's overridden by RoomStore for the synapse master.
  974. raise NotImplementedError()
  975. class RoomStore(RoomBackgroundUpdateStore, RoomWorkerStore, SearchStore):
  976. def __init__(self, database: DatabasePool, db_conn, hs):
  977. super().__init__(database, db_conn, hs)
  978. self.config = hs.config
  979. async def upsert_room_on_join(self, room_id: str, room_version: RoomVersion):
  980. """Ensure that the room is stored in the table
  981. Called when we join a room over federation, and overwrites any room version
  982. currently in the table.
  983. """
  984. await self.db_pool.simple_upsert(
  985. desc="upsert_room_on_join",
  986. table="rooms",
  987. keyvalues={"room_id": room_id},
  988. values={"room_version": room_version.identifier},
  989. insertion_values={"is_public": False, "creator": ""},
  990. # rooms has a unique constraint on room_id, so no need to lock when doing an
  991. # emulated upsert.
  992. lock=False,
  993. )
  994. async def store_room(
  995. self,
  996. room_id: str,
  997. room_creator_user_id: str,
  998. is_public: bool,
  999. room_version: RoomVersion,
  1000. ):
  1001. """Stores a room.
  1002. Args:
  1003. room_id: The desired room ID, can be None.
  1004. room_creator_user_id: The user ID of the room creator.
  1005. is_public: True to indicate that this room should appear in
  1006. public room lists.
  1007. room_version: The version of the room
  1008. Raises:
  1009. StoreError if the room could not be stored.
  1010. """
  1011. try:
  1012. def store_room_txn(txn, next_id):
  1013. self.db_pool.simple_insert_txn(
  1014. txn,
  1015. "rooms",
  1016. {
  1017. "room_id": room_id,
  1018. "creator": room_creator_user_id,
  1019. "is_public": is_public,
  1020. "room_version": room_version.identifier,
  1021. },
  1022. )
  1023. if is_public:
  1024. self.db_pool.simple_insert_txn(
  1025. txn,
  1026. table="public_room_list_stream",
  1027. values={
  1028. "stream_id": next_id,
  1029. "room_id": room_id,
  1030. "visibility": is_public,
  1031. },
  1032. )
  1033. async with self._public_room_id_gen.get_next() as next_id:
  1034. await self.db_pool.runInteraction(
  1035. "store_room_txn", store_room_txn, next_id
  1036. )
  1037. except Exception as e:
  1038. logger.error("store_room with room_id=%s failed: %s", room_id, e)
  1039. raise StoreError(500, "Problem creating room.")
  1040. async def maybe_store_room_on_outlier_membership(
  1041. self, room_id: str, room_version: RoomVersion
  1042. ):
  1043. """
  1044. When we receive an invite, a knock or any other event over federation that may relate
  1045. to a room we are not in, store the version of the room if we don't already know the
  1046. room version.
  1047. """
  1048. await self.db_pool.simple_upsert(
  1049. desc="maybe_store_room_on_outlier_membership",
  1050. table="rooms",
  1051. keyvalues={"room_id": room_id},
  1052. values={},
  1053. insertion_values={
  1054. "room_version": room_version.identifier,
  1055. "is_public": False,
  1056. "creator": "",
  1057. },
  1058. # rooms has a unique constraint on room_id, so no need to lock when doing an
  1059. # emulated upsert.
  1060. lock=False,
  1061. )
  1062. async def set_room_is_public(self, room_id, is_public):
  1063. def set_room_is_public_txn(txn, next_id):
  1064. self.db_pool.simple_update_one_txn(
  1065. txn,
  1066. table="rooms",
  1067. keyvalues={"room_id": room_id},
  1068. updatevalues={"is_public": is_public},
  1069. )
  1070. entries = self.db_pool.simple_select_list_txn(
  1071. txn,
  1072. table="public_room_list_stream",
  1073. keyvalues={
  1074. "room_id": room_id,
  1075. "appservice_id": None,
  1076. "network_id": None,
  1077. },
  1078. retcols=("stream_id", "visibility"),
  1079. )
  1080. entries.sort(key=lambda r: r["stream_id"])
  1081. add_to_stream = True
  1082. if entries:
  1083. add_to_stream = bool(entries[-1]["visibility"]) != is_public
  1084. if add_to_stream:
  1085. self.db_pool.simple_insert_txn(
  1086. txn,
  1087. table="public_room_list_stream",
  1088. values={
  1089. "stream_id": next_id,
  1090. "room_id": room_id,
  1091. "visibility": is_public,
  1092. "appservice_id": None,
  1093. "network_id": None,
  1094. },
  1095. )
  1096. async with self._public_room_id_gen.get_next() as next_id:
  1097. await self.db_pool.runInteraction(
  1098. "set_room_is_public", set_room_is_public_txn, next_id
  1099. )
  1100. self.hs.get_notifier().on_new_replication_data()
  1101. async def set_room_is_public_appservice(
  1102. self, room_id, appservice_id, network_id, is_public
  1103. ):
  1104. """Edit the appservice/network specific public room list.
  1105. Each appservice can have a number of published room lists associated
  1106. with them, keyed off of an appservice defined `network_id`, which
  1107. basically represents a single instance of a bridge to a third party
  1108. network.
  1109. Args:
  1110. room_id (str)
  1111. appservice_id (str)
  1112. network_id (str)
  1113. is_public (bool): Whether to publish or unpublish the room from the
  1114. list.
  1115. """
  1116. def set_room_is_public_appservice_txn(txn, next_id):
  1117. if is_public:
  1118. try:
  1119. self.db_pool.simple_insert_txn(
  1120. txn,
  1121. table="appservice_room_list",
  1122. values={
  1123. "appservice_id": appservice_id,
  1124. "network_id": network_id,
  1125. "room_id": room_id,
  1126. },
  1127. )
  1128. except self.database_engine.module.IntegrityError:
  1129. # We've already inserted, nothing to do.
  1130. return
  1131. else:
  1132. self.db_pool.simple_delete_txn(
  1133. txn,
  1134. table="appservice_room_list",
  1135. keyvalues={
  1136. "appservice_id": appservice_id,
  1137. "network_id": network_id,
  1138. "room_id": room_id,
  1139. },
  1140. )
  1141. entries = self.db_pool.simple_select_list_txn(
  1142. txn,
  1143. table="public_room_list_stream",
  1144. keyvalues={
  1145. "room_id": room_id,
  1146. "appservice_id": appservice_id,
  1147. "network_id": network_id,
  1148. },
  1149. retcols=("stream_id", "visibility"),
  1150. )
  1151. entries.sort(key=lambda r: r["stream_id"])
  1152. add_to_stream = True
  1153. if entries:
  1154. add_to_stream = bool(entries[-1]["visibility"]) != is_public
  1155. if add_to_stream:
  1156. self.db_pool.simple_insert_txn(
  1157. txn,
  1158. table="public_room_list_stream",
  1159. values={
  1160. "stream_id": next_id,
  1161. "room_id": room_id,
  1162. "visibility": is_public,
  1163. "appservice_id": appservice_id,
  1164. "network_id": network_id,
  1165. },
  1166. )
  1167. async with self._public_room_id_gen.get_next() as next_id:
  1168. await self.db_pool.runInteraction(
  1169. "set_room_is_public_appservice",
  1170. set_room_is_public_appservice_txn,
  1171. next_id,
  1172. )
  1173. self.hs.get_notifier().on_new_replication_data()
  1174. async def add_event_report(
  1175. self,
  1176. room_id: str,
  1177. event_id: str,
  1178. user_id: str,
  1179. reason: str,
  1180. content: JsonDict,
  1181. received_ts: int,
  1182. ) -> None:
  1183. next_id = self._event_reports_id_gen.get_next()
  1184. await self.db_pool.simple_insert(
  1185. table="event_reports",
  1186. values={
  1187. "id": next_id,
  1188. "received_ts": received_ts,
  1189. "room_id": room_id,
  1190. "event_id": event_id,
  1191. "user_id": user_id,
  1192. "reason": reason,
  1193. "content": json_encoder.encode(content),
  1194. },
  1195. desc="add_event_report",
  1196. )
  1197. async def get_event_report(self, report_id: int) -> Optional[Dict[str, Any]]:
  1198. """Retrieve an event report
  1199. Args:
  1200. report_id: ID of reported event in database
  1201. Returns:
  1202. event_report: json list of information from event report
  1203. """
  1204. def _get_event_report_txn(txn, report_id):
  1205. sql = """
  1206. SELECT
  1207. er.id,
  1208. er.received_ts,
  1209. er.room_id,
  1210. er.event_id,
  1211. er.user_id,
  1212. er.content,
  1213. events.sender,
  1214. room_stats_state.canonical_alias,
  1215. room_stats_state.name,
  1216. event_json.json AS event_json
  1217. FROM event_reports AS er
  1218. LEFT JOIN events
  1219. ON events.event_id = er.event_id
  1220. JOIN event_json
  1221. ON event_json.event_id = er.event_id
  1222. JOIN room_stats_state
  1223. ON room_stats_state.room_id = er.room_id
  1224. WHERE er.id = ?
  1225. """
  1226. txn.execute(sql, [report_id])
  1227. row = txn.fetchone()
  1228. if not row:
  1229. return None
  1230. event_report = {
  1231. "id": row[0],
  1232. "received_ts": row[1],
  1233. "room_id": row[2],
  1234. "event_id": row[3],
  1235. "user_id": row[4],
  1236. "score": db_to_json(row[5]).get("score"),
  1237. "reason": db_to_json(row[5]).get("reason"),
  1238. "sender": row[6],
  1239. "canonical_alias": row[7],
  1240. "name": row[8],
  1241. "event_json": db_to_json(row[9]),
  1242. }
  1243. return event_report
  1244. return await self.db_pool.runInteraction(
  1245. "get_event_report", _get_event_report_txn, report_id
  1246. )
  1247. async def get_event_reports_paginate(
  1248. self,
  1249. start: int,
  1250. limit: int,
  1251. direction: str = "b",
  1252. user_id: Optional[str] = None,
  1253. room_id: Optional[str] = None,
  1254. ) -> Tuple[List[Dict[str, Any]], int]:
  1255. """Retrieve a paginated list of event reports
  1256. Args:
  1257. start: event offset to begin the query from
  1258. limit: number of rows to retrieve
  1259. direction: Whether to fetch the most recent first (`"b"`) or the
  1260. oldest first (`"f"`)
  1261. user_id: search for user_id. Ignored if user_id is None
  1262. room_id: search for room_id. Ignored if room_id is None
  1263. Returns:
  1264. event_reports: json list of event reports
  1265. count: total number of event reports matching the filter criteria
  1266. """
  1267. def _get_event_reports_paginate_txn(txn):
  1268. filters = []
  1269. args = []
  1270. if user_id:
  1271. filters.append("er.user_id LIKE ?")
  1272. args.extend(["%" + user_id + "%"])
  1273. if room_id:
  1274. filters.append("er.room_id LIKE ?")
  1275. args.extend(["%" + room_id + "%"])
  1276. if direction == "b":
  1277. order = "DESC"
  1278. else:
  1279. order = "ASC"
  1280. where_clause = "WHERE " + " AND ".join(filters) if len(filters) > 0 else ""
  1281. sql = """
  1282. SELECT COUNT(*) as total_event_reports
  1283. FROM event_reports AS er
  1284. {}
  1285. """.format(
  1286. where_clause
  1287. )
  1288. txn.execute(sql, args)
  1289. count = txn.fetchone()[0]
  1290. sql = """
  1291. SELECT
  1292. er.id,
  1293. er.received_ts,
  1294. er.room_id,
  1295. er.event_id,
  1296. er.user_id,
  1297. er.content,
  1298. events.sender,
  1299. room_stats_state.canonical_alias,
  1300. room_stats_state.name
  1301. FROM event_reports AS er
  1302. LEFT JOIN events
  1303. ON events.event_id = er.event_id
  1304. JOIN room_stats_state
  1305. ON room_stats_state.room_id = er.room_id
  1306. {where_clause}
  1307. ORDER BY er.received_ts {order}
  1308. LIMIT ?
  1309. OFFSET ?
  1310. """.format(
  1311. where_clause=where_clause, order=order,
  1312. )
  1313. args += [limit, start]
  1314. txn.execute(sql, args)
  1315. event_reports = []
  1316. for row in txn:
  1317. try:
  1318. s = db_to_json(row[5]).get("score")
  1319. r = db_to_json(row[5]).get("reason")
  1320. except Exception:
  1321. logger.error("Unable to parse json from event_reports: %s", row[0])
  1322. continue
  1323. event_reports.append(
  1324. {
  1325. "id": row[0],
  1326. "received_ts": row[1],
  1327. "room_id": row[2],
  1328. "event_id": row[3],
  1329. "user_id": row[4],
  1330. "score": s,
  1331. "reason": r,
  1332. "sender": row[6],
  1333. "canonical_alias": row[7],
  1334. "name": row[8],
  1335. }
  1336. )
  1337. return event_reports, count
  1338. return await self.db_pool.runInteraction(
  1339. "get_event_reports_paginate", _get_event_reports_paginate_txn
  1340. )
  1341. def get_current_public_room_stream_id(self):
  1342. return self._public_room_id_gen.get_current_token()
  1343. async def block_room(self, room_id: str, user_id: str) -> None:
  1344. """Marks the room as blocked. Can be called multiple times.
  1345. Args:
  1346. room_id: Room to block
  1347. user_id: Who blocked it
  1348. """
  1349. await self.db_pool.simple_upsert(
  1350. table="blocked_rooms",
  1351. keyvalues={"room_id": room_id},
  1352. values={},
  1353. insertion_values={"user_id": user_id},
  1354. desc="block_room",
  1355. )
  1356. await self.db_pool.runInteraction(
  1357. "block_room_invalidation",
  1358. self._invalidate_cache_and_stream,
  1359. self.is_room_blocked,
  1360. (room_id,),
  1361. )