From c8c2f09f206a6eb286b6cb1355b0ff1033304f89 Mon Sep 17 00:00:00 2001 From: alechkos <93551621+alechkos@users.noreply.github.com> Date: Mon, 31 Jul 2023 09:41:17 +0300 Subject: [PATCH] fix: Contact (#2325) * fixed: contact info retrieving * fixed: block method * fixed: isBlocked property is now changeble * added inner function to compare between WWeb versions The function compares the currently used WWeb version with the version that requires the code changes. Its purpose is to ensure the stable operation of the library across different WWeb versions. * added: todo remove useOldImplementation * improved inner function to compare WWeb versions * inner function: added support for beta versions * added support for business accounts * module queries changed to strings * fixed check for wweb beta versions * updated to throw a custom error message * minor changes in inner function --- src/structures/Contact.js | 4 +- src/util/Injected.js | 97 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 93 insertions(+), 8 deletions(-) diff --git a/src/structures/Contact.js b/src/structures/Contact.js index 7c20012..959e626 100644 --- a/src/structures/Contact.js +++ b/src/structures/Contact.js @@ -156,9 +156,10 @@ class Contact extends Base { await this.client.pupPage.evaluate(async (contactId) => { const contact = window.Store.Contact.get(contactId); - await window.Store.BlockContact.blockContact(contact); + await window.Store.BlockContact.blockContact({contact}); }, this.id._serialized); + this.isBlocked = true; return true; } @@ -174,6 +175,7 @@ class Contact extends Base { await window.Store.BlockContact.unblockContact(contact); }, this.id._serialized); + this.isBlocked = false; return true; } diff --git a/src/util/Injected.js b/src/util/Injected.js index 73c00dd..a787190 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -34,6 +34,8 @@ exports.ExposeStore = (moduleRaidStr) => { window.Store.EditMessage = window.mR.findModule('addAndSendMessageEdit')[0]; window.Store.SendSeen = window.mR.findModule('sendSeen')[0]; window.Store.User = window.mR.findModule('getMaybeMeUser')[0]; + window.Store.ContactMethods = window.mR.findModule('getUserid')[0]; + window.Store.BusinessProfileCollection = window.mR.findModule('BusinessProfileCollection')[0].BusinessProfileCollection; window.Store.UploadUtils = window.mR.findModule((module) => (module.default && module.default.encryptAndUpload) ? module.default : null)[0].default; window.Store.UserConstructor = window.mR.findModule((module) => (module.default && module.default.prototype && module.default.prototype.isServer && module.default.prototype.isUser) ? module.default : null)[0].default; window.Store.Validators = window.mR.findModule('findLinks')[0]; @@ -500,19 +502,56 @@ exports.LoadUtils = () => { window.WWebJS.getContactModel = contact => { let res = contact.serialize(); - res.isBusiness = contact.isBusiness; + res.isBusiness = contact.isBusiness === undefined ? false : contact.isBusiness; if (contact.businessProfile) { res.businessProfile = contact.businessProfile.serialize(); } - res.isMe = contact.isMe; - res.isUser = contact.isUser; - res.isGroup = contact.isGroup; - res.isWAContact = contact.isWAContact; - res.isMyContact = contact.isMyContact; + // TODO: remove useOldImplementation and its checks once all clients are updated to >= v2.2327.4 + const useOldImplementation + = window.WWebJS.compareWwebVersions(window.Debug.VERSION, '<', '2.2327.4'); + + res.isMe = useOldImplementation + ? contact.isMe + : window.Store.ContactMethods.getIsMe(contact); + res.isUser = useOldImplementation + ? contact.isUser + : window.Store.ContactMethods.getIsUser(contact); + res.isGroup = useOldImplementation + ? contact.isGroup + : window.Store.ContactMethods.getIsGroup(contact); + res.isWAContact = useOldImplementation + ? contact.isWAContact + : window.Store.ContactMethods.getIsWAContact(contact); + res.isMyContact = useOldImplementation + ? contact.isMyContact + : window.Store.ContactMethods.getIsMyContact(contact); res.isBlocked = contact.isContactBlocked; - res.userid = contact.userid; + res.userid = useOldImplementation + ? contact.userid + : window.Store.ContactMethods.getUserid(contact); + res.isEnterprise = useOldImplementation + ? contact.isEnterprise + : window.Store.ContactMethods.getIsEnterprise(contact); + res.verifiedName = useOldImplementation + ? contact.verifiedName + : window.Store.ContactMethods.getVerifiedName(contact); + res.verifiedLevel = useOldImplementation + ? contact.verifiedLevel + : window.Store.ContactMethods.getVerifiedLevel(contact); + res.statusMute = useOldImplementation + ? contact.statusMute + : window.Store.ContactMethods.getStatusMute(contact); + res.name = useOldImplementation + ? contact.name + : window.Store.ContactMethods.getName(contact); + res.shortName = useOldImplementation + ? contact.shortName + : window.Store.ContactMethods.getShortName(contact); + res.pushname = useOldImplementation + ? contact.pushname + : window.Store.ContactMethods.getPushname(contact); return res; }; @@ -520,6 +559,8 @@ exports.LoadUtils = () => { window.WWebJS.getContact = async contactId => { const wid = window.Store.WidFactory.createWid(contactId); const contact = await window.Store.Contact.find(wid); + const bizProfile = await window.Store.BusinessProfileCollection.fetchBizProfile(wid); + bizProfile.profileOptions && (contact.businessProfile = bizProfile); return window.WWebJS.getContactModel(contact); }; @@ -745,4 +786,46 @@ exports.LoadUtils = () => { throw err; } }; + + /** + * Inner function that compares between two WWeb versions. Its purpose is to help the developer to choose the correct code implementation depending on the comparison value and the WWeb version. + * @param {string} lOperand The left operand for the WWeb version string to compare with + * @param {string} operator The comparison operator + * @param {string} rOperand The right operand for the WWeb version string to compare with + * @returns {boolean} Boolean value that indicates the result of the comparison + */ + window.WWebJS.compareWwebVersions = (lOperand, operator, rOperand) => { + if (!['>', '>=', '<', '<=', '='].includes(operator)) { + throw class _ extends Error { + constructor(m) { super(m); this.name = 'CompareWwebVersionsError'; } + }('Invalid comparison operator is provided'); + + } + if (typeof lOperand !== 'string' || typeof rOperand !== 'string') { + throw class _ extends Error { + constructor(m) { super(m); this.name = 'CompareWwebVersionsError'; } + }('A non-string WWeb version type is provided'); + } + + lOperand = lOperand.replace(/-beta$/, ''); + rOperand = rOperand.replace(/-beta$/, ''); + + while (lOperand.length !== rOperand.length) { + lOperand.length > rOperand.length + ? rOperand = rOperand.concat('0') + : lOperand = lOperand.concat('0'); + } + + lOperand = Number(lOperand.replace(/\./g, '')); + rOperand = Number(rOperand.replace(/\./g, '')); + + return ( + operator === '>' ? lOperand > rOperand : + operator === '>=' ? lOperand >= rOperand : + operator === '<' ? lOperand < rOperand : + operator === '<=' ? lOperand <= rOperand : + operator === '=' ? lOperand === rOperand : + false + ); + }; };