Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

389 linhas
13 KiB

  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, Tuple
  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. class PushRuleRoomFlag:
  40. """Enum for listing possible MSC3931 room version feature flags, for push rules"""
  41. # MSC3932: Room version supports MSC1767 Extensible Events.
  42. EXTENSIBLE_EVENTS = "org.matrix.msc3932.extensible_events"
  43. @attr.s(slots=True, frozen=True, auto_attribs=True)
  44. class RoomVersion:
  45. """An object which describes the unique attributes of a room version."""
  46. identifier: str # the identifier for this version
  47. disposition: str # one of the RoomDispositions
  48. event_format: int # one of the EventFormatVersions
  49. state_res: int # one of the StateResolutionVersions
  50. enforce_key_validity: bool
  51. # Before MSC2432, m.room.aliases had special auth rules and redaction rules
  52. special_case_aliases_auth: bool
  53. # Strictly enforce canonicaljson, do not allow:
  54. # * Integers outside the range of [-2 ^ 53 + 1, 2 ^ 53 - 1]
  55. # * Floats
  56. # * NaN, Infinity, -Infinity
  57. strict_canonicaljson: bool
  58. # MSC2209: Check 'notifications' key while verifying
  59. # m.room.power_levels auth rules.
  60. limit_notifications_power_levels: bool
  61. # No longer include the creator in m.room.create events.
  62. implicit_room_creator: bool
  63. # Apply updated redaction rules algorithm from room version 11.
  64. updated_redaction_rules: bool
  65. # Support the 'restricted' join rule.
  66. restricted_join_rule: bool
  67. # Support for the proper redaction rules for the restricted join rule. This requires
  68. # restricted_join_rule to be enabled.
  69. restricted_join_rule_fix: bool
  70. # Support the 'knock' join rule.
  71. knock_join_rule: bool
  72. # MSC3389: Protect relation information from redaction.
  73. msc3389_relation_redactions: bool
  74. # Support the 'knock_restricted' join rule.
  75. knock_restricted_join_rule: bool
  76. # Enforce integer power levels
  77. enforce_int_power_levels: bool
  78. # MSC3931: Adds a push rule condition for "room version feature flags", making
  79. # some push rules room version dependent. Note that adding a flag to this list
  80. # is not enough to mark it "supported": the push rule evaluator also needs to
  81. # support the flag. Unknown flags are ignored by the evaluator, making conditions
  82. # fail if used.
  83. msc3931_push_features: Tuple[str, ...] # values from PushRuleRoomFlag
  84. class RoomVersions:
  85. V1 = RoomVersion(
  86. "1",
  87. RoomDisposition.STABLE,
  88. EventFormatVersions.ROOM_V1_V2,
  89. StateResolutionVersions.V1,
  90. enforce_key_validity=False,
  91. special_case_aliases_auth=True,
  92. strict_canonicaljson=False,
  93. limit_notifications_power_levels=False,
  94. implicit_room_creator=False,
  95. updated_redaction_rules=False,
  96. restricted_join_rule=False,
  97. restricted_join_rule_fix=False,
  98. knock_join_rule=False,
  99. msc3389_relation_redactions=False,
  100. knock_restricted_join_rule=False,
  101. enforce_int_power_levels=False,
  102. msc3931_push_features=(),
  103. )
  104. V2 = RoomVersion(
  105. "2",
  106. RoomDisposition.STABLE,
  107. EventFormatVersions.ROOM_V1_V2,
  108. StateResolutionVersions.V2,
  109. enforce_key_validity=False,
  110. special_case_aliases_auth=True,
  111. strict_canonicaljson=False,
  112. limit_notifications_power_levels=False,
  113. implicit_room_creator=False,
  114. updated_redaction_rules=False,
  115. restricted_join_rule=False,
  116. restricted_join_rule_fix=False,
  117. knock_join_rule=False,
  118. msc3389_relation_redactions=False,
  119. knock_restricted_join_rule=False,
  120. enforce_int_power_levels=False,
  121. msc3931_push_features=(),
  122. )
  123. V3 = RoomVersion(
  124. "3",
  125. RoomDisposition.STABLE,
  126. EventFormatVersions.ROOM_V3,
  127. StateResolutionVersions.V2,
  128. enforce_key_validity=False,
  129. special_case_aliases_auth=True,
  130. strict_canonicaljson=False,
  131. limit_notifications_power_levels=False,
  132. implicit_room_creator=False,
  133. updated_redaction_rules=False,
  134. restricted_join_rule=False,
  135. restricted_join_rule_fix=False,
  136. knock_join_rule=False,
  137. msc3389_relation_redactions=False,
  138. knock_restricted_join_rule=False,
  139. enforce_int_power_levels=False,
  140. msc3931_push_features=(),
  141. )
  142. V4 = RoomVersion(
  143. "4",
  144. RoomDisposition.STABLE,
  145. EventFormatVersions.ROOM_V4_PLUS,
  146. StateResolutionVersions.V2,
  147. enforce_key_validity=False,
  148. special_case_aliases_auth=True,
  149. strict_canonicaljson=False,
  150. limit_notifications_power_levels=False,
  151. implicit_room_creator=False,
  152. updated_redaction_rules=False,
  153. restricted_join_rule=False,
  154. restricted_join_rule_fix=False,
  155. knock_join_rule=False,
  156. msc3389_relation_redactions=False,
  157. knock_restricted_join_rule=False,
  158. enforce_int_power_levels=False,
  159. msc3931_push_features=(),
  160. )
  161. V5 = RoomVersion(
  162. "5",
  163. RoomDisposition.STABLE,
  164. EventFormatVersions.ROOM_V4_PLUS,
  165. StateResolutionVersions.V2,
  166. enforce_key_validity=True,
  167. special_case_aliases_auth=True,
  168. strict_canonicaljson=False,
  169. limit_notifications_power_levels=False,
  170. implicit_room_creator=False,
  171. updated_redaction_rules=False,
  172. restricted_join_rule=False,
  173. restricted_join_rule_fix=False,
  174. knock_join_rule=False,
  175. msc3389_relation_redactions=False,
  176. knock_restricted_join_rule=False,
  177. enforce_int_power_levels=False,
  178. msc3931_push_features=(),
  179. )
  180. V6 = RoomVersion(
  181. "6",
  182. RoomDisposition.STABLE,
  183. EventFormatVersions.ROOM_V4_PLUS,
  184. StateResolutionVersions.V2,
  185. enforce_key_validity=True,
  186. special_case_aliases_auth=False,
  187. strict_canonicaljson=True,
  188. limit_notifications_power_levels=True,
  189. implicit_room_creator=False,
  190. updated_redaction_rules=False,
  191. restricted_join_rule=False,
  192. restricted_join_rule_fix=False,
  193. knock_join_rule=False,
  194. msc3389_relation_redactions=False,
  195. knock_restricted_join_rule=False,
  196. enforce_int_power_levels=False,
  197. msc3931_push_features=(),
  198. )
  199. V7 = RoomVersion(
  200. "7",
  201. RoomDisposition.STABLE,
  202. EventFormatVersions.ROOM_V4_PLUS,
  203. StateResolutionVersions.V2,
  204. enforce_key_validity=True,
  205. special_case_aliases_auth=False,
  206. strict_canonicaljson=True,
  207. limit_notifications_power_levels=True,
  208. implicit_room_creator=False,
  209. updated_redaction_rules=False,
  210. restricted_join_rule=False,
  211. restricted_join_rule_fix=False,
  212. knock_join_rule=True,
  213. msc3389_relation_redactions=False,
  214. knock_restricted_join_rule=False,
  215. enforce_int_power_levels=False,
  216. msc3931_push_features=(),
  217. )
  218. V8 = RoomVersion(
  219. "8",
  220. RoomDisposition.STABLE,
  221. EventFormatVersions.ROOM_V4_PLUS,
  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. implicit_room_creator=False,
  228. updated_redaction_rules=False,
  229. restricted_join_rule=True,
  230. restricted_join_rule_fix=False,
  231. knock_join_rule=True,
  232. msc3389_relation_redactions=False,
  233. knock_restricted_join_rule=False,
  234. enforce_int_power_levels=False,
  235. msc3931_push_features=(),
  236. )
  237. V9 = RoomVersion(
  238. "9",
  239. RoomDisposition.STABLE,
  240. EventFormatVersions.ROOM_V4_PLUS,
  241. StateResolutionVersions.V2,
  242. enforce_key_validity=True,
  243. special_case_aliases_auth=False,
  244. strict_canonicaljson=True,
  245. limit_notifications_power_levels=True,
  246. implicit_room_creator=False,
  247. updated_redaction_rules=False,
  248. restricted_join_rule=True,
  249. restricted_join_rule_fix=True,
  250. knock_join_rule=True,
  251. msc3389_relation_redactions=False,
  252. knock_restricted_join_rule=False,
  253. enforce_int_power_levels=False,
  254. msc3931_push_features=(),
  255. )
  256. V10 = RoomVersion(
  257. "10",
  258. RoomDisposition.STABLE,
  259. EventFormatVersions.ROOM_V4_PLUS,
  260. StateResolutionVersions.V2,
  261. enforce_key_validity=True,
  262. special_case_aliases_auth=False,
  263. strict_canonicaljson=True,
  264. limit_notifications_power_levels=True,
  265. implicit_room_creator=False,
  266. updated_redaction_rules=False,
  267. restricted_join_rule=True,
  268. restricted_join_rule_fix=True,
  269. knock_join_rule=True,
  270. msc3389_relation_redactions=False,
  271. knock_restricted_join_rule=True,
  272. enforce_int_power_levels=True,
  273. msc3931_push_features=(),
  274. )
  275. MSC1767v10 = RoomVersion(
  276. # MSC1767 (Extensible Events) based on room version "10"
  277. "org.matrix.msc1767.10",
  278. RoomDisposition.UNSTABLE,
  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. implicit_room_creator=False,
  286. updated_redaction_rules=False,
  287. restricted_join_rule=True,
  288. restricted_join_rule_fix=True,
  289. knock_join_rule=True,
  290. msc3389_relation_redactions=False,
  291. knock_restricted_join_rule=True,
  292. enforce_int_power_levels=True,
  293. msc3931_push_features=(PushRuleRoomFlag.EXTENSIBLE_EVENTS,),
  294. )
  295. V11 = RoomVersion(
  296. "11",
  297. RoomDisposition.STABLE,
  298. EventFormatVersions.ROOM_V4_PLUS,
  299. StateResolutionVersions.V2,
  300. enforce_key_validity=True,
  301. special_case_aliases_auth=False,
  302. strict_canonicaljson=True,
  303. limit_notifications_power_levels=True,
  304. implicit_room_creator=True, # Used by MSC3820
  305. updated_redaction_rules=True, # Used by MSC3820
  306. restricted_join_rule=True,
  307. restricted_join_rule_fix=True,
  308. knock_join_rule=True,
  309. msc3389_relation_redactions=False,
  310. knock_restricted_join_rule=True,
  311. enforce_int_power_levels=True,
  312. msc3931_push_features=(),
  313. )
  314. KNOWN_ROOM_VERSIONS: Dict[str, RoomVersion] = {
  315. v.identifier: v
  316. for v in (
  317. RoomVersions.V1,
  318. RoomVersions.V2,
  319. RoomVersions.V3,
  320. RoomVersions.V4,
  321. RoomVersions.V5,
  322. RoomVersions.V6,
  323. RoomVersions.V7,
  324. RoomVersions.V8,
  325. RoomVersions.V9,
  326. RoomVersions.V10,
  327. RoomVersions.V11,
  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.knock_join_rule,
  352. ),
  353. RoomVersionCapability(
  354. "restricted",
  355. RoomVersions.V9,
  356. lambda room_version: room_version.restricted_join_rule,
  357. ),
  358. )
  359. }