Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/config.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { GlobalWebhookConfigConfig } from '@waha/core/config/GlobalWebhookConfig';
import { IGNORE_ALL_MEDIA_MIMETYPE } from '@waha/core/media/MediaManager';
import { IgnoreJidConfig } from '@waha/core/utils/jids';

import { parseBool } from './helpers';
Expand Down Expand Up @@ -65,7 +66,7 @@ export class WhatsappConfigService implements OnApplicationBootstrap {

get mimetypes(): string[] {
if (!this.shouldDownloadMedia) {
return ['mimetype/ignore-all-media'];
return [IGNORE_ALL_MEDIA_MIMETYPE];
}
const types = this.configService.get('WHATSAPP_FILES_MIMETYPES', '');
return types ? types.split(',') : [];
Expand Down
42 changes: 33 additions & 9 deletions src/core/engines/gows/session.gows.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2110,7 +2110,13 @@ export class WhatsappSessionGoWSCore extends WhatsappSession {
},
Message: channelMessage.Message,
};
const message = await this.processIncomingMessage(msg, downloadMedia);
// An explicit downloadMedia=true forces the download even when media is
// globally disabled.
const message = await this.processIncomingMessage(
msg,
downloadMedia,
downloadMedia,
);
const reactions: any =
sortObjectByValues(channelMessage.ReactionCounts) || {};
return {
Expand Down Expand Up @@ -2492,7 +2498,11 @@ export class WhatsappSessionGoWSCore extends WhatsappSession {
const msgs = parseJsonList(response);
const promises = [];
for (const msg of msgs) {
promises.push(this.processIncomingMessage(msg, downloadMedia));
// An explicit downloadMedia=true forces the download even when media is
// globally disabled.
promises.push(
this.processIncomingMessage(msg, downloadMedia, downloadMedia),
);
}
let result = await Promise.all(promises);
result = result.filter(Boolean);
Expand All @@ -2519,7 +2529,13 @@ export class WhatsappSessionGoWSCore extends WhatsappSession {
});
const response = await promisify(this.client.GetMessageById)(request);
const msg = parseJson(response);
return this.processIncomingMessage(msg, query.downloadMedia);
// An explicit downloadMedia=true forces the download even when media is
// globally disabled.
return this.processIncomingMessage(
msg,
query.downloadMedia,
query.downloadMedia,
);
}

/**
Expand Down Expand Up @@ -2683,7 +2699,14 @@ export class WhatsappSessionGoWSCore extends WhatsappSession {
return true;
}

protected async processIncomingMessage(message, downloadMedia = true) {
// `force` comes from an explicit downloadMedia=true on the request and
// downloads the media even when media download is globally disabled
// (WHATSAPP_DOWNLOAD_MEDIA=false).
protected async processIncomingMessage(
message,
downloadMedia = true,
force = false,
) {
// Filter
if (!this.shouldProcessIncomingMessage(message)) {
return null;
Expand All @@ -2692,7 +2715,7 @@ export class WhatsappSessionGoWSCore extends WhatsappSession {
const wamessage = this.toWAMessage(message);
// Media
if (downloadMedia) {
const media = await this.downloadMediaSafe(message);
const media = await this.downloadMediaSafe(message, force);
wamessage.media = media;
}
if (downloadMedia && wamessage.replyTo?.hasMedia) {
Expand All @@ -2703,22 +2726,22 @@ export class WhatsappSessionGoWSCore extends WhatsappSession {
ID: wamessage.replyTo.id || '',
},
};
wamessage.replyTo.media = await this.downloadMediaSafe(msg);
wamessage.replyTo.media = await this.downloadMediaSafe(msg, force);
}
return wamessage;
}

protected async downloadMediaSafe(message) {
protected async downloadMediaSafe(message, force = false) {
try {
return await this.downloadMedia(message);
return await this.downloadMedia(message, force);
} catch (e) {
this.logger.error('Failed when tried to download media for a message');
this.logger.error(e, e.stack);
return null;
}
}

protected async downloadMedia(message) {
protected async downloadMedia(message, force = false) {
let processor: IMediaEngineProcessor<any> = new GOWSEngineMediaProcessor(
this,
);
Expand All @@ -2727,6 +2750,7 @@ export class WhatsappSessionGoWSCore extends WhatsappSession {
processor,
message,
this.name,
force,
);
return media;
}
Expand Down
36 changes: 28 additions & 8 deletions src/core/engines/noweb/session.noweb.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,11 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {

const promises = [];
for (const msg of messages) {
promises.push(this.processIncomingMessage(msg, downloadMedia));
// An explicit downloadMedia=true forces the download even when media is
// globally disabled.
promises.push(
this.processIncomingMessage(msg, downloadMedia, downloadMedia),
);
}
let result = await Promise.all(promises);
result = result.filter(Boolean);
Expand Down Expand Up @@ -1496,7 +1500,13 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
merge,
);
if (!message) return null;
return await this.processIncomingMessage(message, query.downloadMedia);
// An explicit downloadMedia=true forces the download even when media is
// globally disabled.
return await this.processIncomingMessage(
message,
query.downloadMedia,
query.downloadMedia,
);
}

@Activity()
Expand Down Expand Up @@ -3071,9 +3081,13 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
return '';
}

// `force` comes from an explicit downloadMedia=true on the request and
// downloads the media even when media download is globally disabled
// (WHATSAPP_DOWNLOAD_MEDIA=false).
protected async processIncomingMessage(
message,
downloadMedia: boolean,
force = false,
): Promise<WAMessage | null> {
// Filter
if (!this.shouldProcessIncomingMessage(message)) {
Expand All @@ -3086,7 +3100,7 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
}
// Media
if (downloadMedia && wamessage.hasMedia) {
wamessage.media = await this.downloadMediaSafe(message);
wamessage.media = await this.downloadMediaSafe(message, force);
}

if (downloadMedia && wamessage.replyTo?.hasMedia) {
Expand All @@ -3102,7 +3116,7 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
remoteJid: message.key.remoteJid,
},
};
wamessage.replyTo.media = await this.downloadMediaSafe(m);
wamessage.replyTo.media = await this.downloadMediaSafe(m, force);
}
return wamessage;
}
Expand Down Expand Up @@ -3357,23 +3371,29 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
return { id: chatId, presences: presences };
}

protected async downloadMediaSafe(message): Promise<WAMedia | null> {
protected async downloadMediaSafe(
message,
force = false,
): Promise<WAMedia | null> {
try {
return await this.downloadMedia(message);
return await this.downloadMedia(message, force);
} catch (e) {
this.logger.error('Failed when tried to download media for a message');
this.logger.error(e, e.stack);
}
return null;
}

protected async downloadMedia(message): Promise<WAMedia | null> {
protected async downloadMedia(
message,
force = false,
): Promise<WAMedia | null> {
let processor: IMediaEngineProcessor<any> = new NOWEBEngineMediaProcessor(
this,
this.loggerBuilder,
);
processor = new LottieMediaProcessorWrapper(processor, this.logger);
return this.mediaManager.processMedia(processor, message, this.name);
return this.mediaManager.processMedia(processor, message, this.name, force);
}

protected async getMessageOptions(request: {
Expand Down
38 changes: 31 additions & 7 deletions src/core/engines/webjs/session.webjs.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,11 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
);
const promises = [];
for (const msg of messages) {
promises.push(this.processIncomingMessage(msg, downloadMedia));
// An explicit downloadMedia=true forces the download even when media is
// globally disabled.
promises.push(
this.processIncomingMessage(msg, downloadMedia, downloadMedia),
);
}
let result = await Promise.all(promises);
result = result.filter(Boolean);
Expand Down Expand Up @@ -1284,7 +1288,13 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
return null;
});
}
return await this.processIncomingMessage(message, query.downloadMedia);
// An explicit downloadMedia=true forces the download even when media is
// globally disabled.
return await this.processIncomingMessage(
message,
query.downloadMedia,
query.downloadMedia,
);
}

@Activity()
Expand Down Expand Up @@ -1735,9 +1745,12 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
channelMessage: WebjsChannelMessage,
downloadMedia: boolean,
): Promise<ChannelMessage> {
// An explicit downloadMedia=true forces the download even when media is
// globally disabled.
const message = await this.processIncomingMessage(
channelMessage.message,
downloadMedia,
downloadMedia,
);
const reactions = {};
for (const reaction of channelMessage.reactions.sort((x) => -x.count)) {
Expand Down Expand Up @@ -2292,21 +2305,28 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
.switch(this.callRejected$.asObservable());
}

// `force` comes from an explicit downloadMedia=true on the request and
// downloads the media even when media download is globally disabled
// (WHATSAPP_DOWNLOAD_MEDIA=false).
protected async processIncomingMessage(
message: Message,
downloadMedia = true,
force = false,
) {
// Convert
const wamessage = this.toWAMessage(message);
// Media
if (downloadMedia) {
const media = await this.downloadMediaSafe(message);
const media = await this.downloadMediaSafe(message, force);
wamessage.media = media;
}
if (downloadMedia && wamessage.replyTo?.hasMedia) {
const quotedMessage = await message.getQuotedMessage().catch(() => null);
if (quotedMessage) {
wamessage.replyTo.media = await this.downloadMediaSafe(quotedMessage);
wamessage.replyTo.media = await this.downloadMediaSafe(
quotedMessage,
force,
);
}
}
return wamessage;
Expand Down Expand Up @@ -2524,23 +2544,27 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
return contact;
}

protected async downloadMediaSafe(message): Promise<WAMedia | null> {
protected async downloadMediaSafe(
message,
force = false,
): Promise<WAMedia | null> {
try {
return await this.downloadMedia(message);
return await this.downloadMedia(message, force);
} catch (e) {
this.logger.error('Failed when tried to download media for a message');
this.logger.error(e, e.stack);
}
return null;
}

protected async downloadMedia(message: Message) {
protected async downloadMedia(message: Message, force = false) {
let processor = new WEBJSEngineMediaProcessor();
processor = new LottieMediaProcessorWrapper(processor, this.logger);
const media = await this.mediaManager.processMedia(
processor,
message,
this.name,
force,
);
return media;
}
Expand Down
38 changes: 30 additions & 8 deletions src/core/engines/wpp/session.wpp.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,13 @@ export class WhatsappSessionWPPCore extends WhatsappSession {
if (!message) {
return null;
}
return this.processIncomingMessage(message, query.downloadMedia);
// An explicit downloadMedia=true forces the download even when media is
// globally disabled.
return this.processIncomingMessage(
message,
query.downloadMedia,
query.downloadMedia,
);
}

@Activity()
Expand Down Expand Up @@ -2346,25 +2352,38 @@ export class WhatsappSessionWPPCore extends WhatsappSession {
};
}

protected async processIncomingMessage(message: any, downloadMedia = true) {
// `force` comes from an explicit downloadMedia=true on the request and
// downloads the media even when media download is globally disabled
// (WHATSAPP_DOWNLOAD_MEDIA=false).
protected async processIncomingMessage(
message: any,
downloadMedia = true,
force = false,
) {
const wamessage = this.toWAMessage(message);
if (downloadMedia) {
const media = await this.downloadMediaSafe(message);
const media = await this.downloadMediaSafe(message, force);
wamessage.media = media;
}
if (downloadMedia && wamessage.replyTo?.hasMedia) {
const quotedMessage = message?.quotedMsg || message?._data?.quotedMsg;
if (quotedMessage) {
wamessage.replyTo.media = await this.downloadMediaSafe(quotedMessage);
wamessage.replyTo.media = await this.downloadMediaSafe(
quotedMessage,
force,
);
}
}
return wamessage;
}

protected async downloadMedia(message: any): Promise<WAMedia | null> {
protected async downloadMedia(
message: any,
force = false,
): Promise<WAMedia | null> {
let processor = new WPPEngineMediaProcessor(this.wpp);
processor = new LottieMediaProcessorWrapper(processor, this.logger);
return this.mediaManager.processMedia(processor, message, this.name);
return this.mediaManager.processMedia(processor, message, this.name, force);
}

protected checkStatusRequest(request: { contacts?: any[] }) {
Expand All @@ -2375,9 +2394,12 @@ export class WhatsappSessionWPPCore extends WhatsappSession {
}
}

protected async downloadMediaSafe(message): Promise<WAMedia | null> {
protected async downloadMediaSafe(
message,
force = false,
): Promise<WAMedia | null> {
try {
return await this.downloadMedia(message);
return await this.downloadMedia(message, force);
} catch (error) {
this.logger.error('Failed when tried to download media for a message');
this.logger.error(error, error.stack);
Expand Down
1 change: 1 addition & 0 deletions src/core/media/IMediaManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface IMediaManager {
processor: IMediaEngineProcessor<Message>,
message: Message,
session: string,
force?: boolean,
): Promise<WAMedia | null>;
close(): void;
}
Expand Down
Loading