You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

187 lines
5.8 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 Dict
  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)
  36. class RoomVersion:
  37. """An object which describes the unique attributes of a room version."""
  38. identifier = attr.ib(type=str) # the identifier for this version
  39. disposition = attr.ib(type=str) # one of the RoomDispositions
  40. event_format = attr.ib(type=int) # one of the EventFormatVersions
  41. state_res = attr.ib(type=int) # one of the StateResolutionVersions
  42. enforce_key_validity = attr.ib(type=bool)
  43. # Before MSC2261/MSC2432, m.room.aliases had special auth rules and redaction rules
  44. special_case_aliases_auth = attr.ib(type=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 = attr.ib(type=bool)
  50. # MSC2209: Check 'notifications' key while verifying
  51. # m.room.power_levels auth rules.
  52. limit_notifications_power_levels = attr.ib(type=bool)
  53. # MSC2174/MSC2176: Apply updated redaction rules algorithm.
  54. msc2176_redaction_rules = attr.ib(type=bool)
  55. # MSC3083: Support the 'restricted' join_rule.
  56. msc3083_join_rules = attr.ib(type=bool)
  57. class RoomVersions:
  58. V1 = RoomVersion(
  59. "1",
  60. RoomDisposition.STABLE,
  61. EventFormatVersions.V1,
  62. StateResolutionVersions.V1,
  63. enforce_key_validity=False,
  64. special_case_aliases_auth=True,
  65. strict_canonicaljson=False,
  66. limit_notifications_power_levels=False,
  67. msc2176_redaction_rules=False,
  68. msc3083_join_rules=False,
  69. )
  70. V2 = RoomVersion(
  71. "2",
  72. RoomDisposition.STABLE,
  73. EventFormatVersions.V1,
  74. StateResolutionVersions.V2,
  75. enforce_key_validity=False,
  76. special_case_aliases_auth=True,
  77. strict_canonicaljson=False,
  78. limit_notifications_power_levels=False,
  79. msc2176_redaction_rules=False,
  80. msc3083_join_rules=False,
  81. )
  82. V3 = RoomVersion(
  83. "3",
  84. RoomDisposition.STABLE,
  85. EventFormatVersions.V2,
  86. StateResolutionVersions.V2,
  87. enforce_key_validity=False,
  88. special_case_aliases_auth=True,
  89. strict_canonicaljson=False,
  90. limit_notifications_power_levels=False,
  91. msc2176_redaction_rules=False,
  92. msc3083_join_rules=False,
  93. )
  94. V4 = RoomVersion(
  95. "4",
  96. RoomDisposition.STABLE,
  97. EventFormatVersions.V3,
  98. StateResolutionVersions.V2,
  99. enforce_key_validity=False,
  100. special_case_aliases_auth=True,
  101. strict_canonicaljson=False,
  102. limit_notifications_power_levels=False,
  103. msc2176_redaction_rules=False,
  104. msc3083_join_rules=False,
  105. )
  106. V5 = RoomVersion(
  107. "5",
  108. RoomDisposition.STABLE,
  109. EventFormatVersions.V3,
  110. StateResolutionVersions.V2,
  111. enforce_key_validity=True,
  112. special_case_aliases_auth=True,
  113. strict_canonicaljson=False,
  114. limit_notifications_power_levels=False,
  115. msc2176_redaction_rules=False,
  116. msc3083_join_rules=False,
  117. )
  118. V6 = RoomVersion(
  119. "6",
  120. RoomDisposition.STABLE,
  121. EventFormatVersions.V3,
  122. StateResolutionVersions.V2,
  123. enforce_key_validity=True,
  124. special_case_aliases_auth=False,
  125. strict_canonicaljson=True,
  126. limit_notifications_power_levels=True,
  127. msc2176_redaction_rules=False,
  128. msc3083_join_rules=False,
  129. )
  130. MSC2176 = RoomVersion(
  131. "org.matrix.msc2176",
  132. RoomDisposition.UNSTABLE,
  133. EventFormatVersions.V3,
  134. StateResolutionVersions.V2,
  135. enforce_key_validity=True,
  136. special_case_aliases_auth=False,
  137. strict_canonicaljson=True,
  138. limit_notifications_power_levels=True,
  139. msc2176_redaction_rules=True,
  140. msc3083_join_rules=False,
  141. )
  142. MSC3083 = RoomVersion(
  143. "org.matrix.msc3083",
  144. RoomDisposition.UNSTABLE,
  145. EventFormatVersions.V3,
  146. StateResolutionVersions.V2,
  147. enforce_key_validity=True,
  148. special_case_aliases_auth=False,
  149. strict_canonicaljson=True,
  150. limit_notifications_power_levels=True,
  151. msc2176_redaction_rules=False,
  152. msc3083_join_rules=True,
  153. )
  154. KNOWN_ROOM_VERSIONS = {
  155. v.identifier: v
  156. for v in (
  157. RoomVersions.V1,
  158. RoomVersions.V2,
  159. RoomVersions.V3,
  160. RoomVersions.V4,
  161. RoomVersions.V5,
  162. RoomVersions.V6,
  163. RoomVersions.MSC2176,
  164. )
  165. # Note that we do not include MSC3083 here unless it is enabled in the config.
  166. } # type: Dict[str, RoomVersion]