From 9988fc7ecfdc7eca3b1778a1987f1a914425ea9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Phelipe?= <32254362+vphelipe@users.noreply.github.com> Date: Thu, 27 Apr 2023 22:22:23 -0300 Subject: [PATCH] feat: Added WPP.conn.setLimit function (#1069) Co-authored-by: Edgard Lorraine Messias --- src/chat/functions/prepareLinkPreview.ts | 11 ++ src/conn/functions/index.ts | 1 + src/conn/functions/setLimit.ts | 127 ++++++++++++++++++ src/util/linkPreview.ts | 10 +- .../functions/getABPropConfigValue.ts | 33 +++++ src/whatsapp/functions/getNumChatsPinned.ts | 32 +++++ src/whatsapp/functions/index.ts | 2 + src/whatsapp/misc/ServerProps.ts | 31 +++++ src/whatsapp/misc/index.ts | 1 + src/whatsapp/models/ServerPropsModel.ts | 62 +++++++++ src/whatsapp/models/index.ts | 1 + 11 files changed, 306 insertions(+), 5 deletions(-) create mode 100644 src/conn/functions/setLimit.ts create mode 100644 src/whatsapp/functions/getABPropConfigValue.ts create mode 100644 src/whatsapp/functions/getNumChatsPinned.ts create mode 100644 src/whatsapp/misc/ServerProps.ts create mode 100644 src/whatsapp/models/ServerPropsModel.ts diff --git a/src/chat/functions/prepareLinkPreview.ts b/src/chat/functions/prepareLinkPreview.ts index 24f45586ff..65fca7b74a 100644 --- a/src/chat/functions/prepareLinkPreview.ts +++ b/src/chat/functions/prepareLinkPreview.ts @@ -24,6 +24,7 @@ import { fetchLinkPreview, findFirstWebLink, genMinimalLinkPreview, + getABPropConfigValue } from '../../whatsapp/functions'; import { RawMessage } from '..'; @@ -105,6 +106,16 @@ export async function prepareLinkPreview( } webpack.onReady(() => { + + wrapModuleFunction(getABPropConfigValue, (func, ...args) => { + const [key] = args; + switch (key) { + case "high_quality_link_preview_enabled": return true + case "link_preview_wait_time": return 1 + } + return func(...args); + }); + wrapModuleFunction(genMinimalLinkPreview, async (func, ...args) => { const [uri] = args; diff --git a/src/conn/functions/index.ts b/src/conn/functions/index.ts index ffbe39360f..71305b79af 100644 --- a/src/conn/functions/index.ts +++ b/src/conn/functions/index.ts @@ -35,3 +35,4 @@ export { needsUpdate } from './needsUpdate'; export { refreshQR } from './refreshQR'; export { setKeepAlive } from './setKeepAlive'; export { setMultiDevice } from './setMultiDevice'; +export { setLimit } from './setLimit'; diff --git a/src/conn/functions/setLimit.ts b/src/conn/functions/setLimit.ts new file mode 100644 index 0000000000..3692f69045 --- /dev/null +++ b/src/conn/functions/setLimit.ts @@ -0,0 +1,127 @@ +/*! + * Copyright 2021 WPPConnect Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { wrapModuleFunction } from '../../whatsapp/exportModule'; +import { WPPError } from '../../util'; +import { ServerProps } from '../../whatsapp'; +import { getNumChatsPinned } from '../../whatsapp/functions'; +import * as webpack from '../../webpack'; + +/** + * Change limits + * + * @example + * ```javascript + * //Change the maximum size (bytes) for uploading media (max 70MB) + * WPP.conn.setLimit('maxMediaSize',16777216); + * + * //Change the maximum size (bytes) for uploading files (max 1GB) + * WPP.conn.setLimit('maxFileSize',104857600); + * + * //Change the maximum number of contacts that can be selected when sharing (Default 5) + * WPP.conn.setLimit('maxShare',100); + * + * //Change the maximum time (seconds) of a video status + * WPP.conn.setLimit('statusVideoMaxDuration',120); + * + * //Remove pinned conversation limit (only whatsapp web) (Default 3) + * WPP.conn.setLimit('unlimitedPin',true); + * ``` + */ + +let unlimitedPin: undefined | boolean = undefined; + +webpack.onInjected(() => { + + wrapModuleFunction(getNumChatsPinned, (func,...args) => { + const getNumChatsPinnedOriginal = func(...args); + return unlimitedPin ? 1 : getNumChatsPinnedOriginal + }); + +}); + +export function setLimit(key: string, value: boolean | number): any { + switch (key) { + + case "maxMediaSize": { + if (typeof value !== 'number' || value > 73400320) { + throw new WPPError( + `maxMediaSize_error`, + typeof value !== 'number' ? `Value type invalid!` : `Maximum value is 70MB` + ); + } + ServerProps.media = value; + return ServerProps.media; + } + + case "maxFileSize": { + if (typeof value !== 'number' || (value > 1073741824)) { + throw new WPPError( + `maxFileSize_error`, + typeof value !== 'number' ? `Value type invalid!`: `Maximum value is 1GB` + ); + } + ServerProps.maxFileSize = value + return ServerProps.maxFileSize + } + + case "maxShare": { + if (typeof value !== 'number') { + throw new WPPError( + `maxShare_error`, + `Value type invalid!` + ); + } + ServerProps.multicastLimitGlobal = value; + ServerProps.frequentlyForwardedMax = value; + ServerProps.frequentlyForwardedThreshold = value; + return ServerProps.multicastLimitGlobal; + } + + case "statusVideoMaxDuration": { + if (typeof value !== 'number') { + throw new WPPError( + `statusVideoMaxDuration_error`, + `Value type invalid!` + ); + } + ServerProps.statusVideoMaxDuration = value; + return ServerProps.statusVideoMaxDuration; + } + + case "unlimitedPin": { + if (typeof value !== 'boolean') { + throw new WPPError( + `unlimitedPin_error`, + `Value type invalid!` + ); + } + value ? unlimitedPin = value : unlimitedPin = undefined; + return value + } + + default: { + throw new WPPError( + `setLimit_error`, + `Key type invalid!` + ); + } + + } + +} + + diff --git a/src/util/linkPreview.ts b/src/util/linkPreview.ts index b7b4b8c870..d9fde888c1 100644 --- a/src/util/linkPreview.ts +++ b/src/util/linkPreview.ts @@ -156,11 +156,11 @@ export async function generateThumbnailLinkPreviewData(url: string) { const thumbnail = await generateThumbnail(download.data); // Only display High Quality in link preview for wide images - if (download.width / download.height < 1.4) { - return { - thumbnail, - }; - } + // if (download.width / download.height < 1.4) { + // return { + // thumbnail, + // }; + // } const thumbnailHQ = download.data.replace('data:image/jpeg;base64,', ''); diff --git a/src/whatsapp/functions/getABPropConfigValue.ts b/src/whatsapp/functions/getABPropConfigValue.ts new file mode 100644 index 0000000000..59b5926009 --- /dev/null +++ b/src/whatsapp/functions/getABPropConfigValue.ts @@ -0,0 +1,33 @@ +/*! + * Copyright 2021 WPPConnect Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { exportModule } from '../exportModule'; + +/** + * @whatsapp 95547 + * @whatsapp 695547 >= 2.2222.8 + * @whatsapp 925080 >= 2.2228.4 + */ + +export declare function getABPropConfigValue(value: any): any; + +exportModule( + exports, + { + getABPropConfigValue: 'getABPropConfigValue', + }, + (m) => m.getABPropConfigValue +); diff --git a/src/whatsapp/functions/getNumChatsPinned.ts b/src/whatsapp/functions/getNumChatsPinned.ts new file mode 100644 index 0000000000..22efe763bf --- /dev/null +++ b/src/whatsapp/functions/getNumChatsPinned.ts @@ -0,0 +1,32 @@ +/*! + * Copyright 2021 WPPConnect Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { exportModule } from '../exportModule'; + +/** + * @whatsapp 95547 + * @whatsapp 695547 >= 2.2222.8 + * @whatsapp 925080 >= 2.2228.4 + */ +export declare function getNumChatsPinned(value: any): any; + +exportModule( + exports, + { + getNumChatsPinned: 'getNumChatsPinned', + }, + (m) => m.getNumChatsPinned +); diff --git a/src/whatsapp/functions/index.ts b/src/whatsapp/functions/index.ts index ca3d2d5f4e..a36179665d 100644 --- a/src/whatsapp/functions/index.ts +++ b/src/whatsapp/functions/index.ts @@ -33,6 +33,7 @@ export * from './fetchLinkPreview'; export * from './findChat'; export * from './findFirstWebLink'; export * from './generateVideoThumbsAndDuration'; +export * from './getNumChatsPinned'; export * from './genMinimalLinkPreview'; export * from './getCommunityParticipants'; export * from './getFanOutList'; @@ -90,3 +91,4 @@ export * from './updateParticipants'; export * from './uploadProductImage'; export * from './uploadThumbnail'; export * from './upsertVotes'; +export * from './getABPropConfigValue'; diff --git a/src/whatsapp/misc/ServerProps.ts b/src/whatsapp/misc/ServerProps.ts new file mode 100644 index 0000000000..cda0b079a0 --- /dev/null +++ b/src/whatsapp/misc/ServerProps.ts @@ -0,0 +1,31 @@ +/*! + * Copyright 2021 WPPConnect Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { exportModule } from '../exportModule'; +import { ServerPropsModel } from '../models'; + +/** @whatsapp 8080 + * @whatsapp 608080 >= 2.2222.8 + */ +export declare const ServerProps: ServerPropsModel + +exportModule( + exports, + { + ServerProps: 'ServerProps', + }, + (m) => m.getMaxFilesSizeServerProp && m.ServerProps +); diff --git a/src/whatsapp/misc/index.ts b/src/whatsapp/misc/index.ts index facd38c8ca..b8554b1748 100644 --- a/src/whatsapp/misc/index.ts +++ b/src/whatsapp/misc/index.ts @@ -39,3 +39,4 @@ export * from './UserPrefs'; export * from './VCard'; export * from './Wid'; export * from './WidFactory'; +export * from './ServerProps'; diff --git a/src/whatsapp/models/ServerPropsModel.ts b/src/whatsapp/models/ServerPropsModel.ts new file mode 100644 index 0000000000..9f731dbae7 --- /dev/null +++ b/src/whatsapp/models/ServerPropsModel.ts @@ -0,0 +1,62 @@ +/*! + * Copyright 2021 WPPConnect Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { exportModule } from '../exportModule'; +import { + Model, + ModelOptions, + ModelPropertiesContructor, + ModelProxy, +} from './Model'; + +interface Props { + statusVideoMaxDuration: number; + multicastLimitGlobal: number; + frequentlyForwardedThreshold: number; + frequentlyForwardedMax: number; + maxFileSize: number; + media: number; +} + +interface Session { +} + +interface Derived { +} + +/** @whatsapp 17239 + * @whatsapp 317239 >= 2.2222.8 + */ +export declare interface ServerPropsModel + extends ModelProxy {} + +/** @whatsapp 17239 + * @whatsapp 317239 >= 2.2222.8 + */ +export declare class ServerPropsModel extends Model { + constructor( + proterties?: ModelPropertiesContructor, + options?: ModelOptions + ); +} + +exportModule( + exports, + { + ServerPropsModel: 'ServerProps', + }, + (m) => m.getMaxFilesSizeServerProp && m.ServerProps +); diff --git a/src/whatsapp/models/index.ts b/src/whatsapp/models/index.ts index faa9ddd9e8..e1f17ba163 100644 --- a/src/whatsapp/models/index.ts +++ b/src/whatsapp/models/index.ts @@ -67,3 +67,4 @@ export * from './StickerPackModel'; export * from './StreamModel'; export * from './TemplateButtonModel'; export * from './UnreadMentionModel'; +export * from './ServerPropsModel';