room_versions.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. class RoomVersions:
  69. V1 = RoomVersion(
  70. "1",
  71. RoomDisposition.STABLE,
  72. EventFormatVersions.V1,
  73. StateResolutionVersions.V1,
  74. enforce_key_validity=False,
  75. special_case_aliases_auth=True,
  76. strict_canonicaljson=False,
  77. limit_notifications_power_levels=False,
  78. msc2176_redaction_rules=False,
  79. msc3083_join_rules=False,
  80. msc3375_redaction_rules=False,
  81. msc2403_knocking=False,
  82. msc2716_historical=False,
  83. msc2716_redactions=False,
  84. )
  85. V2 = RoomVersion(
  86. "2",
  87. RoomDisposition.STABLE,
  88. EventFormatVersions.V1,
  89. StateResolutionVersions.V2,
  90. enforce_key_validity=False,
  91. special_case_aliases_auth=True,
  92. strict_canonicaljson=False,
  93. limit_notifications_power_levels=False,
  94. msc2176_redaction_rules=False,
  95. msc3083_join_rules=False,
  96. msc3375_redaction_rules=False,
  97. msc2403_knocking=False,
  98. msc2716_historical=False,
  99. msc2716_redactions=False,
  100. )
  101. V3 = RoomVersion(
  102. "3",
  103. RoomDisposition.STABLE,
  104. EventFormatVersions.V2,
  105. StateResolutionVersions.V2,
  106. enforce_key_validity=False,
  107. special_case_aliases_auth=True,
  108. strict_canonicaljson=False,
  109. limit_notifications_power_levels=False,
  110. msc2176_redaction_rules=False,
  111. msc3083_join_rules=False,
  112. msc3375_redaction_rules=False,
  113. msc2403_knocking=False,
  114. msc2716_historical=False,
  115. msc2716_redactions=False,
  116. )
  117. V4 = RoomVersion(
  118. "4",
  119. RoomDisposition.STABLE,
  120. EventFormatVersions.V3,
  121. StateResolutionVersions.V2,
  122. enforce_key_validity=False,
  123. special_case_aliases_auth=True,
  124. strict_canonicaljson=False,
  125. limit_notifications_power_levels=False,
  126. msc2176_redaction_rules=False,
  127. msc3083_join_rules=False,
  128. msc3375_redaction_rules=False,
  129. msc2403_knocking=False,
  130. msc2716_historical=False,
  131. msc2716_redactions=False,
  132. )
  133. V5 = RoomVersion(
  134. "5",
  135. RoomDisposition.STABLE,
  136. EventFormatVersions.V3,
  137. StateResolutionVersions.V2,
  138. enforce_key_validity=True,
  139. special_case_aliases_auth=True,
  140. strict_canonicaljson=False,
  141. limit_notifications_power_levels=False,
  142. msc2176_redaction_rules=False,
  143. msc3083_join_rules=False,
  144. msc3375_redaction_rules=False,
  145. msc2403_knocking=False,
  146. msc2716_historical=False,
  147. msc2716_redactions=False,
  148. )
  149. V6 = RoomVersion(
  150. "6",
  151. RoomDisposition.STABLE,
  152. EventFormatVersions.V3,
  153. StateResolutionVersions.V2,
  154. enforce_key_validity=True,
  155. special_case_aliases_auth=False,
  156. strict_canonicaljson=True,
  157. limit_notifications_power_levels=True,
  158. msc2176_redaction_rules=False,
  159. msc3083_join_rules=False,
  160. msc3375_redaction_rules=False,
  161. msc2403_knocking=False,
  162. msc2716_historical=False,
  163. msc2716_redactions=False,
  164. )
  165. MSC2176 = RoomVersion(
  166. "org.matrix.msc2176",
  167. RoomDisposition.UNSTABLE,
  168. EventFormatVersions.V3,
  169. StateResolutionVersions.V2,
  170. enforce_key_validity=True,
  171. special_case_aliases_auth=False,
  172. strict_canonicaljson=True,
  173. limit_notifications_power_levels=True,
  174. msc2176_redaction_rules=True,
  175. msc3083_join_rules=False,
  176. msc3375_redaction_rules=False,
  177. msc2403_knocking=False,
  178. msc2716_historical=False,
  179. msc2716_redactions=False,
  180. )
  181. V7 = RoomVersion(
  182. "7",
  183. RoomDisposition.STABLE,
  184. EventFormatVersions.V3,
  185. StateResolutionVersions.V2,
  186. enforce_key_validity=True,
  187. special_case_aliases_auth=False,
  188. strict_canonicaljson=True,
  189. limit_notifications_power_levels=True,
  190. msc2176_redaction_rules=False,
  191. msc3083_join_rules=False,
  192. msc3375_redaction_rules=False,
  193. msc2403_knocking=True,
  194. msc2716_historical=False,
  195. msc2716_redactions=False,
  196. )
  197. V8 = RoomVersion(
  198. "8",
  199. RoomDisposition.STABLE,
  200. EventFormatVersions.V3,
  201. StateResolutionVersions.V2,
  202. enforce_key_validity=True,
  203. special_case_aliases_auth=False,
  204. strict_canonicaljson=True,
  205. limit_notifications_power_levels=True,
  206. msc2176_redaction_rules=False,
  207. msc3083_join_rules=True,
  208. msc3375_redaction_rules=False,
  209. msc2403_knocking=True,
  210. msc2716_historical=False,
  211. msc2716_redactions=False,
  212. )
  213. V9 = RoomVersion(
  214. "9",
  215. RoomDisposition.STABLE,
  216. EventFormatVersions.V3,
  217. StateResolutionVersions.V2,
  218. enforce_key_validity=True,
  219. special_case_aliases_auth=False,
  220. strict_canonicaljson=True,
  221. limit_notifications_power_levels=True,
  222. msc2176_redaction_rules=False,
  223. msc3083_join_rules=True,
  224. msc3375_redaction_rules=True,
  225. msc2403_knocking=True,
  226. msc2716_historical=False,
  227. msc2716_redactions=False,
  228. )
  229. MSC2716v3 = RoomVersion(
  230. "org.matrix.msc2716v3",
  231. RoomDisposition.UNSTABLE,
  232. EventFormatVersions.V3,
  233. StateResolutionVersions.V2,
  234. enforce_key_validity=True,
  235. special_case_aliases_auth=False,
  236. strict_canonicaljson=True,
  237. limit_notifications_power_levels=True,
  238. msc2176_redaction_rules=False,
  239. msc3083_join_rules=False,
  240. msc3375_redaction_rules=False,
  241. msc2403_knocking=True,
  242. msc2716_historical=True,
  243. msc2716_redactions=True,
  244. )
  245. KNOWN_ROOM_VERSIONS: Dict[str, RoomVersion] = {
  246. v.identifier: v
  247. for v in (
  248. RoomVersions.V1,
  249. RoomVersions.V2,
  250. RoomVersions.V3,
  251. RoomVersions.V4,
  252. RoomVersions.V5,
  253. RoomVersions.V6,
  254. RoomVersions.MSC2176,
  255. RoomVersions.V7,
  256. RoomVersions.V8,
  257. RoomVersions.V9,
  258. RoomVersions.MSC2716v3,
  259. )
  260. }
  261. @attr.s(slots=True, frozen=True, auto_attribs=True)
  262. class RoomVersionCapability:
  263. """An object which describes the unique attributes of a room version."""
  264. identifier: str # the identifier for this capability
  265. preferred_version: Optional[RoomVersion]
  266. support_check_lambda: Callable[[RoomVersion], bool]
  267. MSC3244_CAPABILITIES = {
  268. cap.identifier: {
  269. "preferred": cap.preferred_version.identifier
  270. if cap.preferred_version is not None
  271. else None,
  272. "support": [
  273. v.identifier
  274. for v in KNOWN_ROOM_VERSIONS.values()
  275. if cap.support_check_lambda(v)
  276. ],
  277. }
  278. for cap in (
  279. RoomVersionCapability(
  280. "knock",
  281. RoomVersions.V7,
  282. lambda room_version: room_version.msc2403_knocking,
  283. ),
  284. RoomVersionCapability(
  285. "restricted",
  286. RoomVersions.V9,
  287. lambda room_version: room_version.msc3083_join_rules,
  288. ),
  289. )
  290. }