mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-26 10:28:38 -06:00
Update whatsapp.service.ts
This commit is contained in:
parent
48e7a410a2
commit
39178b534a
@ -40,7 +40,6 @@ import EventEmitter2 from 'eventemitter2';
|
|||||||
import fs, { existsSync, readFileSync } from 'fs';
|
import fs, { existsSync, readFileSync } from 'fs';
|
||||||
import Long from 'long';
|
import Long from 'long';
|
||||||
import NodeCache from 'node-cache';
|
import NodeCache from 'node-cache';
|
||||||
import { getMIMEType } from 'node-mime-types';
|
|
||||||
import { release } from 'os';
|
import { release } from 'os';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import P from 'pino';
|
import P from 'pino';
|
||||||
@ -66,7 +65,7 @@ import {
|
|||||||
import { Logger } from '../../config/logger.config';
|
import { Logger } from '../../config/logger.config';
|
||||||
import { INSTANCE_DIR, ROOT_DIR } from '../../config/path.config';
|
import { INSTANCE_DIR, ROOT_DIR } from '../../config/path.config';
|
||||||
import { BadRequestException, InternalServerErrorException, NotFoundException } from '../../exceptions';
|
import { BadRequestException, InternalServerErrorException, NotFoundException } from '../../exceptions';
|
||||||
import { getAMQP } from '../../libs/amqp.server';
|
import { getAMQP, removeQueues } from '../../libs/amqp.server';
|
||||||
import { dbserver } from '../../libs/db.connect';
|
import { dbserver } from '../../libs/db.connect';
|
||||||
import { RedisCache } from '../../libs/redis.client';
|
import { RedisCache } from '../../libs/redis.client';
|
||||||
import { getIO } from '../../libs/socket.server';
|
import { getIO } from '../../libs/socket.server';
|
||||||
@ -495,6 +494,14 @@ export class WAStartupService {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async removeRabbitmqQueues() {
|
||||||
|
this.logger.verbose('Removing rabbitmq');
|
||||||
|
|
||||||
|
if (this.localRabbitmq.enabled) {
|
||||||
|
removeQueues(this.instanceName, this.localRabbitmq.events);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async loadTypebot() {
|
private async loadTypebot() {
|
||||||
this.logger.verbose('Loading typebot');
|
this.logger.verbose('Loading typebot');
|
||||||
const data = await this.repository.typebot.find(this.instanceName);
|
const data = await this.repository.typebot.find(this.instanceName);
|
||||||
@ -1240,6 +1247,74 @@ export class WAStartupService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async reloadConnection(): Promise<WASocket> {
|
||||||
|
try {
|
||||||
|
this.instance.authState = await this.defineAuthState();
|
||||||
|
|
||||||
|
const { version } = await fetchLatestBaileysVersion();
|
||||||
|
const session = this.configService.get<ConfigSessionPhone>('CONFIG_SESSION_PHONE');
|
||||||
|
const browser: WABrowserDescription = [session.CLIENT, session.NAME, release()];
|
||||||
|
|
||||||
|
let options;
|
||||||
|
|
||||||
|
if (this.localProxy.enabled) {
|
||||||
|
this.logger.verbose('Proxy enabled');
|
||||||
|
options = {
|
||||||
|
agent: new ProxyAgent(this.localProxy.proxy as any),
|
||||||
|
fetchAgent: new ProxyAgent(this.localProxy.proxy as any),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const socketConfig: UserFacingSocketConfig = {
|
||||||
|
...options,
|
||||||
|
auth: {
|
||||||
|
creds: this.instance.authState.state.creds,
|
||||||
|
keys: makeCacheableSignalKeyStore(this.instance.authState.state.keys, P({ level: 'error' })),
|
||||||
|
},
|
||||||
|
logger: P({ level: this.logBaileys }),
|
||||||
|
printQRInTerminal: false,
|
||||||
|
browser,
|
||||||
|
version,
|
||||||
|
markOnlineOnConnect: this.localSettings.always_online,
|
||||||
|
connectTimeoutMs: 60_000,
|
||||||
|
qrTimeout: 40_000,
|
||||||
|
defaultQueryTimeoutMs: undefined,
|
||||||
|
emitOwnEvents: false,
|
||||||
|
msgRetryCounterCache: this.msgRetryCounterCache,
|
||||||
|
getMessage: async (key) => (await this.getMessage(key)) as Promise<proto.IMessage>,
|
||||||
|
generateHighQualityLinkPreview: true,
|
||||||
|
syncFullHistory: true,
|
||||||
|
userDevicesCache: this.userDevicesCache,
|
||||||
|
transactionOpts: { maxCommitRetries: 1, delayBetweenTriesMs: 10 },
|
||||||
|
patchMessageBeforeSending: (message) => {
|
||||||
|
const requiresPatch = !!(message.buttonsMessage || message.listMessage || message.templateMessage);
|
||||||
|
if (requiresPatch) {
|
||||||
|
message = {
|
||||||
|
viewOnceMessageV2: {
|
||||||
|
message: {
|
||||||
|
messageContextInfo: {
|
||||||
|
deviceListMetadataVersion: 2,
|
||||||
|
deviceListMetadata: {},
|
||||||
|
},
|
||||||
|
...message,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return message;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
this.client = makeWASocket(socketConfig);
|
||||||
|
|
||||||
|
return this.client;
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(error);
|
||||||
|
throw new InternalServerErrorException(error?.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private readonly chatHandle = {
|
private readonly chatHandle = {
|
||||||
'chats.upsert': async (chats: Chat[], database: Database) => {
|
'chats.upsert': async (chats: Chat[], database: Database) => {
|
||||||
this.logger.verbose('Event received: chats.upsert');
|
this.logger.verbose('Event received: chats.upsert');
|
||||||
@ -2308,36 +2383,18 @@ export class WAStartupService {
|
|||||||
mediaMessage.fileName = arrayMatch[1];
|
mediaMessage.fileName = arrayMatch[1];
|
||||||
this.logger.verbose('File name: ' + mediaMessage.fileName);
|
this.logger.verbose('File name: ' + mediaMessage.fileName);
|
||||||
}
|
}
|
||||||
|
let mimetype: string;
|
||||||
|
|
||||||
if (mediaMessage.mediatype === 'image' && !mediaMessage.fileName) {
|
if (mediaMessage.mediatype === 'image' && !mediaMessage.fileName) {
|
||||||
mediaMessage.fileName = 'image.png';
|
mediaMessage.fileName = 'image.png';
|
||||||
|
mimetype = 'image/png';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mediaMessage.mediatype === 'video' && !mediaMessage.fileName) {
|
if (mediaMessage.mediatype === 'video' && !mediaMessage.fileName) {
|
||||||
mediaMessage.fileName = 'video.mp4';
|
mediaMessage.fileName = 'video.mp4';
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
let mimetype: string;
|
|
||||||
|
|
||||||
// novo critério para adotar mimetype quando nao está presente na url e no filename - inicio
|
|
||||||
if (isURL(mediaMessage.media) || mediaMessage.fileName) {
|
|
||||||
if (isURL(mediaMessage.media)) {
|
|
||||||
mimetype = getMIMEType(mediaMessage.media);
|
|
||||||
} else {
|
|
||||||
mimetype = getMIMEType(mediaMessage.fileName);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (mediaMessage.mediatype === 'image') {
|
|
||||||
mimetype = 'image/png';
|
|
||||||
}
|
|
||||||
if (mediaMessage.mediatype === 'video') {
|
|
||||||
mimetype = 'video/mp4';
|
mimetype = 'video/mp4';
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// novo critério para adotar mimetype quando nao está presente na url e no filename - fim
|
|
||||||
|
|
||||||
this.logger.verbose('Mimetype: ' + mimetype);
|
this.logger.verbose('Mimetype: ' + mimetype);
|
||||||
|
|
||||||
prepareMedia[mediaType].caption = mediaMessage?.caption;
|
prepareMedia[mediaType].caption = mediaMessage?.caption;
|
||||||
@ -2713,6 +2770,7 @@ export class WAStartupService {
|
|||||||
|
|
||||||
public async markMessageAsRead(data: ReadMessageDto) {
|
public async markMessageAsRead(data: ReadMessageDto) {
|
||||||
this.logger.verbose('Marking message as read');
|
this.logger.verbose('Marking message as read');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const keys: proto.IMessageKey[] = [];
|
const keys: proto.IMessageKey[] = [];
|
||||||
data.read_messages.forEach((read) => {
|
data.read_messages.forEach((read) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user