fix(notification): decode SSE messages and encode data for in-app notifications

This commit is contained in:
Sheldon Guo 2025-04-27 12:40:42 +08:00
parent e129cfc5ab
commit c0928e0ffd
2 changed files with 16 additions and 13 deletions

View File

@ -8,11 +8,11 @@
*/
import { observable, reaction } from '@formily/reactive';
import { SSEData } from '../../types';
import { messageMapObs, updateUnreadMsgsCount } from './message';
import { fetchChannels } from './channel';
import { getAPIClient } from '../utils';
import { uid } from '@nocobase/utils/client';
import { SSEData } from '../../types';
import { getAPIClient } from '../utils';
import { fetchChannels } from './channel';
import { messageMapObs, updateUnreadMsgsCount } from './message';
export const liveSSEObs = observable<{ value: SSEData | null }>({ value: null });
reaction(
@ -66,7 +66,8 @@ export const startMsgSSEStreamWithRetry: () => () => void = () => {
if (done) break;
const messages = value.split('\n\n').filter(Boolean);
for (const message of messages) {
const sseData: SSEData = JSON.parse(message.replace(/^data:\s*/, '').trim());
const decodedMessage = decodeURIComponent(message);
const sseData: SSEData = JSON.parse(decodedMessage.replace(/^data:\s*/, '').trim());
liveSSEObs.value = sseData;
}
}

View File

@ -60,14 +60,16 @@ export default class InAppNotificationChannel extends BaseNotificationChannel {
for (const clientId in clients) {
const stream = clients[clientId];
stream.write(
`data: ${JSON.stringify({
type: message.type,
data: {
...message.data,
title: message.data.title || '',
content: message.data.content || '',
},
})}\n\n`,
`data: ${encodeURIComponent(
JSON.stringify({
type: message.type,
data: {
...message.data,
title: message.data.title || '',
content: message.data.content || '',
},
}),
)}\n\n`,
);
}
}