Sync update: 2026-05-18 15:45:06
This commit is contained in:
@@ -230,6 +230,8 @@ async function handleMessage(ws, raw) {
|
||||
return sendTo(ws, { type: 'error', message: 'Missing requestId' });
|
||||
}
|
||||
|
||||
console.log(`[WS RECV] Request type: ${type}, requestId: ${requestId}`);
|
||||
|
||||
const respond = (data) => sendTo(ws, { ...data, requestId });
|
||||
|
||||
// Handle type specific requests
|
||||
@@ -239,23 +241,50 @@ async function handleMessage(ws, raw) {
|
||||
case 'ping':
|
||||
return respond({ type: 'pong', ready: clientReady });
|
||||
|
||||
// ── Register FCM Token ─────────────────────────────────────────────
|
||||
case 'register_fcm': {
|
||||
const { token } = payload;
|
||||
if (!token) {
|
||||
return respond({ type: 'error', message: 'Token is required' });
|
||||
}
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const tokenPath = path.join(__dirname, 'fcm_token.json');
|
||||
fs.writeFileSync(tokenPath, JSON.stringify({ token, updatedAt: new Date().toISOString() }));
|
||||
console.log('[FCM] Token registered and saved successfully:', token.substring(0, 15) + '...');
|
||||
return respond({ type: 'fcm_registered', success: true });
|
||||
}
|
||||
|
||||
// ── Conversations ──────────────────────────────────────────────────
|
||||
case 'get_conversations': {
|
||||
if (!clientReady) {
|
||||
return respond({ type: 'error', message: 'WhatsApp is not ready' });
|
||||
}
|
||||
const chats = await waClient.getChats();
|
||||
const limit = parseInt(payload.limit) || 50;
|
||||
const offset = parseInt(payload.offset) || 0;
|
||||
const slice = chats.slice(offset, offset + limit);
|
||||
const formatted = await Promise.all(slice.map(formatChat));
|
||||
|
||||
return respond({
|
||||
type: 'conversations',
|
||||
data: formatted,
|
||||
total: chats.length,
|
||||
requestId
|
||||
});
|
||||
|
||||
try {
|
||||
const fetchChats = async () => {
|
||||
const chats = await waClient.getChats();
|
||||
const limit = parseInt(payload.limit) || 50;
|
||||
const offset = parseInt(payload.offset) || 0;
|
||||
const slice = chats.slice(offset, offset + limit);
|
||||
const formatted = await Promise.all(slice.map(formatChat));
|
||||
return {
|
||||
type: 'conversations',
|
||||
data: formatted,
|
||||
total: chats.length
|
||||
};
|
||||
};
|
||||
|
||||
const timeoutPromise = new Promise((_, reject) =>
|
||||
setTimeout(() => reject(new Error('Server request to WhatsApp timed out')), 25000)
|
||||
);
|
||||
|
||||
const result = await Promise.race([fetchChats(), timeoutPromise]);
|
||||
return respond({ ...result, requestId });
|
||||
} catch (err) {
|
||||
console.error('[WS] get_conversations failed or timed out:', err.message);
|
||||
return respond({ type: 'error', message: err.message || 'Failed to fetch conversations', requestId });
|
||||
}
|
||||
}
|
||||
|
||||
// ── Messages ───────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user