diff --git a/README.md b/README.md index c755d60..3f30ac0 100644 --- a/README.md +++ b/README.md @@ -178,8 +178,12 @@ export type BaileysEventMap = { 'connection.update': Partial /** auth credentials updated -- some pre key state, device ID etc. */ 'creds.update': Partial - /** set chats (history sync), messages are reverse chronologically sorted */ - 'chats.set': { chats: Chat[], messages: WAMessage[], contacts: Contact[] } + /** set chats (history sync), chats are reverse chronologically sorted */ + 'chats.set': { chats: Chat[], isLatest: boolean } + /** set messages (history sync), messages are reverse chronologically sorted */ + 'messages.set': { messages: WAMessage[], isLatest: boolean } + /** set contacts (history sync) */ + 'contacts.set': { contacts: Contact[] } /** upsert chats */ 'chats.upsert': Chat[] /** update the given chats */ diff --git a/src/LegacySocket/chats.ts b/src/LegacySocket/chats.ts index d43cec1..bc0b88f 100644 --- a/src/LegacySocket/chats.ts +++ b/src/LegacySocket/chats.ts @@ -192,7 +192,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => { }) logger.info(`got ${chats.length} chats, extracted ${contacts.length} contacts with name`) - ev.emit('chats.set', { chats, messages: [], contacts }) + ev.emit('chats.set', { chats, isLatest: true }) } }) // got all contacts from phone @@ -208,7 +208,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => { }) logger.info(`got ${contacts.length} contacts`) - ev.emit('chats.set', { chats: [], messages: [], contacts }) + ev.emit('contacts.set', { contacts }) } }) // status updates diff --git a/src/LegacySocket/messages.ts b/src/LegacySocket/messages.ts index ba2e5d8..d1f11a8 100644 --- a/src/LegacySocket/messages.ts +++ b/src/LegacySocket/messages.ts @@ -287,15 +287,15 @@ const makeMessagesSocket = (config: LegacySocketConfig) => { } // messages received - const messagesUpdate = (node: BinaryNode, type: 'prepend' | 'last') => { + const messagesUpdate = (node: BinaryNode, isLatest: boolean) => { const messages = getBinaryNodeMessages(node) messages.reverse() - ev.emit('messages.upsert', { messages, type }) + ev.emit('messages.set', { messages, isLatest }) } - socketEvents.on('CB:action,add:last', json => messagesUpdate(json, 'last')) - socketEvents.on('CB:action,add:unread', json => messagesUpdate(json, 'prepend')) - socketEvents.on('CB:action,add:before', json => messagesUpdate(json, 'prepend')) + socketEvents.on('CB:action,add:last', json => messagesUpdate(json, true)) + socketEvents.on('CB:action,add:unread', json => messagesUpdate(json, false)) + socketEvents.on('CB:action,add:before', json => messagesUpdate(json, false)) // new messages socketEvents.on('CB:action,add:relay,message', (node: BinaryNode) => { diff --git a/src/Socket/messages-recv.ts b/src/Socket/messages-recv.ts index ed9d316..b3bbc20 100644 --- a/src/Socket/messages-recv.ts +++ b/src/Socket/messages-recv.ts @@ -133,7 +133,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { const histNotification = protocolMsg!.historySyncNotification logger.info({ histNotification, id: message.key.id }, 'got history notification') - const info = await downloadAndProcessHistorySyncNotification(histNotification, historyCache) + const { chats, contacts, messages, isLatest } = await downloadAndProcessHistorySyncNotification(histNotification, historyCache) const meJid = authState.creds.me!.id await sendNode({ @@ -145,7 +145,10 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { } }) - info && ev.emit('chats.set', info) + if(chats.length) ev.emit('chats.set', { chats, isLatest }) + if(messages.length) ev.emit('messages.set', { messages, isLatest }) + if(contacts.length) ev.emit('contacts.set', { contacts }) + break case proto.ProtocolMessage.ProtocolMessageType.APP_STATE_SYNC_KEY_SHARE: const keys = protocolMsg.appStateSyncKeyShare!.keys diff --git a/src/Types/Events.ts b/src/Types/Events.ts index e17cc0b..dc7a4d0 100644 --- a/src/Types/Events.ts +++ b/src/Types/Events.ts @@ -13,8 +13,12 @@ export type BaileysEventMap = { 'connection.update': Partial /** credentials updated -- some metadata, keys or something */ 'creds.update': Partial - /** set chats (history sync), messages are reverse chronologically sorted */ - 'chats.set': { chats: Chat[], messages: WAMessage[], contacts: Contact[], isLatest: boolean } + /** set chats (history sync), chats are reverse chronologically sorted */ + 'chats.set': { chats: Chat[], isLatest: boolean } + /** set messages (history sync), messages are reverse chronologically sorted */ + 'messages.set': { messages: WAMessage[], isLatest: boolean } + /** set contacts (history sync) */ + 'contacts.set': { contacts: Contact[] } /** upsert chats */ 'chats.upsert': Chat[] /** update the given chats */ diff --git a/src/Types/Message.ts b/src/Types/Message.ts index 6150290..277c00d 100644 --- a/src/Types/Message.ts +++ b/src/Types/Message.ts @@ -165,7 +165,7 @@ export type MessageContentGenerationOptions = MediaGenerationOptions & { } export type MessageGenerationOptions = MessageContentGenerationOptions & MessageGenerationOptionsFromContent -export type MessageUpdateType = 'append' | 'notify' | 'prepend' | 'last' | 'replace' +export type MessageUpdateType = 'append' | 'notify' | 'replace' export type MessageInfoEventMap = { [jid: string]: Date } export interface MessageInfo { diff --git a/src/Utils/history.ts b/src/Utils/history.ts index 6c556d1..d53e84d 100644 --- a/src/Utils/history.ts +++ b/src/Utils/history.ts @@ -66,13 +66,11 @@ export const processHistoryMessage = (item: proto.IHistorySync, historyCache: Se break } - if(chats.length || contacts.length || messages.length) { - return { - chats, - contacts, - messages, - isLatest, - } + return { + chats, + contacts, + messages, + isLatest, } }