mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: track history being stored
This is done because sometimes we receive history after the first connect too, and to ensure the "isLatest" flag is accurate -- we ensure no history was received previously
This commit is contained in:
@@ -154,15 +154,13 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const processMessageLocal = async(msg: proto.IWebMessageInfo) => {
|
const processMessageLocal = async(msg: proto.IWebMessageInfo) => {
|
||||||
const meId = authState.creds.me!.id
|
|
||||||
// process message and emit events
|
// process message and emit events
|
||||||
const newEvents = await processMessage(
|
const newEvents = await processMessage(
|
||||||
msg,
|
msg,
|
||||||
{
|
{
|
||||||
downloadHistory,
|
downloadHistory,
|
||||||
historyCache,
|
historyCache,
|
||||||
meId,
|
creds: authState.creds,
|
||||||
accountSettings: authState.creds.accountSettings,
|
|
||||||
keyStore: authState.keys,
|
keyStore: authState.keys,
|
||||||
logger,
|
logger,
|
||||||
treatCiphertextMessagesAsReal
|
treatCiphertextMessagesAsReal
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import type { proto } from '../../WAProto'
|
import type { proto } from '../../WAProto'
|
||||||
import type { Contact } from './Contact'
|
import type { Contact } from './Contact'
|
||||||
|
import type { MinimalMessage } from './Message'
|
||||||
|
|
||||||
export type KeyPair = { public: Uint8Array, private: Uint8Array }
|
export type KeyPair = { public: Uint8Array, private: Uint8Array }
|
||||||
export type SignedKeyPair = { keyPair: KeyPair, signature: Uint8Array, keyId: number }
|
export type SignedKeyPair = { keyPair: KeyPair, signature: Uint8Array, keyId: number }
|
||||||
@@ -45,6 +46,8 @@ export type AuthenticationCreds = SignalCreds & {
|
|||||||
|
|
||||||
lastAccountSyncTimestamp?: number
|
lastAccountSyncTimestamp?: number
|
||||||
platform?: string
|
platform?: string
|
||||||
|
|
||||||
|
processedHistoryMessages: MinimalMessage[]
|
||||||
accountSettings: AccountSettings
|
accountSettings: AccountSettings
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { proto } from '../../WAProto'
|
import type { proto } from '../../WAProto'
|
||||||
|
import type { MinimalMessage } from './Message'
|
||||||
|
|
||||||
/** set of statuses visible to other people; see updatePresence() in WhatsAppWeb.Send */
|
/** set of statuses visible to other people; see updatePresence() in WhatsAppWeb.Send */
|
||||||
export type WAPresence = 'unavailable' | 'available' | 'composing' | 'recording' | 'paused'
|
export type WAPresence = 'unavailable' | 'available' | 'composing' | 'recording' | 'paused'
|
||||||
@@ -32,11 +33,12 @@ export type Chat = Omit<proto.IConversation, 'messages'> & {
|
|||||||
pin?: number | null
|
pin?: number | null
|
||||||
archive?: boolean
|
archive?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the last messages in a chat, sorted reverse-chronologically. That is, the latest message should be first in the chat
|
* the last messages in a chat, sorted reverse-chronologically. That is, the latest message should be first in the chat
|
||||||
* for MD modifications, the last message in the array (i.e. the earlist message) must be the last message recv in the chat
|
* for MD modifications, the last message in the array (i.e. the earlist message) must be the last message recv in the chat
|
||||||
* */
|
* */
|
||||||
export type LastMessageList = Pick<proto.IWebMessageInfo, 'key' | 'messageTimestamp'>[] | proto.ISyncActionMessageRange
|
export type LastMessageList = MinimalMessage[] | proto.ISyncActionMessageRange
|
||||||
|
|
||||||
export type ChatModification =
|
export type ChatModification =
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -188,4 +188,6 @@ export type MediaDecryptionKeyInfo = {
|
|||||||
iv: Buffer
|
iv: Buffer
|
||||||
cipherKey: Buffer
|
cipherKey: Buffer
|
||||||
macKey?: Buffer
|
macKey?: Buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type MinimalMessage = Pick<proto.IWebMessageInfo, 'key' | 'messageTimestamp'>
|
||||||
@@ -121,7 +121,7 @@ export const initAuthCreds = (): AuthenticationCreds => {
|
|||||||
signedPreKey: signedKeyPair(identityKey, 1),
|
signedPreKey: signedKeyPair(identityKey, 1),
|
||||||
registrationId: generateRegistrationId(),
|
registrationId: generateRegistrationId(),
|
||||||
advSecretKey: randomBytes(32).toString('base64'),
|
advSecretKey: randomBytes(32).toString('base64'),
|
||||||
|
processedHistoryMessages: [],
|
||||||
nextPreKeyId: 1,
|
nextPreKeyId: 1,
|
||||||
firstUnuploadedPreKeyId: 1,
|
firstUnuploadedPreKeyId: 1,
|
||||||
accountSettings: {
|
accountSettings: {
|
||||||
|
|||||||
@@ -22,10 +22,10 @@ export const downloadHistory = async(msg: proto.IHistorySyncNotification) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const processHistoryMessage = (item: proto.IHistorySync, historyCache: Set<string>) => {
|
export const processHistoryMessage = (item: proto.IHistorySync, historyCache: Set<string>) => {
|
||||||
const isLatest = historyCache.size === 0
|
|
||||||
const messages: proto.IWebMessageInfo[] = []
|
const messages: proto.IWebMessageInfo[] = []
|
||||||
const contacts: Contact[] = []
|
const contacts: Contact[] = []
|
||||||
const chats: Chat[] = []
|
const chats: Chat[] = []
|
||||||
|
|
||||||
switch (item.syncType) {
|
switch (item.syncType) {
|
||||||
case proto.HistorySync.HistorySyncHistorySyncType.INITIAL_BOOTSTRAP:
|
case proto.HistorySync.HistorySyncHistorySyncType.INITIAL_BOOTSTRAP:
|
||||||
case proto.HistorySync.HistorySyncHistorySyncType.RECENT:
|
case proto.HistorySync.HistorySyncHistorySyncType.RECENT:
|
||||||
@@ -71,11 +71,13 @@ export const processHistoryMessage = (item: proto.IHistorySync, historyCache: Se
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const didProcess = !!(chats.length || messages.length || contacts.length)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
chats,
|
chats,
|
||||||
contacts,
|
contacts,
|
||||||
messages,
|
messages,
|
||||||
isLatest,
|
didProcess,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,14 @@
|
|||||||
import type { Logger } from 'pino'
|
import type { Logger } from 'pino'
|
||||||
import { proto } from '../../WAProto'
|
import { proto } from '../../WAProto'
|
||||||
import { AccountSettings, BaileysEventMap, Chat, GroupMetadata, ParticipantAction, SignalKeyStoreWithTransaction, WAMessageStubType } from '../Types'
|
import { AuthenticationCreds, BaileysEventMap, Chat, GroupMetadata, ParticipantAction, SignalKeyStoreWithTransaction, WAMessageStubType } from '../Types'
|
||||||
import { downloadAndProcessHistorySyncNotification, normalizeMessageContent, toNumber } from '../Utils'
|
import { downloadAndProcessHistorySyncNotification, normalizeMessageContent, toNumber } from '../Utils'
|
||||||
import { areJidsSameUser, jidNormalizedUser } from '../WABinary'
|
import { areJidsSameUser, jidNormalizedUser } from '../WABinary'
|
||||||
|
|
||||||
type ProcessMessageContext = {
|
type ProcessMessageContext = {
|
||||||
historyCache: Set<string>
|
historyCache: Set<string>
|
||||||
downloadHistory: boolean
|
downloadHistory: boolean
|
||||||
meId: string
|
creds: AuthenticationCreds
|
||||||
keyStore: SignalKeyStoreWithTransaction
|
keyStore: SignalKeyStoreWithTransaction
|
||||||
accountSettings: AccountSettings
|
|
||||||
logger?: Logger
|
logger?: Logger
|
||||||
treatCiphertextMessagesAsReal?: boolean
|
treatCiphertextMessagesAsReal?: boolean
|
||||||
}
|
}
|
||||||
@@ -39,8 +38,10 @@ export const cleanMessage = (message: proto.IWebMessageInfo, meId: string) => {
|
|||||||
|
|
||||||
const processMessage = async(
|
const processMessage = async(
|
||||||
message: proto.IWebMessageInfo,
|
message: proto.IWebMessageInfo,
|
||||||
{ downloadHistory, historyCache, meId, keyStore, accountSettings, logger, treatCiphertextMessagesAsReal }: ProcessMessageContext
|
{ downloadHistory, historyCache, creds, keyStore, logger, treatCiphertextMessagesAsReal }: ProcessMessageContext
|
||||||
) => {
|
) => {
|
||||||
|
const meId = creds.me!.id
|
||||||
|
const { accountSettings } = creds
|
||||||
const map: Partial<BaileysEventMap<any>> = { }
|
const map: Partial<BaileysEventMap<any>> = { }
|
||||||
|
|
||||||
const chat: Partial<Chat> = { id: jidNormalizedUser(message.key.remoteJid) }
|
const chat: Partial<Chat> = { id: jidNormalizedUser(message.key.remoteJid) }
|
||||||
@@ -77,7 +78,8 @@ const processMessage = async(
|
|||||||
logger?.info({ histNotification, id: message.key.id }, 'got history notification')
|
logger?.info({ histNotification, id: message.key.id }, 'got history notification')
|
||||||
|
|
||||||
if(downloadHistory) {
|
if(downloadHistory) {
|
||||||
const { chats, contacts, messages, isLatest } = await downloadAndProcessHistorySyncNotification(histNotification, historyCache)
|
const { chats, contacts, messages, didProcess } = await downloadAndProcessHistorySyncNotification(histNotification, historyCache)
|
||||||
|
const isLatest = historyCache.size === 0 && !creds.processedHistoryMessages?.length
|
||||||
|
|
||||||
if(chats.length) {
|
if(chats.length) {
|
||||||
map['chats.set'] = { chats, isLatest }
|
map['chats.set'] = { chats, isLatest }
|
||||||
@@ -90,6 +92,16 @@ const processMessage = async(
|
|||||||
if(contacts.length) {
|
if(contacts.length) {
|
||||||
map['contacts.set'] = { contacts }
|
map['contacts.set'] = { contacts }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(didProcess) {
|
||||||
|
map['creds.update'] = {
|
||||||
|
...(map['creds.update'] || {}),
|
||||||
|
processedHistoryMessages: [
|
||||||
|
...(creds.processedHistoryMessages || []),
|
||||||
|
{ key: message.key, timestamp: message.messageTimestamp }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|||||||
Reference in New Issue
Block a user