Removed loadProfilePicturesForChatsAutomatically

This commit is contained in:
Adhiraj Singh
2021-01-05 19:08:25 +05:30
parent 2129c87852
commit b9ece77220
5 changed files with 20 additions and 19 deletions

View File

@@ -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) {

View File

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

View File

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

View File

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

View File

@@ -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 = {