Skip to content

Commit

Permalink
Configurable file download limit - fix #130
Browse files Browse the repository at this point in the history
  • Loading branch information
allgood committed Jul 26, 2024
1 parent d732507 commit 36a789e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
29 changes: 24 additions & 5 deletions backend/src/services/WbotServices/wbotMessageListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import { cacheLayer } from "../../libs/cache";
import { debounce } from "../../helpers/Debounce";
import { getMessageOptions } from "./SendWhatsAppMedia";
import { makeRandomId } from "../../helpers/MakeRandomId";
import { GetCompanySetting } from "../../helpers/CheckSettings";
import CheckSettings, { GetCompanySetting } from "../../helpers/CheckSettings";
import Whatsapp from "../../models/Whatsapp";

type Session = WASocket & {
Expand Down Expand Up @@ -287,14 +287,32 @@ const getMessageMedia = (message: proto.IMessage) => {
);
}

const downloadMedia = async (msg: proto.IWebMessageInfo) => {
const downloadMedia = async (msg: proto.IWebMessageInfo, wbot: Session, ticket: Ticket) => {
const unpackedMessage = getUnpackedMessage(msg);
const message = getMessageMedia(unpackedMessage);

if (!message) {
return null;
}

const fileLimit = parseInt(await CheckSettings("downloadLimit", "15"), 10);
if (wbot && message?.fileLength && +message.fileLength > fileLimit*1024*1024) {
const fileLimitMessage = {
text: `\u200e*Mensagem Automática*:\nNosso sistema aceita apenas arquivos com no máximo ${fileLimit} MiB`
};

const sendMsg = await wbot.sendMessage(
`${ticket.contact.number}@${ticket.isGroup ? "g.us" : "s.whatsapp.net"}`,
fileLimitMessage
);

sendMsg.message.extendedTextMessage.text = "\u200e*Mensagem do sistema*:\nArquivo recebido além do limite de tamanho do sistema, se for necessário ele pode ser obtido no aplicativo do whatsapp.";

// eslint-disable-next-line no-use-before-define
await verifyMessage(sendMsg, ticket, ticket.contact);
throw new Error("ERR_FILESIZE_OVER_LIMIT");
}

// eslint-disable-next-line no-nested-ternary
const messageType = unpackedMessage?.documentMessage
? "document"
Expand Down Expand Up @@ -429,11 +447,12 @@ const verifyQuotedMessage = async (
const verifyMediaMessage = async (
msg: proto.IWebMessageInfo,
ticket: Ticket,
contact: Contact
contact: Contact,
wbot: Session = null
): Promise<Message> => {
const io = getIO();
const quotedMsg = await verifyQuotedMessage(msg);
const media = await downloadMedia(msg);
const media = await downloadMedia(msg, wbot, ticket);

if (!media) {
throw new Error("ERR_WAPP_DOWNLOAD_MEDIA");
Expand Down Expand Up @@ -1443,7 +1462,7 @@ const handleMessage = async (


if (messageMedia) {
await verifyMediaMessage(msg, ticket, contact);
await verifyMediaMessage(msg, ticket, contact, wbot);
} else if (msg.message?.editedMessage?.message?.protocolMessage?.editedMessage) {
// message edited by Whatsapp App
await verifyEditedMessage(msg.message.editedMessage.message.protocolMessage.editedMessage, ticket, msg.message.editedMessage.message.protocolMessage.key.id);
Expand Down
43 changes: 42 additions & 1 deletion frontend/src/components/Settings/Options.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useState, useRef } from "react";

import Grid from "@material-ui/core/Grid";
import MenuItem from "@material-ui/core/MenuItem";
Expand Down Expand Up @@ -102,6 +102,7 @@ export default function Options(props) {
const [chatbotAutoExit, setChatbotAutoExit] = useState("disabled");
const [CheckMsgIsGroup, setCheckMsgIsGroupType] = useState("enabled");
const [apiToken, setApiToken] = useState("");
const [downloadLimit, setDownloadLimit] = useState("15");

const [loadingUserRating, setLoadingUserRating] = useState(false);
const [loadingScheduleType, setLoadingScheduleType] = useState(false);
Expand All @@ -112,9 +113,12 @@ export default function Options(props) {
const [loadingChatbotAutoExit, setLoadingChatbotAutoExit] = useState(false);
const [loadingCheckMsgIsGroup, setCheckMsgIsGroup] = useState(false);
const [loadingApiToken, setLoadingApiToken] = useState(false);
const [loadingDownloadLimit, setLoadingDownloadLimit] = useState(false);
const { getCurrentUserInfo } = useAuth();
const [currentUser, setCurrentUser] = useState({});

const downloadLimitInput = useRef(null);

const { update } = useSettings();

useEffect(() => {
Expand Down Expand Up @@ -158,6 +162,9 @@ export default function Options(props) {

const apiToken = settings.find((s) => s.key === "apiToken");
setApiToken(apiToken?.value || "");

const downloadLimit = settings.find((s) => s.key === "downloadLimit");
setDownloadLimit(downloadLimit?.value || "");
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [settings]);
Expand Down Expand Up @@ -251,6 +258,17 @@ export default function Options(props) {
setLoadingAllowSignup(false);
}

async function handleDownloadLimit(value) {
setDownloadLimit(value);
setLoadingDownloadLimit(true);
await update({
key: "downloadLimit",
value,
});
toast.success("Operação atualizada com sucesso.");
setLoadingDownloadLimit(false);
}

async function generateApiToken() {
const newToken = generateSecureToken(32);
setApiToken(newToken);
Expand Down Expand Up @@ -495,6 +513,7 @@ export default function Options(props) {
<OnlyForSuperUser
user={currentUser}
yes={() => (
<>
<Grid xs={12} sm={6} md={4} item>
<FormControl className={classes.selectContainer}>
<InputLabel id="group-type-label">
Expand All @@ -515,6 +534,28 @@ export default function Options(props) {
</FormHelperText>
</FormControl>
</Grid>


<Grid xs={12} sm={6} md={4} item>
<FormControl className={classes.selectContainer}>
<TextField
id="appname-field"
label="Limite de Download de arquivos (MB)"
variant="standard"
name="appName"
value={downloadLimit}
inputRef={downloadLimitInput}
onChange={(e) => {
setDownloadLimit(e.target.value);
}}
onBlur={async (_) => {
await handleDownloadLimit(downloadLimit);
}}
/>
</FormControl>
</Grid>
</>

)}
/>
</Grid>
Expand Down

0 comments on commit 36a789e

Please sign in to comment.