forked from mautrix/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.go
527 lines (435 loc) · 17.3 KB
/
requests.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package mautrix
import (
"encoding/json"
"strconv"
"github.com/globekeeper/gomautrix/event"
"github.com/globekeeper/gomautrix/id"
"github.com/globekeeper/gomautrix/pushrules"
)
type AuthType string
const (
AuthTypePassword AuthType = "m.login.password"
AuthTypeReCAPTCHA AuthType = "m.login.recaptcha"
AuthTypeOAuth2 AuthType = "m.login.oauth2"
AuthTypeSSO AuthType = "m.login.sso"
AuthTypeEmail AuthType = "m.login.email.identity"
AuthTypeMSISDN AuthType = "m.login.msisdn"
AuthTypeToken AuthType = "m.login.token"
AuthTypeTotp AuthType = "login.totp"
AuthTypeGeodome AuthType = "connect.login.external.geodome"
AuthTypeDummy AuthType = "m.login.dummy"
AuthTypeAppservice AuthType = "m.login.application_service"
AuthTypeSynapseJWT AuthType = "org.matrix.login.jwt"
)
type IdentifierType string
const (
IdentifierTypeUser = "m.id.user"
IdentifierTypeThirdParty = "m.id.thirdparty"
IdentifierTypePhone = "m.id.phone"
)
type Direction rune
const (
DirectionForward Direction = 'f'
DirectionBackward Direction = 'b'
)
// ReqRegister is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3register
type ReqRegister struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
DeviceID id.DeviceID `json:"device_id,omitempty"`
InitialDeviceDisplayName string `json:"initial_device_display_name,omitempty"`
InhibitLogin bool `json:"inhibit_login,omitempty"`
RefreshToken bool `json:"refresh_token,omitempty"`
Auth interface{} `json:"auth,omitempty"`
// Type for registration, only used for appservice user registrations
// https://spec.matrix.org/v1.2/application-service-api/#server-admin-style-permissions
Type AuthType `json:"type,omitempty"`
}
type BaseAuthData struct {
Type AuthType `json:"type"`
Session string `json:"session,omitempty"`
}
type UserIdentifier struct {
Type IdentifierType `json:"type"`
User string `json:"user,omitempty"`
Medium string `json:"medium,omitempty"`
Address string `json:"address,omitempty"`
Country string `json:"country,omitempty"`
Phone string `json:"phone,omitempty"`
}
// ReqLogin is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3login
type ReqLogin struct {
Type AuthType `json:"type"`
Identifier UserIdentifier `json:"identifier"`
Password string `json:"password,omitempty"`
Token string `json:"token,omitempty"`
DeviceID id.DeviceID `json:"device_id,omitempty"`
InitialDeviceDisplayName string `json:"initial_device_display_name,omitempty"`
// Whether or not the returned credentials should be stored in the Client
StoreCredentials bool `json:"-"`
// Whether or not the returned .well-known data should update the homeserver URL in the Client
StoreHomeserverURL bool `json:"-"`
InhibitDevice bool `json:"inhibit_device"`
TotpSid string `json:"totp_sid"`
Passcode string `json:"passcode"`
Sid string `json:"sid,omitempty"`
// Added for Geodome login
Username string `json:"username,omitempty"`
}
// ThirdpartyIdentifier is the Identifier for https://matrix.org/docs/spec/client_server/r0.6.0#third-party-id
type ThirdpartyIdentifier struct {
IDType string `json:"type"` // Set by NewThirdpartyIdentifier
Medium string `json:"medium"`
Address string `json:"address"`
}
// NewThirdpartyIdentifier creates a new UserIdentifier with IDType set to "m.id.thirdparty"
func NewThirdpartyIdentifier(medium, address string) UserIdentifier {
return UserIdentifier{
Type: IdentifierTypeThirdParty,
Medium: medium,
Address: address,
}
}
// NewUserIdentifier creates a new UserIdentifier with IDType set to "m.id.user"
func NewUserIdentifier(user string) UserIdentifier {
return UserIdentifier{
Type: IdentifierTypeUser,
User: user,
}
}
type ReqUIAuthFallback struct {
Session string `json:"session"`
User string `json:"user"`
}
type ReqUIAuthLogin struct {
BaseAuthData
User string `json:"user"`
Password string `json:"password"`
}
// ReqCreateRoom is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3createroom
type ReqCreateRoom struct {
Visibility string `json:"visibility,omitempty"`
RoomAliasName string `json:"room_alias_name,omitempty"`
Name string `json:"name,omitempty"`
Topic string `json:"topic,omitempty"`
Invite []id.UserID `json:"invite,omitempty"`
Invite3PID []ReqInvite3PID `json:"invite_3pid,omitempty"`
CreationContent map[string]interface{} `json:"creation_content,omitempty"`
InitialState []*event.Event `json:"initial_state,omitempty"`
Preset string `json:"preset,omitempty"`
IsDirect bool `json:"is_direct,omitempty"`
RoomVersion string `json:"room_version,omitempty"`
PowerLevelOverride *event.PowerLevelsEventContent `json:"power_level_content_override,omitempty"`
MeowRoomID id.RoomID `json:"fi.mau.room_id,omitempty"`
BeeperAutoJoinInvites bool `json:"com.beeper.auto_join_invites,omitempty"`
}
// ReqRedact is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#put_matrixclientv3roomsroomidredacteventidtxnid
type ReqRedact struct {
Reason string
TxnID string
Extra map[string]interface{}
}
type ReqMembers struct {
At string `json:"at"`
Membership event.Membership `json:"membership,omitempty"`
NotMembership event.Membership `json:"not_membership,omitempty"`
}
// ReqInvite3PID is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3roomsroomidinvite-1
// It is also a JSON object used in https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3createroom
type ReqInvite3PID struct {
IDServer string `json:"id_server"`
Medium string `json:"medium"`
Address string `json:"address"`
}
type ReqLeave struct {
Reason string `json:"reason,omitempty"`
}
// ReqInviteUser is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3roomsroomidinvite
type ReqInviteUser struct {
Reason string `json:"reason,omitempty"`
UserID id.UserID `json:"user_id"`
}
// ReqKickUser is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3roomsroomidkick
type ReqKickUser struct {
Reason string `json:"reason,omitempty"`
UserID id.UserID `json:"user_id"`
}
// ReqBanUser is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3roomsroomidban
type ReqBanUser struct {
Reason string `json:"reason,omitempty"`
UserID id.UserID `json:"user_id"`
}
// ReqUnbanUser is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3roomsroomidunban
type ReqUnbanUser struct {
Reason string `json:"reason,omitempty"`
UserID id.UserID `json:"user_id"`
}
// ReqTyping is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#put_matrixclientv3roomsroomidtypinguserid
type ReqTyping struct {
Typing bool `json:"typing"`
Timeout int64 `json:"timeout,omitempty"`
}
// ReqPresence is the JSON request for https://spec.matrix.org/v1.7/client-server-api/#mpresence
type ReqPresence struct {
Presence event.Presence `json:"presence"`
StatusMsg string `json:"status_msg"`
}
type ReqAliasCreate struct {
RoomID id.RoomID `json:"room_id"`
}
type OneTimeKey struct {
Key id.Curve25519 `json:"key"`
Fallback bool `json:"fallback,omitempty"`
Signatures Signatures `json:"signatures,omitempty"`
Unsigned map[string]any `json:"unsigned,omitempty"`
IsSigned bool `json:"-"`
// Raw data in the one-time key. This must be used for signature verification to ensure unrecognized fields
// aren't thrown away (because that would invalidate the signature).
RawData json.RawMessage `json:"-"`
}
type serializableOTK OneTimeKey
func (otk *OneTimeKey) UnmarshalJSON(data []byte) (err error) {
if len(data) > 0 && data[0] == '"' && data[len(data)-1] == '"' {
err = json.Unmarshal(data, &otk.Key)
otk.Signatures = nil
otk.Unsigned = nil
otk.IsSigned = false
} else {
err = json.Unmarshal(data, (*serializableOTK)(otk))
otk.RawData = data
otk.IsSigned = true
}
return err
}
func (otk *OneTimeKey) MarshalJSON() ([]byte, error) {
if !otk.IsSigned {
return json.Marshal(otk.Key)
} else {
return json.Marshal((*serializableOTK)(otk))
}
}
type ReqUploadKeys struct {
DeviceKeys *DeviceKeys `json:"device_keys,omitempty"`
OneTimeKeys map[id.KeyID]OneTimeKey `json:"one_time_keys"`
}
type ReqKeysSignatures struct {
UserID id.UserID `json:"user_id"`
DeviceID id.DeviceID `json:"device_id,omitempty"`
Algorithms []id.Algorithm `json:"algorithms,omitempty"`
Usage []id.CrossSigningUsage `json:"usage,omitempty"`
Keys map[id.KeyID]string `json:"keys"`
Signatures Signatures `json:"signatures"`
}
type ReqUploadSignatures map[id.UserID]map[string]ReqKeysSignatures
type DeviceKeys struct {
UserID id.UserID `json:"user_id"`
DeviceID id.DeviceID `json:"device_id"`
Algorithms []id.Algorithm `json:"algorithms"`
Keys KeyMap `json:"keys"`
Signatures Signatures `json:"signatures"`
Unsigned map[string]interface{} `json:"unsigned,omitempty"`
}
type CrossSigningKeys struct {
UserID id.UserID `json:"user_id"`
Usage []id.CrossSigningUsage `json:"usage"`
Keys map[id.KeyID]id.Ed25519 `json:"keys"`
Signatures map[id.UserID]map[id.KeyID]string `json:"signatures,omitempty"`
}
func (csk *CrossSigningKeys) FirstKey() id.Ed25519 {
for _, key := range csk.Keys {
return key
}
return ""
}
type UploadCrossSigningKeysReq struct {
Master CrossSigningKeys `json:"master_key"`
SelfSigning CrossSigningKeys `json:"self_signing_key"`
UserSigning CrossSigningKeys `json:"user_signing_key"`
Auth interface{} `json:"auth,omitempty"`
}
type KeyMap map[id.DeviceKeyID]string
func (km KeyMap) GetEd25519(deviceID id.DeviceID) id.Ed25519 {
val, ok := km[id.NewDeviceKeyID(id.KeyAlgorithmEd25519, deviceID)]
if !ok {
return ""
}
return id.Ed25519(val)
}
func (km KeyMap) GetCurve25519(deviceID id.DeviceID) id.Curve25519 {
val, ok := km[id.NewDeviceKeyID(id.KeyAlgorithmCurve25519, deviceID)]
if !ok {
return ""
}
return id.Curve25519(val)
}
type Signatures map[id.UserID]map[id.KeyID]string
type ReqQueryKeys struct {
DeviceKeys DeviceKeysRequest `json:"device_keys"`
Timeout int64 `json:"timeout,omitempty"`
Token string `json:"token,omitempty"`
}
type DeviceKeysRequest map[id.UserID]DeviceIDList
type DeviceIDList []id.DeviceID
type ReqClaimKeys struct {
OneTimeKeys OneTimeKeysRequest `json:"one_time_keys"`
Timeout int64 `json:"timeout,omitempty"`
}
type OneTimeKeysRequest map[id.UserID]map[id.DeviceID]id.KeyAlgorithm
type ReqSendToDevice struct {
Messages map[id.UserID]map[id.DeviceID]*event.Content `json:"messages"`
}
// ReqDeviceInfo is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#put_matrixclientv3devicesdeviceid
type ReqDeviceInfo struct {
DisplayName string `json:"display_name,omitempty"`
}
// ReqDeleteDevice is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#delete_matrixclientv3devicesdeviceid
type ReqDeleteDevice struct {
Auth interface{} `json:"auth,omitempty"`
}
// ReqDeleteDevices is the JSON request for https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3delete_devices
type ReqDeleteDevices struct {
Devices []id.DeviceID `json:"devices"`
Auth interface{} `json:"auth,omitempty"`
}
type ReqPutPushRule struct {
Before string `json:"-"`
After string `json:"-"`
Actions []pushrules.PushActionType `json:"actions"`
Conditions []pushrules.PushCondition `json:"conditions"`
Pattern string `json:"pattern"`
Enabled bool `json:"enabled"`
}
// Deprecated: MSC2716 was abandoned
type ReqBatchSend struct {
PrevEventID id.EventID `json:"-"`
BatchID id.BatchID `json:"-"`
BeeperNewMessages bool `json:"-"`
BeeperMarkReadBy id.UserID `json:"-"`
StateEventsAtStart []*event.Event `json:"state_events_at_start"`
Events []*event.Event `json:"events"`
}
type ReqBeeperBatchSend struct {
// ForwardIfNoMessages should be set to true if the batch should be forward
// backfilled if there are no messages currently in the room.
ForwardIfNoMessages bool `json:"forward_if_no_messages"`
Forward bool `json:"forward"`
MarkReadBy id.UserID `json:"mark_read_by,omitempty"`
Events []*event.Event `json:"events"`
}
type ReqSetReadMarkers struct {
Read id.EventID `json:"m.read,omitempty"`
ReadPrivate id.EventID `json:"m.read.private,omitempty"`
FullyRead id.EventID `json:"m.fully_read,omitempty"`
BeeperReadExtra interface{} `json:"com.beeper.read.extra,omitempty"`
BeeperReadPrivateExtra interface{} `json:"com.beeper.read.private.extra,omitempty"`
BeeperFullyReadExtra interface{} `json:"com.beeper.fully_read.extra,omitempty"`
}
type ReqSendReceipt struct {
ThreadID string `json:"thread_id,omitempty"`
}
// ReqHierarchy contains the parameters for https://spec.matrix.org/v1.4/client-server-api/#get_matrixclientv1roomsroomidhierarchy
//
// As it's a GET method, there is no JSON body, so this is only query parameters.
type ReqHierarchy struct {
// A pagination token from a previous Hierarchy call.
// If specified, max_depth and suggested_only cannot be changed from the first request.
From string
// Limit for the maximum number of rooms to include per response.
// The server will apply a default value if a limit isn't provided.
Limit int
// Limit for how far to go into the space. When reached, no further child rooms will be returned.
// The server will apply a default value if a max depth isn't provided.
MaxDepth *int
// Flag to indicate whether the server should only consider suggested rooms.
// Suggested rooms are annotated in their m.space.child event contents.
SuggestedOnly bool
}
func (req *ReqHierarchy) Query() map[string]string {
query := map[string]string{}
if req == nil {
return query
}
if req.From != "" {
query["from"] = req.From
}
if req.Limit > 0 {
query["limit"] = strconv.Itoa(req.Limit)
}
if req.MaxDepth != nil {
query["max_depth"] = strconv.Itoa(*req.MaxDepth)
}
if req.SuggestedOnly {
query["suggested_only"] = "true"
}
return query
}
type ReqAppservicePing struct {
TxnID string `json:"transaction_id,omitempty"`
}
type ReqBeeperMergeRoom struct {
NewRoom ReqCreateRoom `json:"create"`
Key string `json:"key"`
Rooms []id.RoomID `json:"rooms"`
User id.UserID `json:"user_id"`
}
type BeeperSplitRoomPart struct {
UserID id.UserID `json:"user_id"`
Values []string `json:"values"`
NewRoom ReqCreateRoom `json:"create"`
}
type ReqBeeperSplitRoom struct {
RoomID id.RoomID `json:"-"`
Key string `json:"key"`
Parts []BeeperSplitRoomPart `json:"parts"`
}
type ReqRoomKeysVersionCreate struct {
Algorithm string `json:"algorithm"`
AuthData json.RawMessage `json:"auth_data"`
}
type ReqRoomKeysUpdate struct {
Rooms map[id.RoomID]ReqRoomKeysRoomUpdate `json:"rooms"`
}
type ReqRoomKeysRoomUpdate struct {
Sessions map[id.SessionID]ReqRoomKeysSessionUpdate `json:"sessions"`
}
type ReqRoomKeysSessionUpdate struct {
FirstMessageIndex int `json:"first_message_index"`
ForwardedCount int `json:"forwarded_count"`
IsVerified bool `json:"is_verified"`
SessionData json.RawMessage `json:"session_data"`
}
// ReqGetAccountData is the JSON request for https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-user-userid-account-data-type
type ReqGetAccountData struct {
Type string
}
// ReqEmailRequestToken is the JSON request for
//
// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-register-email-requesttoken
// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-account-password-email-requesttoken
// https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-account-3pid-email-requesttoken
type ReqEmailRequestToken struct {
IdServer string `json:"id_server,omitempty"`
IdAccessToken string `json:"id_access_token,omitempty"`
Secret string `json:"client_secret"`
Email string `json:"email"`
SendAttempt int `json:"send_attempt"`
NextLink string `json:"next_link,omitempty"`
}
type ReqPostThreePID struct {
ThreePIDCredes ThreePIDCreds `json:"three_pid_creds"`
}
type ThreePIDCreds struct {
ClientSecret string `json:"client_secret"`
IdAccessToken string `json:"id_access_token,omitempty"`
IdServer string `json:"id_server"`
Sid string `json:"sid"`
}
type ReqAccountPassword struct {
LogoutDevices bool `json:"logout_devices"`
NewPassword string `json:"new_password"`
Auth interface{} `json:"auth"`
}
type ReqUserDirectorySearch struct {
Limit int32 `json:"limit"`
SearchTerm string `json:"search_term"`
}