diff --git a/packages/api/src/EmbeddedChatApi.ts b/packages/api/src/EmbeddedChatApi.ts index 88eb4c23c..9c9467e0e 100644 --- a/packages/api/src/EmbeddedChatApi.ts +++ b/packages/api/src/EmbeddedChatApi.ts @@ -13,6 +13,7 @@ export default class EmbeddedChatApi { host: string; rid: string; rcClient: Rocketchat; + roomRoute: "channels" | "groups" | null; onMessageCallbacks: ((message: any) => void)[]; onMessageDeleteCallbacks: ((messageId: string) => void)[]; onTypingStatusCallbacks: ((users: string[]) => void)[]; @@ -34,6 +35,7 @@ export default class EmbeddedChatApi { useSsl: !/http:\/\//.test(host), reopen: 20000, }); + this.roomRoute = null; this.onMessageCallbacks = []; this.onMessageDeleteCallbacks = []; this.onTypingStatusCallbacks = []; @@ -485,7 +487,13 @@ export default class EmbeddedChatApi { method: "GET", } ); - return await response.json(); + const roomInfo = await response.json(); + + if (roomInfo?.success && roomInfo?.room?.t) { + this.roomRoute = this.getRoomRoute(roomInfo.room.t); + } + + return roomInfo; } catch (err) { console.error(err); } @@ -521,6 +529,28 @@ export default class EmbeddedChatApi { await this.rcClient.disconnect(); } + getRoomRoute(roomType: string) { + return roomType === "p" ? "groups" : "channels"; + } + + async resolveRoomRoute(isChannelPrivate = false) { + if (this.roomRoute) { + return this.roomRoute; + } + + try { + const roomInfo = await this.channelInfo(); + + if (roomInfo?.success && roomInfo?.room?.t) { + return this.getRoomRoute(roomInfo.room.t); + } + } catch (err) { + console.error(err); + } + + return isChannelPrivate ? "groups" : "channels"; + } + /** * @param {boolean} anonymousMode * @param {Object} options This object should include query or fields. @@ -539,7 +569,7 @@ export default class EmbeddedChatApi { }, isChannelPrivate = false ) { - const roomType = isChannelPrivate ? "groups" : "channels"; + const roomType = await this.resolveRoomRoute(isChannelPrivate); const endp = anonymousMode ? "anonymousread" : "messages"; const query = options?.query ? `&query=${JSON.stringify(options.query)}` @@ -579,7 +609,7 @@ export default class EmbeddedChatApi { }, isChannelPrivate = false ) { - const roomType = isChannelPrivate ? "groups" : "channels"; + const roomType = await this.resolveRoomRoute(isChannelPrivate); const endp = anonymousMode ? "anonymousread" : "messages"; const query = options?.query ? `&query=${JSON.stringify(options.query)}` @@ -628,7 +658,7 @@ export default class EmbeddedChatApi { } async getChannelRoles(isChannelPrivate = false) { - const roomType = isChannelPrivate ? "groups" : "channels"; + const roomType = await this.resolveRoomRoute(isChannelPrivate); try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; const roles = await fetch( @@ -766,7 +796,7 @@ export default class EmbeddedChatApi { } async getAllFiles(isChannelPrivate = false, typeGroup: string) { - const roomType = isChannelPrivate ? "groups" : "channels"; + const roomType = await this.resolveRoomRoute(isChannelPrivate); try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; const url = @@ -1075,7 +1105,7 @@ export default class EmbeddedChatApi { } async getChannelMembers(isChannelPrivate = false) { - const roomType = isChannelPrivate ? "groups" : "channels"; + const roomType = await this.resolveRoomRoute(isChannelPrivate); try { const { userId, authToken } = (await this.auth.getCurrentUser()) || {}; const response = await fetch( diff --git a/packages/react/src/views/ChatHeader/ChatHeader.js b/packages/react/src/views/ChatHeader/ChatHeader.js index a310e538f..107b184c8 100644 --- a/packages/react/src/views/ChatHeader/ChatHeader.js +++ b/packages/react/src/views/ChatHeader/ChatHeader.js @@ -146,7 +146,14 @@ const ChatHeader = ({ } finally { setIsUserAuthenticated(false); } - }, [RCInstance, setIsUserAuthenticated]); + }, [ + RCInstance, + setChannelInfo, + setIsUserAuthenticated, + setMessages, + setShowSidebar, + setUserAvatarUrl, + ]); useEffect(() => { const getMessageLimit = async () => { @@ -182,10 +189,11 @@ const ChatHeader = ({ const res = await RCInstance.channelInfo(); if (res.success) { setChannelInfo(res.room); - if (res.room.t === 'p') setIsChannelPrivate(true); - if (res.room?.teamMain) setIsRoomTeam(true); + setIsChannelPrivate(res.room.t === 'p'); + setIsRoomTeam(Boolean(res.room?.teamMain)); + setIsChannelArchived(false); + setIsChannelReadOnly(Boolean(res.room.ro)); if (res.room.ro) { - setIsChannelReadOnly(true); setMessageAllowed(); } } else if ( @@ -205,6 +213,9 @@ const ChatHeader = ({ const roomInfo = await RCInstance.getRoomInfo(); const roomData = roomInfo.result[roomInfo.result.length - 1]; setChannelInfo(roomData); + setIsChannelPrivate(roomData?.t === 'p'); + setIsRoomTeam(Boolean(roomData?.teamMain)); + setIsChannelReadOnly(Boolean(roomData?.ro)); } else if ('errorType' in res && res.errorType === 'Not Allowed') { dispatchToastMessage({ type: 'error', @@ -230,7 +241,9 @@ const ChatHeader = ({ authenticatedUserId, setMessageLimit, workspaceLevelRoles, + setIsChannelArchived, setIsChannelReadOnly, + setIsRoomTeam, ]); const options = useMemo(