feat: more accurately handle app state sync

This commit is contained in:
Adhiraj Singh
2022-06-11 12:02:08 +05:30
parent 79518787b6
commit 5cb71ac862
7 changed files with 290 additions and 30 deletions

View File

@@ -86,7 +86,7 @@ export const encodeBigEndian = (e: number, t = 4) => {
return a
}
export const toNumber = (t: Long | number) => ((typeof t === 'object' && t) ? ('toNumber' in t ? t.toNumber() : (t as any).low) : t)
export const toNumber = (t: Long | number): number => ((typeof t === 'object' && t) ? ('toNumber' in t ? t.toNumber() : (t as any).low) : t)
export function shallowChanges <T>(old: T, current: T, { lookForDeletedKeys }: {lookForDeletedKeys: boolean}): Partial<T> {
const changes: Partial<T> = {}

View File

@@ -1,8 +1,9 @@
import { promisify } from 'util'
import { inflate } from 'zlib'
import { proto } from '../../WAProto'
import { Chat, Contact } from '../Types'
import { Chat, Contact, InitialReceivedChatsState } from '../Types'
import { isJidUser } from '../WABinary'
import { toNumber } from './generics'
import { downloadContentFromMessage } from './messages-media'
const inflatePromise = promisify(inflate)
@@ -21,7 +22,11 @@ export const downloadHistory = async(msg: proto.IHistorySyncNotification) => {
return syncData
}
export const processHistoryMessage = (item: proto.IHistorySync, historyCache: Set<string>) => {
export const processHistoryMessage = (
item: proto.IHistorySync,
historyCache: Set<string>,
recvChats: InitialReceivedChatsState
) => {
const messages: proto.IWebMessageInfo[] = []
const contacts: Contact[] = []
const chats: Chat[] = []
@@ -40,6 +45,13 @@ export const processHistoryMessage = (item: proto.IHistorySync, historyCache: Se
const uqId = `${message.key.remoteJid}:${message.key.id}`
if(!historyCache.has(uqId)) {
messages.push(message)
const curItem = recvChats[message.key.remoteJid]
const timestamp = toNumber(message.messageTimestamp)
if(!message.key.fromMe && (!curItem || timestamp > curItem.lastMsgRecvTimestamp)) {
recvChats[message.key.remoteJid] = { lastMsgRecvTimestamp: timestamp }
}
historyCache.add(uqId)
}
}
@@ -81,7 +93,11 @@ export const processHistoryMessage = (item: proto.IHistorySync, historyCache: Se
}
}
export const downloadAndProcessHistorySyncNotification = async(msg: proto.IHistorySyncNotification, historyCache: Set<string>) => {
export const downloadAndProcessHistorySyncNotification = async(
msg: proto.IHistorySyncNotification,
historyCache: Set<string>,
recvChats: InitialReceivedChatsState
) => {
const historyMsg = await downloadHistory(msg)
return processHistoryMessage(historyMsg, historyCache)
return processHistoryMessage(historyMsg, historyCache, recvChats)
}

View File

@@ -1,11 +1,12 @@
import type { Logger } from 'pino'
import { proto } from '../../WAProto'
import { AuthenticationCreds, BaileysEventMap, Chat, GroupMetadata, ParticipantAction, SignalKeyStoreWithTransaction, WAMessageStubType } from '../Types'
import { AuthenticationCreds, BaileysEventMap, Chat, GroupMetadata, InitialReceivedChatsState, ParticipantAction, SignalKeyStoreWithTransaction, WAMessageStubType } from '../Types'
import { downloadAndProcessHistorySyncNotification, normalizeMessageContent, toNumber } from '../Utils'
import { areJidsSameUser, jidNormalizedUser } from '../WABinary'
type ProcessMessageContext = {
historyCache: Set<string>
recvChats: InitialReceivedChatsState
downloadHistory: boolean
creds: AuthenticationCreds
keyStore: SignalKeyStoreWithTransaction
@@ -38,7 +39,7 @@ export const cleanMessage = (message: proto.IWebMessageInfo, meId: string) => {
const processMessage = async(
message: proto.IWebMessageInfo,
{ downloadHistory, historyCache, creds, keyStore, logger, treatCiphertextMessagesAsReal }: ProcessMessageContext
{ downloadHistory, historyCache, recvChats, creds, keyStore, logger, treatCiphertextMessagesAsReal }: ProcessMessageContext
) => {
const meId = creds.me!.id
const { accountSettings } = creds
@@ -78,7 +79,7 @@ const processMessage = async(
logger?.info({ histNotification, id: message.key.id }, 'got history notification')
if(downloadHistory) {
const { chats, contacts, messages, didProcess } = await downloadAndProcessHistorySyncNotification(histNotification, historyCache)
const { chats, contacts, messages, didProcess } = await downloadAndProcessHistorySyncNotification(histNotification, historyCache, recvChats)
const isLatest = historyCache.size === 0 && !creds.processedHistoryMessages?.length
if(chats.length) {