Skip to content

Commit

Permalink
fixed read only channel issue
Browse files Browse the repository at this point in the history
added error info when no permission
  • Loading branch information
Spiral-Memory committed Feb 7, 2024
1 parent 3fef94b commit 35d95e0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
20 changes: 20 additions & 0 deletions packages/api/src/EmbeddedChatApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,26 @@ 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 await response.json();
} catch (err) {
console.error(err);
}
}

async close() {
await this.rcClient.unsubscribeAll();
await this.rcClient.disconnect();
Expand Down
37 changes: 33 additions & 4 deletions packages/react/src/components/ChatHeader/ChatHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ const ChatHeader = ({
const setShowAllThreads = useThreadsMessageStore((state => state.setShowAllThreads));
const toastPosition = useToastStore((state) => state.position);

const setCanSendMsg = useUserStore((state) => state.setCanSendMsg);
const authenticatedUserId = useUserStore(state => state.userId);

const handleLogout = useCallback(async () => {
try {
Expand Down Expand Up @@ -121,7 +123,9 @@ const ChatHeader = ({
if (res.success) {
setChannelInfo(res.room);
if (res.room.t === 'p') setIsChannelPrivate(true);
} else {
if (res.room.ro) setMessageAllowed();
}
else {
if ('errorType' in res && res.errorType === 'error-room-not-found') {
dispatchToastMessage({
type: 'error',
Expand All @@ -130,13 +134,38 @@ const ChatHeader = ({
});
await RCInstance.logout();
}
else if ('errorType' in res && res.errorType === 'Not Allowed') {
dispatchToastMessage({
type: 'error',
message: "You don't have permission to access this channel. Logging out",
position: toastPosition,
});
await RCInstance.logout();
}
}

};

const setMessageAllowed = async () => {
const permissionRes = await RCInstance.permissionInfo();
const channelRolesRes = await RCInstance.getChannelRoles(isChannelPrivate);

if (permissionRes.success && channelRolesRes.success) {

const postMsgRoles = permissionRes.update[140]?.roles || [];

const userRoles = channelRolesRes.roles
.filter(chRole => chRole.u?._id === authenticatedUserId)
.flatMap(chRole => chRole.roles);

const canSendMsg = userRoles.length > 0 && postMsgRoles.some(role => userRoles.includes(role));
setCanSendMsg(canSendMsg);
}
}

if (isUserAuthenticated) {
getChannelInfo();
}
}, [isUserAuthenticated, RCInstance, setChannelInfo, setIsChannelPrivate]);
}, [isUserAuthenticated, RCInstance, setChannelInfo, setIsChannelPrivate, authenticatedUserId]);

const menuOptions = useMemo(() => {
const options = [];
Expand Down Expand Up @@ -324,7 +353,7 @@ const ChatHeader = ({
)}

{!isThreadOpen && filtered && (
<DynamicHeader title={headerTitle} iconName={headerTitle && headerTitle.includes('Pin') ? 'pin' : 'star'} />
<DynamicHeader title={headerTitle} iconName={headerTitle && headerTitle.includes('Pin') ? 'pin' : 'star'} />
)}
</Box>
);
Expand Down
5 changes: 3 additions & 2 deletions packages/react/src/components/ChatInput/ChatInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const ChatInput = ({ scrollToBottom }) => {
const isUserAuthenticated = useUserStore(
(state) => state.isUserAuthenticated
);
const canSendMsg = useUserStore((state)=>state.canSendMsg);

const setIsUserAuthenticated = useUserStore(
(state) => state.setIsUserAuthenticated
Expand Down Expand Up @@ -455,8 +456,8 @@ const ChatInput = ({ scrollToBottom }) => {
>
<textarea
rows={1}
disabled={!isUserAuthenticated || isRecordingMessage}
placeholder={isUserAuthenticated ? 'Message' : 'Sign in to chat'}
disabled={!isUserAuthenticated || !canSendMsg || isRecordingMessage}
placeholder={isUserAuthenticated && canSendMsg? 'Message' : isUserAuthenticated ? 'This room is read only' : 'Sign in to chat'}
className={styles.textInput}
onChange={onTextChange}
onKeyUp={showCommands}
Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/store/userStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ const useUserStore = create((set) => ({
};
}),
isUserAuthenticated: false,
canSendMsg: true,
setIsUserAuthenticated: (isUserAuthenticated) =>
set(() => ({ isUserAuthenticated })),
setCanSendMsg: (canSendMsg) =>
set(() => ({ canSendMsg })),
password: null,
setPassword: (password) => set(() => ({ password })),
emailoruser: null,
Expand Down

0 comments on commit 35d95e0

Please sign in to comment.