diff --git a/Example/example.ts b/Example/example.ts index d3e56ab..56efc2c 100644 --- a/Example/example.ts +++ b/Example/example.ts @@ -51,6 +51,10 @@ async function example() { if (chat.presences) { // receive presence updates -- composing, available, etc. Object.values(chat.presences).forEach(presence => console.log( `${presence.name}'s presence is ${presence.lastKnownPresence} in ${chat.jid}`)) } + if(chat.imgUrl) { + console.log('imgUrl of chat changed ', chat.imgUrl) + return + } // only do something when a new message is received if (!chat.hasNewMessage) { if(chat.messages) { diff --git a/src/WAConnection/0.Base.ts b/src/WAConnection/0.Base.ts index cb01ddd..c265a58 100644 --- a/src/WAConnection/0.Base.ts +++ b/src/WAConnection/0.Base.ts @@ -61,7 +61,10 @@ export class WAConnection extends EventEmitter { messageLog: { tag: string, json: string, fromMe: boolean, binaryTags?: any[] }[] = [] maxCachedMessages = 50 - /** @deprecated won't be supported soon */ + /** + * @deprecated + * does not do anything + * */ loadProfilePicturesForChatsAutomatically = false lastChatsReceived: Date diff --git a/src/WAConnection/4.Events.ts b/src/WAConnection/4.Events.ts index 909a537..f9252b3 100644 --- a/src/WAConnection/4.Events.ts +++ b/src/WAConnection/4.Events.ts @@ -318,7 +318,8 @@ export class WAConnection extends Base { }) // profile picture updates this.on('CB:Cmd,type:picture', async json => { - const jid = whatsappID(json[1].jid) + json = json[1] + const jid = whatsappID(json.jid) const imgUrl = await this.getProfilePicture(jid).catch(() => '') const contact = this.contacts[jid] if (contact) { @@ -340,8 +341,10 @@ export class WAConnection extends Base { 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 }) + if(this.user.name !== name) { + this.user.name = name // update on client too + this.emit ('contact-update', { jid: this.user.jid, name }) + } } }) // read updates @@ -406,7 +409,7 @@ export class WAConnection extends Base { } } /** inserts an empty chat into the DB */ - protected async chatAdd (jid: string, name?: string) { + protected chatAdd (jid: string, name?: string) { const chat: WAChat = { jid, name, @@ -416,11 +419,7 @@ export class WAConnection extends Base { modify_tag: '', spam: 'false' } - if(this.chats.insertIfAbsent (chat).length) { - if (this.loadProfilePicturesForChatsAutomatically) { - await this.setProfilePicture (chat) - } this.emit ('chat-new', chat) return chat } diff --git a/src/WAConnection/5.User.ts b/src/WAConnection/5.User.ts index 1e3a206..3eb8ba3 100644 --- a/src/WAConnection/5.User.ts +++ b/src/WAConnection/5.User.ts @@ -139,21 +139,12 @@ export class WAConnection extends Base { * @param searchString optionally search for users * @returns the chats & the cursor to fetch the next page */ - async loadChats (count: number, before: string | null, options: WALoadChatOptions = {}) { + loadChats (count: number, before: string | null, options: WALoadChatOptions = {}) { const searchString = options.searchString?.toLowerCase() const chats = this.chats.paginated (before, count, options && (chat => ( (typeof options?.custom !== 'function' || options?.custom(chat)) && (typeof searchString === 'undefined' || chat.name?.toLowerCase().includes (searchString) || chat.jid?.includes(searchString)) ))) - let loadPP = this.loadProfilePicturesForChatsAutomatically - if (typeof options.loadProfilePicture !== 'undefined') loadPP = options.loadProfilePicture - if (loadPP) { - await Promise.all ( - chats.map (async chat => ( - typeof chat.imgUrl === 'undefined' && await this.setProfilePicture (chat) - )) - ) - } const cursor = (chats[chats.length-1] && chats.length >= count) && this.chatOrderingKey.key (chats[chats.length-1]) return { chats, cursor } } diff --git a/src/WAConnection/Constants.ts b/src/WAConnection/Constants.ts index 1a639a5..4c3e95d 100644 --- a/src/WAConnection/Constants.ts +++ b/src/WAConnection/Constants.ts @@ -81,6 +81,10 @@ export enum ReconnectMode { export type WALoadChatOptions = { searchString?: string custom?: (c: WAChat) => boolean + /** + * @deprecated + * does not do anything now + */ loadProfilePicture?: boolean } export type WAConnectOptions = {