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
6 changed files with 117 additions and 20 deletions

View file

@ -151,7 +151,14 @@ client.on('message', async msg => {
await client.sendMessage(msg.from, media, { isViewOnce: true }); await client.sendMessage(msg.from, media, { isViewOnce: true });
} }
} else if (msg.body === '!location') { } else if (msg.body === '!location') {
msg.reply(new Location(37.422, -122.084, 'Googleplex\nGoogle Headquarters')); // only latitude and longitude
await msg.reply(new Location(37.422, -122.084));
// location with name only
await msg.reply(new Location(37.422, -122.084, { name: 'Googleplex' }));
// location with address only
await msg.reply(new Location(37.422, -122.084, { address: '1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA' }));
// location with name, address and url
await msg.reply(new Location(37.422, -122.084, { name: 'Googleplex', address: '1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA', url: 'https://google.com' }));
} else if (msg.location) { } else if (msg.location) {
msg.reply(msg.location); msg.reply(msg.location);
} else if (msg.body.startsWith('!status ')) { } else if (msg.body.startsWith('!status ')) {

18
index.d.ts vendored
View file

@ -879,13 +879,23 @@ declare namespace WAWebJS {
_serialized: string, _serialized: string,
} }
/** Options for sending a location */
export interface LocationSendOptions {
/** Location name */
name?: string;
/** Location address */
address?: string;
/** URL address to be shown within a location message */
url?: string;
}
/** Location information */ /** Location information */
export class Location { export class Location {
description?: string | null latitude: string;
latitude: string longitude: string;
longitude: string options?: LocationSendOptions;
constructor(latitude: number, longitude: number, description?: string) constructor(latitude: number, longitude: number, options?: LocationSendOptions)
} }
export interface Label { export interface Label {

View file

@ -285,13 +285,13 @@ class Client extends EventEmitter {
*/ */
window.compareWwebVersions = (lOperand, operator, rOperand) => { window.compareWwebVersions = (lOperand, operator, rOperand) => {
if (!['>', '>=', '<', '<=', '='].includes(operator)) { if (!['>', '>=', '<', '<=', '='].includes(operator)) {
throw class _ extends Error { throw new class _ extends Error {
constructor(m) { super(m); this.name = 'CompareWwebVersionsError'; } constructor(m) { super(m); this.name = 'CompareWwebVersionsError'; }
}('Invalid comparison operator is provided'); }('Invalid comparison operator is provided');
} }
if (typeof lOperand !== 'string' || typeof rOperand !== 'string') { if (typeof lOperand !== 'string' || typeof rOperand !== 'string') {
throw class _ extends Error { throw new class _ extends Error {
constructor(m) { super(m); this.name = 'CompareWwebVersionsError'; } constructor(m) { super(m); this.name = 'CompareWwebVersionsError'; }
}('A non-string WWeb version type is provided'); }('A non-string WWeb version type is provided');
} }

View file

@ -1,5 +1,13 @@
'use strict'; 'use strict';
/**
* Location send options
* @typedef {Object} LocationSendOptions
* @property {string} [name] Location name
* @property {string} [address] Location address
* @property {string} [url] URL address to be shown within a location message
*/
/** /**
* Location information * Location information
*/ */
@ -7,9 +15,9 @@ class Location {
/** /**
* @param {number} latitude * @param {number} latitude
* @param {number} longitude * @param {number} longitude
* @param {?string} description * @param {LocationSendOptions} [options] Location send options
*/ */
constructor(latitude, longitude, description) { constructor(latitude, longitude, options = {}) {
/** /**
* Location latitude * Location latitude
* @type {number} * @type {number}
@ -24,9 +32,29 @@ class Location {
/** /**
* Name for the location * Name for the location
* @type {?string} * @type {string|undefined}
*/ */
this.description = description; this.name = options.name;
/**
* Location address
* @type {string|undefined}
*/
this.address = options.address;
/**
* URL address to be shown within a location message
* @type {string|undefined}
*/
this.url = options.url;
/**
* Location full description
* @type {string|undefined}
*/
this.description = this.name && this.address
? `${this.name}\n${this.address}`
: this.name || this.address || '';
} }
} }

View file

@ -151,7 +151,21 @@ class Message extends Base {
* Location information contained in the message, if the message is type "location" * Location information contained in the message, if the message is type "location"
* @type {Location} * @type {Location}
*/ */
this.location = data.type === MessageTypes.LOCATION ? new Location(data.lat, data.lng, data.loc) : undefined; this.location = (() => {
if (data.type !== MessageTypes.LOCATION) {
return undefined;
}
let description;
if (data.loc && typeof data.loc === 'string') {
let splitted = data.loc.split('\n');
description = {
name: splitted[0],
address: splitted[1],
url: data.clientUrl
};
}
return new Location(data.lat, data.lng, description);
})();
/** /**
* List of vCards contained in the message. * List of vCards contained in the message.

View file

@ -80,11 +80,9 @@ exports.ExposeStore = (moduleRaidStr) => {
}; };
} }
if (window.mR.findModule('ChatCollection')[0] && window.mR.findModule('ChatCollection')[0].ChatCollection) { // eslint-disable-next-line no-undef
if (typeof window.mR.findModule('ChatCollection')[0].ChatCollection.findImpl === 'undefined' && typeof window.mR.findModule('ChatCollection')[0].ChatCollection._find != 'undefined') { 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;
window.mR.findModule('ChatCollection')[0].ChatCollection.findImpl = window.mR.findModule('ChatCollection')[0].ChatCollection._find;
}
}
// TODO remove these once everybody has been updated to WWebJS with legacy sessions removed // TODO remove these once everybody has been updated to WWebJS with legacy sessions removed
const _linkPreview = window.mR.findModule('queryLinkPreview'); const _linkPreview = window.mR.findModule('queryLinkPreview');
@ -103,6 +101,42 @@ exports.ExposeStore = (moduleRaidStr) => {
if(_features) { if(_features) {
window.Store.Features = _features.LegacyPhoneFeatures; 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 = () => { exports.LoadUtils = () => {
@ -159,11 +193,15 @@ exports.LoadUtils = () => {
let locationOptions = {}; let locationOptions = {};
if (options.location) { if (options.location) {
let { latitude, longitude, description, url } = options.location;
url = window.Store.Validators.findLink(url)?.href;
url && !description && (description = url);
locationOptions = { locationOptions = {
type: 'location', type: 'location',
loc: options.location.description, loc: description,
lat: options.location.latitude, lat: latitude,
lng: options.location.longitude lng: longitude,
clientUrl: url
}; };
delete options.location; delete options.location;
} }