room_versions.py 12 KB

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