From d92c8592b09fd046f428f132606d39d19ffda76e Mon Sep 17 00:00:00 2001 From: Vishal Narkhede Date: Fri, 2 Feb 2024 14:02:35 +0100 Subject: [PATCH] refactor: code review changes --- src/client.ts | 70 +++++++++++++++++++++++++++++++++------------------ src/thread.ts | 1 + src/types.ts | 15 +++++++++++ 3 files changed, 61 insertions(+), 25 deletions(-) diff --git a/src/client.ts b/src/client.ts index fe3c48e83..9b6bcfa87 100644 --- a/src/client.ts +++ b/src/client.ts @@ -167,6 +167,8 @@ import { UserSort, GetThreadAPIResponse, PartialThreadUpdate, + QueryThreadsOptions, + GetThreadOptions, } from './types'; import { InsightMetrics, postInsights } from './insights'; import { Thread } from './thread'; @@ -2582,15 +2584,18 @@ export class StreamChat[], next: string }} Returns the list of threads and the next cursor. + */ + async queryThreads(options?: QueryThreadsOptions) { const opts = { limit: 10, participant_limit: 10, @@ -2600,23 +2605,29 @@ export class StreamChat>(this.baseURL + `/threads`, opts); - const threads: Thread[] = []; - - for (const t of res.threads) { - const thread = new Thread(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} 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, @@ -2629,9 +2640,17 @@ export class StreamChat(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} 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. @@ -2651,14 +2670,15 @@ export class StreamChat>(this.baseURL + `/threads/${messageId}`, { - ...partialThreadObject, - }); + return await this.patch>( + this.baseURL + `/threads/${messageId}`, + partialThreadObject, + ); } getUserAgent() { diff --git a/src/thread.ts b/src/thread.ts index 478e53359..2278f43a1 100644 --- a/src/thread.ts +++ b/src/thread.ts @@ -20,6 +20,7 @@ type ThreadReadStatus; } >; + export class Thread { id: string; latestReplies: FormatMessageResponse[] = []; diff --git a/src/types.ts b/src/types.ts index e46eecef6..23deac25e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -507,10 +507,25 @@ export type PartialThreadUpdate = { unset?: Partial>; }; +export type QueryThreadsOptions = { + limit?: number; + next?: string; + participant_limit?: number; + reply_limit?: number; + watch?: boolean; +}; + export type QueryThreadsAPIResponse = APIResponse & { threads: ThreadResponse[]; next?: string; }; + +export type GetThreadOptions = { + participant_limit?: number; + reply_limit?: number; + watch?: boolean; +}; + export type GetThreadAPIResponse = APIResponse & { thread: ThreadResponse; };