mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Merge pull request #278 from edgardmessias/feat_pushname
Added method to update the profile name
This commit is contained in:
@@ -45,6 +45,33 @@ WAConnectionTest('Misc', conn => {
|
|||||||
|
|
||||||
await conn.setStatus (response.status) // update back
|
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 () => {
|
it('should return the stories', async () => {
|
||||||
await conn.getStories()
|
await conn.getStories()
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import * as QR from 'qrcode-terminal'
|
import * as QR from 'qrcode-terminal'
|
||||||
import { WAConnection as Base } from './3.Connect'
|
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 { whatsappID, unixTimestampSeconds, isGroupID, GET_MESSAGE_ID, WA_MESSAGE_ID, waMessageKey, newMessagesDB, shallowChanges, toNumber } from './Utils'
|
||||||
import KeyedDB from '@adiwajshing/keyed-db'
|
import KeyedDB from '@adiwajshing/keyed-db'
|
||||||
import { Mutex } from './Mutex'
|
import { Mutex } from './Mutex'
|
||||||
@@ -298,10 +298,21 @@ export class WAConnection extends Base {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
// status updates
|
// status updates
|
||||||
this.on('CB:Status', async json => {
|
this.on('CB:Status,status', async json => {
|
||||||
const jid = whatsappID(json[1].id)
|
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 })
|
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
|
// read updates
|
||||||
this.on ('CB:action,,read', async json => {
|
this.on ('CB:action,,read', async json => {
|
||||||
const update = json[2][0][1]
|
const update = json[2][0][1]
|
||||||
@@ -351,7 +362,6 @@ export class WAConnection extends Base {
|
|||||||
|
|
||||||
// blocklist updates
|
// blocklist updates
|
||||||
this.on('CB:Blocklist', json => {
|
this.on('CB:Blocklist', json => {
|
||||||
if (!json) return
|
|
||||||
json = json[1]
|
json = json[1]
|
||||||
const initial = this.blocklist
|
const initial = this.blocklist
|
||||||
this.blocklist = json.blocklist
|
this.blocklist = json.blocklist
|
||||||
@@ -632,8 +642,13 @@ export class WAConnection extends Base {
|
|||||||
* @deprecated use `chat-update`
|
* @deprecated use `chat-update`
|
||||||
* */
|
* */
|
||||||
on (event: 'user-presence-update', listener: (update: PresenceUpdate) => void): this
|
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
|
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 */
|
/** when a new chat is added */
|
||||||
on (event: 'chat-new', listener: (chat: WAChat) => void): this
|
on (event: 'chat-new', listener: (chat: WAChat) => void): this
|
||||||
/** when contacts are sent by WA */
|
/** when contacts are sent by WA */
|
||||||
|
|||||||
@@ -76,10 +76,31 @@ export class WAConnection extends Base {
|
|||||||
Buffer.from (status, 'utf-8')
|
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 })
|
this.emit ('user-status-update', { jid: this.user.jid, status })
|
||||||
return response
|
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 */
|
/** Get your contacts */
|
||||||
async getContacts() {
|
async getContacts() {
|
||||||
const json = ['query', { epoch: this.msgCount.toString(), type: 'contacts' }, null]
|
const json = ['query', { epoch: this.msgCount.toString(), type: 'contacts' }, null]
|
||||||
|
|||||||
@@ -212,6 +212,7 @@ export interface WAContact {
|
|||||||
export interface WAUser extends WAContact {
|
export interface WAUser extends WAContact {
|
||||||
phone: any
|
phone: any
|
||||||
}
|
}
|
||||||
|
export type WAContactUpdate = Partial<WAContact> & { jid: string, status?: string }
|
||||||
export interface WAChat {
|
export interface WAChat {
|
||||||
jid: string
|
jid: string
|
||||||
|
|
||||||
@@ -476,4 +477,5 @@ export type BaileysEvent =
|
|||||||
'received-pong' |
|
'received-pong' |
|
||||||
'credentials-updated' |
|
'credentials-updated' |
|
||||||
'connection-validated' |
|
'connection-validated' |
|
||||||
'blocklist-update'
|
'blocklist-update' |
|
||||||
|
'contact-update'
|
||||||
Reference in New Issue
Block a user