Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): send no mark-as-read from a read-only key

Yudhi Armyndharisa940a1e83e38
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.ts
index 62731c73a..2320d1a97 100644
--- a/dashboard/src/pages/Chats.test.ts
+++ b/dashboard/src/pages/Chats.test.ts
@@ -626,6 +626,62 @@ test('a read-only key is offered no status compose trigger', async () => {
}
});
+test('a writer key opening a chat clears its unread badge', async () => {
+ const { screen, fireEvent, within, waitFor } = rtl;
+ const { container } = renderChats();
+ await screen.findByText('Main (15551234567)');
+ assert.ok(await screen.findByLabelText('2 unread messages'), 'the fixture chat shows no unread badge');
+ fireEvent.click(await screen.findByText('Alice'));
+ await within(container.querySelector('.room-messages') as HTMLElement).findByText('hello from alice');
+ await waitFor(() => assert.ok(!screen.queryByLabelText('2 unread messages'), 'opening the chat kept its badge'));
+});
+
+test('a read-only key opening a chat sends no mark-as-read', async () => {
+ const { screen, fireEvent, within, waitFor } = rtl;
+ resetFetchCalls();
+ window.localStorage.setItem('openwa_user_role', 'viewer');
+ try {
+ const { container } = renderChats();
+ await screen.findByText('Main (15551234567)');
+ fireEvent.click(await screen.findByText('Alice'));
+ await within(container.querySelector('.room-messages') as HTMLElement).findByText('hello from alice');
+ // Past the mark-as-read quiet window, so a queued call would have gone out.
+ await new Promise(resolve => setTimeout(resolve, 1_000));
+ assert.ok(!findFetchCall('POST', `/api/sessions/${SESSION.id}/chats/read`), 'a viewer key marked the chat read');
+ // The chat is still unread on the gateway, so the sidebar badge keeps its count.
+ assert.ok(
+ screen.queryByLabelText('2 unread messages'),
+ 'the unread badge was cleared for a chat never marked read',
+ );
+ // A message arriving in the open chat is unread on the gateway too, so it counts.
+ const socket = lastSocket();
+ assert.ok(socket, 'expected the page to have opened a socket');
+ socket.receive('message', {
+ type: 'event',
+ timestamp: new Date(1_700_002_000_000).toISOString(),
+ payload: {
+ event: 'message.received',
+ sessionId: SESSION.id,
+ data: {
+ id: 'wamid.live.viewer',
+ chatId: CHAT.id,
+ from: CHAT.id,
+ to: 'me',
+ body: 'second from alice',
+ type: 'text',
+ fromMe: false,
+ timestamp: 1_700_001_500,
+ },
+ },
+ });
+ await waitFor(() =>
+ assert.ok(screen.queryByLabelText('3 unread messages'), 'the open chat did not count the arrival'),
+ );
+ } finally {
+ window.localStorage.setItem('openwa_user_role', 'admin');
+ }
+});
+
// Stage a file in the open room and wait for the preview banner. A non-image type is used on
// purpose: the image branch calls URL.createObjectURL, which JSDOM does not implement.
//
diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsx
index 444517904..86bb45d5d 100644
--- a/dashboard/src/pages/Chats.tsx
+++ b/dashboard/src/pages/Chats.tsx
@@ -35,6 +35,7 @@ import {
import { useWebSocket } from '../hooks/useWebSocket';
import { useDocumentTitle } from '../hooks/useDocumentTitle';
import { useToast } from '../hooks/useToast';
+import { useRole } from '../hooks/useRole';
import { PageHeader } from '../components/PageHeader';
import { GlobalSearch } from '../components/GlobalSearch';
import {
@@ -120,6 +121,7 @@ export function Chats() {
const { t } = useTranslation();
useDocumentTitle(t('nav.chats'));
const { error: showErrorToast, warning: showWarningToast } = useToast();
+ const { canWrite } = useRole();
// Sessions list & active session
const [sessions, setSessions] = useState<Session[]>([]);
@@ -344,11 +346,12 @@ export function Chats() {
// where those queued reads belong.
useEffect(() => () => markReadCoalescer.flush(), [markReadCoalescer]);
+ // Marking a chat read is an operator write; a read-only key would only collect 403 toasts.
const markChatRead = useCallback(
(chatId: string) => {
- markReadCoalescer.call(chatId);
+ if (canWrite) markReadCoalescer.call(chatId);
},
- [markReadCoalescer],
+ [markReadCoalescer, canWrite],
);
// 3. WebSocket integration for real-time messages
@@ -394,7 +397,8 @@ export function Chats() {
let needsSidebarRefetch = false;
setChats(prevChats => {
const result = applyIncomingToChatList(prevChats, newMsg, {
- activeChatId: activeChat?.id,
+ // Only a chat this key marks read is exempt from the unread count (see markChatRead).
+ activeChatId: canWrite ? activeChat?.id : undefined,
// A location message's body is the (multi-KB) base64 map thumbnail; show a label instead.
locationLabel: `πŸ“ ${t('chats.media.location')}`,
});
@@ -405,7 +409,7 @@ export function Chats() {
void loadChats(selectedSessionId);
}
},
- [selectedSessionId, activeChat, loadChats, markChatRead, appendMessage, onMessageAppended, t],
+ [selectedSessionId, activeChat, canWrite, loadChats, markChatRead, appendMessage, onMessageAppended, t],
);
const handleIncomingMessageAck = useCallback(
@@ -671,9 +675,11 @@ export function Chats() {
useEffect(() => {
if (!activeChat) return;
markChatRead(activeChat.id);
- setChats(prev => prev.map(c => (c.id === activeChat.id ? { ...c, unreadCount: 0 } : c)));
+ // A read-only key sends no mark-as-read, so the chat stays unread on the gateway; clearing the
+ // badge here would only have the next chat-list load bring it back.
+ if (canWrite) setChats(prev => prev.map(c => (c.id === activeChat.id ? { ...c, unreadCount: 0 } : c)));
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, [activeChat?.id, markChatRead]);
+ }, [activeChat?.id, markChatRead, canWrite]);
// --- Global search: jump to a hit's chat (and best-effort scroll to the message) ---
// A cross-session hit switches session, which asynchronously reloads the chats list β€” so the