forked from AcalephStorage/consul-alerts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
room.go
284 lines (248 loc) · 8.27 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
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
package hipchat
import (
"fmt"
"net/http"
"net/url"
"strconv"
)
// RoomService gives access to the room related methods of the API.
type RoomService struct {
client *Client
}
// Rooms represents a HipChat room list.
type Rooms struct {
Items []Room `json:"items"`
StartIndex int `json:"startIndex"`
MaxResults int `json:"maxResults"`
Links PageLinks `json:"links"`
}
// Room represents a HipChat room.
type Room struct {
ID int `json:"id"`
Links RoomLinks `json:"links"`
Name string `json:"name"`
XmppJid string `json:"xmpp_jid"`
Statistics RoomStatistics `json:"statistics"`
Created string `json:"created"`
IsArchived bool `json:"is_archived"`
Privacy string `json:"privacy"`
IsGuestAccessible bool `json:"is_guess_accessible"`
Topic string `json:"topic"`
Participants []User `json:"participants"`
Owner User `json:"owner"`
GuestAccessURL string `json:"guest_access_url"`
}
// RoomStatistics represents the HipChat room statistics.
type RoomStatistics struct {
Links Links `json:"links"`
}
// CreateRoomRequest represents a HipChat room creation request.
type CreateRoomRequest struct {
Topic string `json:"topic,omitempty"`
GuestAccess bool `json:"guest_access,omitempty"`
Name string `json:"name,omitempty"`
OwnerUserID string `json:"owner_user_id,omitempty"`
Privacy string `json:"privacy,omitempty"`
}
// UpdateRoomRequest represents a HipChat room update request.
type UpdateRoomRequest struct {
Name string `json:"name"`
Topic string `json:"topic"`
IsGuestAccess bool `json:"is_guest_access"`
IsArchived bool `json:"is_archived"`
Privacy string `json:"privacy"`
Owner ID `json:"owner"`
}
// RoomLinks represents the HipChat room links.
type RoomLinks struct {
Links
Webhooks string `json:"webhooks"`
Members string `json:"members"`
Participants string `json:"participants"`
}
// NotificationRequest represents a HipChat room notification request.
type NotificationRequest struct {
Color string `json:"color,omitempty"`
Message string `json:"message,omitempty"`
Notify bool `json:"notify,omitempty"`
MessageFormat string `json:"message_format,omitempty"`
}
// ShareFileRequest represents a HipChat room file share request.
type ShareFileRequest struct {
Path string `json:"path"`
Filename string `json:"filename,omitempty"`
Message string `json:"message,omitempty"`
}
// HistoryRequest represents a HipChat room chat history request.
type HistoryRequest struct {
Date string `json:"date"`
Timezone string `json:"timezone"`
StartIndex int `json:"start-index"`
MaxResults int `json:"max-results"`
Reverse bool `json:"reverse"`
}
// History represents a HipChat room chat history.
type History struct {
Items []Message `json:"items"`
StartIndex int `json:"startIndex"`
MaxResults int `json:"maxResults"`
Links PageLinks `json:"links"`
}
// Message represents a HipChat message.
type Message struct {
Date string `json:"date"`
From interface{} `json:"from"` // string | obj <- weak
Id string `json:"id"`
Mentions []User `json:"mentions"`
Message string `json:"message"`
MessageFormat string `json:"message_format"`
Type string `json:"type"`
}
// SetTopicRequest represents a hipchat update topic request
type SetTopicRequest struct {
Topic string `json:"topic"`
}
// InviteRequest represents a hipchat invite to room request
type InviteRequest struct {
Reason string `json:"reason"`
}
// List returns all the rooms authorized.
//
// HipChat API docs: https://www.hipchat.com/docs/apiv2/method/get_all_rooms
func (r *RoomService) List() (*Rooms, *http.Response, error) {
req, err := r.client.NewRequest("GET", "room", nil)
if err != nil {
return nil, nil, err
}
rooms := new(Rooms)
resp, err := r.client.Do(req, rooms)
if err != nil {
return nil, resp, err
}
return rooms, resp, nil
}
// Get returns the room specified by the id.
//
// HipChat API docs: https://www.hipchat.com/docs/apiv2/method/get_room
func (r *RoomService) Get(id string) (*Room, *http.Response, error) {
req, err := r.client.NewRequest("GET", fmt.Sprintf("room/%s", id), nil)
if err != nil {
return nil, nil, err
}
room := new(Room)
resp, err := r.client.Do(req, room)
if err != nil {
return nil, resp, err
}
return room, resp, nil
}
// Notification sends a notification to the room specified by the id.
//
// HipChat API docs: https://www.hipchat.com/docs/apiv2/method/send_room_notification
func (r *RoomService) Notification(id string, notifReq *NotificationRequest) (*http.Response, error) {
req, err := r.client.NewRequest("POST", fmt.Sprintf("room/%s/notification", id), notifReq)
if err != nil {
return nil, err
}
return r.client.Do(req, nil)
}
// ShareFile sends a file to the room specified by the id.
//
// HipChat API docs: https://www.hipchat.com/docs/apiv2/method/share_file_with_room
func (r *RoomService) ShareFile(id string, shareFileReq *ShareFileRequest) (*http.Response, error) {
req, err := r.client.NewFileUploadRequest("POST", fmt.Sprintf("room/%s/share/file", id), shareFileReq)
if err != nil {
return nil, err
}
return r.client.Do(req, nil)
}
// Create creates a new room.
//
// HipChat API docs: https://www.hipchat.com/docs/apiv2/method/create_room
func (r *RoomService) Create(roomReq *CreateRoomRequest) (*Room, *http.Response, error) {
req, err := r.client.NewRequest("POST", "room", roomReq)
if err != nil {
return nil, nil, err
}
room := new(Room)
resp, err := r.client.Do(req, room)
if err != nil {
return nil, resp, err
}
return room, resp, nil
}
// Delete deletes an existing room.
//
// HipChat API docs: https://www.hipchat.com/docs/apiv2/method/delete_room
func (r *RoomService) Delete(id string) (*http.Response, error) {
req, err := r.client.NewRequest("DELETE", fmt.Sprintf("room/%s", id), nil)
if err != nil {
return nil, err
}
return r.client.Do(req, nil)
}
// Update updates an existing room.
//
// HipChat API docs: https://www.hipchat.com/docs/apiv2/method/update_room
func (r *RoomService) Update(id string, roomReq *UpdateRoomRequest) (*http.Response, error) {
req, err := r.client.NewRequest("PUT", fmt.Sprintf("room/%s", id), roomReq)
if err != nil {
return nil, err
}
return r.client.Do(req, nil)
}
// History fetches a room's chat history.
//
// HipChat API docs: https://www.hipchat.com/docs/apiv2/method/view_room_history
func (r *RoomService) History(id string, roomReq *HistoryRequest) (*History, *http.Response, error) {
u := fmt.Sprintf("room/%s/history", id)
// Form query parameters
if roomReq != nil {
p := url.Values{}
if roomReq.Date != "" {
p.Add("date", roomReq.Date)
}
if roomReq.Timezone != "" {
p.Add("timezone", roomReq.Timezone)
}
if roomReq.StartIndex != 0 {
p.Add("start-index", strconv.FormatInt(int64(roomReq.StartIndex), 10))
}
if roomReq.MaxResults != 0 {
p.Add("max-results", strconv.FormatInt(int64(roomReq.MaxResults), 10))
}
// There's no way to tell whether caller set a boolean or not. We have to always set
// it.
p.Add("reverse", strconv.FormatBool(roomReq.Reverse))
u += "?" + p.Encode()
}
req, err := r.client.NewRequest("GET", u, nil)
h := new(History)
resp, err := r.client.Do(req, &h)
if err != nil {
return nil, resp, err
}
return h, resp, nil
}
// Set Room topic.
//
// HipChat API docs: https://www.hipchat.com/docs/apiv2/method/set_topic
func (r *RoomService) SetTopic(id string, topic string) (*http.Response, error) {
topicReq := &SetTopicRequest{Topic: topic}
req, err := r.client.NewRequest("PUT", fmt.Sprintf("room/%s/topic", id), topicReq)
if err != nil {
return nil, err
}
return r.client.Do(req, nil)
}
// Invite someone to the Room.
//
// HipChat API docs: https://www.hipchat.com/docs/apiv2/method/invite_user
func (r *RoomService) Invite(room string, user string, reason string) (*http.Response, error) {
reasonReq := &InviteRequest{Reason: reason}
req, err := r.client.NewRequest("POST", fmt.Sprintf("room/%s/invite/%s", room, user), reasonReq)
if err != nil {
return nil, err
}
return r.client.Do(req, nil)
}