Skip to content

Commit

Permalink
feat: add batch method for deactivate and reactivate users (#1074)
Browse files Browse the repository at this point in the history
* feat: add batch method for deactivate and reactivate users
  • Loading branch information
yaziine committed Dec 13, 2022
1 parent f18c14e commit b6d14ab
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
58 changes: 47 additions & 11 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
CreateImportResponse,
CreateImportURLResponse,
CustomPermissionOptions,
DeactivateUsersOptions,
DefaultGenerics,
DeleteCampaignOptions,
DeleteChannelsResponse,
Expand Down Expand Up @@ -120,6 +121,8 @@ import {
PushProviderUpsertResponse,
QueryChannelsAPIResponse,
ReactionResponse,
ReactivateUserOptions,
ReactivateUsersOptions,
Recipient,
RecipientFilters,
RecipientQueryOptions,
Expand Down Expand Up @@ -1961,35 +1964,68 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
*
* @param {string[]} user_ids which users to restore
*
* @return {APIResponse} A task ID
* @return {APIResponse} An API response
*/
async restoreUsers(user_ids: string[]) {
return await this.post<APIResponse>(this.baseURL + `/users/restore`, {
user_ids,
});
}

async reactivateUser(
userID: string,
options?: {
created_by_id?: string;
name?: string;
restore_messages?: boolean;
},
) {
/**
* reactivateUser - Reactivate one user
*
* @param {string} userID which user to reactivate
* @param {ReactivateUserOptions} [options]
*
* @return {UserResponse} Reactivated user
*/
async reactivateUser(userID: string, options?: ReactivateUserOptions) {
return await this.post<APIResponse & { user: UserResponse<StreamChatGenerics> }>(
this.baseURL + `/users/${userID}/reactivate`,
{ ...options },
);
}

async deactivateUser(userID: string, options?: { created_by_id?: string; mark_messages_deleted?: boolean }) {
/**
* reactivateUsers - Reactivate many users asynchronously
*
* @param {string[]} user_ids which users to reactivate
* @param {ReactivateUsersOptions} [options]
*
* @return {TaskResponse} A task ID
*/
async reactivateUsers(user_ids: string[], options?: ReactivateUsersOptions) {
return await this.post<APIResponse & TaskResponse>(this.baseURL + `/users/reactivate`, { user_ids, ...options });
}

/**
* deactivateUser - Deactivate one user
*
* @param {string} userID which user to deactivate
* @param {DeactivateUsersOptions} [options]
*
* @return {UserResponse} Deactivated user
*/
async deactivateUser(userID: string, options?: DeactivateUsersOptions) {
return await this.post<APIResponse & { user: UserResponse<StreamChatGenerics> }>(
this.baseURL + `/users/${userID}/deactivate`,
{ ...options },
);
}

/**
* deactivateUsers - Deactivate many users asynchronously
*
* @param {string[]} user_ids which users to deactivate
* @param {DeactivateUsersOptions} [options]
*
* @return {TaskResponse} A task ID
*/
async deactivateUsers(user_ids: string[], options?: DeactivateUsersOptions) {
return await this.post<APIResponse & TaskResponse>(this.baseURL + `/users/deactivate`, { user_ids, ...options });
}

async exportUser(userID: string, options?: Record<string, string>) {
return await this.get<
APIResponse & {
Expand Down Expand Up @@ -2978,7 +3014,7 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
* @param {string[]} user_ids which users to delete
* @param {DeleteUserOptions} options Configuration how to delete users
*
* @return {APIResponse} A task ID
* @return {TaskResponse} A task ID
*/
async deleteUsers(user_ids: string[], options: DeleteUserOptions) {
if (options?.user !== 'soft' && options?.user !== 'hard') {
Expand Down
16 changes: 16 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,11 @@ export type CustomPermissionOptions = {
same_team?: boolean;
};

export type DeactivateUsersOptions = {
created_by_id?: string;
mark_messages_deleted?: boolean;
};

// TODO: rename to UpdateChannelOptions in the next major update and use it in channel._update and/or channel.update
export type InviteOptions<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = {
accept_invite?: boolean;
Expand Down Expand Up @@ -884,6 +889,17 @@ export type QueryMembersOptions = {
user_id_lte?: string;
};

export type ReactivateUserOptions = {
created_by_id?: string;
name?: string;
restore_messages?: boolean;
};

export type ReactivateUsersOptions = {
created_by_id?: string;
restore_messages?: boolean;
};

export type SearchOptions<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = {
limit?: number;
next?: string;
Expand Down

0 comments on commit b6d14ab

Please sign in to comment.