Skip to content

Commit

Permalink
Merge branch 'master' into refresh-token
Browse files Browse the repository at this point in the history
  • Loading branch information
guerinoni authored Jan 30, 2024
2 parents 99b754a + d2bf603 commit 50e2aac
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 64 deletions.
158 changes: 115 additions & 43 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import {
GetMessageAPIResponse,
GetRateLimitsResponse,
GetUnreadCountAPIResponse,
GetUnreadCountBatchAPIResponse,
ListChannelResponse,
ListCommandsResponse,
ListImportsPaginationOptions,
Expand Down Expand Up @@ -123,12 +124,10 @@ import {
PushProviderListResponse,
PushProviderUpsertResponse,
QueryChannelsAPIResponse,
QuerySegmentsOptions,
ReactionResponse,
ReactivateUserOptions,
ReactivateUsersOptions,
Recipient,
RecipientFilters,
RecipientQueryOptions,
ReservedMessageFields,
ReviewFlagReportOptions,
ReviewFlagReportResponse,
Expand All @@ -138,8 +137,7 @@ import {
SearchPayload,
Segment,
SegmentData,
SegmentFilters,
SegmentQueryOptions,
SegmentType,
SendFileAPIResponse,
StreamChatOptions,
SyncOptions,
Expand All @@ -159,6 +157,7 @@ import {
UpdatedMessage,
UpdateMessageAPIResponse,
UpdateMessageOptions,
UpdateSegmentData,
UserCustomEvent,
UserFilters,
UserOptions,
Expand Down Expand Up @@ -1688,10 +1687,28 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
);
}

/**
* getUnreadCount - Returns unread counts for a single user
*
* @param {string} [userID] User ID.
*
* @return {<GetUnreadCountAPIResponse>}
*/
async getUnreadCount(userID?: string) {
return await this.get<GetUnreadCountAPIResponse>(this.baseURL + '/unread', userID ? { user_id: userID } : {});
}

/**
* getUnreadCountBatch - Returns unread counts for multiple users at once. Only works server side.
*
* @param {string[]} [userIDs] List of user IDs to fetch unread counts for.
*
* @return {<GetUnreadCountBatchAPIResponse>}
*/
async getUnreadCountBatch(userIDs: string[]) {
return await this.post<GetUnreadCountBatchAPIResponse>(this.baseURL + '/unread_batch', { user_ids: userIDs });
}

/**
* removeDevice - Removes the device with the given id. Clientside users can only delete their own devices
*
Expand Down Expand Up @@ -2824,56 +2841,132 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
}

/**
* createSegment - Creates a Campaign Segment
* createSegment - Creates a segment
*
* @private
* @param {SegmentType} type Segment type
* @param {string} id Segment ID (valid UUID)
* @param {string} name Segment name (valid UUID)
* @param {SegmentData} params Segment data
*
* @return {Segment} The Created Segment
* @return {Segment} The created Segment
*/
private async createSegment(type: SegmentType, id: string, name: string, data?: SegmentData): Promise<Segment> {
const body = {
id,
type,
name,
data,
};
const { segment } = await this.post<{ segment: Segment }>(this.baseURL + `/segments`, body);
return segment;
}

/**
* createUserSegment - Creates a user segment
*
* @param {string} id Segment ID (valid UUID)
* @param {string} name Segment name
* @param {SegmentData} data Segment data
*
* @return {Segment} The created Segment
*/
async createUserSegment(id: string, name: string, data?: SegmentData): Promise<Segment> {
return await this.createSegment('user', id, name, data);
}

/**
* createChannelSegment - Creates a channel segment
*
* @param {string} id Segment ID (valid UUID)
* @param {string} name Segment name
* @param {SegmentData} data Segment data
*
* @return {Segment} The created Segment
*/
async createChannelSegment(id: string, name: string, data?: SegmentData): Promise<Segment> {
return await this.createSegment('channel', id, name, data);
}

/**
* updateSegment - Update a segment
*
* @param {string} id Segment ID
* @param {Partial<UpdateSegmentData>} data Data to update
*
* @return {Segment} Updated Segment
*/
async createSegment(params: SegmentData) {
const { segment } = await this.post<{ segment: Segment }>(this.baseURL + `/segments`, { segment: params });
async updateSegment(id: string, data: Partial<UpdateSegmentData>) {
const { segment } = await this.put<{ segment: Segment }>(this.baseURL + `/segments/${id}`, data);
return segment;
}

/**
* querySegments - Query Campaign Segments
* addSegmentTargets - Add targets to a segment
*
* @param {string} id Segment ID
* @param {string[]} targets Targets to add to the segment
*
* @return {APIResponse} API response
*/
async addSegmentTargets(id: string, targets: string[]) {
const body = { targets };
return await this.post<APIResponse>(this.baseURL + `/segments/${id}/addtargets`, body);
}

/**
* deleteSegmentTargets - Delete targets from a segment
*
* @param {string} id Segment ID
* @param {string[]} targets Targets to add to the segment
*
* @return {APIResponse} API response
*/
async deleteSegmentTargets(id: string, targets: string[]) {
const body = { targets };
return await this.post<APIResponse>(this.baseURL + `/segments/${id}/deletetargets`, body);
}

/**
* querySegments - Query Segments
*
* @param {filter} filter MongoDB style filter conditions
* @param {QuerySegmentsOptions} options Options for sorting/paginating the results
*
* @return {Segment[]} Segments
*/
async querySegments(filters: SegmentFilters, options: SegmentQueryOptions = {}) {
async querySegments(filter: {}, options: QuerySegmentsOptions = {}) {
return await this.get<{
segments: Segment[];
}>(this.baseURL + `/segments`, {
payload: {
filter_conditions: filters,
filter,
...options,
},
});
}

/**
* updateSegment - Update a Campaign Segment
* deleteSegment - Delete a Campaign Segment
*
* @param {string} id Segment ID
* @param {Partial<SegmentData>} params Segment data
*
* @return {Segment} Updated Segment
* @return {Promise<APIResponse>} The Server Response
*/
async updateSegment(id: string, params: Partial<SegmentData>) {
const { segment } = await this.put<{ segment: Segment }>(this.baseURL + `/segments/${id}`, { segment: params });
return segment;
async deleteSegment(id: string) {
return await this.delete<APIResponse>(this.baseURL + `/segments/${id}`);
}

/**
* deleteSegment - Delete a Campaign Segment
* segmentTargetExists - Check if a target exists in a segment
*
* @param {string} id Segment ID
* @param {string} segmentId Segment ID
* @param {string} targetId Target ID
*
* @return {Promise<APIResponse>} The Server Response
*/
async deleteSegment(id: string) {
return this.delete<APIResponse>(this.baseURL + `/segments/${id}`);
async segmentTargetExists(segmentId: string, targetId: string) {
return await this.get<APIResponse>(this.baseURL + `/segments/${segmentId}/target/${targetId}`);
}

/**
Expand Down Expand Up @@ -2987,27 +3080,6 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
return await this.post<APIResponse & TestCampaignResponse>(this.baseURL + `/campaigns/${id}/test`, { users });
}

/**
* queryRecipients - Query Campaign Recipient Results
*
*
* @return {Recipient[]} Recipients
*/
async queryRecipients(filters: RecipientFilters, options: RecipientQueryOptions = {}) {
return await this.get<{
campaigns: Record<string, Campaign>;
recipients: Recipient[];
segments: Record<string, Segment>;
channels?: Record<string, ChannelResponse<StreamChatGenerics>>;
users?: Record<string, UserResponse<StreamChatGenerics>>;
}>(this.baseURL + `/recipients`, {
payload: {
filter_conditions: filters,
...options,
},
});
}

/**
* enrichURL - Get OpenGraph data of the given link
*
Expand Down
56 changes: 35 additions & 21 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ export type AppSettingsAPIResponse<StreamChatGenerics extends ExtendableGenerics
campaign_enabled?: boolean;
cdn_expiration_seconds?: number;
custom_action_handler_url?: string;
datadog_info?: {
api_key: string;
site: string;
};
disable_auth_checks?: boolean;
disable_permissions_checks?: boolean;
enforce_unique_usernames?: 'no' | 'app' | 'team';
Expand Down Expand Up @@ -247,6 +251,7 @@ export type BannedUsersResponse<StreamChatGenerics extends ExtendableGenerics =

export type BlockListResponse = BlockList & {
created_at?: string;
type?: string;
updated_at?: string;
};

Expand Down Expand Up @@ -508,6 +513,10 @@ export type GetUnreadCountAPIResponse = APIResponse & {
total_unread_count: number;
};

export type GetUnreadCountBatchAPIResponse = APIResponse & {
counts_by_user: { [userId: string]: GetUnreadCountAPIResponse };
};

export type ListChannelResponse<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = APIResponse & {
channel_types: Record<
string,
Expand Down Expand Up @@ -2400,22 +2409,43 @@ export type DeleteUserOptions = {
user?: DeleteType;
};

export type SegmentType = 'channel' | 'user';

export type SegmentData = {
description: string;
filter: {};
name: string;
type: 'channel' | 'user';
};

export type Segment = {
created_at: string;
deleted_at: string;
id: string;
in_use: boolean;
locked: boolean;
name: string;
size: number;
status: 'computing' | 'ready';
type: SegmentType;
updated_at: string;
} & SegmentData;

export type UpdateSegmentData = {
name: string;
} & SegmentData;

export type SortParam = {
field: string;
direction?: AscDesc;
};

export type Pager = {
limit?: number;
next?: string;
prev?: string;
};

export type QuerySegmentsOptions = {
sort?: SortParam[];
} & Pager;

export type CampaignSortField = {
field: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -2433,12 +2463,9 @@ export type CampaignQueryOptions = {
};

export type SegmentQueryOptions = CampaignQueryOptions;
export type RecipientQueryOptions = CampaignQueryOptions;

// TODO: add better typing
export type SegmentFilters = {};
export type CampaignFilters = {};
export type RecipientFilters = {};

export type CampaignData = {
attachments: Attachment[];
Expand Down Expand Up @@ -2479,20 +2506,7 @@ export type TestCampaignResponse = {
results?: Record<string, string>;
};

export type DeleteCampaignOptions = {
recipients?: boolean;
};

export type Recipient = {
campaign_id: string;
channel_cid: string;
created_at: string;
status: 'pending' | 'sent' | 'failed';
updated_at: string;
details?: string;
message_id?: string;
receiver_id?: string;
};
export type DeleteCampaignOptions = {};

export type TaskStatus = {
created_at: string;
Expand Down

0 comments on commit 50e2aac

Please sign in to comment.