-
Notifications
You must be signed in to change notification settings - Fork 3
/
eventapi_chat.go
57 lines (49 loc) · 1.17 KB
/
eventapi_chat.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
package webapi
import (
"encoding/json"
model "github.com/raceresult/go-model"
)
// Chat contains all api endpoints regarding chat
type Chat struct {
api *EventAPI
}
// newChat creates a new Chat api endpoint group
func newChat(api *EventAPI) *Chat {
return &Chat{
api: api,
}
}
// GetMessages returns the chat messages starting at a certain ID
func (q *Chat) GetMessages(minID int) ([]model.ChatMessage, error) {
values := urlValues{
"minID": minID,
}
bts, err := q.api.get("chat/getmessages", values)
if err != nil {
return nil, err
}
var dest []model.ChatMessage
if err := json.Unmarshal(bts, &dest); err != nil {
return nil, err
}
return dest, nil
}
// GetUsers registers a user and returns a list of all users
func (q *Chat) GetUsers(username string) ([]string, error) {
values := urlValues{
"username": username,
}
bts, err := q.api.get("chat/getusers", values)
if err != nil {
return nil, err
}
return parseJsonStringArr(bts)
}
// PostMessage posts a new message
func (q *Chat) PostMessage(username, msg string) error {
values := urlValues{
"username": username,
}
_, err := q.api.post("chat/postmessage", values, []byte(msg))
return err
}