Skip to content

Commit 3645ac6

Browse files
committed
Merge branch 'develop' of https://github.com/EvolutionAPI/evolution-api into develop
2 parents 9764719 + b3aeed7 commit 3645ac6

File tree

12 files changed

+69
-33
lines changed

12 files changed

+69
-33
lines changed

src/api/controllers/instance.controller.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export class InstanceController {
4343
private readonly proxyService: ProxyController,
4444
private readonly cache: RedisCache,
4545
private readonly chatwootCache: CacheService,
46+
private readonly messagesLostCache: CacheService,
4647
) {}
4748

4849
private readonly logger = new Logger(InstanceController.name);
@@ -108,6 +109,7 @@ export class InstanceController {
108109
this.repository,
109110
this.cache,
110111
this.chatwootCache,
112+
this.messagesLostCache,
111113
);
112114
} else {
113115
instance = new BaileysStartupService(
@@ -116,6 +118,7 @@ export class InstanceController {
116118
this.repository,
117119
this.cache,
118120
this.chatwootCache,
121+
this.messagesLostCache,
119122
);
120123
}
121124

src/api/integrations/chatwoot/controllers/chatwoot.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { isURL } from 'class-validator';
22

3+
import { CacheEngine } from '../../../../cache/cacheengine';
34
import { ConfigService, HttpServer } from '../../../../config/env.config';
45
import { Logger } from '../../../../config/logger.config';
56
import { BadRequestException } from '../../../../exceptions';
67
import { InstanceDto } from '../../../dto/instance.dto';
78
import { RepositoryBroker } from '../../../repository/repository.manager';
89
import { waMonitor } from '../../../server.module';
910
import { CacheService } from '../../../services/cache.service';
10-
import { CacheEngine } from '../cache/cacheengine';
1111
import { ChatwootDto } from '../dto/chatwoot.dto';
1212
import { ChatwootService } from '../services/chatwoot.service';
1313

src/api/server.module.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { CacheEngine } from '../cache/cacheengine';
12
import { configService } from '../config/env.config';
23
import { eventEmitter } from '../config/event.config';
34
import { Logger } from '../config/logger.config';
@@ -14,7 +15,6 @@ import { WebhookController } from './controllers/webhook.controller';
1415
import { ChamaaiController } from './integrations/chamaai/controllers/chamaai.controller';
1516
import { ChamaaiRepository } from './integrations/chamaai/repository/chamaai.repository';
1617
import { ChamaaiService } from './integrations/chamaai/services/chamaai.service';
17-
import { CacheEngine } from './integrations/chatwoot/cache/cacheengine';
1818
import { ChatwootController } from './integrations/chatwoot/controllers/chatwoot.controller';
1919
import { ChatwootRepository } from './integrations/chatwoot/repository/chatwoot.repository';
2020
import { ChatwootService } from './integrations/chatwoot/services/chatwoot.service';
@@ -109,8 +109,16 @@ export const repository = new RepositoryBroker(
109109

110110
export const cache = new RedisCache();
111111
const chatwootCache = new CacheService(new CacheEngine(configService, ChatwootService.name).getEngine());
112+
const messagesLostCache = new CacheService(new CacheEngine(configService, 'baileys').getEngine());
112113

113-
export const waMonitor = new WAMonitoringService(eventEmitter, configService, repository, cache, chatwootCache);
114+
export const waMonitor = new WAMonitoringService(
115+
eventEmitter,
116+
configService,
117+
repository,
118+
cache,
119+
chatwootCache,
120+
messagesLostCache,
121+
);
114122

115123
const authService = new AuthService(configService, waMonitor, repository);
116124

@@ -160,6 +168,7 @@ export const instanceController = new InstanceController(
160168
proxyController,
161169
cache,
162170
chatwootCache,
171+
messagesLostCache,
163172
);
164173
export const sendMessageController = new SendMessageController(waMonitor);
165174
export const chatController = new ChatController(waMonitor);

src/api/services/monitor.service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class WAMonitoringService {
3636
private readonly repository: RepositoryBroker,
3737
private readonly cache: RedisCache,
3838
private readonly chatwootCache: CacheService,
39+
private readonly messagesLostCache: CacheService,
3940
) {
4041
this.logger.verbose('instance created');
4142

@@ -346,6 +347,7 @@ export class WAMonitoringService {
346347
this.repository,
347348
this.cache,
348349
this.chatwootCache,
350+
this.messagesLostCache,
349351
);
350352

351353
instance.instanceName = name;
@@ -356,6 +358,7 @@ export class WAMonitoringService {
356358
this.repository,
357359
this.cache,
358360
this.chatwootCache,
361+
this.messagesLostCache,
359362
);
360363

361364
instance.instanceName = name;

src/api/services/whatsapp.service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export class WAStartupService {
7171
public chamaaiService = new ChamaaiService(waMonitor, this.configService);
7272

7373
public set instanceName(name: string) {
74+
this.logger.setInstance(name);
75+
7476
this.logger.verbose(`Initializing instance '${name}'`);
7577
if (!name) {
7678
this.logger.verbose('Instance name not found, generating random name with uuid');

src/api/services/whatsapp/whatsapp.baileys.service.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,24 @@ import { Events, MessageSubtype, TypeMediaMessage, wa } from '../../types/wa.typ
121121
import { CacheService } from './../cache.service';
122122
import { WAStartupService } from './../whatsapp.service';
123123

124-
// const retryCache = {};
125-
126124
export class BaileysStartupService extends WAStartupService {
127125
constructor(
128126
public readonly configService: ConfigService,
129127
public readonly eventEmitter: EventEmitter2,
130128
public readonly repository: RepositoryBroker,
131129
public readonly cache: RedisCache,
132130
public readonly chatwootCache: CacheService,
131+
public readonly messagesLostCache: CacheService,
133132
) {
134133
super(configService, eventEmitter, repository, chatwootCache);
135134
this.logger.verbose('BaileysStartupService initialized');
136135
this.cleanStore();
137136
this.instance.qrcode = { count: 0 };
138137
this.mobile = false;
138+
139+
setTimeout(() => {
140+
this.recoveringMessages();
141+
}, 30000);
139142
}
140143

141144
private readonly msgRetryCounterCache: CacheStore = new NodeCache();
@@ -148,6 +151,18 @@ export class BaileysStartupService extends WAStartupService {
148151
public phoneNumber: string;
149152
public mobile: boolean;
150153

154+
private async recoveringMessages() {
155+
this.logger.info('Recovering messages');
156+
this.messagesLostCache.keys().then((keys) => {
157+
keys.forEach(async (key) => {
158+
const message = await this.messagesLostCache.get(key.split(':')[2]);
159+
160+
if (message.messageStubParameters && message.messageStubParameters[0] === 'Message absent from node')
161+
await this.client.sendMessageAck(JSON.parse(message.messageStubParameters[1], BufferJSON.reviver));
162+
});
163+
});
164+
}
165+
151166
public get connectionStatus() {
152167
this.logger.verbose('Getting connection status');
153168
return this.stateConnection;
@@ -378,10 +393,12 @@ export class BaileysStartupService extends WAStartupService {
378393
│ CONNECTED TO WHATSAPP │
379394
└──────────────────────────────┘`.replace(/^ +/gm, ' '),
380395
);
381-
this.logger.info(`
396+
this.logger.info(
397+
`
382398
wuid: ${formattedWuid}
383399
name: ${formattedName}
384-
`);
400+
`,
401+
);
385402

386403
if (this.localChatwoot.enabled) {
387404
this.chatwootService.eventWhatsapp(
@@ -1046,10 +1063,18 @@ export class BaileysStartupService extends WAStartupService {
10461063

10471064
if (received.messageStubParameters && received.messageStubParameters[0] === 'Message absent from node') {
10481065
this.logger.info('Recovering message lost');
1049-
await this.client.sendMessageAck(JSON.parse(received.messageStubParameters[1], BufferJSON.reviver));
1066+
1067+
await this.messagesLostCache.set(received.key.id, received);
10501068
continue;
10511069
}
10521070

1071+
const retryCache = (await this.messagesLostCache.get(received.key.id)) || null;
1072+
1073+
if (retryCache) {
1074+
this.logger.info('Recovered message lost');
1075+
await this.messagesLostCache.delete(received.key.id);
1076+
}
1077+
10531078
if (
10541079
(type !== 'notify' && type !== 'append') ||
10551080
received.message?.protocolMessage ||
@@ -1248,7 +1273,6 @@ export class BaileysStartupService extends WAStartupService {
12481273
}
12491274
}
12501275

1251-
// if (key.remoteJid !== 'status@broadcast' && !key?.remoteJid?.match(/(:\d+)/)) {
12521276
if (key.remoteJid !== 'status@broadcast') {
12531277
this.logger.verbose('Message update is valid');
12541278

@@ -1479,27 +1503,12 @@ export class BaileysStartupService extends WAStartupService {
14791503
if (events['messages.upsert']) {
14801504
this.logger.verbose('Listening event: messages.upsert');
14811505
const payload = events['messages.upsert'];
1482-
// if (payload.messages.find((a) => a?.messageStubType === 2)) {
1483-
// const msg = payload.messages[0];
1484-
// retryCache[msg.key.id] = msg;
1485-
// return;
1486-
// }
14871506
this.messageHandle['messages.upsert'](payload, database, settings);
14881507
}
14891508

14901509
if (events['messages.update']) {
14911510
this.logger.verbose('Listening event: messages.update');
14921511
const payload = events['messages.update'];
1493-
// payload.forEach((message) => {
1494-
// if (retryCache[message.key.id]) {
1495-
// this.client.ev.emit('messages.upsert', {
1496-
// messages: [message],
1497-
// type: 'notify',
1498-
// });
1499-
// delete retryCache[message.key.id];
1500-
// return;
1501-
// }
1502-
// });
15031512
this.messageHandle['messages.update'](payload, database, settings);
15041513
}
15051514

src/api/services/whatsapp/whatsapp.business.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class BusinessStartupService extends WAStartupService {
3636
public readonly repository: RepositoryBroker,
3737
public readonly cache: RedisCache,
3838
public readonly chatwootCache: CacheService,
39+
public readonly messagesLostCache: CacheService,
3940
) {
4041
super(configService, eventEmitter, repository, chatwootCache);
4142
this.logger.verbose('BusinessStartupService initialized');

src/api/integrations/chatwoot/cache/cacheengine.ts renamed to src/cache/cacheengine.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { CacheConf, ConfigService } from '../../../../config/env.config';
2-
import { ICache } from '../../../abstract/abstract.cache';
1+
import { ICache } from '../api/abstract/abstract.cache';
2+
import { CacheConf, ConfigService } from '../config/env.config';
33
import { LocalCache } from './localcache';
44
import { RedisCache } from './rediscache';
55

src/api/integrations/chatwoot/cache/localcache.ts renamed to src/cache/localcache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import NodeCache from 'node-cache';
22

3-
import { CacheConf, CacheConfLocal, ConfigService } from '../../../../config/env.config';
4-
import { ICache } from '../../../abstract/abstract.cache';
3+
import { ICache } from '../api/abstract/abstract.cache';
4+
import { CacheConf, CacheConfLocal, ConfigService } from '../config/env.config';
55

66
export class LocalCache implements ICache {
77
private conf: CacheConfLocal;

src/api/integrations/chatwoot/cache/rediscache.client.ts renamed to src/cache/rediscache.client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createClient, RedisClientType } from 'redis';
22

3-
import { CacheConf, CacheConfRedis, configService } from '../../../../config/env.config';
4-
import { Logger } from '../../../../config/logger.config';
3+
import { CacheConf, CacheConfRedis, configService } from '../config/env.config';
4+
import { Logger } from '../config/logger.config';
55

66
class Redis {
77
private logger = new Logger(Redis.name);

0 commit comments

Comments
 (0)