From b329c73b208c8e61ae0fc64980aa64a68f98385d Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Tue, 27 Sep 2022 11:28:41 +0530 Subject: [PATCH] feat: handle picture change events --- Example/example.ts | 11 +++++++++++ src/Socket/messages-recv.ts | 23 ++++++++++++++++++++--- src/Types/Contact.ts | 9 ++++++++- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/Example/example.ts b/Example/example.ts index 89b374e..1e99f09 100644 --- a/Example/example.ts +++ b/Example/example.ts @@ -152,6 +152,17 @@ const startSock = async() => { console.log(events['chats.update']) } + if(events['contacts.update']) { + for(const contact of events['contacts.update']) { + if(typeof contact.imgUrl !== 'undefined') { + const newUrl = contact.imgUrl === null ? null : await sock!.profilePictureUrl(contact.id!) + console.log( + `contact ${contact.id} has a new profile pic: ${newUrl}`, + ) + } + } + } + if(events['chats.delete']) { console.log('chats deleted ', events['chats.delete']) } diff --git a/src/Socket/messages-recv.ts b/src/Socket/messages-recv.ts index 9d5515e..c6403af 100644 --- a/src/Socket/messages-recv.ts +++ b/src/Socket/messages-recv.ts @@ -261,6 +261,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { const result: Partial = { } const [child] = getAllBinaryNodeChildren(node) const nodeType = node.attrs.type + const from = jidNormalizedUser(node.attrs.from) switch (nodeType) { case 'w:gp2': @@ -291,10 +292,26 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { break case 'picture': const setPicture = getBinaryNodeChild(node, 'set') - if(setPicture) { + const delPicture = getBinaryNodeChild(node, 'delete') + + ev.emit('contacts.update', [{ + id: from, + imgUrl: setPicture ? 'changed' : null + }]) + + if(isJidGroup(from)) { + const node = setPicture || delPicture result.messageStubType = WAMessageStubType.GROUP_CHANGE_ICON - result.messageStubParameters = [ setPicture.attrs.id ] - result.participant = setPicture.attrs.author + + if(setPicture) { + result.messageStubParameters = [ setPicture.attrs.id ] + } + + result.participant = node?.attrs.author + result.key = { + ...result.key || {}, + participant: setPicture?.attrs.author + } } break diff --git a/src/Types/Contact.ts b/src/Types/Contact.ts index f5e74b3..5da78e8 100644 --- a/src/Types/Contact.ts +++ b/src/Types/Contact.ts @@ -7,6 +7,13 @@ export interface Contact { /** I have no idea */ verifiedName?: string // Baileys Added - imgUrl?: string + /** + * Url of the profile picture of the contact + * + * 'changed' => if the profile picture has changed + * null => if the profile picture has not been set (default profile picture) + * any other string => url of the profile picture + */ + imgUrl?: string | null | 'changed' status?: string } \ No newline at end of file