This commit is contained in:
@@ -265,4 +265,42 @@ describe('customer service media upload', () => {
|
|||||||
'https://cdn.example.com/public/image.png',
|
'https://cdn.example.com/public/image.png',
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('renders inline Drafty images sent by the IM web client', () => {
|
||||||
|
const onMessages = vi.fn();
|
||||||
|
new CustomerServiceChat(session(), onMessages, { origin: 'https://www.yuexuan1.shop' });
|
||||||
|
const tinode = mocks.instances[0];
|
||||||
|
tinode.topic.messages.mockImplementation((callback: (message: unknown) => void) => {
|
||||||
|
callback({
|
||||||
|
seq: 4,
|
||||||
|
from: 'usr_support',
|
||||||
|
ts: '2026-10-19T10:02:32.090Z',
|
||||||
|
content: {
|
||||||
|
ent: [{
|
||||||
|
tp: 'IM',
|
||||||
|
data: {
|
||||||
|
val: 'aGVsbG8=',
|
||||||
|
mime: 'image/png',
|
||||||
|
name: 'proof.png',
|
||||||
|
width: 70,
|
||||||
|
height: 70,
|
||||||
|
},
|
||||||
|
}],
|
||||||
|
fmt: [{ len: 1 }],
|
||||||
|
txt: ' ',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
tinode.topic.onData?.();
|
||||||
|
|
||||||
|
expect(onMessages).toHaveBeenCalledWith([
|
||||||
|
expect.objectContaining({
|
||||||
|
seq: 4,
|
||||||
|
type: 'image',
|
||||||
|
media_url: 'data:image/png;base64,aGVsbG8=',
|
||||||
|
file_name: 'proof.png',
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -349,10 +349,14 @@ function messageContent(content: unknown, mediaURL: (ref?: string) => string) {
|
|||||||
const media = drafty.ent?.find((entity) => entity?.tp === 'IM' || entity?.tp === 'VD');
|
const media = drafty.ent?.find((entity) => entity?.tp === 'IM' || entity?.tp === 'VD');
|
||||||
if (media?.data) {
|
if (media?.data) {
|
||||||
const type = media.tp === 'VD' ? 'video' : 'image';
|
const type = media.tp === 'VD' ? 'video' : 'image';
|
||||||
|
const inline = typeof media.data.val === 'string' ? media.data.val : '';
|
||||||
|
const mime = typeof media.data.mime === 'string' && media.data.mime.startsWith(`${type}/`)
|
||||||
|
? media.data.mime
|
||||||
|
: type === 'video' ? 'video/webm' : 'image/jpeg';
|
||||||
return {
|
return {
|
||||||
text: '',
|
text: '',
|
||||||
type,
|
type,
|
||||||
media_url: mediaURL(String(media.data.ref || '')),
|
media_url: inline ? `data:${mime};base64,${inline}` : mediaURL(String(media.data.ref || '')),
|
||||||
preview_url: mediaURL(String(media.data.preref || '')),
|
preview_url: mediaURL(String(media.data.preref || '')),
|
||||||
file_name: String(media.data.name || ''),
|
file_name: String(media.data.name || ''),
|
||||||
} as const;
|
} as const;
|
||||||
|
|||||||
Reference in New Issue
Block a user