mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-24 17:38:40 -06:00
refactor(chatwoot): remove message ids cache in chatwoot to use chatwoot's api itself.
Remove use of disc cache to optimize performance. BREAKING CHANGE: to make this, we need to use the param `source_id` from message in chatwoot. This param is only available from api in chatwoot version => 3.4.0.
This commit is contained in:
parent
8b5f73badd
commit
bfa7d429bd
@ -7,7 +7,7 @@ import { ChatwootDto } from '../dto/chatwoot.dto';
|
|||||||
import { InstanceDto } from '../dto/instance.dto';
|
import { InstanceDto } from '../dto/instance.dto';
|
||||||
import { RepositoryBroker } from '../repository/repository.manager';
|
import { RepositoryBroker } from '../repository/repository.manager';
|
||||||
import { ChatwootService } from '../services/chatwoot.service';
|
import { ChatwootService } from '../services/chatwoot.service';
|
||||||
import { instanceController } from '../whatsapp.module';
|
import { waMonitor } from '../whatsapp.module';
|
||||||
|
|
||||||
const logger = new Logger('ChatwootController');
|
const logger = new Logger('ChatwootController');
|
||||||
|
|
||||||
@ -94,7 +94,7 @@ export class ChatwootController {
|
|||||||
|
|
||||||
public async receiveWebhook(instance: InstanceDto, data: any) {
|
public async receiveWebhook(instance: InstanceDto, data: any) {
|
||||||
logger.verbose('requested receiveWebhook from ' + instance.instanceName + ' instance');
|
logger.verbose('requested receiveWebhook from ' + instance.instanceName + ' instance');
|
||||||
const chatwootService = instanceController.getChatwootService();
|
const chatwootService = new ChatwootService(waMonitor, this.configService, this.repository);
|
||||||
|
|
||||||
return chatwootService.receiveWebhook(instance, data);
|
return chatwootService.receiveWebhook(instance, data);
|
||||||
}
|
}
|
||||||
|
@ -659,10 +659,4 @@ export class InstanceController {
|
|||||||
this.logger.verbose('requested refreshToken');
|
this.logger.verbose('requested refreshToken');
|
||||||
return await this.authService.refreshToken(oldToken);
|
return await this.authService.refreshToken(oldToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getChatwootService() {
|
|
||||||
this.logger.verbose('getting chatwootService object instance');
|
|
||||||
|
|
||||||
return this.chatwootService;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,6 @@ import { Events } from '../types/wa.types';
|
|||||||
import { WAMonitoringService } from './monitor.service';
|
import { WAMonitoringService } from './monitor.service';
|
||||||
|
|
||||||
export class ChatwootService {
|
export class ChatwootService {
|
||||||
private messageCache: Record<string, Set<number>>;
|
|
||||||
|
|
||||||
private readonly logger = new Logger(ChatwootService.name);
|
private readonly logger = new Logger(ChatwootService.name);
|
||||||
|
|
||||||
private provider: any;
|
private provider: any;
|
||||||
@ -27,35 +25,7 @@ export class ChatwootService {
|
|||||||
private readonly waMonitor: WAMonitoringService,
|
private readonly waMonitor: WAMonitoringService,
|
||||||
private readonly configService: ConfigService,
|
private readonly configService: ConfigService,
|
||||||
private readonly repository: RepositoryBroker,
|
private readonly repository: RepositoryBroker,
|
||||||
) {
|
) {}
|
||||||
// messageCache is used to support Chatwoot version <= 3.3.1.
|
|
||||||
// after this version we can remove use of message cache and use source_id to check webhook needs to be ignored
|
|
||||||
this.messageCache = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
private isMessageInCache(instance: InstanceDto, id: number, remove = true) {
|
|
||||||
this.logger.verbose('check if message is in cache');
|
|
||||||
if (!this.messageCache[instance.instanceName]) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const hasId = this.messageCache[instance.instanceName].has(id);
|
|
||||||
|
|
||||||
if (remove) {
|
|
||||||
this.messageCache[instance.instanceName].delete(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
return hasId;
|
|
||||||
}
|
|
||||||
|
|
||||||
private addMessageToCache(instance: InstanceDto, id: number) {
|
|
||||||
this.logger.verbose('add message to cache');
|
|
||||||
|
|
||||||
if (!this.messageCache[instance.instanceName]) {
|
|
||||||
this.messageCache[instance.instanceName] = new Set();
|
|
||||||
}
|
|
||||||
this.messageCache[instance.instanceName].add(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async getProvider(instance: InstanceDto) {
|
private async getProvider(instance: InstanceDto) {
|
||||||
this.logger.verbose('get provider to instance: ' + instance.instanceName);
|
this.logger.verbose('get provider to instance: ' + instance.instanceName);
|
||||||
@ -1112,14 +1082,8 @@ export class ChatwootService {
|
|||||||
if (body.message_type === 'outgoing' && body?.conversation?.messages?.length && chatId !== '123456') {
|
if (body.message_type === 'outgoing' && body?.conversation?.messages?.length && chatId !== '123456') {
|
||||||
this.logger.verbose('check if is group');
|
this.logger.verbose('check if is group');
|
||||||
|
|
||||||
// messageCache is used to support Chatwoot version <= 3.3.1.
|
if (body?.conversation?.messages[0]?.source_id?.substring(0, 5) === 'WAID:') {
|
||||||
// after this version we can remove use of message cache and use only source_id value check
|
this.logger.verbose('message sent directly from whatsapp. Webhook ignored.');
|
||||||
// use of source_id is better for performance
|
|
||||||
if (
|
|
||||||
body?.conversation?.messages[0]?.source_id?.substring(0, 5) === 'WAID:' ||
|
|
||||||
this.isMessageInCache(instance, body.id)
|
|
||||||
) {
|
|
||||||
this.logger.verbose('message is cached');
|
|
||||||
return { message: 'bot' };
|
return { message: 'bot' };
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1607,8 +1571,6 @@ export class ChatwootService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.addMessageToCache(instance, send.id);
|
|
||||||
|
|
||||||
return send;
|
return send;
|
||||||
} else {
|
} else {
|
||||||
this.logger.verbose('message is not group');
|
this.logger.verbose('message is not group');
|
||||||
@ -1629,8 +1591,6 @@ export class ChatwootService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.addMessageToCache(instance, send.id);
|
|
||||||
|
|
||||||
return send;
|
return send;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1655,7 +1615,6 @@ export class ChatwootService {
|
|||||||
this.logger.warn('message not sent');
|
this.logger.warn('message not sent');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.addMessageToCache(instance, send.id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@ -1713,8 +1672,6 @@ export class ChatwootService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.addMessageToCache(instance, send.id);
|
|
||||||
|
|
||||||
return send;
|
return send;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1750,8 +1707,6 @@ export class ChatwootService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.addMessageToCache(instance, send.id);
|
|
||||||
|
|
||||||
return send;
|
return send;
|
||||||
} else {
|
} else {
|
||||||
this.logger.verbose('message is not group');
|
this.logger.verbose('message is not group');
|
||||||
@ -1773,8 +1728,6 @@ export class ChatwootService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.addMessageToCache(instance, send.id);
|
|
||||||
|
|
||||||
return send;
|
return send;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,7 +75,6 @@ import { getIO } from '../../libs/socket.server';
|
|||||||
import { getSQS, removeQueues as removeQueuesSQS } from '../../libs/sqs.server';
|
import { getSQS, removeQueues as removeQueuesSQS } from '../../libs/sqs.server';
|
||||||
import { useMultiFileAuthStateDb } from '../../utils/use-multi-file-auth-state-db';
|
import { useMultiFileAuthStateDb } from '../../utils/use-multi-file-auth-state-db';
|
||||||
import { useMultiFileAuthStateRedisDb } from '../../utils/use-multi-file-auth-state-redis-db';
|
import { useMultiFileAuthStateRedisDb } from '../../utils/use-multi-file-auth-state-redis-db';
|
||||||
import { instanceController } from '../../whatsapp/whatsapp.module';
|
|
||||||
import {
|
import {
|
||||||
ArchiveChatDto,
|
ArchiveChatDto,
|
||||||
DeleteMessage,
|
DeleteMessage,
|
||||||
@ -132,6 +131,7 @@ import { RepositoryBroker } from '../repository/repository.manager';
|
|||||||
import { Events, MessageSubtype, TypeMediaMessage, wa } from '../types/wa.types';
|
import { Events, MessageSubtype, TypeMediaMessage, wa } from '../types/wa.types';
|
||||||
import { waMonitor } from '../whatsapp.module';
|
import { waMonitor } from '../whatsapp.module';
|
||||||
import { ChamaaiService } from './chamaai.service';
|
import { ChamaaiService } from './chamaai.service';
|
||||||
|
import { ChatwootService } from './chatwoot.service';
|
||||||
import { TypebotService } from './typebot.service';
|
import { TypebotService } from './typebot.service';
|
||||||
|
|
||||||
const retryCache = {};
|
const retryCache = {};
|
||||||
@ -169,7 +169,7 @@ export class WAStartupService {
|
|||||||
|
|
||||||
private phoneNumber: string;
|
private phoneNumber: string;
|
||||||
|
|
||||||
private chatwootService = instanceController.getChatwootService();
|
private chatwootService = new ChatwootService(waMonitor, this.configService, this.repository);
|
||||||
|
|
||||||
private typebotService = new TypebotService(waMonitor, this.configService, this.eventEmitter);
|
private typebotService = new TypebotService(waMonitor, this.configService, this.eventEmitter);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user