Skip to content

Commit

Permalink
implement recent history sync
Browse files Browse the repository at this point in the history
  • Loading branch information
adiwajshing committed Oct 8, 2021
1 parent edc10d0 commit 91bbf57
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ docs
browser-token.json
Proxy
messages*.json
test.ts
test.ts
TestData
33 changes: 27 additions & 6 deletions src/Socket/messages-recv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,27 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
ev.emit('auth-state.update', authState)
})
}

const processMessage = async(message: proto.IWebMessageInfo, chatUpdate: Partial<Chat>) => {
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(
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/Types/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 91bbf57

Please sign in to comment.