/
client_actions.go
187 lines (146 loc) · 4.9 KB
/
client_actions.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
package matrixapi
import (
"fmt"
)
// JoinedRooms returns a list of the user's current rooms.
// If fromCache is true then local cache will be used.
// https://matrix.org/docs/spec/client_server/r0.4.0.html#get-matrix-client-r0-joined-rooms
func (client *Client) JoinedRooms(fromCache bool) (map[string]*JoinedRoomC, error) {
const path = "/_matrix/client/r0/joined_rooms"
if !fromCache { // Update current room list from server
var joinedRoomReply JoinedRoomReply
err := client.do("GET", path, nil, &joinedRoomReply)
if err != nil {
return nil, err
}
updatedJoinedRooms := make(map[string]*JoinedRoomC)
for _, roomID := range joinedRoomReply.JoinedRooms {
if _, exists := client.joinedRooms[roomID]; exists {
updatedJoinedRooms[roomID] = client.joinedRooms[roomID]
} else {
updatedJoinedRooms[roomID] = &JoinedRoomC{}
}
}
client.joinedRooms = updatedJoinedRooms
}
return client.joinedRooms, nil
}
// TODO: make universal {roomId}/send/{eventType}/{txnId}
// https://matrix.org/docs/spec/client_server/r0.4.0.html#id258
func (client *Client) SendTextMessage(roomID string, text string) (eventID string, err error) {
path := fmt.Sprintf("/_matrix/client/r0/rooms/%s/send/m.room.message", roomID)
request := SendMessageRequest{
MessageType: MessageTypeText,
Body: text}
var reply SendMessageReply
err = client.do("POST", path, request, &reply)
if err != nil {
return "", err
}
return reply.EventID, nil
}
// TODO: make universal {roomId}/send/{eventType}/{txnId}
// https://matrix.org/docs/spec/client_server/r0.4.0.html#id258
func (client *Client) SendTextReply(roomID string, messageID string, text string) (eventID string, err error) {
path := fmt.Sprintf("/_matrix/client/r0/rooms/%s/send/m.room.message", roomID)
request := SendMessageRequest{
MessageType: MessageTypeText,
Body: text,
RelatesTo: MRelatesTo{
InReplyTo: MInReplyTo{
EventID: messageID}}}
var reply SendMessageReply
err = client.do("POST", path, request, &reply)
if err != nil {
return "", err
}
return reply.EventID, nil
}
func (client *Client) Logout() error {
const path = "/_matrix/client/r0/logout"
return client.do("POST", path, struct{}{}, nil)
}
func (client *Client) CreateRoom(createRoomRequest CreateRoomRequest) (roomID string, err error) {
const path = "/_matrix/client/r0/createRoom"
var reply CreateRoomReply
err = client.do("POST", path, createRoomRequest, &reply)
if err != nil {
return
}
return reply.RoomID, nil
}
func (client *Client) LeaveRoom(room string) error {
path := fmt.Sprintf("/_matrix/client/r0/rooms/%s/leave", room)
return client.do("POST", path, struct{}{}, nil)
}
func (client *Client) Sync() error {
// https://matrix.org/docs/spec/client_server/r0.4.0.html#get-matrix-client-r0-sync
const path = "/_matrix/client/r0/sync"
var syncRequest SyncRequest
syncRequest.Since = client.nextBatch
var reply SyncReply
err := client.do("GET", path, &syncRequest, &reply)
if err != nil {
return err
}
client.nextBatch = reply.NextBatch
for roomID, room := range reply.Rooms.Join {
for _, event := range room.Timeline.Events {
client.processRoomEvent(roomID, event)
}
}
for roomID, invite := range reply.Rooms.Invite {
inviteC := &InviteC{Members: make(map[string]*RoomMember)}
client.Invites[roomID] = inviteC
for _, event := range invite.InviteState.Events {
client.processInviteEvent(inviteC, event)
}
if client.InviteHandler != nil {
client.InviteHandler(roomID, inviteC.InviterID)
}
}
return nil
}
func (client *Client) JoinRoom(roomID string) error {
// https://matrix.org/docs/spec/client_server/r0.4.0.html#id276
var path = fmt.Sprintf("/_matrix/client/r0/rooms/%s/join", roomID)
var reply JoinRoomReply
err := client.do("POST", path, struct{}{}, &reply)
if err != nil {
return err
}
// Удаляем инвайты
delete(client.Invites, roomID)
return nil
}
// Versions returns versions of the specification supported by the server
// https://matrix.org/docs/spec/client_server/r0.4.0.html#get-well-known-matrix-client
func (client *Client) Versions() ([]string, error) {
const path = "/_matrix/client/versions"
var reply VersionsReply
err := client.do("GET", path, struct{}{}, &reply)
if err != nil {
return nil, err
}
return reply.Versions, nil
}
// https://matrix.org/docs/spec/client_server/r0.4.0.html#post-matrix-client-r0-logout-all
func (client *Client) LogoutAll() error {
const path = "/_matrix/client/r0/logout/all"
err := client.do("POST", path, nil, nil)
if err != nil {
return err
}
return nil
}
// TODO: return cached client.UserID?
// https://matrix.org/docs/spec/client_server/r0.4.0.html#get-matrix-client-r0-account-whoami
func (client *Client) WhoAmI() (userID string, err error) {
const path = "/_matrix/client/r0/account/whoami"
var whoAmIReply WhoAmIReply
err = client.do("GET", path, nil, &whoAmIReply)
if err != nil {
return "", err
}
return whoAmIReply.UserID, nil
}