feat: add contacts to chats.set event

This commit is contained in:
Adhiraj Singh
2021-12-20 13:59:04 +05:30
parent e9d90aa0a5
commit 677f50baaa
4 changed files with 26 additions and 10 deletions

View File

@@ -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') {

View File

@@ -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

View File

@@ -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

View File

@@ -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 */