Sync update: 2026-05-18 16:14:25
This commit is contained in:
@@ -59,18 +59,23 @@ function sendTo(ws, payload) {
|
||||
async function formatChat(chat) {
|
||||
let avatar = null;
|
||||
try {
|
||||
// Ultra-fast memory-based avatar fetch with strict 300ms fallback to prevent hangs
|
||||
avatar = await Promise.race([
|
||||
chat.client.pupPage.evaluate((chatId) => {
|
||||
try {
|
||||
const contact = window.Store.Contact.get(chatId);
|
||||
return contact && contact.profilePicThumb ? (contact.profilePicThumb.imgFull || contact.profilePicThumb.img) : null;
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}, chat.id._serialized),
|
||||
new Promise(resolve => setTimeout(() => resolve(null), 300))
|
||||
]);
|
||||
// 1. Try memory-based avatar lookup first (takes < 1ms)
|
||||
avatar = await chat.client.pupPage.evaluate((chatId) => {
|
||||
try {
|
||||
const contact = window.Store.Contact.get(chatId);
|
||||
return contact && contact.profilePicThumb ? (contact.profilePicThumb.imgFull || contact.profilePicThumb.img) : null;
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}, chat.id._serialized);
|
||||
|
||||
// 2. If memory has no avatar, fallback to strict-timeout network query (max 800ms)
|
||||
if (!avatar) {
|
||||
avatar = await Promise.race([
|
||||
chat.getProfilePicUrl().catch(() => null),
|
||||
new Promise(resolve => setTimeout(() => resolve(null), 800))
|
||||
]);
|
||||
}
|
||||
} catch (_) {}
|
||||
|
||||
// Last Message formatting
|
||||
@@ -355,6 +360,53 @@ async function handleMessage(ws, raw) {
|
||||
});
|
||||
}
|
||||
|
||||
// ── Media ──────────────────────────────────────────────────────────
|
||||
case 'get_media': {
|
||||
if (!clientReady) {
|
||||
return respond({ type: 'error', message: 'WhatsApp is not ready' });
|
||||
}
|
||||
const { messageId } = payload;
|
||||
if (!messageId) {
|
||||
return respond({ type: 'error', message: 'messageId is required' });
|
||||
}
|
||||
try {
|
||||
// Extract chatId from messageId (format: true_447701407332@c.us_3EB0C8B196C5F354)
|
||||
const parts = messageId.split('_');
|
||||
if (parts.length < 2) {
|
||||
return respond({ type: 'error', message: 'Invalid messageId format' });
|
||||
}
|
||||
const chatId = parts[1];
|
||||
const chat = await waClient.getChatById(chatId);
|
||||
const messages = await chat.fetchMessages({ limit: 100 });
|
||||
const msg = messages.find(m => m.id._serialized === messageId);
|
||||
|
||||
if (!msg) {
|
||||
return respond({ type: 'error', message: 'Message not found in chat history' });
|
||||
}
|
||||
if (!msg.hasMedia) {
|
||||
return respond({ type: 'error', message: 'Message has no media attachments' });
|
||||
}
|
||||
|
||||
console.log(`[WS] Downloading media for message: ${messageId}`);
|
||||
const media = await msg.downloadMedia();
|
||||
if (!media) {
|
||||
return respond({ type: 'error', message: 'Failed to download media file from WhatsApp servers' });
|
||||
}
|
||||
|
||||
return respond({
|
||||
type: 'media',
|
||||
messageId: messageId,
|
||||
data: media.data, // base64 string
|
||||
mimetype: media.mimetype,
|
||||
filename: media.filename || 'file',
|
||||
requestId
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('[WS] get_media failed:', err.message);
|
||||
return respond({ type: 'error', message: err.message || 'Failed to download media', requestId });
|
||||
}
|
||||
}
|
||||
|
||||
// ── Send Message ───────────────────────────────────────────────────
|
||||
case 'send_message': {
|
||||
if (!clientReady) {
|
||||
|
||||
Reference in New Issue
Block a user