Explicit waitForConnection + option to not load profile picture for new chats

This commit is contained in:
Adhiraj Singh
2020-10-19 15:22:44 +05:30
parent bb78138506
commit 588209d57c
4 changed files with 10 additions and 3 deletions

View File

@@ -65,6 +65,7 @@ export class WAConnection extends EventEmitter {
messageLog: { tag: string, json: string, fromMe: boolean, binaryTags?: any[] }[] = []
maxCachedMessages = 50
loadProfilePicturesForChatsAutomatically = true
chats = new KeyedDB (Utils.waChatKey(false), value => value.jid)
contacts: { [k: string]: WAContact } = {}

View File

@@ -199,8 +199,9 @@ export class WAConnection extends Base {
name
}
this.chats.insert (chat)
await this.setProfilePicture (chat)
if (this.loadProfilePicturesForChatsAutomatically) {
await this.setProfilePicture (chat)
}
this.emit ('chat-new', chat)
return chat

View File

@@ -8,6 +8,7 @@ import {
} from '../WAConnection/Constants'
import { generateProfilePicture, waChatKey, whatsappID, unixTimestampSeconds } from './Utils'
import { Mutex } from './Mutex'
import { type } from 'os'
// All user related functions -- get profile picture, set status etc.
@@ -101,7 +102,9 @@ export class WAConnection extends Base {
(typeof options?.custom !== 'function' || options?.custom(chat)) &&
(typeof options?.searchString === 'undefined' || chat.name?.includes (options.searchString) || chat.jid?.startsWith(options.searchString))
)))
if (options.loadProfilePicture !== false) {
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)

View File

@@ -85,6 +85,8 @@ export class WAConnection extends Base {
}
/** Prepare a media message for sending */
async prepareMessageMedia(buffer: Buffer, mediaType: MessageType, options: MessageOptions = {}) {
await this.waitForConnection ()
if (mediaType === MessageType.document && !options.mimetype) {
throw new Error('mimetype required to send a document')
}