Skip to content

Commit

Permalink
refactor: code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalnarkhede committed Feb 2, 2024
1 parent e584fe8 commit d92c859
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 25 deletions.
70 changes: 45 additions & 25 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ import {
UserSort,
GetThreadAPIResponse,
PartialThreadUpdate,
QueryThreadsOptions,
GetThreadOptions,
} from './types';
import { InsightMetrics, postInsights } from './insights';
import { Thread } from './thread';
Expand Down Expand Up @@ -2582,15 +2584,18 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
);
}

async queryThreads(options?: {
limit?: number;
next?: string;
participant_limit?: number;
prev?: string;
reply_limit?: number;
watch?: boolean;
}) {
// eslint-disable-next-line
/**
* queryThreads - returns the list of threads of current user.
*
* @param {QueryThreadsOptions} options Options object for pagination and limiting the participants and replies.
* @param {number} options.limit Limits the number of threads to be returned.
* @param {boolean} options.watch Subscribes the user to the channels of the threads.
* @param {number} options.participant_limit Limits the number of participants returned per threads.
* @param {number} options.reply_limit Limits the number of replies returned per threads.
*
* @returns {{ threads: Thread<StreamChatGenerics>[], next: string }} Returns the list of threads and the next cursor.
*/
async queryThreads(options?: QueryThreadsOptions) {
const opts = {
limit: 10,
participant_limit: 10,
Expand All @@ -2600,23 +2605,29 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
};

const res = await this.post<QueryThreadsAPIResponse<StreamChatGenerics>>(this.baseURL + `/threads`, opts);
const threads: Thread<StreamChatGenerics>[] = [];

for (const t of res.threads) {
const thread = new Thread<StreamChatGenerics>(this, t);
threads.push(thread);
}

return {
threads,
threads: res.threads.map((thread) => new Thread(this, thread)),
next: res.next,
};
}

async getThread(
messageId: string,
options: { participant_limit?: number; reply_limit?: number; watch?: boolean } = {},
) {
/**
* getThread - returns the thread of a message by its id.
*
* @param {string} messageId The message id
* @param {GetThreadOptions} options Options object for pagination and limiting the participants and replies.
* @param {boolean} options.watch Subscribes the user to the channel of the thread.
* @param {number} options.participant_limit Limits the number of participants returned per threads.
* @param {number} options.reply_limit Limits the number of replies returned per threads.
*
* @returns {Thread<StreamChatGenerics>} Returns the thread.
*/
async getThread(messageId: string, options: GetThreadOptions = {}) {
if (!messageId) {
throw Error('Please specify the message id when calling partialUpdateThread');
}

const opts = {
participant_limit: 100,
reply_limit: 3,
Expand All @@ -2629,9 +2640,17 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
return new Thread<StreamChatGenerics>(this, res.thread);
}

/**
* partialUpdateThread - updates the given thread
*
* @param {string} messageId The id of the thread message which needs to be updated.
* @param {PartialThreadUpdate} partialThreadObject should contain "set" or "unset" params for any of the thread's non-reserved fields.
*
* @returns {GetThreadAPIResponse<StreamChatGenerics>} Returns the updated thread.
*/
async partialUpdateThread(messageId: string, partialThreadObject: PartialThreadUpdate) {
if (!messageId) {
throw Error('Please specify the message id when calling updateThread');
throw Error('Please specify the message id when calling partialUpdateThread');
}

// check for reserved fields from ThreadResponse type within partialThreadObject's set and unset.
Expand All @@ -2651,14 +2670,15 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
for (const key in { ...partialThreadObject.set, ...partialThreadObject.unset }) {
if (reservedThreadFields.includes(key)) {
throw Error(
`You cannot set ${key} field. ${key} is reserved for server-side use. Please omit ${key} from your set object.`,
`You cannot set ${key} field on Thread object. ${key} is reserved for server-side use. Please omit ${key} from your set object.`,
);
}
}

return await this.patch<GetThreadAPIResponse<StreamChatGenerics>>(this.baseURL + `/threads/${messageId}`, {
...partialThreadObject,
});
return await this.patch<GetThreadAPIResponse<StreamChatGenerics>>(
this.baseURL + `/threads/${messageId}`,
partialThreadObject,
);
}

getUserAgent() {
Expand Down
1 change: 1 addition & 0 deletions src/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type ThreadReadStatus<StreamChatGenerics extends ExtendableGenerics = DefaultGen
user: UserResponse<StreamChatGenerics>;
}
>;

export class Thread<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> {
id: string;
latestReplies: FormatMessageResponse<StreamChatGenerics>[] = [];
Expand Down
15 changes: 15 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,25 @@ export type PartialThreadUpdate = {
unset?: Partial<Record<string, unknown>>;
};

export type QueryThreadsOptions = {
limit?: number;
next?: string;
participant_limit?: number;
reply_limit?: number;
watch?: boolean;
};

export type QueryThreadsAPIResponse<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = APIResponse & {
threads: ThreadResponse<StreamChatGenerics>[];
next?: string;
};

export type GetThreadOptions = {
participant_limit?: number;
reply_limit?: number;
watch?: boolean;
};

export type GetThreadAPIResponse<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = APIResponse & {
thread: ThreadResponse<StreamChatGenerics>;
};
Expand Down

0 comments on commit d92c859

Please sign in to comment.