Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions packages/api/src/EmbeddedChatApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)[];
Expand All @@ -34,6 +35,7 @@ export default class EmbeddedChatApi {
useSsl: !/http:\/\//.test(host),
reopen: 20000,
});
this.roomRoute = null;
this.onMessageCallbacks = [];
this.onMessageDeleteCallbacks = [];
this.onTypingStatusCallbacks = [];
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.
Expand All @@ -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)}`
Expand Down Expand Up @@ -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)}`
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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(
Expand Down
21 changes: 17 additions & 4 deletions packages/react/src/views/ChatHeader/ChatHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,14 @@ const ChatHeader = ({
} finally {
setIsUserAuthenticated(false);
}
}, [RCInstance, setIsUserAuthenticated]);
}, [
RCInstance,
setChannelInfo,
setIsUserAuthenticated,
setMessages,
setShowSidebar,
setUserAvatarUrl,
]);

useEffect(() => {
const getMessageLimit = async () => {
Expand Down Expand Up @@ -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 (
Expand All @@ -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',
Expand All @@ -230,7 +241,9 @@ const ChatHeader = ({
authenticatedUserId,
setMessageLimit,
workspaceLevelRoles,
setIsChannelArchived,
setIsChannelReadOnly,
setIsRoomTeam,
]);

const options = useMemo(
Expand Down