forked from matrix-org/gomatrixserverlib
/
eventversion.go
247 lines (222 loc) · 8.71 KB
/
eventversion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package gomatrixserverlib
import "fmt"
// RoomVersion refers to the room version for a specific room.
type RoomVersion string
// StateResAlgorithm refers to a version of the state resolution algorithm.
type StateResAlgorithm int
// EventFormat refers to the formatting of the event fields struct.
type EventFormat int
// EventIDFormat refers to the formatting used to generate new event IDs.
type EventIDFormat int
// RedactionAlgorithm refers to the redaction algorithm used in a room version.
type RedactionAlgorithm int
// Room version constants. These are strings because the version grammar
// allows for future expansion.
// https://matrix.org/docs/spec/#room-version-grammar
const (
RoomVersionV1 RoomVersion = "1"
RoomVersionV2 RoomVersion = "2"
RoomVersionV3 RoomVersion = "3"
RoomVersionV4 RoomVersion = "4"
RoomVersionV5 RoomVersion = "5"
RoomVersionV6 RoomVersion = "6"
)
// Event format constants.
const (
EventFormatV1 EventFormat = iota + 1 // prev_events and auth_events as event references
EventFormatV2 // prev_events and auth_events as string array of event IDs
)
// Event ID format constants.
const (
EventIDFormatV1 EventIDFormat = iota + 1 // randomised
EventIDFormatV2 // base64-encoded hash of event
EventIDFormatV3 // URL-safe base64-encoded hash of event
)
// State resolution constants.
const (
StateResV1 StateResAlgorithm = iota + 1 // state resolution v1
StateResV2 // state resolution v2
)
// Redaction algorithm.
const (
RedactionAlgorithmV1 RedactionAlgorithm = iota + 1 // default algorithm
RedactionAlgorithmV2 // no special meaning for m.room.aliases
)
var roomVersionMeta = map[RoomVersion]RoomVersionDescription{
RoomVersionV1: {
Supported: true,
Stable: true,
stateResAlgorithm: StateResV1,
eventFormat: EventFormatV1,
eventIDFormat: EventIDFormatV1,
redactionAlgorithm: RedactionAlgorithmV1,
enforceSignatureChecks: false,
enforceCanonicalJSON: false,
powerLevelsIncludeNotifications: false,
},
RoomVersionV2: {
Supported: true,
Stable: true,
stateResAlgorithm: StateResV2,
eventFormat: EventFormatV1,
eventIDFormat: EventIDFormatV1,
redactionAlgorithm: RedactionAlgorithmV1,
enforceSignatureChecks: false,
enforceCanonicalJSON: false,
powerLevelsIncludeNotifications: false,
},
RoomVersionV3: {
Supported: true,
Stable: true,
stateResAlgorithm: StateResV2,
eventFormat: EventFormatV2,
eventIDFormat: EventIDFormatV2,
redactionAlgorithm: RedactionAlgorithmV1,
enforceSignatureChecks: false,
enforceCanonicalJSON: false,
powerLevelsIncludeNotifications: false,
},
RoomVersionV4: {
Supported: true,
Stable: true,
stateResAlgorithm: StateResV2,
eventFormat: EventFormatV2,
eventIDFormat: EventIDFormatV3,
redactionAlgorithm: RedactionAlgorithmV1,
enforceSignatureChecks: false,
enforceCanonicalJSON: false,
powerLevelsIncludeNotifications: false,
},
RoomVersionV5: {
Supported: true,
Stable: true,
stateResAlgorithm: StateResV2,
eventFormat: EventFormatV2,
eventIDFormat: EventIDFormatV3,
redactionAlgorithm: RedactionAlgorithmV1,
enforceSignatureChecks: true,
enforceCanonicalJSON: false,
powerLevelsIncludeNotifications: false,
},
RoomVersionV6: {
Supported: true,
Stable: true,
stateResAlgorithm: StateResV2,
eventFormat: EventFormatV2,
eventIDFormat: EventIDFormatV3,
redactionAlgorithm: RedactionAlgorithmV2,
enforceSignatureChecks: true,
enforceCanonicalJSON: true,
powerLevelsIncludeNotifications: true,
},
}
// RoomVersions returns information about room versions currently
// implemented by this commit of gomatrixserverlib.
func RoomVersions() map[RoomVersion]RoomVersionDescription {
return roomVersionMeta
}
// SupportedRoomVersions returns a map of descriptions for room
// versions that are marked as supported.
func SupportedRoomVersions() map[RoomVersion]RoomVersionDescription {
versions := make(map[RoomVersion]RoomVersionDescription)
for id, version := range RoomVersions() {
if version.Supported {
versions[id] = version
}
}
return versions
}
// StableRoomVersions returns a map of descriptions for room
// versions that are marked as stable.
func StableRoomVersions() map[RoomVersion]RoomVersionDescription {
versions := make(map[RoomVersion]RoomVersionDescription)
for id, version := range RoomVersions() {
if version.Supported && version.Stable {
versions[id] = version
}
}
return versions
}
// RoomVersionDescription contains information about a given room version, e.g. which
// state resolution algorithm or event ID format to use.
// RoomVersionDescription contains information about a room version,
// namely whether it is marked as supported or stable in this server
// version, along with the state resolution algorithm, event ID etc
// formats used.
//
// A version is supported if the server has some support for rooms
// that are this version. A version is marked as stable or unstable
// in order to hint whether the version should be used to clients
// calling the /capabilities endpoint.
// https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-capabilities
type RoomVersionDescription struct {
stateResAlgorithm StateResAlgorithm
eventFormat EventFormat
eventIDFormat EventIDFormat
redactionAlgorithm RedactionAlgorithm
enforceSignatureChecks bool
enforceCanonicalJSON bool
powerLevelsIncludeNotifications bool
Supported bool
Stable bool
}
// StateResAlgorithm returns the state resolution for the given room version.
func (v RoomVersion) StateResAlgorithm() (StateResAlgorithm, error) {
if r, ok := roomVersionMeta[v]; ok {
return r.stateResAlgorithm, nil
}
return 0, UnsupportedRoomVersionError{v}
}
// EventFormat returns the event format for the given room version.
func (v RoomVersion) EventFormat() (EventFormat, error) {
if r, ok := roomVersionMeta[v]; ok {
return r.eventFormat, nil
}
return 0, UnsupportedRoomVersionError{v}
}
// EventIDFormat returns the event ID format for the given room version.
func (v RoomVersion) EventIDFormat() (EventIDFormat, error) {
if r, ok := roomVersionMeta[v]; ok {
return r.eventIDFormat, nil
}
return 0, UnsupportedRoomVersionError{v}
}
// RedactionAlgorithm returns the redaction algorithm for the given room version.
func (v RoomVersion) RedactionAlgorithm() (RedactionAlgorithm, error) {
if r, ok := roomVersionMeta[v]; ok {
return r.redactionAlgorithm, nil
}
return 0, UnsupportedRoomVersionError{v}
}
// StrictValidityChecking returns true if the given room version calls for
// strict signature checking (room version 5 and onward) or false otherwise.
func (v RoomVersion) StrictValidityChecking() (bool, error) {
if r, ok := roomVersionMeta[v]; ok {
return r.enforceSignatureChecks, nil
}
return false, UnsupportedRoomVersionError{v}
}
// PowerLevelsIncludeNotifications returns true if the given room version calls
// for the power level checks to cover the `notifications` key or false otherwise.
func (v RoomVersion) PowerLevelsIncludeNotifications() (bool, error) {
if r, ok := roomVersionMeta[v]; ok {
return r.powerLevelsIncludeNotifications, nil
}
return false, UnsupportedRoomVersionError{v}
}
// PowerLevelsIncludeNotifications returns true if the given room version calls
// for the power level checks to cover the `notifications` key or false otherwise.
func (v RoomVersion) EnforceCanonicalJSON() (bool, error) {
if r, ok := roomVersionMeta[v]; ok {
return r.enforceCanonicalJSON, nil
}
return false, UnsupportedRoomVersionError{v}
}
// UnsupportedRoomVersionError occurs when a call has been made with a room
// version that is not supported by this version of gomatrixserverlib.
type UnsupportedRoomVersionError struct {
Version RoomVersion
}
func (e UnsupportedRoomVersionError) Error() string {
return fmt.Sprintf("gomatrixserverlib: unsupported room version '%s'", e.Version)
}