diff --git a/.gitignore b/.gitignore index ad1c15d..ee4cfa5 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ docs browser-token.json Proxy messages*.json -test.ts \ No newline at end of file +test.ts +TestData \ No newline at end of file diff --git a/src/Socket/messages-recv.ts b/src/Socket/messages-recv.ts index cebe683..1165119 100644 --- a/src/Socket/messages-recv.ts +++ b/src/Socket/messages-recv.ts @@ -99,14 +99,27 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { ev.emit('auth-state.update', authState) }) } - const processMessage = async(message: proto.IWebMessageInfo, chatUpdate: Partial) => { const protocolMsg = message.message?.protocolMessage if(protocolMsg) { switch(protocolMsg.type) { case proto.ProtocolMessage.ProtocolMessageType.HISTORY_SYNC_NOTIFICATION: - const history = await downloadHistory(protocolMsg!.historySyncNotification) + const histNotification = protocolMsg!.historySyncNotification + + logger.info({ type: histNotification.syncType!, id: message.key.id }, 'got history notification') + const history = await downloadHistory(histNotification) + processHistoryMessage(history) + + const meJid = authState.creds.me!.id + await sendNode({ + tag: 'receipt', + attrs: { + id: message.key.id, + type: 'hist_sync', + to: jidEncode(jidDecode(meJid).user, 'c.us') + } + }) break case proto.ProtocolMessage.ProtocolMessageType.APP_STATE_SYNC_KEY_REQUEST: const keys = await Promise.all( @@ -207,22 +220,30 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { } const processHistoryMessage = (item: proto.HistorySync) => { + const messages: proto.IWebMessageInfo[] = [] switch(item.syncType) { case proto.HistorySync.HistorySyncHistorySyncType.INITIAL_BOOTSTRAP: - const messages: proto.IWebMessageInfo[] = [] const chats = item.conversations!.map( c => { const chat: Chat = { ...c } //@ts-expect-error delete chat.messages - for(const item of c.messages || []) { - messages.push(item.message) + if(c.messages?.[0]) { + messages.push(c.messages![0].message!) } return chat } ) ev.emit('chats.set', { chats, messages }) - ev.emit('connection.update', { receivedPendingNotifications: true }) + break + case proto.HistorySync.HistorySyncHistorySyncType.RECENT: + // push remaining messages + for(const conv of item.conversations) { + for(const m of (conv.messages || []).slice(1)) { + messages.push(m.message!) + } + } + ev.emit('messages.upsert', { messages, type: 'prepend' }) break case proto.HistorySync.HistorySyncHistorySyncType.PUSH_NAME: const contacts = item.pushnames.map( diff --git a/src/Types/Message.ts b/src/Types/Message.ts index 120c895..c890a82 100644 --- a/src/Types/Message.ts +++ b/src/Types/Message.ts @@ -139,7 +139,7 @@ export type MessageContentGenerationOptions = MediaGenerationOptions & { } export type MessageGenerationOptions = MessageContentGenerationOptions & MessageGenerationOptionsFromContent -export type MessageUpdateType = 'append' | 'notify' +export type MessageUpdateType = 'append' | 'notify' | 'prepend' export type MessageInfoEventMap = { [jid: string]: Date } export interface MessageInfo {