@@ -126,6 +126,7 @@ export class WAStartupService {
126126 private readonly repository : RepositoryBroker ,
127127 private readonly cache : RedisCache ,
128128 ) {
129+ this . logger . verbose ( 'WAStartupService initialized' ) ;
129130 this . cleanStore ( ) ;
130131 this . instance . qrcode = { count : 0 } ;
131132 }
@@ -148,6 +149,7 @@ export class WAStartupService {
148149 return ;
149150 }
150151 this . instance . name = name ;
152+ this . logger . verbose ( `Instance '${ this . instance . name } ' initialized` ) ;
151153 this . sendDataWebhook ( Events . STATUS_INSTANCE , {
152154 instance : this . instance . name ,
153155 status : 'created' ,
@@ -163,9 +165,12 @@ export class WAStartupService {
163165 }
164166
165167 public async getProfileName ( ) {
168+ this . logger . verbose ( 'Getting profile name' ) ;
166169 let profileName = this . client . user ?. name ?? this . client . user ?. verifiedName ;
167170 if ( ! profileName ) {
171+ this . logger . verbose ( 'Profile name not found, trying to get from database' ) ;
168172 if ( this . configService . get < Database > ( 'DATABASE' ) . ENABLED ) {
173+ this . logger . verbose ( 'Database enabled, trying to get from database' ) ;
169174 const collection = dbserver
170175 . getClient ( )
171176 . db (
@@ -175,10 +180,12 @@ export class WAStartupService {
175180 . collection ( this . instanceName ) ;
176181 const data = await collection . findOne ( { _id : 'creds' } ) ;
177182 if ( data ) {
183+ this . logger . verbose ( 'Profile name found in database' ) ;
178184 const creds = JSON . parse ( JSON . stringify ( data ) , BufferJSON . reviver ) ;
179185 profileName = creds . me ?. name || creds . me ?. verifiedName ;
180186 }
181187 } else if ( existsSync ( join ( INSTANCE_DIR , this . instanceName , 'creds.json' ) ) ) {
188+ this . logger . verbose ( 'Profile name found in file' ) ;
182189 const creds = JSON . parse (
183190 readFileSync ( join ( INSTANCE_DIR , this . instanceName , 'creds.json' ) , {
184191 encoding : 'utf-8' ,
@@ -187,41 +194,71 @@ export class WAStartupService {
187194 profileName = creds . me ?. name || creds . me ?. verifiedName ;
188195 }
189196 }
197+
198+ this . logger . verbose ( `Profile name: ${ profileName } ` ) ;
190199 return profileName ;
191200 }
192201
193202 public async getProfileStatus ( ) {
203+ this . logger . verbose ( 'Getting profile status' ) ;
194204 const status = await this . client . fetchStatus ( this . instance . wuid ) ;
195205
206+ this . logger . verbose ( `Profile status: ${ status . status } ` ) ;
196207 return status . status ;
197208 }
198209
199210 public get profilePictureUrl ( ) {
211+ this . logger . verbose ( 'Getting profile picture url' ) ;
200212 return this . instance . profilePictureUrl ;
201213 }
202214
203215 public get qrCode ( ) : wa . QrCode {
216+ this . logger . verbose ( 'Getting qrcode' ) ;
204217 return {
205218 code : this . instance . qrcode ?. code ,
206219 base64 : this . instance . qrcode ?. base64 ,
207220 } ;
208221 }
209222
210223 private async loadWebhook ( ) {
224+ this . logger . verbose ( 'Loading webhook' ) ;
211225 const data = await this . repository . webhook . find ( this . instanceName ) ;
212226 this . localWebhook . url = data ?. url ;
227+ this . logger . verbose ( `Webhook url: ${ this . localWebhook . url } ` ) ;
228+
213229 this . localWebhook . enabled = data ?. enabled ;
230+ this . logger . verbose ( `Webhook enabled: ${ this . localWebhook . enabled } ` ) ;
231+
214232 this . localWebhook . events = data ?. events ;
233+ this . logger . verbose ( `Webhook events: ${ this . localWebhook . events } ` ) ;
234+
215235 this . localWebhook . webhook_by_events = data ?. webhook_by_events ;
236+ this . logger . verbose ( `Webhook by events: ${ this . localWebhook . webhook_by_events } ` ) ;
237+
238+ this . logger . verbose ( 'Webhook loaded' ) ;
216239 }
217240
218241 public async setWebhook ( data : WebhookRaw ) {
242+ this . logger . verbose ( 'Setting webhook' ) ;
219243 await this . repository . webhook . create ( data , this . instanceName ) ;
244+ this . logger . verbose ( `Webhook url: ${ data . url } ` ) ;
245+ this . logger . verbose ( `Webhook events: ${ data . events } ` ) ;
220246 Object . assign ( this . localWebhook , data ) ;
247+ this . logger . verbose ( 'Webhook set' ) ;
221248 }
222249
223250 public async findWebhook ( ) {
224- return await this . repository . webhook . find ( this . instanceName ) ;
251+ this . logger . verbose ( 'Finding webhook' ) ;
252+ const data = await this . repository . webhook . find ( this . instanceName ) ;
253+
254+ if ( ! data ) {
255+ this . logger . verbose ( 'Webhook not found' ) ;
256+ throw new NotFoundException ( 'Webhook not found' ) ;
257+ }
258+
259+ this . logger . verbose ( `Webhook url: ${ data . url } ` ) ;
260+ this . logger . verbose ( `Webhook events: ${ data . events } ` ) ;
261+ return data ;
225262 }
226263
227264 public async sendDataWebhook < T = any > ( event : Events , data : T , local = true ) {
@@ -474,6 +511,13 @@ export class WAStartupService {
474511 this . instance . wuid ,
475512 ) } /*.json`,
476513 ) ;
514+ this . logger . verbose (
515+ `Cleaned ${ join (
516+ this . storePath ,
517+ key . toLowerCase ( ) . replace ( '_' , '-' ) ,
518+ this . instance . wuid ,
519+ ) } /*.json`,
520+ ) ;
477521 }
478522 }
479523 } catch ( error ) { }
@@ -1189,20 +1233,21 @@ export class WAStartupService {
11891233 private async convertToWebP ( image : string ) {
11901234 try {
11911235 let imagePath : string ;
1192- const outputPath = `${ join ( process . cwd ( ) , 'temp' , 'sticker.webp' ) } ` ;
1236+ const timestamp = new Date ( ) . getTime ( ) ;
1237+ const outputPath = `${ join ( process . cwd ( ) , 'temp' , `${ timestamp } .webp` ) } ` ;
11931238
11941239 if ( isBase64 ( image ) ) {
11951240 const base64Data = image . replace ( / ^ d a t a : i m a g e \/ ( j p e g | p n g | g i f ) ; b a s e 6 4 , / , '' ) ;
11961241 const imageBuffer = Buffer . from ( base64Data , 'base64' ) ;
1197- imagePath = `${ join ( process . cwd ( ) , 'temp' , ' temp-sticker .png' ) } ` ;
1242+ imagePath = `${ join ( process . cwd ( ) , 'temp' , ` temp-${ timestamp } .png` ) } ` ;
11981243 await sharp ( imageBuffer ) . toFile ( imagePath ) ;
11991244 } else {
12001245 const timestamp = new Date ( ) . getTime ( ) ;
12011246 const url = `${ image } ?timestamp=${ timestamp } ` ;
12021247
12031248 const response = await axios . get ( url , { responseType : 'arraybuffer' } ) ;
12041249 const imageBuffer = Buffer . from ( response . data , 'binary' ) ;
1205- imagePath = `${ join ( process . cwd ( ) , 'temp' , ' temp-sticker .png' ) } ` ;
1250+ imagePath = `${ join ( process . cwd ( ) , 'temp' , ` temp-${ timestamp } .png` ) } ` ;
12061251 await sharp ( imageBuffer ) . toFile ( imagePath ) ;
12071252 }
12081253
0 commit comments