-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadminapi.go
99 lines (88 loc) · 2.71 KB
/
adminapi.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
package main
import (
"context"
"fmt"
"math/rand"
"net/http"
"time"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/id"
)
type ReqDeleteRoom struct {
RoomID id.RoomID `json:"-"`
Purge bool `json:"purge"`
ForcePurge bool `json:"force_purge"`
}
type RespDeleteRoom struct {
KickedUsers []id.UserID `json:"kicked_users"`
FailedToKickUsers []id.UserID `json:"failed_to_kick_users"`
LocalAliases []id.RoomAlias `json:"local_aliases"`
NewRoomID id.RoomID `json:"new_room_id,omitempty"`
}
var fakeDeleteResponse = RespDeleteRoom{
KickedUsers: []id.UserID{"@fake:user.com"},
FailedToKickUsers: []id.UserID{},
LocalAliases: []id.RoomAlias{},
}
// https://matrix-org.github.io/synapse/latest/admin_api/rooms.html#delete-room-api
func adminDeleteRoom(ctx context.Context, req ReqDeleteRoom) (*RespDeleteRoom, error) {
var resp RespDeleteRoom
var err error
if cfg.DryRun {
select {
case <-time.After(time.Duration(rand.Float64() * 5 * float64(time.Second))):
resp = fakeDeleteResponse
case <-ctx.Done():
err = fmt.Errorf("context errored in dry run mode: %w", ctx.Err())
}
} else {
url := adminClient.BuildBaseURL("_synapse", "admin", "v1", "rooms", req.RoomID)
_, err = adminClient.MakeFullRequest(mautrix.FullRequest{
Method: http.MethodDelete,
URL: url,
RequestJSON: &req,
ResponseJSON: &resp,
Context: ctx,
})
}
return &resp, err
}
type RespListMembers struct {
Members []id.UserID `json:"members"`
Total int `json:"total"`
}
// https://matrix-org.github.io/synapse/latest/admin_api/rooms.html#room-members-api
func adminListRoomMembers(ctx context.Context, roomID id.RoomID) ([]id.UserID, error) {
url := adminClient.BuildBaseURL("_synapse", "admin", "v1", "rooms", roomID, "members")
var resp RespListMembers
_, err := adminClient.MakeFullRequest(mautrix.FullRequest{
Method: http.MethodGet,
URL: url,
ResponseJSON: &resp,
Context: ctx,
})
if err != nil {
return nil, err
}
return resp.Members, nil
}
type ReqAdminLogin struct {
ValidUntilMS int64 `json:"valid_until_ms"`
UserID id.UserID `json:"-"`
}
type RespAdminLogin struct {
AccessToken string `json:"access_token"`
}
// https://matrix-org.github.io/synapse/latest/admin_api/user_admin_api.html#login-as-a-user
func adminLogin(ctx context.Context, req ReqAdminLogin) (*RespAdminLogin, error) {
url := adminClient.BuildBaseURL("_synapse", "admin", "v1", "users", req.UserID, "login")
var resp RespAdminLogin
_, err := adminClient.MakeFullRequest(mautrix.FullRequest{
Method: http.MethodPost,
URL: url,
RequestJSON: &req,
ResponseJSON: &resp,
Context: ctx,
})
return &resp, err
}