Fixes, and cleanup

This commit is contained in:
purpshell
2022-08-15 00:16:55 +03:00
parent 19273a434e
commit 6de1ede32b
2 changed files with 105 additions and 84 deletions

View File

@@ -1,20 +1,22 @@
'use strict'; 'use strict';
const MessageMedia = require('./MessageMedia'); const MessageMedia = require('./MessageMedia');
const Util = require('../util/Util');
/** /**
* Button spec used in Buttons constructor * Button spec used in Buttons constructor
* @typedef {Object} ButtonSpec * @typedef {Object} ButtonSpec
* @property {string=} id - Custom ID to set on the button. A random one will be generated if one is not passed.
* @property {string} body - The text to show on the button. * @property {string} body - The text to show on the button.
* @property {string=} id - Custom ID to set on the button. A random one will be generated if one is not passed.
* @property {string=} url - Custom URL to set on the button. Optional and will change the type of the button
* @property {string=} number - Custom URL to set on the button. Optional and will change the type of the button
*/ */
/** /**
* @typedef {Object} FormattedButtonSpec * @typedef {Object} FormattedButtonSpec
* @property {string} buttonId * @property {number} index
* @property {number} type * @property {{displayText: string, url: string}=} urlButton
* @property {Object} buttonText * @property {{displayText: string, phoneNumber: string}=} callButton
* @property {{displayText: string, id: string}=} quickReplyButton
*/ */
/** /**
@@ -76,15 +78,34 @@ class Buttons {
const regularButtons = buttons.filter(button => !button.url && !button.number).slice(0,3); const regularButtons = buttons.filter(button => !button.url && !button.number).slice(0,3);
buttons = especialButtons.concat(regularButtons); buttons = especialButtons.concat(regularButtons);
return buttons.map((btn) => { return buttons.map((button, index) => {
if (btn.url && btn.number) throw 'button can\'t be with url and number together'; if (button.url && button.number && button.id) throw 'Only pick one of the following (url/number/id)';
return { if (button.number) {
buttonId: btn.id ? String(btn.id) : Util.generateHash(6), return {
url: btn.url, index,
phoneNumber: btn.number, callButton: {
buttonText: btn.body, displayText: button.body,
type: 1 phoneNumber: button.number || ''
}; }
};
} else if (button.url) {
return {
index,
urlButton: {
displayText: button.body,
url: button.url || ''
}
};
} else {
return {
index,
quickReplyButton: {
displayText: button.body,
id: button.id || index
}
};
}
}); });
} }

View File

@@ -71,12 +71,47 @@ exports.ExposeStore = (moduleRaidStr) => {
}; };
} }
// The following was implemented and inspired from wppconnect/wa-js at
// https://github.com/wppconnect-team/wa-js/tree/main/src/chat/functions/prepareMessageButtons.ts
// Find proxy modules
window.findProxyModel = (name) => {
const baseName = name.replace(/Model$/, '');
const names = [baseName];
// ChatModel => "chat"
names.push(baseName.replace(/^(\w)/, (l) => l.toLowerCase()));
// CartItemModel => "cart-item"
// ProductListModel => "product_list"
const parts = baseName.split(/(?=[A-Z])/);
names.push(parts.join('-').toLowerCase());
names.push(parts.join('_').toLowerCase());
const results = window.mR.findModule((m) =>
names.includes(
m.default?.prototype?.proxyName ||
m[name]?.prototype?.proxyName ||
m[baseName]?.prototype?.proxyName
)
)[0];
return results.default || results[name] || results[baseName];
};
// Function to modify functions. // Function to modify functions.
window.injectToFunction = (selector, callback) => { window.injectToFunction = (selector, callback) => {
const oldFunct = window.mR.findModule(selector.name)[selector.index][selector.property]; const oldFunct = window.mR.findModule(selector.name)[selector.index][selector.property];
window.mR.findModule(selector.name)[selector.index][selector.property] = (...args) => callback(oldFunct, args); window.mR.findModule(selector.name)[selector.index][selector.property] = (...args) => callback(oldFunct, args);
}; };
// Find button models
window.Store.TemplateButtonModel = window.findProxyModel('TemplateButtonModel');
window.Store.TemplateButtonCollection = window.mR.findModule('TemplateButtonCollection')[0].TemplateButtonCollection;
// Modify functions
window.injectToFunction({ window.injectToFunction({
index: 0, index: 0,
name: 'createMsgProtobuf', name: 'createMsgProtobuf',
@@ -173,8 +208,7 @@ exports.ExposeStore = (moduleRaidStr) => {
index: 0, index: 0,
name: 'createMsgProtobuf', name: 'createMsgProtobuf',
property: 'createMsgProtobuf' property: 'createMsgProtobuf'
}, }, (func, args) => {
(func, args) => {
const proto = func(...args); const proto = func(...args);
if (proto.templateMessage) { if (proto.templateMessage) {
proto.viewOnceMessage = { proto.viewOnceMessage = {
@@ -184,14 +218,6 @@ exports.ExposeStore = (moduleRaidStr) => {
}; };
delete proto.templateMessage; delete proto.templateMessage;
} }
if (proto.buttonsMessage) {
proto.viewOnceMessage = {
message: {
buttonsMessage: proto.buttonsMessage,
},
};
delete proto.buttonsMessage;
}
if (proto.listMessage) { if (proto.listMessage) {
proto.viewOnceMessage = { proto.viewOnceMessage = {
message: { message: {
@@ -233,16 +259,16 @@ exports.ExposeStore = (moduleRaidStr) => {
const [proto] = args; const [proto] = args;
if (proto.ephemeralMessage) { if (proto.ephemeralMessage) {
const { message: n } = proto.ephemeralMessage; const { message } = proto.ephemeralMessage;
return n ? func(n) : 'text'; return message ? func(message) : 'text';
} }
if (proto.deviceSentMessage) { if (proto.deviceSentMessage) {
const { message: n } = proto.deviceSentMessage; const { message } = proto.deviceSentMessage;
return n ? func(n) : 'text'; return message ? func(message) : 'text';
} }
if (proto.viewOnceMessage) { if (proto.viewOnceMessage) {
const { message: n } = proto.viewOnceMessage; const { message } = proto.viewOnceMessage;
return n ? func(n) : 'text'; return message ? func(message) : 'text';
} }
return func(...args); return func(...args);
@@ -267,16 +293,16 @@ exports.ExposeStore = (moduleRaidStr) => {
}, (func, args) => { }, (func, args) => {
const [proto] = args; const [proto] = args;
if (proto.deviceSentMessage) { if (proto.deviceSentMessage) {
const {message: n} = proto.deviceSentMessage; const { message } = proto.deviceSentMessage;
return n ? func(n) : null; return message ? func(message) : null;
} }
if (proto.ephemeralMessage) { if (proto.ephemeralMessage) {
const {message: n} = proto.ephemeralMessage; const { message } = proto.ephemeralMessage;
return n ? func(n) : null; return message ? func(message) : null;
} }
if (proto.viewOnceMessage) { if (proto.viewOnceMessage) {
const {message: n} = proto.viewOnceMessage; const { message } = proto.viewOnceMessage;
return n ? func (n) : null; return message ? func(message) : null;
} }
return func(...args); return func(...args);
@@ -299,64 +325,38 @@ exports.LoadUtils = () => {
window.WWebJS.prepareMessageButtons = (buttonsOptions) => { window.WWebJS.prepareMessageButtons = (buttonsOptions) => {
const returnObject = {}; const returnObject = {};
(window.Store.ReplyButtonModel) || (window.Store.ReplyButtonModel = window.mR.findModule(m => m.default && m?.default?.prototype?.proxyName === 'replyButton')[0].default);
(window.Store.TemplateButtonModel) || (window.Store.TemplateButtonModel = window.mR.findModule(m => m.default && m?.default?.prototype?.proxyName === 'templateButton')[0].default);
(window.Store.TemplateButtonCollection) || (window.Store.TemplateButtonCollection = window.mR.findModule('TemplateButtonCollection')[0].TemplateButtonCollection);
(window.Store.ButtonCollection) || (window.Store.ButtonCollection = window.mR.findModule('ButtonCollection')[0].ButtonCollection);
if (!buttonsOptions.buttons) { if (!buttonsOptions.buttons) {
return returnObject; return returnObject;
} }
if (!Array.isArray(buttonsOptions.buttons)) {
throw 'Buttons options is not a array';
}
returnObject.title = buttonsOptions.title; returnObject.title = buttonsOptions.title;
returnObject.footer = buttonsOptions.footer; returnObject.footer = buttonsOptions.footer;
returnObject.isFromTemplate = !0; returnObject.isFromTemplate = true;
returnObject.buttons = new window.Store.TemplateButtonCollection; returnObject.buttons = new window.Store.TemplateButtonCollection;
returnObject.hydratedButtons = buttonsOptions.buttons.map((e, t) => {
if ('phoneNumber' in e) {
return {
index: t, callButton: {
displayText: e.buttonText, phoneNumber: e.phoneNumber
}
};
} else if ('url' in e) {
return {
index: t, urlButton: {
displayText: e.buttonText, url: e.url
}
};
} else {
return {
index: t, quickReplyButton: {
displayText: e.buttonText, id: e.buttonId || `${t}`
}
};
}
}); returnObject.hydratedButtons = buttonsOptions.buttons;
returnObject.buttons.add(returnObject.hydratedButtons.map((e, t) => { returnObject.buttons.add(returnObject.hydratedButtons.map((button, index) => {
var r, n, o, i; const buttonIndex = button.index ? button.index : index;
const s = `${null != e.index ? e.index : t}`; if (button.urlButton) {
if (e.urlButton) {
return new window.Store.TemplateButtonModel({ return new window.Store.TemplateButtonModel({
id: s, id: buttonIndex,
displayText: null === (r = e.urlButton) || void 0 === r ? void 0 : r.displayText, displayText: button.urlButton?.displayText || '',
url: null === (n = e.urlButton) || void 0 === n ? void 0 : n.url, url: button.urlButton?.url,
subtype: 'url' subtype: 'url'
}); });
} else if (e.callButton) { } else if (button.callButton) {
return new window.Store.TemplateButtonModel({ return new window.Store.TemplateButtonModel({
id: s, displayText: e.callButton.displayText, phoneNumber: e.callButton.phoneNumber, subtype: 'call' id: buttonIndex,
displayText: button.callButton?.displayText,
phoneNumber: button.callButton?.phoneNumber,
subtype: 'call'
}); });
} else { } else {
return new window.Store.TemplateButtonModel({ return new window.Store.TemplateButtonModel({
id: s, id: buttonIndex,
displayText: null === (o = e.quickReplyButton) || void 0 === o ? void 0 : o.displayText, displayText: button.quickReplyButton?.displayText,
selectionId: null === (i = e.quickReplyButton) || void 0 === i ? void 0 : i.id, selectionId: button.quickReplyButton?.id,
subtype: 'quick_reply' subtype: 'quick_reply'
}); });
} }
@@ -442,7 +442,7 @@ exports.LoadUtils = () => {
delete options.linkPreview; delete options.linkPreview;
// Not supported yet by WhatsApp Web on MD // Not supported yet by WhatsApp Web on MD
if(!window.Store.MDBackend) { if (!window.Store.MDBackend) {
const link = window.Store.Validators.findLink(content); const link = window.Store.Validators.findLink(content);
if (link) { if (link) {
const preview = await window.Store.Wap.queryLinkPreview(link.url); const preview = await window.Store.Wap.queryLinkPreview(link.url);
@@ -454,7 +454,7 @@ exports.LoadUtils = () => {
} }
let buttonOptions = {}; let buttonOptions = {};
if(options.buttons){ if (options.buttons) {
let caption; let caption;
if (options.buttons.type === 'chat') { if (options.buttons.type === 'chat') {
content = options.buttons.body; content = options.buttons.body;
@@ -472,8 +472,8 @@ exports.LoadUtils = () => {
} }
let listOptions = {}; let listOptions = {};
if(options.list){ if (options.list) {
if(window.Store.Conn.platform === 'smba' || window.Store.Conn.platform === 'smbi'){ if (window.Store.Conn.platform === 'smba' || window.Store.Conn.platform === 'smbi') {
throw '[LT01] Whatsapp business can\'t send this yet'; throw '[LT01] Whatsapp business can\'t send this yet';
} }
listOptions = { listOptions = {