fix: sending locations (#2339)

* added function to to modify module functions

* fix for locations to be sent

* changes for class Location

- description property was splited into name and address
- added url property
- added ability to pass name, address and url as options while creating an instance

* docs updated

* usage example updated

* added url checking

* fixed url

* updated location property in Message class

* fixed url when http prefix is provided

* injectToFunction updated to be more flexible

* docs clarification

* ESLint fix

* fix: injectToFunction fixed to work properly

* minor fix in injectToFunction

* refactor: added usage of url validator

* small change to improve performance for findImpl fix

* window.compareWwebVersions fixed
This commit is contained in:
alechkos 2023-09-20 23:11:52 +03:00 • committed by GitHub
commit ecf19eb9a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -80,11 +80,9 @@ exports.ExposeStore = (moduleRaidStr) => {
};
}
if (window.mR.findModule('ChatCollection')[0] && window.mR.findModule('ChatCollection')[0].ChatCollection) {
if (typeof window.mR.findModule('ChatCollection')[0].ChatCollection.findImpl === 'undefined' && typeof window.mR.findModule('ChatCollection')[0].ChatCollection._find != 'undefined') {
window.mR.findModule('ChatCollection')[0].ChatCollection.findImpl = window.mR.findModule('ChatCollection')[0].ChatCollection._find;
}
}
// eslint-disable-next-line no-undef
if ((m = window.mR.findModule('ChatCollection')[0]) && m.ChatCollection && typeof m.ChatCollection.findImpl === 'undefined' && typeof m.ChatCollection._find !== 'undefined') m.ChatCollection.findImpl = m.ChatCollection._find;
// TODO remove these once everybody has been updated to WWebJS with legacy sessions removed
const _linkPreview = window.mR.findModule('queryLinkPreview');
@ -103,6 +101,42 @@ exports.ExposeStore = (moduleRaidStr) => {
if(_features) {
window.Store.Features = _features.LegacyPhoneFeatures;
}
/**
* Target options object description
* @typedef {Object} TargetOptions
* @property {string|number} moduleId The name or a key of the target module to search
* @property {number} index The index value of the target module
* @property {string} property The function name to get from a module
*/
/**
* Function to modify functions
* Referenced from and modified:
* @see https://github.com/pedroslopez/whatsapp-web.js/pull/1636/commits/81111faa058d8e715285a2bfc9a42636074f7c3d#diff-de25cb4b9105890088bb033eac000d1dd2104d3498a8523082dc7eaf319738b8R75-R78
* @param {TargetOptions} target Options specifying the target function to search for modifying
* @param {Function} callback Modified function
*/
window.injectToFunction = (target, callback) => {
const module = typeof target.moduleId === 'string'
? window.mR.findModule(target.moduleId)
: window.mR.modules[target.moduleId];
const originalFunction = module[target.index][target.property];
const modifiedFunction = (...args) => callback(originalFunction, ...args);
module[target.index][target.property] = modifiedFunction;
};
/**
* Referenced from and modified:
* @see https://github.com/wppconnect-team/wa-js/blob/e19164e83cfa68b828493e6ff046c0a3d46a4942/src/chat/functions/sendLocationMessage.ts#L156
*/
window.injectToFunction({ moduleId: 'mediaTypeFromProtobuf', index: 0, property: 'mediaTypeFromProtobuf' }, (func, ...args) => { const [proto] = args; return proto.locationMessage ? null : func(...args); });
/**
* Referenced from and modified:
* @see https://github.com/wppconnect-team/wa-js/blob/e19164e83cfa68b828493e6ff046c0a3d46a4942/src/chat/functions/sendLocationMessage.ts#L164
*/
window.injectToFunction({ moduleId: 'typeAttributeFromProtobuf', index: 0, property: 'typeAttributeFromProtobuf' }, (func, ...args) => { const [proto] = args; return proto.locationMessage ? 'text' : func(...args); });
};
exports.LoadUtils = () => {
@ -159,11 +193,15 @@ exports.LoadUtils = () => {
let locationOptions = {};
if (options.location) {
let { latitude, longitude, description, url } = options.location;
url = window.Store.Validators.findLink(url)?.href;
url && !description && (description = url);
locationOptions = {
type: 'location',
loc: options.location.description,
lat: options.location.latitude,
lng: options.location.longitude
loc: description,
lat: latitude,
lng: longitude,
clientUrl: url
};
delete options.location;
}