mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: add contacts to chats.set event
This commit is contained in:
@@ -26,7 +26,9 @@ const startSock = () => {
|
||||
}
|
||||
|
||||
sock.ev.on('messages.upsert', async m => {
|
||||
console.log(JSON.stringify(m, undefined, 2))
|
||||
if(m.type === 'append' || m.type === 'notify') {
|
||||
console.log(JSON.stringify(m, undefined, 2))
|
||||
}
|
||||
|
||||
const msg = m.messages[0]
|
||||
if(!msg.key.fromMe && m.type === 'notify') {
|
||||
|
||||
@@ -170,7 +170,12 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
|
||||
socketEvents.on('CB:response,type:chat', async ({ content: data }: BinaryNode) => {
|
||||
chatsDebounceTimeout.cancel()
|
||||
if(Array.isArray(data)) {
|
||||
const contacts: Contact[] = []
|
||||
const chats = data.map(({ attrs }): Chat => {
|
||||
const id = jidNormalizedUser(attrs.jid)
|
||||
if(attrs.name) {
|
||||
contacts.push({ id, name: attrs.name })
|
||||
}
|
||||
return {
|
||||
id: jidNormalizedUser(attrs.jid),
|
||||
conversationTimestamp: attrs.t ? +attrs.t : undefined,
|
||||
@@ -186,8 +191,8 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
|
||||
}
|
||||
})
|
||||
|
||||
logger.info(`got ${chats.length} chats`)
|
||||
ev.emit('chats.set', { chats, messages: [] })
|
||||
logger.info(`got ${chats.length} chats, extracted ${contacts.length} contacts with name`)
|
||||
ev.emit('chats.set', { chats, messages: [], contacts })
|
||||
}
|
||||
})
|
||||
// got all contacts from phone
|
||||
@@ -203,7 +208,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
|
||||
})
|
||||
|
||||
logger.info(`got ${contacts.length} contacts`)
|
||||
ev.emit('contacts.upsert', contacts)
|
||||
ev.emit('chats.set', { chats: [], messages: [], contacts })
|
||||
}
|
||||
})
|
||||
// status updates
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
|
||||
import { SocketConfig, WAMessageStubType, ParticipantAction, Chat, GroupMetadata, WAMessageKey } from "../Types"
|
||||
import { SocketConfig, WAMessageStubType, ParticipantAction, Chat, GroupMetadata, WAMessageKey, Contact } from "../Types"
|
||||
import { decodeMessageStanza, encodeBigEndian, toNumber, downloadHistory, generateSignalPubKey, xmppPreKey, xmppSignedPreKey } from "../Utils"
|
||||
import { BinaryNode, jidDecode, jidEncode, isJidStatusBroadcast, areJidsSameUser, getBinaryNodeChildren, jidNormalizedUser, getAllBinaryNodeChildren, BinaryNodeAttributes, isJidGroup } from '../WABinary'
|
||||
import { proto } from "../../WAProto"
|
||||
@@ -222,11 +222,18 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
|
||||
const processHistoryMessage = (item: proto.HistorySync) => {
|
||||
const messages: proto.IWebMessageInfo[] = []
|
||||
const contacts: Contact[] = []
|
||||
switch(item.syncType) {
|
||||
case proto.HistorySync.HistorySyncHistorySyncType.INITIAL_BOOTSTRAP:
|
||||
const chats = item.conversations!.map(
|
||||
c => {
|
||||
const chat: Chat = { ...c }
|
||||
if(chat.name) {
|
||||
contacts.push({
|
||||
id: chat.id,
|
||||
name: chat.name
|
||||
})
|
||||
}
|
||||
//@ts-expect-error
|
||||
delete chat.messages
|
||||
for(const msg of c.messages || []) {
|
||||
@@ -237,7 +244,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
return chat
|
||||
}
|
||||
)
|
||||
ev.emit('chats.set', { chats, messages })
|
||||
ev.emit('chats.set', { chats, messages, contacts })
|
||||
break
|
||||
case proto.HistorySync.HistorySyncHistorySyncType.RECENT:
|
||||
// push remaining messages
|
||||
@@ -251,10 +258,12 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
}
|
||||
break
|
||||
case proto.HistorySync.HistorySyncHistorySyncType.PUSH_NAME:
|
||||
const contacts = item.pushnames.map(
|
||||
p => ({ notify: p.pushname, id: p.id })
|
||||
contacts.push(
|
||||
...item.pushnames.map(
|
||||
p => ({ notify: p.pushname, id: p.id })
|
||||
)
|
||||
)
|
||||
ev.emit('contacts.upsert', contacts)
|
||||
ev.emit('chats.set', { chats: [], messages: [], contacts })
|
||||
break
|
||||
case proto.HistorySync.HistorySyncHistorySyncType.INITIAL_STATUS_V3:
|
||||
// TODO
|
||||
|
||||
@@ -14,7 +14,7 @@ export type BaileysEventMap<T> = {
|
||||
/** credentials updated -- some metadata, keys or something */
|
||||
'creds.update': Partial<T>
|
||||
/** set chats (history sync), messages are reverse chronologically sorted */
|
||||
'chats.set': { chats: Chat[], messages: WAMessage[] }
|
||||
'chats.set': { chats: Chat[], messages: WAMessage[], contacts: Contact[] }
|
||||
/** upsert chats */
|
||||
'chats.upsert': Chat[]
|
||||
/** update the given chats */
|
||||
|
||||
Reference in New Issue
Block a user