Skip to content

Commit fbad4a6

Browse files
itsmeadiaditya
andauthored
[FEEDS-115]chore: add batch users support (#618)
* chore: add batch users support * chore: remove unused var --------- Co-authored-by: aditya <[email protected]>
1 parent be9e842 commit fbad4a6

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

src/batch_operations.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { StreamClient, APIResponse, DefaultGenerics } from './client';
22
import utils from './utils';
3+
import { StreamUser } from './user';
34

45
type BaseFollowRelation = {
56
source: string;
@@ -84,9 +85,45 @@ function unfollowMany(this: StreamClient, unfollows: UnfollowRelation[]) {
8485
token: this.getOrCreateToken(),
8586
});
8687
}
88+
export type AddUsersResponse = APIResponse & {
89+
created_users: StreamUser[];
90+
};
91+
92+
export type GetUsersResponse = APIResponse & {
93+
users: StreamUser[];
94+
};
95+
96+
function addUsers(this: StreamClient, users: StreamUser[], overrideExisting: boolean = false) {
97+
return this.post<AddUsersResponse>({
98+
url: 'users/',
99+
body: {
100+
users,
101+
override: overrideExisting,
102+
},
103+
token: this.getOrCreateToken(),
104+
});
105+
}
106+
107+
function getUsers(this: StreamClient, ids: string[]) {
108+
return this.get<GetUsersResponse>({
109+
url: 'users/',
110+
qs: { ids: ids.join(',') },
111+
token: this.getOrCreateToken(),
112+
});
113+
}
114+
function deleteUsers(this: StreamClient, ids: string[]) {
115+
return this.delete<string[]>({
116+
url: 'users/',
117+
qs: { ids: ids.join(',') },
118+
token: this.getOrCreateToken(),
119+
});
120+
}
87121

88122
export default {
89123
addToMany,
90124
followMany,
91125
unfollowMany,
126+
addUsers,
127+
getUsers,
128+
deleteUsers,
92129
};

src/client.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ import { StreamUser } from './user';
1616
import { JWTScopeToken, JWTUserSessionToken } from './signing';
1717
import { FeedError, StreamApiError, SiteError } from './errors';
1818
import utils from './utils';
19-
import BatchOperations, { FollowRelation, UnfollowRelation } from './batch_operations';
19+
import BatchOperations, {
20+
AddUsersResponse,
21+
FollowRelation,
22+
GetUsersResponse,
23+
UnfollowRelation,
24+
} from './batch_operations';
2025
import createRedirectUrl from './redirect_url';
2126
import {
2227
StreamFeed,
@@ -188,7 +193,9 @@ export class StreamClient<StreamFeedGenerics extends DefaultGenerics = DefaultGe
188193
followMany?: (this: StreamClient, follows: FollowRelation[], activityCopyLimit?: number) => Promise<APIResponse>; // eslint-disable-line no-use-before-define
189194
unfollowMany?: (this: StreamClient, unfollows: UnfollowRelation[]) => Promise<APIResponse>; // eslint-disable-line no-use-before-define
190195
createRedirectUrl?: (this: StreamClient, targetUrl: string, userId: string, events: unknown[]) => string; // eslint-disable-line no-use-before-define
191-
196+
addUsers?: (this: StreamClient, users: StreamUser[], overrideExisting: boolean) => Promise<AddUsersResponse>; // eslint-disable-line no-use-before-define
197+
getUsers?: (this: StreamClient, ids: string[]) => Promise<GetUsersResponse>; // eslint-disable-line no-use-before-define
198+
deleteUsers?: (this: StreamClient, ids: string[]) => Promise<string[]>; // eslint-disable-line no-use-before-define
192199
/**
193200
* Initialize a client
194201
* @link https://getstream.io/activity-feeds/docs/node/#setup
@@ -286,6 +293,9 @@ export class StreamClient<StreamFeedGenerics extends DefaultGenerics = DefaultGe
286293
this.followMany = BatchOperations.followMany;
287294
this.unfollowMany = BatchOperations.unfollowMany;
288295
this.createRedirectUrl = createRedirectUrl;
296+
this.addUsers = BatchOperations.addUsers;
297+
this.getUsers = BatchOperations.getUsers;
298+
this.deleteUsers = BatchOperations.deleteUsers;
289299
}
290300
}
291301

test/integration/node/client_test.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import expect from 'expect.js';
33
import { connect } from '../../../src';
44
import * as errors from '../../../src/errors';
55

6-
import { init, beforeEachFn } from '../utils/hooks';
6+
import { init, beforeEachFn, randUserId } from '../utils/hooks';
77

88
describe('[INTEGRATION] Stream client (Node)', function () {
99
init.call(this);
@@ -217,6 +217,36 @@ describe('[INTEGRATION] Stream client (Node)', function () {
217217
});
218218
});
219219

220+
it('add multiple users', function () {
221+
const id1 = randUserId('user1');
222+
const id2 = randUserId('user2');
223+
224+
const users = [
225+
{ id: id1, data: { name: 'u1' } },
226+
{ id: id2, data: { name: 'u2' } },
227+
];
228+
229+
return this.client
230+
.addUsers(users, true)
231+
.then((response) => {
232+
expect(response.created_users.length).to.be(2);
233+
return this.client.getUsers([id1, id2]).then((getUsersRes) => {
234+
expect(getUsersRes.users.length).to.be(2);
235+
getUsersRes.users.forEach((user) => {
236+
expect(user.data).to.eql(users.find((u) => u.id === user.id).data);
237+
});
238+
});
239+
})
240+
.then(() => {
241+
// delete users
242+
return this.client.deleteUsers([id1, id2]).then(() => {
243+
return this.client.getUsers([id1, id2]).then((getUsersRes) => {
244+
expect(getUsersRes.users.length).to.be(0);
245+
});
246+
});
247+
});
248+
});
249+
220250
it('supports batch following', function () {
221251
const follows = [
222252
{

0 commit comments

Comments
 (0)