From 677f50baaa3e858242835e5e02ba2309980aa004 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Mon, 20 Dec 2021 13:59:04 +0530 Subject: [PATCH] feat: add contacts to chats.set event --- Example/example-legacy.ts | 4 +++- src/LegacySocket/chats.ts | 11 ++++++++--- src/Socket/messages-recv.ts | 19 ++++++++++++++----- src/Types/Events.ts | 2 +- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Example/example-legacy.ts b/Example/example-legacy.ts index 2660a80..9a7df6f 100644 --- a/Example/example-legacy.ts +++ b/Example/example-legacy.ts @@ -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') { diff --git a/src/LegacySocket/chats.ts b/src/LegacySocket/chats.ts index c9612d9..f692eb4 100644 --- a/src/LegacySocket/chats.ts +++ b/src/LegacySocket/chats.ts @@ -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 diff --git a/src/Socket/messages-recv.ts b/src/Socket/messages-recv.ts index 51cac7b..16cc761 100644 --- a/src/Socket/messages-recv.ts +++ b/src/Socket/messages-recv.ts @@ -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 diff --git a/src/Types/Events.ts b/src/Types/Events.ts index 33889a7..7aa534e 100644 --- a/src/Types/Events.ts +++ b/src/Types/Events.ts @@ -14,7 +14,7 @@ export type BaileysEventMap = { /** credentials updated -- some metadata, keys or something */ 'creds.update': Partial /** 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 */