refactor: chats.set event

This commit is contained in:
Adhiraj Singh
2022-01-16 13:02:38 +05:30
parent 09553def8a
commit 7bb6e3ceeb
7 changed files with 30 additions and 21 deletions

View File

@@ -178,8 +178,12 @@ export type BaileysEventMap = {
'connection.update': Partial<ConnectionState> 'connection.update': Partial<ConnectionState>
/** auth credentials updated -- some pre key state, device ID etc. */ /** auth credentials updated -- some pre key state, device ID etc. */
'creds.update': Partial<AuthenticationCreds> 'creds.update': Partial<AuthenticationCreds>
/** set chats (history sync), messages are reverse chronologically sorted */ /** set chats (history sync), chats are reverse chronologically sorted */
'chats.set': { chats: Chat[], messages: WAMessage[], contacts: Contact[] } 'chats.set': { chats: Chat[], isLatest: boolean }
/** set messages (history sync), messages are reverse chronologically sorted */
'messages.set': { messages: WAMessage[], isLatest: boolean }
/** set contacts (history sync) */
'contacts.set': { contacts: Contact[] }
/** upsert chats */ /** upsert chats */
'chats.upsert': Chat[] 'chats.upsert': Chat[]
/** update the given chats */ /** update the given chats */

View File

@@ -192,7 +192,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
}) })
logger.info(`got ${chats.length} chats, extracted ${contacts.length} contacts with name`) logger.info(`got ${chats.length} chats, extracted ${contacts.length} contacts with name`)
ev.emit('chats.set', { chats, messages: [], contacts }) ev.emit('chats.set', { chats, isLatest: true })
} }
}) })
// got all contacts from phone // got all contacts from phone
@@ -208,7 +208,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
}) })
logger.info(`got ${contacts.length} contacts`) logger.info(`got ${contacts.length} contacts`)
ev.emit('chats.set', { chats: [], messages: [], contacts }) ev.emit('contacts.set', { contacts })
} }
}) })
// status updates // status updates

View File

@@ -287,15 +287,15 @@ const makeMessagesSocket = (config: LegacySocketConfig) => {
} }
// messages received // messages received
const messagesUpdate = (node: BinaryNode, type: 'prepend' | 'last') => { const messagesUpdate = (node: BinaryNode, isLatest: boolean) => {
const messages = getBinaryNodeMessages(node) const messages = getBinaryNodeMessages(node)
messages.reverse() messages.reverse()
ev.emit('messages.upsert', { messages, type }) ev.emit('messages.set', { messages, isLatest })
} }
socketEvents.on('CB:action,add:last', json => messagesUpdate(json, 'last')) socketEvents.on('CB:action,add:last', json => messagesUpdate(json, true))
socketEvents.on('CB:action,add:unread', json => messagesUpdate(json, 'prepend')) socketEvents.on('CB:action,add:unread', json => messagesUpdate(json, false))
socketEvents.on('CB:action,add:before', json => messagesUpdate(json, 'prepend')) socketEvents.on('CB:action,add:before', json => messagesUpdate(json, false))
// new messages // new messages
socketEvents.on('CB:action,add:relay,message', (node: BinaryNode) => { socketEvents.on('CB:action,add:relay,message', (node: BinaryNode) => {

View File

@@ -133,7 +133,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
const histNotification = protocolMsg!.historySyncNotification const histNotification = protocolMsg!.historySyncNotification
logger.info({ histNotification, id: message.key.id }, 'got history notification') logger.info({ histNotification, id: message.key.id }, 'got history notification')
const info = await downloadAndProcessHistorySyncNotification(histNotification, historyCache) const { chats, contacts, messages, isLatest } = await downloadAndProcessHistorySyncNotification(histNotification, historyCache)
const meJid = authState.creds.me!.id const meJid = authState.creds.me!.id
await sendNode({ await sendNode({
@@ -145,7 +145,10 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
} }
}) })
info && ev.emit('chats.set', info) if(chats.length) ev.emit('chats.set', { chats, isLatest })
if(messages.length) ev.emit('messages.set', { messages, isLatest })
if(contacts.length) ev.emit('contacts.set', { contacts })
break break
case proto.ProtocolMessage.ProtocolMessageType.APP_STATE_SYNC_KEY_SHARE: case proto.ProtocolMessage.ProtocolMessageType.APP_STATE_SYNC_KEY_SHARE:
const keys = protocolMsg.appStateSyncKeyShare!.keys const keys = protocolMsg.appStateSyncKeyShare!.keys

View File

@@ -13,8 +13,12 @@ export type BaileysEventMap<T> = {
'connection.update': Partial<ConnectionState> 'connection.update': Partial<ConnectionState>
/** credentials updated -- some metadata, keys or something */ /** credentials updated -- some metadata, keys or something */
'creds.update': Partial<T> 'creds.update': Partial<T>
/** set chats (history sync), messages are reverse chronologically sorted */ /** set chats (history sync), chats are reverse chronologically sorted */
'chats.set': { chats: Chat[], messages: WAMessage[], contacts: Contact[], isLatest: boolean } 'chats.set': { chats: Chat[], isLatest: boolean }
/** set messages (history sync), messages are reverse chronologically sorted */
'messages.set': { messages: WAMessage[], isLatest: boolean }
/** set contacts (history sync) */
'contacts.set': { contacts: Contact[] }
/** upsert chats */ /** upsert chats */
'chats.upsert': Chat[] 'chats.upsert': Chat[]
/** update the given chats */ /** update the given chats */

View File

@@ -165,7 +165,7 @@ export type MessageContentGenerationOptions = MediaGenerationOptions & {
} }
export type MessageGenerationOptions = MessageContentGenerationOptions & MessageGenerationOptionsFromContent export type MessageGenerationOptions = MessageContentGenerationOptions & MessageGenerationOptionsFromContent
export type MessageUpdateType = 'append' | 'notify' | 'prepend' | 'last' | 'replace' export type MessageUpdateType = 'append' | 'notify' | 'replace'
export type MessageInfoEventMap = { [jid: string]: Date } export type MessageInfoEventMap = { [jid: string]: Date }
export interface MessageInfo { export interface MessageInfo {

View File

@@ -66,13 +66,11 @@ export const processHistoryMessage = (item: proto.IHistorySync, historyCache: Se
break break
} }
if(chats.length || contacts.length || messages.length) { return {
return { chats,
chats, contacts,
contacts, messages,
messages, isLatest,
isLatest,
}
} }
} }