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 => {
|
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]
|
const msg = m.messages[0]
|
||||||
if(!msg.key.fromMe && m.type === 'notify') {
|
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) => {
|
socketEvents.on('CB:response,type:chat', async ({ content: data }: BinaryNode) => {
|
||||||
chatsDebounceTimeout.cancel()
|
chatsDebounceTimeout.cancel()
|
||||||
if(Array.isArray(data)) {
|
if(Array.isArray(data)) {
|
||||||
|
const contacts: Contact[] = []
|
||||||
const chats = data.map(({ attrs }): Chat => {
|
const chats = data.map(({ attrs }): Chat => {
|
||||||
|
const id = jidNormalizedUser(attrs.jid)
|
||||||
|
if(attrs.name) {
|
||||||
|
contacts.push({ id, name: attrs.name })
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
id: jidNormalizedUser(attrs.jid),
|
id: jidNormalizedUser(attrs.jid),
|
||||||
conversationTimestamp: attrs.t ? +attrs.t : undefined,
|
conversationTimestamp: attrs.t ? +attrs.t : undefined,
|
||||||
@@ -186,8 +191,8 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
logger.info(`got ${chats.length} chats`)
|
logger.info(`got ${chats.length} chats, extracted ${contacts.length} contacts with name`)
|
||||||
ev.emit('chats.set', { chats, messages: [] })
|
ev.emit('chats.set', { chats, messages: [], contacts })
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// got all contacts from phone
|
// got all contacts from phone
|
||||||
@@ -203,7 +208,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
logger.info(`got ${contacts.length} contacts`)
|
logger.info(`got ${contacts.length} contacts`)
|
||||||
ev.emit('contacts.upsert', contacts)
|
ev.emit('chats.set', { chats: [], messages: [], contacts })
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// status updates
|
// 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 { decodeMessageStanza, encodeBigEndian, toNumber, downloadHistory, generateSignalPubKey, xmppPreKey, xmppSignedPreKey } from "../Utils"
|
||||||
import { BinaryNode, jidDecode, jidEncode, isJidStatusBroadcast, areJidsSameUser, getBinaryNodeChildren, jidNormalizedUser, getAllBinaryNodeChildren, BinaryNodeAttributes, isJidGroup } from '../WABinary'
|
import { BinaryNode, jidDecode, jidEncode, isJidStatusBroadcast, areJidsSameUser, getBinaryNodeChildren, jidNormalizedUser, getAllBinaryNodeChildren, BinaryNodeAttributes, isJidGroup } from '../WABinary'
|
||||||
import { proto } from "../../WAProto"
|
import { proto } from "../../WAProto"
|
||||||
@@ -222,11 +222,18 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
|
|
||||||
const processHistoryMessage = (item: proto.HistorySync) => {
|
const processHistoryMessage = (item: proto.HistorySync) => {
|
||||||
const messages: proto.IWebMessageInfo[] = []
|
const messages: proto.IWebMessageInfo[] = []
|
||||||
|
const contacts: Contact[] = []
|
||||||
switch(item.syncType) {
|
switch(item.syncType) {
|
||||||
case proto.HistorySync.HistorySyncHistorySyncType.INITIAL_BOOTSTRAP:
|
case proto.HistorySync.HistorySyncHistorySyncType.INITIAL_BOOTSTRAP:
|
||||||
const chats = item.conversations!.map(
|
const chats = item.conversations!.map(
|
||||||
c => {
|
c => {
|
||||||
const chat: Chat = { ...c }
|
const chat: Chat = { ...c }
|
||||||
|
if(chat.name) {
|
||||||
|
contacts.push({
|
||||||
|
id: chat.id,
|
||||||
|
name: chat.name
|
||||||
|
})
|
||||||
|
}
|
||||||
//@ts-expect-error
|
//@ts-expect-error
|
||||||
delete chat.messages
|
delete chat.messages
|
||||||
for(const msg of c.messages || []) {
|
for(const msg of c.messages || []) {
|
||||||
@@ -237,7 +244,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
return chat
|
return chat
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
ev.emit('chats.set', { chats, messages })
|
ev.emit('chats.set', { chats, messages, contacts })
|
||||||
break
|
break
|
||||||
case proto.HistorySync.HistorySyncHistorySyncType.RECENT:
|
case proto.HistorySync.HistorySyncHistorySyncType.RECENT:
|
||||||
// push remaining messages
|
// push remaining messages
|
||||||
@@ -251,10 +258,12 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
|||||||
}
|
}
|
||||||
break
|
break
|
||||||
case proto.HistorySync.HistorySyncHistorySyncType.PUSH_NAME:
|
case proto.HistorySync.HistorySyncHistorySyncType.PUSH_NAME:
|
||||||
const contacts = item.pushnames.map(
|
contacts.push(
|
||||||
p => ({ notify: p.pushname, id: p.id })
|
...item.pushnames.map(
|
||||||
|
p => ({ notify: p.pushname, id: p.id })
|
||||||
|
)
|
||||||
)
|
)
|
||||||
ev.emit('contacts.upsert', contacts)
|
ev.emit('chats.set', { chats: [], messages: [], contacts })
|
||||||
break
|
break
|
||||||
case proto.HistorySync.HistorySyncHistorySyncType.INITIAL_STATUS_V3:
|
case proto.HistorySync.HistorySyncHistorySyncType.INITIAL_STATUS_V3:
|
||||||
// TODO
|
// TODO
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export type BaileysEventMap<T> = {
|
|||||||
/** 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), messages are reverse chronologically sorted */
|
||||||
'chats.set': { chats: Chat[], messages: WAMessage[] }
|
'chats.set': { chats: Chat[], messages: WAMessage[], contacts: Contact[] }
|
||||||
/** upsert chats */
|
/** upsert chats */
|
||||||
'chats.upsert': Chat[]
|
'chats.upsert': Chat[]
|
||||||
/** update the given chats */
|
/** update the given chats */
|
||||||
|
|||||||
Reference in New Issue
Block a user