room_versions.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. # Copyright 2019 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Callable, Dict, Optional
  15. import attr
  16. class EventFormatVersions:
  17. """This is an internal enum for tracking the version of the event format,
  18. independently of the room version.
  19. To reduce confusion, the event format versions are named after the room
  20. versions that they were used or introduced in.
  21. The concept of an 'event format version' is specific to Synapse (the
  22. specification does not mention this term.)
  23. """
  24. ROOM_V1_V2 = 1 # $id:server event id format: used for room v1 and v2
  25. ROOM_V3 = 2 # MSC1659-style $hash event id format: used for room v3
  26. ROOM_V4_PLUS = 3 # MSC1884-style $hash format: introduced for room v4
  27. KNOWN_EVENT_FORMAT_VERSIONS = {
  28. EventFormatVersions.ROOM_V1_V2,
  29. EventFormatVersions.ROOM_V3,
  30. EventFormatVersions.ROOM_V4_PLUS,
  31. }
  32. class StateResolutionVersions:
  33. """Enum to identify the state resolution algorithms"""
  34. V1 = 1 # room v1 state res
  35. V2 = 2 # MSC1442 state res: room v2 and later
  36. class RoomDisposition:
  37. STABLE = "stable"
  38. UNSTABLE = "unstable"
  39. @attr.s(slots=True, frozen=True, auto_attribs=True)
  40. class RoomVersion:
  41. """An object which describes the unique attributes of a room version."""
  42. identifier: str # the identifier for this version
  43. disposition: str # one of the RoomDispositions
  44. event_format: int # one of the EventFormatVersions
  45. state_res: int # one of the StateResolutionVersions
  46. enforce_key_validity: bool
  47. # Before MSC2432, m.room.aliases had special auth rules and redaction rules
  48. special_case_aliases_auth: bool
  49. # Strictly enforce canonicaljson, do not allow:
  50. # * Integers outside the range of [-2 ^ 53 + 1, 2 ^ 53 - 1]
  51. # * Floats
  52. # * NaN, Infinity, -Infinity
  53. strict_canonicaljson: bool
  54. # MSC2209: Check 'notifications' key while verifying
  55. # m.room.power_levels auth rules.
  56. limit_notifications_power_levels: bool
  57. # MSC2174/MSC2176: Apply updated redaction rules algorithm.
  58. msc2176_redaction_rules: bool
  59. # MSC3083: Support the 'restricted' join_rule.
  60. msc3083_join_rules: bool
  61. # MSC3375: Support for the proper redaction rules for MSC3083. This mustn't
  62. # be enabled if MSC3083 is not.
  63. msc3375_redaction_rules: bool
  64. # MSC2403: Allows join_rules to be set to 'knock', changes auth rules to allow sending
  65. # m.room.membership event with membership 'knock'.
  66. msc2403_knocking: bool
  67. # MSC2716: Adds m.room.power_levels -> content.historical field to control
  68. # whether "insertion", "chunk", "marker" events can be sent
  69. msc2716_historical: bool
  70. # MSC2716: Adds support for redacting "insertion", "chunk", and "marker" events
  71. msc2716_redactions: bool
  72. # MSC3787: Adds support for a `knock_restricted` join rule, mixing concepts of
  73. # knocks and restricted join rules into the same join condition.
  74. msc3787_knock_restricted_join_rule: bool
  75. # MSC3667: Enforce integer power levels
  76. msc3667_int_only_power_levels: bool
  77. class RoomVersions:
  78. V1 = RoomVersion(
  79. "1",
  80. RoomDisposition.STABLE,
  81. EventFormatVersions.ROOM_V1_V2,
  82. StateResolutionVersions.V1,
  83. enforce_key_validity=False,
  84. special_case_aliases_auth=True,
  85. strict_canonicaljson=False,
  86. limit_notifications_power_levels=False,
  87. msc2176_redaction_rules=False,
  88. msc3083_join_rules=False,
  89. msc3375_redaction_rules=False,
  90. msc2403_knocking=False,
  91. msc2716_historical=False,
  92. msc2716_redactions=False,
  93. msc3787_knock_restricted_join_rule=False,
  94. msc3667_int_only_power_levels=False,
  95. )
  96. V2 = RoomVersion(
  97. "2",
  98. RoomDisposition.STABLE,
  99. EventFormatVersions.ROOM_V1_V2,
  100. StateResolutionVersions.V2,
  101. enforce_key_validity=False,
  102. special_case_aliases_auth=True,
  103. strict_canonicaljson=False,
  104. limit_notifications_power_levels=False,
  105. msc2176_redaction_rules=False,
  106. msc3083_join_rules=False,
  107. msc3375_redaction_rules=False,
  108. msc2403_knocking=False,
  109. msc2716_historical=False,
  110. msc2716_redactions=False,
  111. msc3787_knock_restricted_join_rule=False,
  112. msc3667_int_only_power_levels=False,
  113. )
  114. V3 = RoomVersion(
  115. "3",
  116. RoomDisposition.STABLE,
  117. EventFormatVersions.ROOM_V3,
  118. StateResolutionVersions.V2,
  119. enforce_key_validity=False,
  120. special_case_aliases_auth=True,
  121. strict_canonicaljson=False,
  122. limit_notifications_power_levels=False,
  123. msc2176_redaction_rules=False,
  124. msc3083_join_rules=False,
  125. msc3375_redaction_rules=False,
  126. msc2403_knocking=False,
  127. msc2716_historical=False,
  128. msc2716_redactions=False,
  129. msc3787_knock_restricted_join_rule=False,
  130. msc3667_int_only_power_levels=False,
  131. )
  132. V4 = RoomVersion(
  133. "4",
  134. RoomDisposition.STABLE,
  135. EventFormatVersions.ROOM_V4_PLUS,
  136. StateResolutionVersions.V2,
  137. enforce_key_validity=False,
  138. special_case_aliases_auth=True,
  139. strict_canonicaljson=False,
  140. limit_notifications_power_levels=False,
  141. msc2176_redaction_rules=False,
  142. msc3083_join_rules=False,
  143. msc3375_redaction_rules=False,
  144. msc2403_knocking=False,
  145. msc2716_historical=False,
  146. msc2716_redactions=False,
  147. msc3787_knock_restricted_join_rule=False,
  148. msc3667_int_only_power_levels=False,
  149. )
  150. V5 = RoomVersion(
  151. "5",
  152. RoomDisposition.STABLE,
  153. EventFormatVersions.ROOM_V4_PLUS,
  154. StateResolutionVersions.V2,
  155. enforce_key_validity=True,
  156. special_case_aliases_auth=True,
  157. strict_canonicaljson=False,
  158. limit_notifications_power_levels=False,
  159. msc2176_redaction_rules=False,
  160. msc3083_join_rules=False,
  161. msc3375_redaction_rules=False,
  162. msc2403_knocking=False,
  163. msc2716_historical=False,
  164. msc2716_redactions=False,
  165. msc3787_knock_restricted_join_rule=False,
  166. msc3667_int_only_power_levels=False,
  167. )
  168. V6 = RoomVersion(
  169. "6",
  170. RoomDisposition.STABLE,
  171. EventFormatVersions.ROOM_V4_PLUS,
  172. StateResolutionVersions.V2,
  173. enforce_key_validity=True,
  174. special_case_aliases_auth=False,
  175. strict_canonicaljson=True,
  176. limit_notifications_power_levels=True,
  177. msc2176_redaction_rules=False,
  178. msc3083_join_rules=False,
  179. msc3375_redaction_rules=False,
  180. msc2403_knocking=False,
  181. msc2716_historical=False,
  182. msc2716_redactions=False,
  183. msc3787_knock_restricted_join_rule=False,
  184. msc3667_int_only_power_levels=False,
  185. )
  186. MSC2176 = RoomVersion(
  187. "org.matrix.msc2176",
  188. RoomDisposition.UNSTABLE,
  189. EventFormatVersions.ROOM_V4_PLUS,
  190. StateResolutionVersions.V2,
  191. enforce_key_validity=True,
  192. special_case_aliases_auth=False,
  193. strict_canonicaljson=True,
  194. limit_notifications_power_levels=True,
  195. msc2176_redaction_rules=True,
  196. msc3083_join_rules=False,
  197. msc3375_redaction_rules=False,
  198. msc2403_knocking=False,
  199. msc2716_historical=False,
  200. msc2716_redactions=False,
  201. msc3787_knock_restricted_join_rule=False,
  202. msc3667_int_only_power_levels=False,
  203. )
  204. V7 = RoomVersion(
  205. "7",
  206. RoomDisposition.STABLE,
  207. EventFormatVersions.ROOM_V4_PLUS,
  208. StateResolutionVersions.V2,
  209. enforce_key_validity=True,
  210. special_case_aliases_auth=False,
  211. strict_canonicaljson=True,
  212. limit_notifications_power_levels=True,
  213. msc2176_redaction_rules=False,
  214. msc3083_join_rules=False,
  215. msc3375_redaction_rules=False,
  216. msc2403_knocking=True,
  217. msc2716_historical=False,
  218. msc2716_redactions=False,
  219. msc3787_knock_restricted_join_rule=False,
  220. msc3667_int_only_power_levels=False,
  221. )
  222. V8 = RoomVersion(
  223. "8",
  224. RoomDisposition.STABLE,
  225. EventFormatVersions.ROOM_V4_PLUS,
  226. StateResolutionVersions.V2,
  227. enforce_key_validity=True,
  228. special_case_aliases_auth=False,
  229. strict_canonicaljson=True,
  230. limit_notifications_power_levels=True,
  231. msc2176_redaction_rules=False,
  232. msc3083_join_rules=True,
  233. msc3375_redaction_rules=False,
  234. msc2403_knocking=True,
  235. msc2716_historical=False,
  236. msc2716_redactions=False,
  237. msc3787_knock_restricted_join_rule=False,
  238. msc3667_int_only_power_levels=False,
  239. )
  240. V9 = RoomVersion(
  241. "9",
  242. RoomDisposition.STABLE,
  243. EventFormatVersions.ROOM_V4_PLUS,
  244. StateResolutionVersions.V2,
  245. enforce_key_validity=True,
  246. special_case_aliases_auth=False,
  247. strict_canonicaljson=True,
  248. limit_notifications_power_levels=True,
  249. msc2176_redaction_rules=False,
  250. msc3083_join_rules=True,
  251. msc3375_redaction_rules=True,
  252. msc2403_knocking=True,
  253. msc2716_historical=False,
  254. msc2716_redactions=False,
  255. msc3787_knock_restricted_join_rule=False,
  256. msc3667_int_only_power_levels=False,
  257. )
  258. MSC3787 = RoomVersion(
  259. "org.matrix.msc3787",
  260. RoomDisposition.UNSTABLE,
  261. EventFormatVersions.ROOM_V4_PLUS,
  262. StateResolutionVersions.V2,
  263. enforce_key_validity=True,
  264. special_case_aliases_auth=False,
  265. strict_canonicaljson=True,
  266. limit_notifications_power_levels=True,
  267. msc2176_redaction_rules=False,
  268. msc3083_join_rules=True,
  269. msc3375_redaction_rules=True,
  270. msc2403_knocking=True,
  271. msc2716_historical=False,
  272. msc2716_redactions=False,
  273. msc3787_knock_restricted_join_rule=True,
  274. msc3667_int_only_power_levels=False,
  275. )
  276. V10 = RoomVersion(
  277. "10",
  278. RoomDisposition.STABLE,
  279. EventFormatVersions.ROOM_V4_PLUS,
  280. StateResolutionVersions.V2,
  281. enforce_key_validity=True,
  282. special_case_aliases_auth=False,
  283. strict_canonicaljson=True,
  284. limit_notifications_power_levels=True,
  285. msc2176_redaction_rules=False,
  286. msc3083_join_rules=True,
  287. msc3375_redaction_rules=True,
  288. msc2403_knocking=True,
  289. msc2716_historical=False,
  290. msc2716_redactions=False,
  291. msc3787_knock_restricted_join_rule=True,
  292. msc3667_int_only_power_levels=True,
  293. )
  294. MSC2716v4 = RoomVersion(
  295. "org.matrix.msc2716v4",
  296. RoomDisposition.UNSTABLE,
  297. EventFormatVersions.ROOM_V4_PLUS,
  298. StateResolutionVersions.V2,
  299. enforce_key_validity=True,
  300. special_case_aliases_auth=False,
  301. strict_canonicaljson=True,
  302. limit_notifications_power_levels=True,
  303. msc2176_redaction_rules=False,
  304. msc3083_join_rules=False,
  305. msc3375_redaction_rules=False,
  306. msc2403_knocking=True,
  307. msc2716_historical=True,
  308. msc2716_redactions=True,
  309. msc3787_knock_restricted_join_rule=False,
  310. msc3667_int_only_power_levels=False,
  311. )
  312. KNOWN_ROOM_VERSIONS: Dict[str, RoomVersion] = {
  313. v.identifier: v
  314. for v in (
  315. RoomVersions.V1,
  316. RoomVersions.V2,
  317. RoomVersions.V3,
  318. RoomVersions.V4,
  319. RoomVersions.V5,
  320. RoomVersions.V6,
  321. RoomVersions.MSC2176,
  322. RoomVersions.V7,
  323. RoomVersions.V8,
  324. RoomVersions.V9,
  325. RoomVersions.MSC3787,
  326. RoomVersions.V10,
  327. RoomVersions.MSC2716v4,
  328. )
  329. }
  330. @attr.s(slots=True, frozen=True, auto_attribs=True)
  331. class RoomVersionCapability:
  332. """An object which describes the unique attributes of a room version."""
  333. identifier: str # the identifier for this capability
  334. preferred_version: Optional[RoomVersion]
  335. support_check_lambda: Callable[[RoomVersion], bool]
  336. MSC3244_CAPABILITIES = {
  337. cap.identifier: {
  338. "preferred": cap.preferred_version.identifier
  339. if cap.preferred_version is not None
  340. else None,
  341. "support": [
  342. v.identifier
  343. for v in KNOWN_ROOM_VERSIONS.values()
  344. if cap.support_check_lambda(v)
  345. ],
  346. }
  347. for cap in (
  348. RoomVersionCapability(
  349. "knock",
  350. RoomVersions.V7,
  351. lambda room_version: room_version.msc2403_knocking,
  352. ),
  353. RoomVersionCapability(
  354. "restricted",
  355. RoomVersions.V9,
  356. lambda room_version: room_version.msc3083_join_rules,
  357. ),
  358. )
  359. }