forked from jbogarin/go-cisco-webex-teams
/
rooms_api.go
241 lines (189 loc) · 6.96 KB
/
rooms_api.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
package webexteams
import (
"fmt"
"strings"
"time"
"github.com/go-resty/resty"
"github.com/google/go-querystring/query"
"github.com/peterhellberg/link"
)
// RoomsService is the service to communicate with the Rooms API endpoint
type RoomsService service
// RoomCreateRequest is the Room Create Request Parameters
type RoomCreateRequest struct {
Title string `json:"title,omitempty"` // A user-friendly name for the room.
TeamID string `json:"teamId,omitempty"` // The ID for the team with which this room is associated.
}
// RoomUpdateRequest is the Room Update Request Parameters
type RoomUpdateRequest struct {
Title string `json:"title,omitempty"` // Room title.
}
// Room is the Room definition
type Room struct {
ID string `json:"id,omitempty"` // Room ID.
Title string `json:"title,omitempty"` // Room title.
RoomType string `json:"type,omitempty"` // Room type (group or direct).
IsLocked bool `json:"isLocked,omitempty"` // Room is moderated.
TeamID string `json:"teamId,omitempty"` // Room Team ID.
CreatorID string `json:"creatorId,omitempty"` // Room creator Person ID.
LastActivity time.Time `json:"lastActivity,omitempty"` // Room last activity date/time.
Created time.Time `json:"created,omitempty"` // Room creation date/time.
}
// Rooms is the List of Rooms
type Rooms struct {
Items []Room `json:"items,omitempty"`
}
// AddRoom is used to append a room to a slice of rooms
func (rooms *Rooms) AddRoom(item Room) []Room {
rooms.Items = append(rooms.Items, item)
return rooms.Items
}
func roomsPagination(linkHeader string, size, max int) *Rooms {
items := &Rooms{}
for _, l := range link.Parse(linkHeader) {
if l.Rel == "next" {
response, err := RestyClient.R().
SetResult(&Rooms{}).
Get(l.URI)
if err != nil {
return nil
}
items = response.Result().(*Rooms)
if size != 0 {
size = size + len(items.Items)
if size < max {
rooms := roomsPagination(response.Header().Get("Link"), size, max)
for _, room := range rooms.Items {
items.AddRoom(room)
}
}
} else {
rooms := roomsPagination(response.Header().Get("Link"), size, max)
for _, room := range rooms.Items {
items.AddRoom(room)
}
}
}
}
return items
}
// CreateRoom Creates a room. The authenticated user is automatically added as a member of the room.
/* Creates a room. The authenticated user is automatically added as a member of the room. See the Memberships API to learn how to add more people to the room.
To create a 1-to-1 room, use the Create Messages endpoint to send a message directly to another person by using the toPersonID or toPersonEmail parameters.
@param roomCreateRequest
@return Room
*/
func (s *RoomsService) CreateRoom(roomCreateRequest *RoomCreateRequest) (*Room, *resty.Response, error) {
path := "/rooms/"
response, err := RestyClient.R().
SetBody(roomCreateRequest).
SetResult(&Room{}).
Post(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Room)
return result, response, err
}
// DeleteRoom Deletes a room, by ID. Deleted rooms cannot be recovered.
/* Deletes a room, by ID. Deleted rooms cannot be recovered.
Deleting a room that is part of a team will archive the room instead.
Specify the room ID in the roomID parameter in the URI
@param roomID Room ID.
@return
*/
func (s *RoomsService) DeleteRoom(roomID string) (*resty.Response, error) {
path := "/rooms/{roomId}"
path = strings.Replace(path, "{"+"roomId"+"}", fmt.Sprintf("%v", roomID), -1)
response, err := RestyClient.R().
Delete(path)
if err != nil {
return nil, err
}
return response, err
}
// GetRoom Shows details for a room, by ID.
/* Shows details for a room, by ID.
The title of the room for 1-to-1 rooms will be the display name of the other person.
Specify the room ID in the roomID parameter in the URI.
@param roomID Room ID.
@return Room
*/
func (s *RoomsService) GetRoom(roomID string) (*Room, *resty.Response, error) {
path := "/rooms/{roomId}"
path = strings.Replace(path, "{"+"roomId"+"}", fmt.Sprintf("%v", roomID), -1)
response, err := RestyClient.R().
SetResult(&Room{}).
Get(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Room)
return result, response, err
}
// ListRoomsQueryParams are the query params for the ListRooms API Call
type ListRoomsQueryParams struct {
TeamID string `url:"teamId,omitempty"` // Limit the rooms to those associated with a team, by ID.
RoomType string `url:"type,omitempty"` // direct returns all 1-to-1 rooms. group returns all group rooms.
SortBy string `url:"sortBy,omitempty"` // Sort results by room ID (id), most recent activity (lastactivity), or most recently created (created).
Max int `url:"max,omitempty"` // Limit the maximum number of items in the response.
Paginate bool // Indicates if pagination is needed
}
// ListRooms List rooms.
/* List rooms.
The title of the room for 1-to-1 rooms will be the display name of the other person.
By default, lists rooms to which the authenticated user belongs.
Long result sets will be split into pages.
@param "teamId" (string) Limit the rooms to those associated with a team, by ID.
@param "type_" (string) direct returns all 1-to-1 rooms. group returns all group rooms.
@param "sortBy" (string) Sort results by room ID (id), most recent activity (lastactivity), or most recently created (created).
@param "max" (int) Limit the maximum number of items in the response.
@param paginate (bool) indicates if pagination is needed
@return Rooms
*/
func (s *RoomsService) ListRooms(queryParams *ListRoomsQueryParams) (*Rooms, *resty.Response, error) {
path := "/rooms/"
queryParamsString, _ := query.Values(queryParams)
response, err := RestyClient.R().
SetQueryString(queryParamsString.Encode()).
SetResult(&Rooms{}).
Get(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Rooms)
if queryParams.Paginate == true {
items := roomsPagination(response.Header().Get("Link"), 0, 0)
for _, room := range items.Items {
result.AddRoom(room)
}
} else {
if len(result.Items) < queryParams.Max {
items := roomsPagination(response.Header().Get("Link"), len(result.Items), queryParams.Max)
for _, room := range items.Items {
result.AddRoom(room)
}
}
}
return result, response, err
}
// UpdateRoom Updates details for a room, by ID.
/* Updates details for a room, by ID.
Specify the room ID in the roomID parameter in the URI.
@param roomID Room ID.
@param roomUpdateRequest
@return Room
*/
func (s *RoomsService) UpdateRoom(roomID string, roomUpdateRequest *RoomUpdateRequest) (*Room, *resty.Response, error) {
path := "/rooms/{roomId}"
path = strings.Replace(path, "{"+"roomId"+"}", fmt.Sprintf("%v", roomID), -1)
response, err := RestyClient.R().
SetBody(roomUpdateRequest).
SetResult(&Room{}).
Put(path)
if err != nil {
return nil, nil, err
}
result := response.Result().(*Room)
return result, response, err
}