From b960710f19b5422bdf39c5f9eb90a64f4580de31 Mon Sep 17 00:00:00 2001 From: Edgard Date: Sat, 1 Jul 2023 19:11:42 -0300 Subject: [PATCH] fix: Fixed compatibility of ContactModel for WhatsApp >= 2.2327.4 (fix #1208) --- src/contact/index.ts | 2 + src/contact/patch.ts | 71 ++++++ src/whatsapp/functions/contactFunctions.ts | 276 +++++++++++++++++++++ src/whatsapp/functions/index.ts | 1 + src/whatsapp/models/ContactModel.ts | 134 ++++++++++ 5 files changed, 484 insertions(+) create mode 100644 src/contact/patch.ts create mode 100644 src/whatsapp/functions/contactFunctions.ts diff --git a/src/contact/index.ts b/src/contact/index.ts index 627a05e987..d4bb8b1b2c 100644 --- a/src/contact/index.ts +++ b/src/contact/index.ts @@ -14,4 +14,6 @@ * limitations under the License. */ +import './patch'; + export * from './functions'; diff --git a/src/contact/patch.ts b/src/contact/patch.ts new file mode 100644 index 0000000000..2e0e0105c4 --- /dev/null +++ b/src/contact/patch.ts @@ -0,0 +1,71 @@ +/*! + * Copyright 2023 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 * as webpack from '../webpack'; +import { ContactModel, functions } from '../whatsapp'; + +webpack.onInjected(applyPatch); + +function applyPatch() { + const funcs: { + [key: string]: (...args: any[]) => any; + } = { + isMyContact: functions.getIsMyContact, + mentionName: functions.getMentionName, + notifyName: functions.getNotifyName, + pnForLid: functions.getPnForLid, + displayNameOrPnForLid: functions.getDisplayNameOrPnForLid, + formattedPhone: functions.getFormattedPhone, + userid: functions.getUserid, + userhash: functions.getUserhash, + searchName: functions.getSearchName, + searchVerifiedName: functions.getSearchVerifiedName, + header: functions.getHeader, + isMe: functions.getIsMe, + isUser: functions.getIsUser, + isGroup: functions.getIsGroup, + isBroadcast: functions.getIsBroadcast, + isPSA: functions.getIsPSA, + isIAS: functions.getIsIAS, + isSupportAccount: functions.getIsSupportAccount, + formattedShortNameWithNonBreakingSpaces: + functions.getFormattedShortNameWithNonBreakingSpaces, + formattedShortName: functions.getFormattedShortName, + formattedName: functions.getFormattedName, + formattedUser: functions.getFormattedUser, + isWAContact: functions.getIsWAContact, + canRequestPhoneNumber: functions.getCanRequestPhoneNumber, + showBusinessCheckmarkAsPrimary: functions.getShowBusinessCheckmarkAsPrimary, + showBusinessCheckmarkAsSecondary: + functions.getShowBusinessCheckmarkAsSecondary, + showBusinessCheckmarkInChatlist: + functions.getShowBusinessCheckmarkInChatlist, + isDisplayNameApproved: functions.getIsDisplayNameApproved, + shouldForceBusinessUpdate: functions.getShouldForceBusinessUpdate, + }; + + for (const attr in funcs) { + const func = funcs[attr]; + if (typeof (ContactModel.prototype as any)[attr] === 'undefined') { + Object.defineProperty(ContactModel.prototype, attr, { + get: function () { + return func(this); + }, + configurable: true, + }); + } + } +} diff --git a/src/whatsapp/functions/contactFunctions.ts b/src/whatsapp/functions/contactFunctions.ts new file mode 100644 index 0000000000..37233f8cb5 --- /dev/null +++ b/src/whatsapp/functions/contactFunctions.ts @@ -0,0 +1,276 @@ +/*! + * Copyright 2023 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 { injectFallbackModule } from '../../webpack'; +import { exportModule } from '../exportModule'; +import { ContactModel } from '../models'; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getMentionName(contact: ContactModel): string; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getNotifyName(contact: ContactModel): string; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getPremiumMessageName(contact: ContactModel): string; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getUserid(contact: ContactModel): any; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getUserhash(contact: ContactModel): any; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getSearchVerifiedName(contact: ContactModel): any; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getHeader(contact: ContactModel): any; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getIsMe(contact: ContactModel): any; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getIsUser(contact: ContactModel): any; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getIsGroup(contact: ContactModel): any; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getIsBroadcast(contact: ContactModel): any; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getIsPSA(contact: ContactModel): any; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getIsIAS(contact: ContactModel): any; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getIsSupportAccount(contact: ContactModel): any; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getIsWAContact(contact: ContactModel): any; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getIsMyContact(contact: ContactModel): boolean; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getCanRequestPhoneNumber( + contact: ContactModel +): boolean; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getShowBusinessCheckmarkAsPrimary( + contact: ContactModel +): any; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getShowBusinessCheckmarkAsSecondary( + contact: ContactModel +): any; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getShowBusinessCheckmarkInChatlist( + contact: ContactModel +): any; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getIsDisplayNameApproved(contact: ContactModel): any; + +/** + * @whatsapp 660666 >= 2.2327.4 + */ +export declare function getShouldForceBusinessUpdate( + contact: ContactModel +): any; + +exportModule( + exports, + { + getNotifyName: 'getNotifyName', + getMentionName: 'getMentionName', + getPremiumMessageName: 'getPremiumMessageName', + getUserid: 'getUserid', + getUserhash: 'getUserhash', + getSearchVerifiedName: 'getSearchVerifiedName', + getHeader: 'getHeader', + getIsMe: 'getIsMe', + getIsUser: 'getIsUser', + getIsGroup: 'getIsGroup', + getIsBroadcast: 'getIsBroadcast', + getIsPSA: 'getIsPSA', + getIsIAS: 'getIsIAS', + getIsSupportAccount: 'getIsSupportAccount', + getIsWAContact: 'getIsWAContact', + getIsMyContact: 'getIsMyContact', + getCanRequestPhoneNumber: 'getCanRequestPhoneNumber', + getShowBusinessCheckmarkAsPrimary: 'getShowBusinessCheckmarkAsPrimary', + getShowBusinessCheckmarkAsSecondary: 'getShowBusinessCheckmarkAsSecondary', + getShowBusinessCheckmarkInChatlist: 'getShowBusinessCheckmarkInChatlist', + getIsDisplayNameApproved: 'getIsDisplayNameApproved', + getShouldForceBusinessUpdate: 'getShouldForceBusinessUpdate', + }, + (m) => m.getIsMyContact +); + +injectFallbackModule('getIsMyContact', { + getNotifyName: (contact: ContactModel) => contact.notifyName, + getMentionName: (contact: ContactModel) => contact.mentionName, + getPremiumMessageName: (contact: ContactModel) => contact.premiumMessageName, + getUserid: (contact: ContactModel) => contact.userid, + getUserhash: (contact: ContactModel) => contact.userhash, + getSearchVerifiedName: (contact: ContactModel) => contact.searchVerifiedName, + getHeader: (contact: ContactModel) => contact.header, + getIsMe: (contact: ContactModel) => contact.isMe, + getIsUser: (contact: ContactModel) => contact.isUser, + getIsGroup: (contact: ContactModel) => contact.isGroup, + getIsBroadcast: (contact: ContactModel) => contact.isBroadcast, + getIsPSA: (contact: ContactModel) => contact.isPSA, + getIsIAS: (contact: ContactModel) => contact.isIAS, + getIsSupportAccount: (contact: ContactModel) => contact.isSupportAccount, + getIsWAContact: (contact: ContactModel) => contact.isWAContact, + getIsMyContact: (contact: ContactModel) => contact.isMyContact, + getCanRequestPhoneNumber: (contact: ContactModel) => + contact.canRequestPhoneNumber, + getShowBusinessCheckmarkAsPrimary: (contact: ContactModel) => + contact.showBusinessCheckmarkAsPrimary, + getShowBusinessCheckmarkAsSecondary: (contact: ContactModel) => + contact.showBusinessCheckmarkAsSecondary, + getShowBusinessCheckmarkInChatlist: (contact: ContactModel) => + contact.showBusinessCheckmarkInChatlist, + getIsDisplayNameApproved: (contact: ContactModel) => + contact.isDisplayNameApproved, + getShouldForceBusinessUpdate: (contact: ContactModel) => + contact.shouldForceBusinessUpdate, +}); + +/** + * @whatsapp 714574 >= 2.2327.4 + */ +export declare function getDisplayName(contact: ContactModel): string; + +/** + * @whatsapp 714574 >= 2.2327.4 + */ +export declare function getPnForLid(contact: ContactModel): any; + +/** + * @whatsapp 714574 >= 2.2327.4 + */ +export declare function getDisplayNameOrPnForLid(contact: ContactModel): any; + +/** + * @whatsapp 714574 >= 2.2327.4 + */ +export declare function getFormattedPhone(contact: ContactModel): any; + +/** + * @whatsapp 714574 >= 2.2327.4 + */ +export declare function getSearchName(contact: ContactModel): any; + +/** + * @whatsapp 714574 >= 2.2327.4 + */ +export declare function getFormattedShortNameWithNonBreakingSpaces( + contact: ContactModel +): any; + +/** + * @whatsapp 714574 >= 2.2327.4 + */ +export declare function getFormattedShortName(contact: ContactModel): any; + +/** + * @whatsapp 714574 >= 2.2327.4 + */ +export declare function getFormattedName(contact: ContactModel): any; + +/** + * @whatsapp 714574 >= 2.2327.4 + */ +export declare function getFormattedUser(contact: ContactModel): any; + +exportModule( + exports, + { + getDisplayName: 'getDisplayName', + getPnForLid: 'getPnForLid', + getDisplayNameOrPnForLid: 'getDisplayNameOrPnForLid', + getFormattedPhone: 'getFormattedPhone', + getSearchName: 'getSearchName', + getFormattedShortNameWithNonBreakingSpaces: + 'getFormattedShortNameWithNonBreakingSpaces', + getFormattedShortName: 'getFormattedShortName', + getFormattedName: 'getFormattedName', + getFormattedUser: 'getFormattedUser', + }, + (m) => m.getDisplayName +); +injectFallbackModule('getDisplayName', { + getDisplayName: (contact: ContactModel) => contact.displayName, + getPnForLid: (contact: ContactModel) => contact.pnForLid, + getDisplayNameOrPnForLid: (contact: ContactModel) => + contact.displayNameOrPnForLid, + getFormattedPhone: (contact: ContactModel) => contact.formattedPhone, + getSearchName: (contact: ContactModel) => contact.searchName, + getFormattedShortNameWithNonBreakingSpaces: (contact: ContactModel) => + contact.formattedShortNameWithNonBreakingSpaces, + getFormattedShortName: (contact: ContactModel) => contact.formattedShortName, + getFormattedName: (contact: ContactModel) => contact.formattedName, + getFormattedUser: (contact: ContactModel) => contact.formattedUser, +}); diff --git a/src/whatsapp/functions/index.ts b/src/whatsapp/functions/index.ts index 976804f243..fef22973fd 100644 --- a/src/whatsapp/functions/index.ts +++ b/src/whatsapp/functions/index.ts @@ -22,6 +22,7 @@ export * from './canEditMsg'; export * from './canReplyMsg'; export * from './changeOptInStatusForExternalWebBeta'; export * from './collections'; +export * from './contactFunctions'; export * from './createFanoutMsgStanza'; export * from './createGroup'; export * from './createMsgProtobuf'; diff --git a/src/whatsapp/models/ContactModel.ts b/src/whatsapp/models/ContactModel.ts index aea728ad09..da29948da7 100644 --- a/src/whatsapp/models/ContactModel.ts +++ b/src/whatsapp/models/ContactModel.ts @@ -59,27 +59,161 @@ interface Session { } interface Derived { + /** + * Deprecated in favor of getNotifyName + * @deprecated + */ notifyName?: any; + /** + * Deprecated in favor of getMentionName + * @deprecated + */ mentionName?: any; + /** + * Deprecated in favor of getDisplayName + * @deprecated + */ displayName?: any; + /** + * Deprecated in favor of getPnForLid + * @deprecated + */ + pnForLid?: any; + /** + * Deprecated in favor of getPremiumMessageName + * @deprecated + */ + premiumMessageName?: any; + /** + * Deprecated in favor of getDisplayNameOrPnForLid + * @deprecated + */ + displayNameOrPnForLid?: any; + /** + * Deprecated in favor of getFormattedPhone + * @deprecated + */ + formattedPhone?: any; + /** + * Deprecated in favor of getUserid + * @deprecated + */ userid?: any; + /** + * Deprecated in favor of getUserhash + * @deprecated + */ userhash?: any; + /** + * Deprecated in favor of getSearchName + * @deprecated + */ searchName?: any; + /** + * Deprecated in favor of getSearchVerifiedName + * @deprecated + */ searchVerifiedName?: any; + /** + * Deprecated in favor of getHeader + * @deprecated + */ header?: any; + /** + * Deprecated in favor of getIsMe + * @deprecated + */ isMe: boolean; + /** + * Deprecated in favor of getIsUser + * @deprecated + */ isUser: boolean; + /** + * Deprecated in favor of getIsGroup + * @deprecated + */ isGroup: boolean; + /** + * Deprecated in favor of getIsBroadcast + * @deprecated + */ isBroadcast: boolean; + /** + * Deprecated in favor of getIsPSA + * @deprecated + */ isPSA: boolean; + /** + * Deprecated in favor of getIsIAS + * @deprecated + */ + isIAS: boolean; + /** + * Deprecated in favor of getIsSupportAccount + * @deprecated + */ + isSupportAccount: boolean; + /** + * Deprecated in favor of getFormattedShortNameWithNonBreakingSpaces + * @deprecated + */ formattedShortNameWithNonBreakingSpaces?: any; + /** + * Deprecated in favor of getFormattedShortName + * @deprecated + */ formattedShortName?: any; + /** + * Deprecated in favor of getFormattedName + * @deprecated + */ formattedName?: any; + /** + * Deprecated in favor of getFormattedUser + * @deprecated + */ formattedUser?: any; + /** + * Deprecated in favor of getIsWAContact + * @deprecated + */ isWAContact: boolean; + /** + * Deprecated in favor of getIsMyContact + * @deprecated + */ isMyContact: boolean; + /** + * Deprecated in favor of getCanRequestPhoneNumber + * @deprecated + */ + canRequestPhoneNumber: boolean; + /** + * Deprecated in favor of getShowBusinessCheckmarkAsPrimary + * @deprecated + */ showBusinessCheckmarkAsPrimary?: any; + /** + * Deprecated in favor of getShowBusinessCheckmarkAsSecondary + * @deprecated + */ showBusinessCheckmarkAsSecondary?: any; + /** + * Deprecated in favor of getShowBusinessCheckmarkInChatlist + * @deprecated + */ + showBusinessCheckmarkInChatlist?: any; + /** + * Deprecated in favor of getIsDisplayNameApproved + * @deprecated + */ + isDisplayNameApproved?: any; + /** + * Deprecated in favor of getShouldForceBusinessUpdate + * @deprecated + */ + shouldForceBusinessUpdate?: any; } /** @whatsapp 121