This repository was archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient.go
More file actions
415 lines (353 loc) · 15.1 KB
/
client.go
File metadata and controls
415 lines (353 loc) · 15.1 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Package chatkit is the Golang server SDK for Pusher Chatkit.
// This package provides functionality to interact with various Chatkit services.
//
// More information can be found in the Chatkit docs: https://docs.pusher.com/chatkit/overview/.
//
// Please report any bugs or feature requests at: https://github.com/pusher/chatkit-server-go.
package chatkit
import (
"context"
"net/http"
"github.com/pusher/chatkit-server-go/internal/authenticator"
"github.com/pusher/chatkit-server-go/internal/authorizer"
"github.com/pusher/chatkit-server-go/internal/core"
"github.com/pusher/chatkit-server-go/internal/cursors"
"github.com/pusher/pusher-platform-go/auth"
platformclient "github.com/pusher/pusher-platform-go/client"
"github.com/pusher/pusher-platform-go/instance"
)
// Public interface for the library.
// It allows interacting with different Chatkit services.
type Client struct {
coreServiceV2 core.Service
coreServiceV6 core.Service
authorizerService authorizer.Service
cursorsService cursors.Service
authenticatorService authenticator.Service
}
// NewClient returns an instantiated instance that fulfils the Client interface.
func NewClient(instanceLocator string, key string) (*Client, error) {
locatorComponents, err := instance.ParseInstanceLocator(instanceLocator)
if err != nil {
return nil, err
}
keyComponents, err := instance.ParseKey(key)
if err != nil {
return nil, err
}
baseClient := platformclient.New(platformclient.Options{
Host: locatorComponents.Host(),
})
coreInstanceV2, err := instance.New(instance.Options{
Locator: instanceLocator,
Key: key,
ServiceName: "chatkit",
ServiceVersion: "v2",
Client: baseClient,
})
if err != nil {
return nil, err
}
coreInstanceV6, err := instance.New(instance.Options{
Locator: instanceLocator,
Key: key,
ServiceName: "chatkit",
ServiceVersion: "v6",
Client: baseClient,
})
if err != nil {
return nil, err
}
authorizerInstance, err := instance.New(instance.Options{
Locator: instanceLocator,
Key: key,
ServiceName: "chatkit_authorizer",
ServiceVersion: "v2",
Client: baseClient,
})
if err != nil {
return nil, err
}
cursorsInstance, err := instance.New(instance.Options{
Locator: instanceLocator,
Key: key,
ServiceName: "chatkit_cursors",
ServiceVersion: "v2",
Client: baseClient,
})
if err != nil {
return nil, err
}
return &Client{
coreServiceV2: core.NewService(coreInstanceV2),
coreServiceV6: core.NewService(coreInstanceV6),
authorizerService: authorizer.NewService(authorizerInstance),
cursorsService: cursors.NewService(cursorsInstance),
authenticatorService: authenticator.NewService(
locatorComponents.InstanceID,
keyComponents.Key,
keyComponents.Secret,
),
}, nil
}
// GetUserReadCursors returns a list of cursors that have been set across different rooms
// for the user.
func (c *Client) GetUserReadCursors(ctx context.Context, userID string) ([]Cursor, error) {
return c.cursorsService.GetUserReadCursors(ctx, userID)
}
// SetReadCursor sets the cursor position for a room for a user.
// The position points to the message ID of a message that was sent to that room.
func (c *Client) SetReadCursor(ctx context.Context, userID string, roomID string, position uint) error {
return c.cursorsService.SetReadCursor(ctx, userID, roomID, position)
}
// GetReadCursorsForRoom returns a list of cursors that have been set for a room.
// This returns cursors irrespective of the user that set them.
func (c *Client) GetReadCursorsForRoom(ctx context.Context, roomID string) ([]Cursor, error) {
return c.cursorsService.GetReadCursorsForRoom(ctx, roomID)
}
// GetReadCursor returns a single cursor that was set by a user in a room.
func (c *Client) GetReadCursor(ctx context.Context, userID string, roomID string) (Cursor, error) {
return c.cursorsService.GetReadCursor(ctx, userID, roomID)
}
// CursorsRequest allows performing a request to the cursors service that returns a raw HTTP
// response.
func (c *Client) CursorsRequest(
ctx context.Context,
options platformclient.RequestOptions,
) (*http.Response, error) {
return c.cursorsService.Request(ctx, options)
}
// GetRoles retrieves all roles associated with an instance.
func (c *Client) GetRoles(ctx context.Context) ([]Role, error) {
return c.authorizerService.GetRoles(ctx)
}
// CreateGlobalRole allows creating a globally scoped role.
func (c *Client) CreateGlobalRole(ctx context.Context, options CreateRoleOptions) error {
return c.authorizerService.CreateGlobalRole(ctx, options)
}
// CreateRoomRole allows creating a room scoped role.
func (c *Client) CreateRoomRole(ctx context.Context, options CreateRoleOptions) error {
return c.authorizerService.CreateRoomRole(ctx, options)
}
// DeleteGlobalRole deletes a previously created globally scoped role.
func (c *Client) DeleteGlobalRole(ctx context.Context, roleName string) error {
return c.authorizerService.DeleteGlobalRole(ctx, roleName)
}
// DeleteRoomRole deletes a previously created room scoped role.
func (c *Client) DeleteRoomRole(ctx context.Context, roleName string) error {
return c.authorizerService.DeleteRoomRole(ctx, roleName)
}
// GetPermissionsForGlobalRole returns permissions associated with a previously created global role.
func (c *Client) GetPermissionsForGlobalRole(
ctx context.Context,
roleName string,
) ([]string, error) {
return c.authorizerService.GetPermissionsForGlobalRole(ctx, roleName)
}
// GetPermissionsForRoomRole returns permissions associated with a previously created room role.
func (c *Client) GetPermissionsForRoomRole(
ctx context.Context,
roleName string,
) ([]string, error) {
return c.authorizerService.GetPermissionsForRoomRole(ctx, roleName)
}
// UpdatePermissionsForGlobalRole allows adding or removing permissions from a previously created
// globally scoped role.
func (c *Client) UpdatePermissionsForGlobalRole(
ctx context.Context,
roleName string,
options UpdateRolePermissionsOptions,
) error {
return c.authorizerService.UpdatePermissionsForGlobalRole(ctx, roleName, options)
}
// UpdatePermissionsForRoomROle allows adding or removing permissions from a previously created
// room scoped role.
func (c *Client) UpdatePermissionsForRoomRole(
ctx context.Context,
roleName string,
options UpdateRolePermissionsOptions,
) error {
return c.authorizerService.UpdatePermissionsForRoomRole(ctx, roleName, options)
}
// GetUserRoles returns roles assosciated with a user.
func (c *Client) GetUserRoles(ctx context.Context, userID string) ([]Role, error) {
return c.authorizerService.GetUserRoles(ctx, userID)
}
// AssignGlobalRoleToUser assigns a previously created globally scoped role to a user.
func (c *Client) AssignGlobalRoleToUser(ctx context.Context, userID string, roleName string) error {
return c.authorizerService.AssignGlobalRoleToUser(ctx, userID, roleName)
}
// AssignRoomRoleToUser assigns a previously created room scoped role to a user.
func (c *Client) AssignRoomRoleToUser(
ctx context.Context,
userID string,
roomID string,
roleName string,
) error {
return c.authorizerService.AssignRoomRoleToUser(ctx, userID, roomID, roleName)
}
// RemoveGlobalRoleForUser removes a previously assigned globally scoped role from a user.
// Users can only have one globall scoped role associated at any point.
func (c *Client) RemoveGlobalRoleForUser(ctx context.Context, userID string) error {
return c.authorizerService.RemoveGlobalRoleForUser(ctx, userID)
}
// RemoveRoomRoleForUser removes a previously assigned room scoped role from a user.
// Users can have multiple room roles associated with them, but only one role per room.
func (c *Client) RemoveRoomRoleForUser(ctx context.Context, userID string, roomID string) error {
return c.authorizerService.RemoveRoomRoleForUser(ctx, userID, roomID)
}
// AuthorizerRequest allows performing requests to the authorizer service
// and returns a raw HTTP response.
func (c *Client) AuthorizerRequest(
ctx context.Context,
options platformclient.RequestOptions,
) (*http.Response, error) {
return c.authorizerService.Request(ctx, options)
}
// GetUser retrieves a previously created Chatkit user.
func (c *Client) GetUser(ctx context.Context, userID string) (User, error) {
return c.coreServiceV6.GetUser(ctx, userID)
}
// GetUsers retrieves a list of users based on the options provided.
func (c *Client) GetUsers(ctx context.Context, options *GetUsersOptions) ([]User, error) {
return c.coreServiceV6.GetUsers(ctx, options)
}
// GetUsersByID retrieves a list of users for the given id's.
func (c *Client) GetUsersByID(ctx context.Context, userIDs []string) ([]User, error) {
return c.coreServiceV6.GetUsersByID(ctx, userIDs)
}
// CreateUser creates a new chatkit user.
func (c *Client) CreateUser(ctx context.Context, options CreateUserOptions) error {
return c.coreServiceV6.CreateUser(ctx, options)
}
// CreateUsers creates a batch of users.
func (c *Client) CreateUsers(ctx context.Context, users []CreateUserOptions) error {
return c.coreServiceV6.CreateUsers(ctx, users)
}
// UpdateUser allows updating a previously created user.
func (c *Client) UpdateUser(ctx context.Context, userID string, options UpdateUserOptions) error {
return c.coreServiceV6.UpdateUser(ctx, userID, options)
}
// DeleteUser deletes a previously created user.
func (c *Client) DeleteUser(ctx context.Context, userID string) error {
return c.coreServiceV6.DeleteUser(ctx, userID)
}
// GetRoom retrieves an existing room.
func (c *Client) GetRoom(ctx context.Context, roomID string) (Room, error) {
return c.coreServiceV6.GetRoom(ctx, roomID)
}
// GetRooms retrieves a list of rooms based on the options provided.
func (c *Client) GetRooms(ctx context.Context, options GetRoomsOptions) ([]core.RoomWithoutMembers, error) {
return c.coreServiceV6.GetRooms(ctx, options)
}
// GetUserRooms retrieves a list of rooms the user is an existing member of.
func (c *Client) GetUserRooms(ctx context.Context, userID string) ([]Room, error) {
return c.coreServiceV6.GetUserRooms(ctx, userID)
}
// GetUserJoinableRooms retrieves a list of rooms the use can join (not an existing member of)
// Private rooms are not returned as part of the response.
func (c *Client) GetUserJoinableRooms(ctx context.Context, userID string) ([]Room, error) {
return c.coreServiceV6.GetUserJoinableRooms(ctx, userID)
}
// CreateRoom creates a new room.
func (c *Client) CreateRoom(ctx context.Context, options CreateRoomOptions) (Room, error) {
return c.coreServiceV6.CreateRoom(ctx, options)
}
// UpdateRoom allows updating an existing room.
func (c *Client) UpdateRoom(ctx context.Context, roomID string, options UpdateRoomOptions) error {
return c.coreServiceV6.UpdateRoom(ctx, roomID, options)
}
// DeleteRoom deletes an existing room.
func (c *Client) DeleteRoom(ctx context.Context, roomID string) error {
return c.coreServiceV6.DeleteRoom(ctx, roomID)
}
// AddUsersToRoom adds new users to an existing room.
func (c *Client) AddUsersToRoom(ctx context.Context, roomID string, userIDs []string) error {
return c.coreServiceV6.AddUsersToRoom(ctx, roomID, userIDs)
}
// RemoveUsersFromRoom removes existing members from a room.
func (c *Client) RemoveUsersFromRoom(ctx context.Context, roomID string, userIDs []string) error {
return c.coreServiceV6.RemoveUsersFromRoom(ctx, roomID, userIDs)
}
// SendMessage publishes a new message to a room.
func (c *Client) SendMessage(ctx context.Context, options SendMessageOptions) (uint, error) {
return c.coreServiceV2.SendMessage(ctx, options)
}
// SendMultipartMessage publishes a new multipart message to a room.
func (c *Client) SendMultipartMessage(
ctx context.Context,
options SendMultipartMessageOptions,
) (uint, error) {
return c.coreServiceV6.SendMultipartMessage(ctx, options)
}
// SendSimpleMessage publishes a new simple multipart message to a room.
func (c *Client) SendSimpleMessage(
ctx context.Context,
options SendSimpleMessageOptions,
) (uint, error) {
return c.coreServiceV6.SendSimpleMessage(ctx, options)
}
// GetRoomMessages retrieves messages previously sent to a room based on the options provided.
func (c *Client) GetRoomMessages(
ctx context.Context,
roomID string,
options GetRoomMessagesOptions,
) ([]Message, error) {
return c.coreServiceV2.GetRoomMessages(ctx, roomID, options)
}
// FetchMultipartMessage retrieves a single message previously sent to a room based on the options provided.
func (c *Client) FetchMultipartMessage(
ctx context.Context,
options FetchMultipartMessageOptions,
) (MultipartMessage, error) {
return c.coreServiceV6.FetchMultipartMessage(ctx, options)
}
// FetchMultipartMessages retrieves messages previously sent to a room based on
// the options provided.
func (c *Client) FetchMultipartMessages(
ctx context.Context,
roomID string,
options GetRoomMessagesOptions,
) ([]MultipartMessage, error) {
return c.coreServiceV6.FetchMultipartMessages(ctx, roomID, options)
}
// DeleteMessage allows a previously sent message to be deleted.
func (c *Client) DeleteMessage(ctx context.Context, options DeleteMessageOptions) error {
return c.coreServiceV6.DeleteMessage(ctx, options)
}
// EditMessage identifies an existing message by both its room and message id
// in order to replace it's content and sender id with updated values.
func (c *Client) EditMessage(ctx context.Context, roomID string, messageID uint, options EditMessageOptions) error {
return c.coreServiceV2.EditMessage(ctx, roomID, messageID, options)
}
// EditMultipartMessage identifies an existing message by both its room and message id
// in order to replace it's content and sender id with updated values.
func (c *Client) EditMultipartMessage(ctx context.Context, roomID string, messageID uint, options EditMultipartMessageOptions) error {
return c.coreServiceV6.EditMultipartMessage(ctx, roomID, messageID, options)
}
// EditSimpleMessage identifies an existing message by both its room and message id
// in order to replace it's content and sender id with updated values.
func (c *Client) EditSimpleMessage(ctx context.Context, roomID string, messageID uint, options EditSimpleMessageOptions) error {
return c.coreServiceV6.EditSimpleMessage(ctx, roomID, messageID, options)
}
// CoreRequest allows making requests to the core chatkit service and returns a raw HTTP response.
func (c *Client) CoreRequest(
ctx context.Context,
options platformclient.RequestOptions,
) (*http.Response, error) {
return c.coreServiceV6.Request(ctx, options)
}
// Authenticate returns a token response along with headers and status code to be used within
// the context of a token provider.
// Currently, the only supported GrantType is GrantTypeClientCredentials.
func (c *Client) Authenticate(payload auth.Payload, options auth.Options) (*auth.Response, error) {
return c.authenticatorService.Authenticate(payload, options)
}
// GenerateAccessToken generates a JWT token based on the options provided.
func (c *Client) GenerateAccessToken(options auth.Options) (auth.TokenWithExpiry, error) {
return c.authenticatorService.GenerateAccessToken(options)
}
// GenerateSuToken generates a JWT token with the `su` claim.
func (c *Client) GenerateSUToken(options auth.Options) (auth.TokenWithExpiry, error) {
return c.authenticatorService.GenerateSUToken(options)
}