From 14bf3a441a0a193b323ba96ee01bed3aed2f3f5b Mon Sep 17 00:00:00 2001 From: Edgard Messias Date: Mon, 14 Dec 2020 18:01:50 -0300 Subject: [PATCH] Added method to update the profile name --- src/Tests/Tests.Misc.ts | 27 +++++++++++++++++++++++++++ src/WAConnection/4.Events.ts | 23 +++++++++++++++++++---- src/WAConnection/5.User.ts | 23 ++++++++++++++++++++++- src/WAConnection/Constants.ts | 4 +++- 4 files changed, 71 insertions(+), 6 deletions(-) diff --git a/src/Tests/Tests.Misc.ts b/src/Tests/Tests.Misc.ts index 7f9fced..61fcc3a 100644 --- a/src/Tests/Tests.Misc.ts +++ b/src/Tests/Tests.Misc.ts @@ -45,6 +45,33 @@ WAConnectionTest('Misc', conn => { await conn.setStatus (response.status) // update back }) + it('should update profile name', async () => { + const newName = 'v cool name' + + await delay (1000) + + const originalName = conn.user.name! + + const waitForEvent = new Promise (resolve => { + conn.on ('contact-update', ({name}) => { + assert.strictEqual (name, newName) + conn.removeAllListeners ('contact-update') + resolve () + }) + }) + + await conn.updateProfileName (newName) + + await waitForEvent + + await delay (1000) + + assert.strictEqual (conn.user.name, newName) + + await delay (1000) + + await conn.updateProfileName (originalName) // update back + }) it('should return the stories', async () => { await conn.getStories() }) diff --git a/src/WAConnection/4.Events.ts b/src/WAConnection/4.Events.ts index 7ce9bb9..2456a67 100644 --- a/src/WAConnection/4.Events.ts +++ b/src/WAConnection/4.Events.ts @@ -1,6 +1,6 @@ import * as QR from 'qrcode-terminal' import { WAConnection as Base } from './3.Connect' -import { WAMessageStatusUpdate, WAMessage, WAContact, WAChat, WAMessageProto, WA_MESSAGE_STUB_TYPE, WA_MESSAGE_STATUS_TYPE, PresenceUpdate, BaileysEvent, DisconnectReason, WAOpenResult, Presence, AuthenticationCredentials, WAParticipantAction, WAGroupMetadata, WAUser, WANode, WAPresenceData, WAChatUpdate, BlocklistUpdate } from './Constants' +import { WAMessageStatusUpdate, WAMessage, WAContact, WAChat, WAMessageProto, WA_MESSAGE_STUB_TYPE, WA_MESSAGE_STATUS_TYPE, PresenceUpdate, BaileysEvent, DisconnectReason, WAOpenResult, Presence, AuthenticationCredentials, WAParticipantAction, WAGroupMetadata, WAUser, WANode, WAPresenceData, WAChatUpdate, BlocklistUpdate, WAContactUpdate } from './Constants' import { whatsappID, unixTimestampSeconds, isGroupID, GET_MESSAGE_ID, WA_MESSAGE_ID, waMessageKey, newMessagesDB, shallowChanges, toNumber } from './Utils' import KeyedDB from '@adiwajshing/keyed-db' import { Mutex } from './Mutex' @@ -298,10 +298,21 @@ export class WAConnection extends Base { } }) // status updates - this.on('CB:Status', async json => { + this.on('CB:Status,status', async json => { const jid = whatsappID(json[1].id) + this.emit ('contact-update', { jid, status: json[1].status }) + + // emit deprecated this.emit ('user-status-update', { jid, status: json[1].status }) }) + // User Profile Name Updates + this.on ('CB:Conn,pushname', json => { + if (this.user) { + const name = json[1].pushname + this.user.name = name // update on client too + this.emit ('contact-update', { jid: this.user.jid, name }) + } + }) // read updates this.on ('CB:action,,read', async json => { const update = json[2][0][1] @@ -351,7 +362,6 @@ export class WAConnection extends Base { // blocklist updates this.on('CB:Blocklist', json => { - if (!json) return json = json[1] const initial = this.blocklist this.blocklist = json.blocklist @@ -632,8 +642,13 @@ export class WAConnection extends Base { * @deprecated use `chat-update` * */ on (event: 'user-presence-update', listener: (update: PresenceUpdate) => void): this - /** when a user's status is updated */ + /** + * when a user's status is updated + * @deprecated use `contact-update` + */ on (event: 'user-status-update', listener: (update: {jid: string, status?: string}) => void): this + /** when a user's status is updated */ + on (event: 'contact-update', listener: (update: WAContactUpdate) => void): this /** when a new chat is added */ on (event: 'chat-new', listener: (chat: WAChat) => void): this /** when contacts are sent by WA */ diff --git a/src/WAConnection/5.User.ts b/src/WAConnection/5.User.ts index f69d18a..c8e4f4b 100644 --- a/src/WAConnection/5.User.ts +++ b/src/WAConnection/5.User.ts @@ -76,10 +76,31 @@ export class WAConnection extends Base { Buffer.from (status, 'utf-8') ] ] - ) + ) + this.emit ('contact-update', { jid: this.user.jid, status }) + + // emit deprecated this.emit ('user-status-update', { jid: this.user.jid, status }) return response } + async updateProfileName (name: string) { + const response = (await this.setQuery ( + [ + [ + 'profile', + { + name + }, + null + ] + ] + )) as any as {status: number, pushname: string} + if (response.status === 200) { + this.user.name = response.pushname; + this.emit ('contact-update', { jid: this.user.jid, name }) + } + return response + } /** Get your contacts */ async getContacts() { const json = ['query', { epoch: this.msgCount.toString(), type: 'contacts' }, null] diff --git a/src/WAConnection/Constants.ts b/src/WAConnection/Constants.ts index e5f82ba..e074073 100644 --- a/src/WAConnection/Constants.ts +++ b/src/WAConnection/Constants.ts @@ -212,6 +212,7 @@ export interface WAContact { export interface WAUser extends WAContact { phone: any } +export type WAContactUpdate = Partial & { jid: string, status?: string } export interface WAChat { jid: string @@ -474,4 +475,5 @@ export type BaileysEvent = 'received-pong' | 'credentials-updated' | 'connection-validated' | - 'blocklist-update' \ No newline at end of file + 'blocklist-update' | + 'contact-update' \ No newline at end of file