Merge pull request #278 from edgardmessias/feat_pushname

Added method to update the profile name
This commit is contained in:
Adhiraj Singh
2020-12-29 23:31:57 +05:30
committed by GitHub
4 changed files with 71 additions and 6 deletions

View File

@@ -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<void> (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()
})

View File

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

View File

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

View File

@@ -212,6 +212,7 @@ export interface WAContact {
export interface WAUser extends WAContact {
phone: any
}
export type WAContactUpdate = Partial<WAContact> & { jid: string, status?: string }
export interface WAChat {
jid: string
@@ -476,4 +477,5 @@ export type BaileysEvent =
'received-pong' |
'credentials-updated' |
'connection-validated' |
'blocklist-update'
'blocklist-update' |
'contact-update'