From 5352ede9d794a8dc429a076fe826b10de182d3f7 Mon Sep 17 00:00:00 2001 From: devanshkansagra <125076549+devanshkansagra@users.noreply.github.com.> Date: Mon, 9 Jun 2025 22:46:07 +0530 Subject: [PATCH 1/3] Migrate to DDPSDK --- packages/api/package.json | 2 + packages/api/src/EmbeddedChatApi.ts | 132 ++--- packages/auth/package.json | 4 + packages/auth/src/IRocketChatAuthOptions.ts | 5 +- packages/auth/src/RocketChatAuth.ts | 16 +- packages/auth/src/auth.ts | 4 +- .../auth/src/loginWithOAuthServiceToken.ts | 10 +- packages/auth/src/loginWithPassword.ts | 12 +- packages/auth/src/loginWithResumeToken.ts | 10 +- .../ChatInput/ChatInputFormattingToolbar.js | 30 +- yarn.lock | 542 +++++++++++++++++- 11 files changed, 631 insertions(+), 136 deletions(-) diff --git a/packages/api/package.json b/packages/api/package.json index a05ca8e630..c1bc0984f8 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -24,6 +24,8 @@ }, "dependencies": { "@embeddedchat/auth": "workspace:^", + "@rocket.chat/ddp-client": "0.2.27", + "@rocket.chat/emitter": "^0.31.25", "@rocket.chat/sdk": "^1.0.0-alpha.42" } } diff --git a/packages/api/src/EmbeddedChatApi.ts b/packages/api/src/EmbeddedChatApi.ts index 46690e24e6..0f0076b9da 100644 --- a/packages/api/src/EmbeddedChatApi.ts +++ b/packages/api/src/EmbeddedChatApi.ts @@ -7,12 +7,15 @@ import { ApiError, } from "@embeddedchat/auth"; +import { DDPSDK } from "@rocket.chat/ddp-client"; +import { Emitter } from "@rocket.chat/emitter"; + // mutliple typing status can come at the same time they should be processed in order. let typingHandlerLock = 0; export default class EmbeddedChatApi { host: string; rid: string; - rcClient: Rocketchat; + rcClient: DDPSDK; onMessageCallbacks: ((message: any) => void)[]; onMessageDeleteCallbacks: ((messageId: string) => void)[]; onTypingStatusCallbacks: ((users: string[]) => void)[]; @@ -28,12 +31,7 @@ export default class EmbeddedChatApi { ) { this.host = host; this.rid = rid; - this.rcClient = new Rocketchat({ - protocol: "ddp", - host: this.host, - useSsl: !/http:\/\//.test(host), - reopen: 20000, - }); + this.rcClient = DDPSDK.create(this.host); this.onMessageCallbacks = []; this.onMessageDeleteCallbacks = []; this.onTypingStatusCallbacks = []; @@ -42,6 +40,7 @@ export default class EmbeddedChatApi { this.onUiInteractionCallbacks = []; this.auth = new RocketChatAuth({ host: this.host, + rcClient: this.rcClient, deleteToken, getToken, saveToken, @@ -161,6 +160,7 @@ export default class EmbeddedChatApi { break; case "TOKEN": if (!auth.credentials) { + console.log("No Token found"); return; } await this.auth.loginWithOAuthServiceToken(auth.credentials); @@ -187,18 +187,33 @@ export default class EmbeddedChatApi { */ async connect() { try { - await this.close(); // before connection, all previous subscriptions should be cancelled - await this.rcClient.connect({}); + await this.rcClient.connection.connect(); const token = (await this.auth.getCurrentUser())?.authToken; - await this.rcClient.resume({ token }); - await this.rcClient.subscribeRoom(this.rid); - await this.rcClient.onMessage((data: any) => { + await this.rcClient.account.loginWithToken(token) + await this.rcClient.stream( + "notify-room", + `${this.rid}/user-activity`, + (...props: [string, string[]]) => { + const [username, activities] = props; + this.handleTypingEvent({ typingUser: username, isTyping: activities.includes("user-typing") }); + } + ); + + await this.rcClient.stream( + "notify-room", + `${this.rid}/deleteMessage`, + (ddpMessage: any) => { + const messageId = ddpMessage._id; + this.onMessageDeleteCallbacks.map((callback) => callback(messageId)); + } + ); + + await this.rcClient.stream("room-messages",this.rid, (data: any) => { if (!data) { return; } const message = JSON.parse(JSON.stringify(data)); if (message.ts?.$date) { - console.log(message.ts?.$date); message.ts = message.ts.$date; } if (!message.ts) { @@ -206,69 +221,6 @@ export default class EmbeddedChatApi { } this.onMessageCallbacks.map((callback) => callback(message)); }); - await this.rcClient.subscribe( - "stream-notify-room", - `${this.rid}/user-activity` - ); - await this.rcClient.onStreamData( - "stream-notify-room", - (ddpMessage: any) => { - const [roomId, event] = ddpMessage.fields.eventName.split("/"); - - if (roomId !== this.rid) { - return; - } - - if (event === "user-activity") { - const typingUser = ddpMessage.fields.args[0]; - const isTyping = ddpMessage.fields.args[1]?.includes("user-typing"); - this.handleTypingEvent({ typingUser, isTyping }); - } - - if (event === "typing") { - const typingUser = ddpMessage.fields.args[0]; - const isTyping = ddpMessage.fields.args[1]; - this.handleTypingEvent({ typingUser, isTyping }); - } - if (event === "deleteMessage") { - const messageId = ddpMessage.fields.args[0]?._id; - this.onMessageDeleteCallbacks.map((callback) => - callback(messageId) - ); - } - } - ); - await this.rcClient.subscribeNotifyUser(); - await this.rcClient.onStreamData( - "stream-notify-user", - (ddpMessage: any) => { - const [, event] = ddpMessage.fields.eventName.split("/"); - const args: any[] = ddpMessage.fields.args - ? Array.isArray(ddpMessage.fields.args) - ? ddpMessage.fields.args - : [ddpMessage.fields.args] - : []; - if (event === "message") { - const data = args[0]; - if (!data || data?.rid !== this.rid) { - return; - } - const message = JSON.parse(JSON.stringify(data)); - if (message.ts?.$date) { - message.ts = message.ts.$date; - } - if (!message.ts) { - message.ts = new Date().toISOString(); - } - message.renderType = "blocks"; - this.onMessageCallbacks.map((callback) => callback(message)); - } else if (event === "uiInteraction") { - this.onUiInteractionCallbacks.forEach((callback) => - callback(args[0]) - ); - } - } - ); } catch (err) { await this.close(); } @@ -531,15 +483,15 @@ export default class EmbeddedChatApi { }, method: "GET", }); - return await response.json(); + return response.json(); } catch (err) { console.error(err); } } async close() { - await this.rcClient.unsubscribeAll(); - await this.rcClient.disconnect(); + await this.rcClient.client.unsubscribe(this.rid); + this.rcClient.connection.close(); } /** @@ -652,18 +604,15 @@ export default class EmbeddedChatApi { const roomType = isChannelPrivate ? "groups" : "channels"; try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const roles = await fetch( - `${this.host}/api/v1/${roomType}.roles?roomId=${this.rid}`, + const roles = await this.rcClient.rest.get( + `/v1/${roomType}.roles` as any, { - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "GET", - } + roomId: this.rid, + userId: userId, + authToken: authToken, + } as any ); - return await roles.json(); + return roles; } catch (err) { console.log(err); } @@ -726,7 +675,7 @@ export default class EmbeddedChatApi { async sendTypingStatus(username: string, typing: boolean) { try { - this.rcClient.methodCall( + await this.rcClient.call( "stream-notify-room", `${this.rid}/user-activity`, username, @@ -736,6 +685,7 @@ export default class EmbeddedChatApi { console.error(err); } } + /** * @param {*} message should be a string or an rc message object @@ -1261,4 +1211,4 @@ export default class EmbeddedChatApi { const data = response.json(); return data; } -} +} \ No newline at end of file diff --git a/packages/auth/package.json b/packages/auth/package.json index 749c3b8dfb..5cf3b7c360 100644 --- a/packages/auth/package.json +++ b/packages/auth/package.json @@ -20,5 +20,9 @@ "rollup": "^3.23.0", "rollup-plugin-dts": "^6.0.1", "rollup-plugin-esbuild": "^5.0.0" + }, + "dependencies": { + "@rocket.chat/ddp-client": "0.2.27", + "@rocket.chat/emitter": "^0.31.25" } } diff --git a/packages/auth/src/IRocketChatAuthOptions.ts b/packages/auth/src/IRocketChatAuthOptions.ts index 5c146dc4cf..307a4d09e7 100644 --- a/packages/auth/src/IRocketChatAuthOptions.ts +++ b/packages/auth/src/IRocketChatAuthOptions.ts @@ -1,6 +1,9 @@ +import { DDPSDK } from "@rocket.chat/ddp-client"; + export interface IRocketChatAuthOptions { host: string; + rcClient: DDPSDK; saveToken: (token: string) => Promise; getToken: () => Promise; deleteToken: () => Promise; -} +} \ No newline at end of file diff --git a/packages/auth/src/RocketChatAuth.ts b/packages/auth/src/RocketChatAuth.ts index 0f2c55f196..1aa59e2cbb 100644 --- a/packages/auth/src/RocketChatAuth.ts +++ b/packages/auth/src/RocketChatAuth.ts @@ -5,9 +5,11 @@ import { IRocketChatAuthOptions } from "./IRocketChatAuthOptions"; import { Api, ApiError } from "./Api"; import loginWithRocketChatOAuth from "./loginWithRocketChatOAuth"; import handleSecureLogin from "./handleSecureLogin"; +import { DDPSDK } from "@rocket.chat/ddp-client"; class RocketChatAuth { host: string; api: Api; + rcClient: DDPSDK currentUser: any; lastFetched: Date; authListeners: ((user: object | null) => void)[] = []; @@ -16,12 +18,14 @@ class RocketChatAuth { getToken: () => Promise; constructor({ host, + rcClient, saveToken, getToken, deleteToken, }: IRocketChatAuthOptions) { this.host = host; this.api = new Api(host); + this.rcClient = rcClient; this.lastFetched = new Date(0); this.currentUser = null; this.getToken = getToken; @@ -59,11 +63,11 @@ class RocketChatAuth { }: { user: string; password: string; - code?: string | number; + code?: string; }) { const response = await loginWithPassword( { - api: this.api, + api: this.rcClient, }, { user, @@ -87,7 +91,7 @@ class RocketChatAuth { }) { const response = await loginWithOAuthServiceToken( { - api: this.api, + api: this.rcClient, }, credentials ); @@ -118,7 +122,7 @@ class RocketChatAuth { async loginWithResumeToken(resume: string) { const response = await loginWithResumeToken( { - api: this.api, + api: this.rcClient, }, { resume, @@ -207,7 +211,7 @@ class RocketChatAuth { */ async logout() { try { - await this.api.post(`/api/v1/logout`, undefined, { + await this.rcClient.rest.post('/v1/logout', undefined, { headers: { "X-Auth-Token": this.currentUser.authToken, "X-User-Id": this.currentUser.userId, @@ -224,4 +228,4 @@ class RocketChatAuth { } } -export default RocketChatAuth; +export default RocketChatAuth; \ No newline at end of file diff --git a/packages/auth/src/auth.ts b/packages/auth/src/auth.ts index c753541028..f10663d5d8 100644 --- a/packages/auth/src/auth.ts +++ b/packages/auth/src/auth.ts @@ -3,16 +3,18 @@ import RocketChatAuth from "./RocketChatAuth"; const rocketChatAuth = ({ host, + rcClient, saveToken, getToken, deleteToken, }: IRocketChatAuthOptions) => { return new RocketChatAuth({ host, + rcClient, saveToken, getToken, deleteToken, }); }; -export { rocketChatAuth }; +export { rocketChatAuth }; \ No newline at end of file diff --git a/packages/auth/src/loginWithOAuthServiceToken.ts b/packages/auth/src/loginWithOAuthServiceToken.ts index 64518ee44c..6fe946e93a 100644 --- a/packages/auth/src/loginWithOAuthServiceToken.ts +++ b/packages/auth/src/loginWithOAuthServiceToken.ts @@ -1,8 +1,8 @@ -import { Api } from "./Api"; +import { DDPSDK } from "@rocket.chat/ddp-client"; const loginWithOAuthServiceToken = async ( config: { - api: Api; + api: DDPSDK; }, credentials: { service: string; @@ -10,8 +10,8 @@ const loginWithOAuthServiceToken = async ( [key: string]: string; } ) => { - const response = await config.api.post("/api/v1/login", credentials); - return response.data; + const response = await config.api.rest.post("/v1/login", credentials as any); + return response }; -export default loginWithOAuthServiceToken; +export default loginWithOAuthServiceToken; \ No newline at end of file diff --git a/packages/auth/src/loginWithPassword.ts b/packages/auth/src/loginWithPassword.ts index 7843efb38e..7681565b1b 100644 --- a/packages/auth/src/loginWithPassword.ts +++ b/packages/auth/src/loginWithPassword.ts @@ -1,8 +1,8 @@ -import { Api } from "./Api"; +import { DDPSDK } from "@rocket.chat/ddp-client"; const loginWithPassword = async ( config: { - api: Api; + api: DDPSDK; }, { user, @@ -14,12 +14,12 @@ const loginWithPassword = async ( code?: string | number; } ) => { - const response = await config.api.post("/api/v1/login", { + const response = await config.api.rest.post("/v1/login", { user, password, - code, + code: code !== undefined ? String(code) : undefined, }); - return response.data; + return response; }; -export default loginWithPassword; +export default loginWithPassword; \ No newline at end of file diff --git a/packages/auth/src/loginWithResumeToken.ts b/packages/auth/src/loginWithResumeToken.ts index b9b1f2b6ea..25ea9aa3fd 100644 --- a/packages/auth/src/loginWithResumeToken.ts +++ b/packages/auth/src/loginWithResumeToken.ts @@ -1,15 +1,15 @@ -import { Api } from "./Api"; +import { DDPSDK } from "@rocket.chat/ddp-client"; const loginWithResumeToken = async ( config: { - api: Api; + api: DDPSDK; }, credentials: { resume: string; } ) => { - const response = await config.api.post("/api/v1/login", credentials); - return response.data; + const response = await config.api.rest.post("/v1/login", credentials as any); + return response; }; -export default loginWithResumeToken; +export default loginWithResumeToken; \ No newline at end of file diff --git a/packages/react/src/views/ChatInput/ChatInputFormattingToolbar.js b/packages/react/src/views/ChatInput/ChatInputFormattingToolbar.js index 5d8c20a600..d6f74212e4 100644 --- a/packages/react/src/views/ChatInput/ChatInputFormattingToolbar.js +++ b/packages/react/src/views/ChatInput/ChatInputFormattingToolbar.js @@ -194,24 +194,18 @@ const ChatInputFormattingToolbar = ({ .map((name) => formatter.find((item) => item.name === name)) .map((item) => isPopoverOpen && popOverItems.includes('formatter') ? ( - <> - { - if (isRecordingMessage) return; - handleFormatterClick(item); - }} - css={styles.popOverItemStyles} - > - - {item.name} - - + { + if (isRecordingMessage) return; + handleFormatterClick(item); + }} + css={styles.popOverItemStyles} + > + + {item.name} + ) : ( =4.8.0 <5.4.0" + bin: + typia: lib/executable/typia.js + checksum: 2e1ead0f3496099938cb45aa3fb624ff82589b50c6472be150e45a4f14a8e04c3285c1a67ba6e46be5632c7eb40aaccc564cddcf0b5205bb9e623b6ae642f94b + languageName: node + linkType: hard + "ua-parser-js@npm:^1.0.35": version: 1.0.37 resolution: "ua-parser-js@npm:1.0.37" From 1c64b3e838156e0faac3818605f928c9fbc1731d Mon Sep 17 00:00:00 2001 From: devanshkansagra <125076549+devanshkansagra@users.noreply.github.com.> Date: Mon, 9 Jun 2025 22:48:25 +0530 Subject: [PATCH 2/3] Formatted with prettier --- packages/api/src/EmbeddedChatApi.ts | 14 ++++++++------ packages/auth/src/IRocketChatAuthOptions.ts | 2 +- packages/auth/src/RocketChatAuth.ts | 6 +++--- packages/auth/src/auth.ts | 2 +- packages/auth/src/loginWithOAuthServiceToken.ts | 4 ++-- packages/auth/src/loginWithPassword.ts | 2 +- packages/auth/src/loginWithResumeToken.ts | 2 +- 7 files changed, 17 insertions(+), 15 deletions(-) diff --git a/packages/api/src/EmbeddedChatApi.ts b/packages/api/src/EmbeddedChatApi.ts index 0f0076b9da..685abbeaff 100644 --- a/packages/api/src/EmbeddedChatApi.ts +++ b/packages/api/src/EmbeddedChatApi.ts @@ -189,16 +189,19 @@ export default class EmbeddedChatApi { try { await this.rcClient.connection.connect(); const token = (await this.auth.getCurrentUser())?.authToken; - await this.rcClient.account.loginWithToken(token) + await this.rcClient.account.loginWithToken(token); await this.rcClient.stream( "notify-room", `${this.rid}/user-activity`, (...props: [string, string[]]) => { const [username, activities] = props; - this.handleTypingEvent({ typingUser: username, isTyping: activities.includes("user-typing") }); + this.handleTypingEvent({ + typingUser: username, + isTyping: activities.includes("user-typing"), + }); } ); - + await this.rcClient.stream( "notify-room", `${this.rid}/deleteMessage`, @@ -208,7 +211,7 @@ export default class EmbeddedChatApi { } ); - await this.rcClient.stream("room-messages",this.rid, (data: any) => { + await this.rcClient.stream("room-messages", this.rid, (data: any) => { if (!data) { return; } @@ -685,7 +688,6 @@ export default class EmbeddedChatApi { console.error(err); } } - /** * @param {*} message should be a string or an rc message object @@ -1211,4 +1213,4 @@ export default class EmbeddedChatApi { const data = response.json(); return data; } -} \ No newline at end of file +} diff --git a/packages/auth/src/IRocketChatAuthOptions.ts b/packages/auth/src/IRocketChatAuthOptions.ts index 307a4d09e7..7e42ab284e 100644 --- a/packages/auth/src/IRocketChatAuthOptions.ts +++ b/packages/auth/src/IRocketChatAuthOptions.ts @@ -6,4 +6,4 @@ export interface IRocketChatAuthOptions { saveToken: (token: string) => Promise; getToken: () => Promise; deleteToken: () => Promise; -} \ No newline at end of file +} diff --git a/packages/auth/src/RocketChatAuth.ts b/packages/auth/src/RocketChatAuth.ts index 1aa59e2cbb..7d5075da53 100644 --- a/packages/auth/src/RocketChatAuth.ts +++ b/packages/auth/src/RocketChatAuth.ts @@ -9,7 +9,7 @@ import { DDPSDK } from "@rocket.chat/ddp-client"; class RocketChatAuth { host: string; api: Api; - rcClient: DDPSDK + rcClient: DDPSDK; currentUser: any; lastFetched: Date; authListeners: ((user: object | null) => void)[] = []; @@ -211,7 +211,7 @@ class RocketChatAuth { */ async logout() { try { - await this.rcClient.rest.post('/v1/logout', undefined, { + await this.rcClient.rest.post("/v1/logout", undefined, { headers: { "X-Auth-Token": this.currentUser.authToken, "X-User-Id": this.currentUser.userId, @@ -228,4 +228,4 @@ class RocketChatAuth { } } -export default RocketChatAuth; \ No newline at end of file +export default RocketChatAuth; diff --git a/packages/auth/src/auth.ts b/packages/auth/src/auth.ts index f10663d5d8..d1c08c1d5e 100644 --- a/packages/auth/src/auth.ts +++ b/packages/auth/src/auth.ts @@ -17,4 +17,4 @@ const rocketChatAuth = ({ }); }; -export { rocketChatAuth }; \ No newline at end of file +export { rocketChatAuth }; diff --git a/packages/auth/src/loginWithOAuthServiceToken.ts b/packages/auth/src/loginWithOAuthServiceToken.ts index 6fe946e93a..1ec9ae6900 100644 --- a/packages/auth/src/loginWithOAuthServiceToken.ts +++ b/packages/auth/src/loginWithOAuthServiceToken.ts @@ -11,7 +11,7 @@ const loginWithOAuthServiceToken = async ( } ) => { const response = await config.api.rest.post("/v1/login", credentials as any); - return response + return response; }; -export default loginWithOAuthServiceToken; \ No newline at end of file +export default loginWithOAuthServiceToken; diff --git a/packages/auth/src/loginWithPassword.ts b/packages/auth/src/loginWithPassword.ts index 7681565b1b..099d826340 100644 --- a/packages/auth/src/loginWithPassword.ts +++ b/packages/auth/src/loginWithPassword.ts @@ -22,4 +22,4 @@ const loginWithPassword = async ( return response; }; -export default loginWithPassword; \ No newline at end of file +export default loginWithPassword; diff --git a/packages/auth/src/loginWithResumeToken.ts b/packages/auth/src/loginWithResumeToken.ts index 25ea9aa3fd..6fc406aece 100644 --- a/packages/auth/src/loginWithResumeToken.ts +++ b/packages/auth/src/loginWithResumeToken.ts @@ -12,4 +12,4 @@ const loginWithResumeToken = async ( return response; }; -export default loginWithResumeToken; \ No newline at end of file +export default loginWithResumeToken; From 65bd0043e5973435ac62c97f075002305701d316 Mon Sep 17 00:00:00 2001 From: devanshkansagra <125076549+devanshkansagra@users.noreply.github.com.> Date: Tue, 17 Jun 2025 16:51:13 +0530 Subject: [PATCH 3/3] Update REST API methods which were supported by DDPSDK, rest were unchanged --- packages/api/src/EmbeddedChatApi.ts | 529 ++++++++----------- packages/e2e-react/src/App.tsx | 7 +- packages/react/src/hooks/useFetchChatData.js | 5 +- packages/react/src/views/EmbeddedChat.js | 8 +- 4 files changed, 235 insertions(+), 314 deletions(-) diff --git a/packages/api/src/EmbeddedChatApi.ts b/packages/api/src/EmbeddedChatApi.ts index 685abbeaff..813561b55e 100644 --- a/packages/api/src/EmbeddedChatApi.ts +++ b/packages/api/src/EmbeddedChatApi.ts @@ -353,32 +353,25 @@ export default class EmbeddedChatApi { async updateUserNameThroughSuggestion(userid: string) { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch( - `${this.host}/api/v1/users.getUsernameSuggestion`, + const response: any = await this.rcClient.rest.get( + "/v1/users.getUsernameSuggestion" as any, { - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "GET", - } + authToken, + userId, + } as any ); - const suggestedUsername = await response.json(); - - if (suggestedUsername.success) { - const response2 = await fetch(`${this.host}/api/v1/users.update`, { - body: `{"userId": "${userid}", "data": { "username": "${suggestedUsername.result}" }}`, - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "POST", - }); - - return await response2.json(); + if (response.success) { + const response2 = await this.rcClient.rest.post( + `/v1/users.update` as any, + { userId: userid, data: { username: response.result } }, + { + authToken, + userId, + } as any + ); + + return response2; } } catch (error) { console.error(error); @@ -393,17 +386,16 @@ export default class EmbeddedChatApi { if (usernameRegExp.test(newUserName)) { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch(`${this.host}/api/v1/users.update`, { - body: `{"userId": "${userid}", "data": { "username": "${newUserName}" }}`, - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "POST", - }); + const response = await this.rcClient.rest.post( + `/v1/users.update` as any, + { userId: userid, data: { username: newUserName } }, + { + authToken, + userId, + } as any + ); - const result = await response.json(); + const result: any = response; if ( !result.success && @@ -423,18 +415,17 @@ export default class EmbeddedChatApi { async channelInfo() { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch( - `${this.host}/api/v1/rooms.info?roomId=${this.rid}`, + + const response = await this.rcClient.rest.get( + "/v1/rooms.info" as any, + { roomId: this.rid }, { - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "GET", - } + userId: userId, + authToken: authToken, + } as any ); - return await response.json(); + + return response; } catch (err) { console.error(err); } @@ -478,17 +469,17 @@ export default class EmbeddedChatApi { async permissionInfo() { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch(`${this.host}/api/v1/permissions.listAll`, { - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "GET", - }); - return response.json(); + + const response = await this.rcClient.rest.get( + "/v1/permissions.listAll" as any, + { + authToken: authToken, + userId: userId, + } as any + ); + return response; } catch (err) { - console.error(err); + console.log(err); } } @@ -517,26 +508,17 @@ export default class EmbeddedChatApi { ) { const roomType = isChannelPrivate ? "groups" : "channels"; const endp = anonymousMode ? "anonymousread" : "messages"; - const query = options?.query - ? `&query=${JSON.stringify(options.query)}` - : ""; - const field = options?.field - ? `&field=${JSON.stringify(options.field)}` - : ""; try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const messages = await fetch( - `${this.host}/api/v1/${roomType}.${endp}?roomId=${this.rid}${query}${field}`, + const messages = await this.rcClient.rest.get( + `/v1/${roomType}.${endp}` as any, + { roomId: this.rid, query: options.query, field: options.field }, { - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "GET", - } + authToken, + userId, + } as any ); - return await messages.json(); + return messages; } catch (err) { console.log(err); } @@ -566,18 +548,15 @@ export default class EmbeddedChatApi { const offset = options?.offset ? options.offset : 0; try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const messages = await fetch( - `${this.host}/api/v1/${roomType}.${endp}?roomId=${this.rid}${query}${field}&offset=${offset}`, + const messages = await this.rcClient.rest.get( + `/v1/${roomType}.${endp}` as any, + { roomId: this.rid, query, field, offset: offset }, { - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "GET", - } + authToken, + userId, + } as any ); - return await messages.json(); + return messages; } catch (err) { console.log(err); } @@ -586,18 +565,15 @@ export default class EmbeddedChatApi { async getThreadMessages(tmid: string, isChannelPrivate = false) { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const messages = await fetch( - `${this.host}/api/v1/chat.getThreadMessages?roomId=${this.rid}&tmid=${tmid}`, + const messages = await this.rcClient.rest.get( + `/v1/chat.getThreadMessages` as any, + { roomId: this.rid, tmid: tmid }, { - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "GET", - } + authToken, + userId, + } as any ); - return await messages.json(); + return messages; } catch (err) { console.log(err); } @@ -609,8 +585,8 @@ export default class EmbeddedChatApi { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; const roles = await this.rcClient.rest.get( `/v1/${roomType}.roles` as any, + { roomId: this.rid }, { - roomId: this.rid, userId: userId, authToken: authToken, } as any @@ -624,23 +600,19 @@ export default class EmbeddedChatApi { async getUsersInRole(role: string) { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const roles = await fetch( - `${this.host}/api/v1/roles.getUsersInRole?role=${role}`, - { - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "GET", - } + + const roles = await this.rcClient.rest.get( + "/v1/roles.getUsersInRole" as any, + { role }, + { authToken, userId } as any ); - return await roles.json(); + return roles; } catch (err) { console.log(err); } } + // Not Working in DDPSDK's REST API async getUserRoles() { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; @@ -709,16 +681,17 @@ export default class EmbeddedChatApi { } try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch(`${this.host}/api/v1/chat.sendMessage`, { - body: JSON.stringify({ message: messageObj }), - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "POST", - }); - return await response.json(); + + const response = await this.rcClient.rest.post( + "/v1/chat.sendMessage" as any, + { message: messageObj }, + { + authToken, + userId, + } as any + ); + + return response; } catch (err) { console.error(err); } @@ -727,16 +700,16 @@ export default class EmbeddedChatApi { async deleteMessage(msgId: string) { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch(`${this.host}/api/v1/chat.delete`, { - body: `{"roomId": "${this.rid}", "msgId": "${msgId}"}`, - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "POST", - }); - return await response.json(); + + const response = await this.rcClient.rest.post( + "/v1/chat.delete" as any, + { roomId: this.rid, msgId: msgId, asUser: true }, + { + authToken, + userId, + } as any + ); + return response; } catch (err) { console.error(err); } @@ -745,21 +718,22 @@ export default class EmbeddedChatApi { async updateMessage(msgId: string, text: string) { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch(`${this.host}/api/v1/chat.update`, { - body: `{"roomId": "${this.rid}", "msgId": "${msgId}","text" : "${text}" }`, - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "POST", - }); - return await response.json(); + + const response = await this.rcClient.rest.post( + "/v1/chat.update" as any, + { roomId: this.rid, msgId: msgId, text }, + { + authToken, + userId, + } as any + ); + return response; } catch (err) { console.error(err); } } + // Not Working in DDPSDK's REST API async getAllFiles(isChannelPrivate = false, typeGroup: string) { const roomType = isChannelPrivate ? "groups" : "channels"; try { @@ -785,18 +759,15 @@ export default class EmbeddedChatApi { async getAllImages() { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch( - `${this.host}/api/v1/rooms.images?roomId=${this.rid}`, + const response = await this.rcClient.rest.get( + `/v1/rooms.images` as any, + { roomId: this.rid }, { - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "GET", - } + authToken, + userId, + } as any ); - return await response.json(); + return response; } catch (err) { console.error(err); } @@ -805,16 +776,12 @@ export default class EmbeddedChatApi { async starMessage(mid: string) { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch(`${this.host}/api/v1/chat.starMessage`, { - body: `{"messageId": "${mid}"}`, - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "POST", - }); - return await response.json(); + const response = await this.rcClient.rest.post( + `/v1/chat.starMessage` as any, + { messageId: mid }, + { authToken, userId } as any + ); + return response; } catch (err) { console.error(err); } @@ -823,16 +790,12 @@ export default class EmbeddedChatApi { async unstarMessage(mid: string) { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch(`${this.host}/api/v1/chat.unStarMessage`, { - body: `{"messageId": "${mid}"}`, - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "POST", - }); - return await response.json(); + const response = await this.rcClient.rest.post( + `/v1/chat.unStarMessage` as any, + { messageId: mid }, + { authToken, userId } as any + ); + return response; } catch (err) { console.error(err); } @@ -841,18 +804,15 @@ export default class EmbeddedChatApi { async getStarredMessages() { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch( - `${this.host}/api/v1/chat.getStarredMessages?roomId=${this.rid}`, + const response = await this.rcClient.rest.get( + `/v1/chat.getStarredMessages` as any, + { roomId: this.rid }, { - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "GET", - } + authToken, + userId, + } as any ); - return await response.json(); + return response; } catch (err) { console.error(err); } @@ -861,18 +821,15 @@ export default class EmbeddedChatApi { async getPinnedMessages() { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch( - `${this.host}/api/v1/chat.getPinnedMessages?roomId=${this.rid}`, + const response = await this.rcClient.rest.get( + `/v1/chat.getPinnedMessages` as any, + { roomId: this.rid }, { - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "GET", - } + authToken, + userId, + } as any ); - return await response.json(); + return response; } catch (err) { console.error(err); } @@ -881,18 +838,15 @@ export default class EmbeddedChatApi { async getMentionedMessages() { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch( - `${this.host}/api/v1/chat.getMentionedMessages?roomId=${this.rid}`, + const response = await this.rcClient.rest.get( + `/v1/chat.getMentionedMessages` as any, + { roomId: this.rid }, { - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "GET", - } + authToken, + userId, + } as any ); - return await response.json(); + return response; } catch (err) { console.error(err); } @@ -901,16 +855,16 @@ export default class EmbeddedChatApi { async pinMessage(mid: string) { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch(`${this.host}/api/v1/chat.pinMessage`, { - body: `{"messageId": "${mid}"}`, - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "POST", - }); - return await response.json(); + + const response = await this.rcClient.rest.post( + `/v1/chat.pinMessage` as any, + { messageId: mid }, + { + authToken, + userId, + } as any + ); + return response; } catch (err) { return { error: err, @@ -921,16 +875,15 @@ export default class EmbeddedChatApi { async unpinMessage(mid: string) { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch(`${this.host}/api/v1/chat.unPinMessage`, { - body: `{"messageId": "${mid}"}`, - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "POST", - }); - return await response.json(); + const response = await this.rcClient.rest.post( + `/v1/chat.unPinMessage` as any, + { messageId: mid }, + { + authToken, + userId, + } as any + ); + return response; } catch (err) { console.error(err); } @@ -939,16 +892,16 @@ export default class EmbeddedChatApi { async reactToMessage(emoji: string, messageId: string, shouldReact: string) { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch(`${this.host}/api/v1/chat.react`, { - body: `{"messageId": "${messageId}", "emoji": "${emoji}", "shouldReact": ${shouldReact}}`, - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, + const response = await this.rcClient.rest.post( + `/v1/chat.react` as any, + { + messageId: messageId, + emoji: emoji, + shouldReact: shouldReact, }, - method: "POST", - }); - return await response.json(); + { authToken, userId } as any + ); + return response; } catch (err) { console.error(err); } @@ -957,16 +910,15 @@ export default class EmbeddedChatApi { async reportMessage(messageId: string, description: string) { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch(`${this.host}/api/v1/chat.reportMessage`, { - body: `{"messageId": "${messageId}", "description": "${description}"}`, - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, + const response = await this.rcClient.rest.post( + `/v1/chat.reportMessage` as any, + { + messageId: messageId, + description: description, }, - method: "POST", - }); - return await response.json(); + { authToken, userId } as any + ); + return response; } catch (err) { console.error(err); } @@ -975,21 +927,18 @@ export default class EmbeddedChatApi { async findOrCreateInvite() { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch(`${this.host}/api/v1/findOrCreateInvite`, { - method: "POST", - body: JSON.stringify({ rid: this.rid, days: 1, maxUses: 10 }), - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - }); - return await response.json(); + const response = await this.rcClient.rest.post( + `${this.host}/api/v1/findOrCreateInvite` as any, + { rid: this.rid, days: 1, maxUses: 10 }, + { authToken, userId } as any + ); + return response; } catch (err) { console.log(err); } } + // Not Working in DDPSDK's REST API async sendAttachment( file: File, fileName: string, @@ -1024,20 +973,17 @@ export default class EmbeddedChatApi { async me() { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch(`${this.host}/api/v1/me`, { - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "GET", - }); - return await response.json(); + const response = await this.rcClient.rest.get(`/v1/me`, { + authToken, + userId, + } as any); + return response; } catch (err) { console.error(err); } } + // Not Working in DDPSDK's REST API async getChannelMembers(isChannelPrivate = false) { const roomType = isChannelPrivate ? "groups" : "channels"; try { @@ -1062,18 +1008,12 @@ export default class EmbeddedChatApi { async getSearchMessages(text: string) { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch( - `${this.host}/api/v1/chat.search?roomId=${this.rid}&searchText=${text}`, - { - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "GET", - } + const response = await this.rcClient.rest.get( + `/v1/chat.search` as any, + { roomId: this.rid, searchText: text }, + { authToken, userId } as any ); - return await response.json(); + return response; } catch (err) { console.error(err); } @@ -1082,18 +1022,12 @@ export default class EmbeddedChatApi { async getMessageLimit() { try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch( - `${this.host}/api/v1/settings/Message_MaxAllowedSize`, - { - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - method: "GET", - } + + const response = await this.rcClient.rest.get( + `/v1/settings/Message_MaxAllowedSize` as any, + { authToken, userId } as any ); - return await response.json(); + return response; } catch (err) { console.error(err); } @@ -1129,6 +1063,7 @@ export default class EmbeddedChatApi { } } + // Not Working in DDPSDK's REST API async getCommandsList() { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; const response = await fetch(`${this.host}/api/v1/commands.list`, { @@ -1143,6 +1078,7 @@ export default class EmbeddedChatApi { return data; } + // Not Working in DDPSDK's REST API async execCommand({ command, params }: { command: string; params: string }) { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; const response = await fetch(`${this.host}/api/v1/commands.run`, { @@ -1165,52 +1101,33 @@ export default class EmbeddedChatApi { async getUserStatus(reqUserId: string) { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch( - `${this.host}/api/v1/users.getStatus?userId=${reqUserId}`, - { - method: "GET", - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - } + + const response = await this.rcClient.rest.get( + "/v1/users.getStatus" as any, + { userId: reqUserId }, + { authToken, userId } as any ); - const data = response.json(); - return data; + return response; } async userInfo(reqUserId: string) { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch( - `${this.host}/api/v1/users.info?userId=${reqUserId}`, - { - method: "GET", - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - } + const response = await this.rcClient.rest.get( + "/v1/users.info" as any, + { userId: reqUserId }, + { authToken, userId } as any ); - const data = response.json(); - return data; + return response; } async userData(username: string) { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; - const response = await fetch( - `${this.host}/api/v1/users.info?username=${username}`, - { - method: "GET", - headers: { - "Content-Type": "application/json", - "X-Auth-Token": authToken, - "X-User-Id": userId, - }, - } + + const response = await this.rcClient.rest.get( + "/v1/users.info" as any, + { username }, + { authToken, userId } as any ); - const data = response.json(); - return data; + return response; } } diff --git a/packages/e2e-react/src/App.tsx b/packages/e2e-react/src/App.tsx index d0f16785a3..efb88c1aa8 100644 --- a/packages/e2e-react/src/App.tsx +++ b/packages/e2e-react/src/App.tsx @@ -2,12 +2,7 @@ import { EmbeddedChat } from "@embeddedchat/react"; function App() { - return ( - - ); + return ; } export default App; diff --git a/packages/react/src/hooks/useFetchChatData.js b/packages/react/src/hooks/useFetchChatData.js index 2078fdf05d..fcf80c39f0 100644 --- a/packages/react/src/hooks/useFetchChatData.js +++ b/packages/react/src/hooks/useFetchChatData.js @@ -147,7 +147,10 @@ const useFetchChatData = (showRoles) => { } if (showRoles) { - const { roles } = await RCInstance.getChannelRoles(isChannelPrivate); + const channelRolesRes = await RCInstance.getChannelRoles( + isChannelPrivate + ); + const roles = channelRolesRes?.roles || []; const fetchedRoles = await RCInstance.getUserRoles(); const fetchedAdmins = fetchedRoles?.result; diff --git a/packages/react/src/views/EmbeddedChat.js b/packages/react/src/views/EmbeddedChat.js index f3b94c7b48..afe4175101 100644 --- a/packages/react/src/views/EmbeddedChat.js +++ b/packages/react/src/views/EmbeddedChat.js @@ -124,8 +124,14 @@ const EmbeddedChat = (props) => { setIsLoginIn(true); try { await RCInstance.autoLogin(auth); + + // Todo: Fix error 401 when fetching permissions List on autologin + + // const permissions = await RCInstance.permissionInfo(); + // setUserPinPermissions(permissions.update[150]); + // setEditMessagePermissions(permissions.update[28]); } catch (error) { - console.error(error); + console.log(error); } finally { setIsLoginIn(false); }