fix: Message.getInfo (#2574)

* module fixed + improved modules retrieving

* wait for msg data to be updated

* added random sleep value between 1100-1200 ms

* added check for msg is sent by current user

* node 13 compatible

* docs fixed

* msg.fromMe fixed

* timestamp calculation fixed

* window.injectToFunction clarifications
This commit is contained in:
alechkos 2023-11-24 12:18:18 +02:00 • committed by GitHub
commit 7ff9c9807b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 20 deletions

6
index.d.ts vendored
View file

@ -981,7 +981,7 @@ declare namespace WAWebJS {
* The custom message secret, can be used as a poll ID
* @note It has to be a unique vector with a length of 32
*/
messageSecret: ?Array<number>
messageSecret: Array<number>|undefined
}
/** Represents a Poll on WhatsApp */
@ -1341,7 +1341,7 @@ declare namespace WAWebJS {
code: number;
message: string;
isInviteV4Sent: boolean,
};
}
};
/** An object that handles options for adding participants */
@ -1410,7 +1410,7 @@ declare namespace WAWebJS {
/** Group participants */
participants: Array<GroupParticipant>;
/** Adds a list of participants by ID to the group */
addParticipants: (participantIds: string|string[], options?: AddParticipantsOptions) => Promise<Object.<string, AddParticipantsResult>|string>;
addParticipants: (participantIds: string | string[], options?: AddParticipantsOptions) => Promise<{ [key: string]: AddParticipantsResult } | string>;
/** Removes a list of participants by ID to the group */
removeParticipants: (participantIds: string[]) => Promise<{ status: number }>;
/** Promotes participants by IDs to admins */

View file

@ -534,15 +534,20 @@ class Message extends Base {
*/
/**
* Get information about message delivery status. May return null if the message does not exist or is not sent by you.
* Get information about message delivery status.
* May return null if the message does not exist or is not sent by you.
* @returns {Promise<?MessageInfo>}
*/
async getInfo() {
const info = await this.client.pupPage.evaluate(async (msgId) => {
const msg = window.Store.Msg.get(msgId);
if (!msg) return null;
if (!msg || !msg.id.fromMe) return null;
return await window.Store.MessageInfo.sendQueryMsgInfo(msg.id);
return new Promise((resolve) => {
setTimeout(async () => {
resolve(await window.Store.getMsgInfo(msg.id));
}, (Date.now() - msg.t * 1000 < 1250) && Math.floor(Math.random() * (1200 - 1100 + 1)) + 1100 || 0);
});
}, this.id._serialized);
return info;

View file

@ -23,7 +23,6 @@ exports.ExposeStore = (moduleRaidStr) => {
window.Store.MediaTypes = window.mR.findModule('msgToMediaType')[0];
window.Store.MediaUpload = window.mR.findModule('uploadMedia')[0];
window.Store.MsgKey = window.mR.findModule((module) => module.default && module.default.fromString)[0].default;
window.Store.MessageInfo = window.mR.findModule('sendQueryMsgInfo')[0];
window.Store.OpaqueData = window.mR.findModule(module => module.default && module.default.createFromData)[0].default;
window.Store.QueryProduct = window.mR.findModule('queryProduct')[0];
window.Store.QueryOrder = window.mR.findModule('queryOrder')[0];
@ -58,16 +57,17 @@ exports.ExposeStore = (moduleRaidStr) => {
window.Store.LidUtils = window.mR.findModule('getCurrentLid')[0];
window.Store.WidToJid = window.mR.findModule('widToUserJid')[0];
window.Store.JidToWid = window.mR.findModule('userJidToUserWid')[0];
window.Store.Settings = {
...window.mR.findModule('ChatlistPanelState')[0],
setPushname: window.mR.findModule((m) => m.setPushname && !m.ChatlistPanelState)[0].setPushname
};
/* eslint-disable no-undef, no-cond-assign */
window.Store.QueryExist = ((m = window.mR.findModule('queryExists')[0]) ? m.queryExists : window.mR.findModule('queryExist')[0].queryWidExists);
window.Store.ReplyUtils = (m = window.mR.findModule('canReplyMsg')).length > 0 && m[0];
window.Store.getMsgInfo = m = window.mR.findModule('sendQueryMsgInfo')[0] ? m.sendQueryMsgInfo : window.mR.findModule('queryMsgInfo')[0].queryMsgInfo;
/* eslint-enable no-undef, no-cond-assign */
window.Store.Settings = {
...window.mR.findModule('ChatlistPanelState')[0],
setPushname: window.mR.findModule((m) => m.setPushname && !m.ChatlistPanelState)[0].setPushname
};
window.Store.StickerTools = {
...window.mR.findModule('toWebpSticker')[0],
...window.mR.findModule('addWebpMetadata')[0]
@ -128,9 +128,9 @@ exports.ExposeStore = (moduleRaidStr) => {
/**
* Target options object description
* @typedef {Object} TargetOptions
* @property {string|number} moduleId The name or a key of the target module to search
* @property {string|number} module 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
* @property {string} function The function name to get from a module
*/
/**
@ -139,17 +139,17 @@ exports.ExposeStore = (moduleRaidStr) => {
* @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 module = typeof target.module === 'string'
? window.mR.findModule(target.module)
: window.mR.modules[target.module];
const originalFunction = module[target.index][target.function];
const modifiedFunction = (...args) => callback(originalFunction, ...args);
module[target.index][target.property] = modifiedFunction;
module[target.index][target.function] = modifiedFunction;
};
window.injectToFunction({ moduleId: 'mediaTypeFromProtobuf', index: 0, property: 'mediaTypeFromProtobuf' }, (func, ...args) => { const [proto] = args; return proto.locationMessage ? null : func(...args); });
window.injectToFunction({ module: 'mediaTypeFromProtobuf', index: 0, function: 'mediaTypeFromProtobuf' }, (func, ...args) => { const [proto] = args; return proto.locationMessage ? null : func(...args); });
window.injectToFunction({ moduleId: 'typeAttributeFromProtobuf', index: 0, property: 'typeAttributeFromProtobuf' }, (func, ...args) => { const [proto] = args; return proto.locationMessage || proto.groupInviteMessage ? 'text' : func(...args); });
window.injectToFunction({ module: 'typeAttributeFromProtobuf', index: 0, function: 'typeAttributeFromProtobuf' }, (func, ...args) => { const [proto] = args; return proto.locationMessage || proto.groupInviteMessage ? 'text' : func(...args); });
};
exports.LoadUtils = () => {