-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroom.go
95 lines (80 loc) · 2.28 KB
/
room.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
package rc
import (
"net/url"
"time"
"github.com/mitchellh/mapstructure"
)
// Generated by https://quicktype.io
type RoomList struct {
Update []ListRoom `json:"update"`
Remove []ListRoom `json:"remove"`
Success bool `json:"success"`
}
type RoomWrapper struct {
Room Room `json:"room"`
Success bool `json:"success"`
}
type ListRoom struct {
ID string `json:"_id"`
Name string `json:"name,omitempty"`
Type string `json:"t"`
UpdatedAt time.Time `json:"_updatedAt"`
LastMessage RoomMessage `json:"lastMessage,omitempty"`
Default bool `json:"default,omitempty"`
}
type Room struct {
ListRoom
Ts time.Time `json:"ts"`
Usernames []interface{} `json:"usernames"`
Msgs int64 `json:"msgs"`
UsersCount int64 `json:"usersCount"`
LastMessageTS time.Time `json:"lm"`
}
type RoomMessage struct {
ID string `json:"_id,omitempty"`
RoomID string `json:"rid,omitempty"`
Msg string `json:"msg,omitempty"`
Timestamp RoomTS `json:"ts,omitempty"`
User RoomUser `json:"u,omitempty"`
Unread bool `json:"unread,omitempty"`
UpdatedAt RoomTS `json:"_updatedAt,omitempty"`
URLS []interface{} `json:"urls,omitempty"`
Mentions []interface{} `json:"mentions,omitempty"`
Channels []interface{} `json:"channels,omitempty"`
}
type RoomUser struct {
ID string `json:"_id"`
Username string `json:"username"`
Name string `json:"name",omitempty`
}
type RoomTS struct {
Timestamp float64 `json:"$date"`
}
func (c *Client) GetRooms() (*RoomList, error) {
rooms := &RoomList{}
if err := c.c.get("/rooms.get", nil).JSON(rooms); err != nil {
return nil, err
}
return rooms, nil
}
func (c *Client) GetRoomsRT() (*RoomList, error) {
param := map[string]int{"$date": 0}
rroom, err := c.d.call("rooms/get", param)
if err != nil {
return nil, err
}
rooms := &RoomList{}
err = mapstructure.Decode(rroom, rooms)
if err != nil {
return nil, err
}
return rooms, nil
}
func (c *Client) GetRoomByName(name string) (*Room, error) {
roomW := &RoomWrapper{}
if err := c.c.get("/rooms.info", url.Values{"roomName": []string{name}}).JSON(roomW); err != nil {
return nil, err
}
room := roomW.Room
return &room, nil
}