mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
implement recent history sync
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -11,4 +11,5 @@ docs
|
|||||||
browser-token.json
|
browser-token.json
|
||||||
Proxy
|
Proxy
|
||||||
messages*.json
|
messages*.json
|
||||||
test.ts
|
test.ts
|
||||||
|
TestData
|
||||||
@@ -99,14 +99,27 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
ev.emit('auth-state.update', authState)
|
ev.emit('auth-state.update', authState)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const processMessage = async(message: proto.IWebMessageInfo, chatUpdate: Partial<Chat>) => {
|
const processMessage = async(message: proto.IWebMessageInfo, chatUpdate: Partial<Chat>) => {
|
||||||
const protocolMsg = message.message?.protocolMessage
|
const protocolMsg = message.message?.protocolMessage
|
||||||
if(protocolMsg) {
|
if(protocolMsg) {
|
||||||
switch(protocolMsg.type) {
|
switch(protocolMsg.type) {
|
||||||
case proto.ProtocolMessage.ProtocolMessageType.HISTORY_SYNC_NOTIFICATION:
|
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)
|
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
|
break
|
||||||
case proto.ProtocolMessage.ProtocolMessageType.APP_STATE_SYNC_KEY_REQUEST:
|
case proto.ProtocolMessage.ProtocolMessageType.APP_STATE_SYNC_KEY_REQUEST:
|
||||||
const keys = await Promise.all(
|
const keys = await Promise.all(
|
||||||
@@ -207,22 +220,30 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const processHistoryMessage = (item: proto.HistorySync) => {
|
const processHistoryMessage = (item: proto.HistorySync) => {
|
||||||
|
const messages: proto.IWebMessageInfo[] = []
|
||||||
switch(item.syncType) {
|
switch(item.syncType) {
|
||||||
case proto.HistorySync.HistorySyncHistorySyncType.INITIAL_BOOTSTRAP:
|
case proto.HistorySync.HistorySyncHistorySyncType.INITIAL_BOOTSTRAP:
|
||||||
const messages: proto.IWebMessageInfo[] = []
|
|
||||||
const chats = item.conversations!.map(
|
const chats = item.conversations!.map(
|
||||||
c => {
|
c => {
|
||||||
const chat: Chat = { ...c }
|
const chat: Chat = { ...c }
|
||||||
//@ts-expect-error
|
//@ts-expect-error
|
||||||
delete chat.messages
|
delete chat.messages
|
||||||
for(const item of c.messages || []) {
|
if(c.messages?.[0]) {
|
||||||
messages.push(item.message)
|
messages.push(c.messages![0].message!)
|
||||||
}
|
}
|
||||||
return chat
|
return chat
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
ev.emit('chats.set', { chats, messages })
|
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
|
break
|
||||||
case proto.HistorySync.HistorySyncHistorySyncType.PUSH_NAME:
|
case proto.HistorySync.HistorySyncHistorySyncType.PUSH_NAME:
|
||||||
const contacts = item.pushnames.map(
|
const contacts = item.pushnames.map(
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ export type MessageContentGenerationOptions = MediaGenerationOptions & {
|
|||||||
}
|
}
|
||||||
export type MessageGenerationOptions = MessageContentGenerationOptions & MessageGenerationOptionsFromContent
|
export type MessageGenerationOptions = MessageContentGenerationOptions & MessageGenerationOptionsFromContent
|
||||||
|
|
||||||
export type MessageUpdateType = 'append' | 'notify'
|
export type MessageUpdateType = 'append' | 'notify' | 'prepend'
|
||||||
|
|
||||||
export type MessageInfoEventMap = { [jid: string]: Date }
|
export type MessageInfoEventMap = { [jid: string]: Date }
|
||||||
export interface MessageInfo {
|
export interface MessageInfo {
|
||||||
|
|||||||
Reference in New Issue
Block a user