Final fixes

This commit is contained in:
Rajeh Taher
2023-02-21 00:04:51 +02:00
committed by GitHub
parent c0f9e78f6c
commit e810b5146d
2 changed files with 77 additions and 89 deletions

View File

@@ -31,7 +31,7 @@ class Buttons {
* @param {string?} footer * @param {string?} footer
* @param {boolean?} templateOverride * @param {boolean?} templateOverride
*/ */
constructor(body, buttons, title, footer, templateOverride) { constructor(body, buttons, title, footer, templateOverride = false) {
/** /**
* Message body * Message body
* @type {string|MessageMedia} * @type {string|MessageMedia}
@@ -61,9 +61,14 @@ class Buttons {
* buttons of message * buttons of message
* @type {FormattedButtonSpec[]} * @type {FormattedButtonSpec[]}
*/ */
this.buttons = this._format(buttons, templateOverride); this.buttons = this._format(buttons);
if(!this.buttons.length){ throw '[BT01] No buttons';} if(!this.buttons.length){ throw '[BT01] No buttons';}
/**
* Override buttons with templates
* @type {boolean}
*/
this.useTemplateButtons = templateOverride;
} }
/** /**
@@ -71,11 +76,11 @@ class Buttons {
* @param {ButtonSpec[]} buttons * @param {ButtonSpec[]} buttons
* @returns {FormattedButtonSpec[]} * @returns {FormattedButtonSpec[]}
*/ */
_format(buttons, templateOverride){ _format(buttons){
// Limit the buttons (max 3 of regular and 3 of special buttons) 5 buttons total at the same time // Limit the buttons (max 3 of regular and 3 of special buttons) 5 buttons total at the same time
const templateButtons = buttons.filter(button => button.url || button.number); const templateButtons = buttons.filter(button => button.url || button.number);
const regularButtons = buttons.filter(button => !button.url && !button.number); const regularButtons = buttons.filter(button => !button.url && !button.number);
buttons = templateButtons.concat(regularButtons); buttons = regularButtons.concat(templateButtons);
return buttons.map((button, index) => { return buttons.map((button, index) => {
if (button.url && button.number && button.id) throw 'Only pick one of the following (url/number/id)'; if (button.url && button.number && button.id) throw 'Only pick one of the following (url/number/id)';
@@ -98,21 +103,13 @@ class Buttons {
} }
}; };
} else { } else {
if (templateOverride) return { return {
index, index,
quickReplyButton: { quickReplyButton: {
displayText: button.body, displayText: button.body,
id: button.id || `${index}` id: button.id || `${index}`
} }
}; };
return {
index,
regularButtons: {
id: button.id,
text: button.body
}
};
} }
}); });

View File

@@ -212,6 +212,7 @@ exports.ExposeStore = (moduleRaidStr) => {
return proto; return proto;
}); });
setTimeout(() => {
window.injectToFunction({ window.injectToFunction({
index: 0, index: 0,
name: 'createMsgProtobuf', name: 'createMsgProtobuf',
@@ -244,6 +245,7 @@ exports.ExposeStore = (moduleRaidStr) => {
} }
return proto; return proto;
}); });
}, 100);
window.injectToFunction({ window.injectToFunction({
index: 0, index: 0,
@@ -286,6 +288,10 @@ exports.ExposeStore = (moduleRaidStr) => {
return 'text'; return 'text';
} }
if (proto.listMessage) {
return 'text';
}
return func(...args); return func(...args);
}); });
@@ -295,23 +301,9 @@ exports.ExposeStore = (moduleRaidStr) => {
property: 'mediaTypeFromProtobuf' property: 'mediaTypeFromProtobuf'
}, (func, args) => { }, (func, args) => {
const [proto] = args; const [proto] = args;
if (proto.deviceSentMessage) {
const { message } = proto.deviceSentMessage;
return message ? func(message) : null;
}
if (proto.ephemeralMessage) {
const { message } = proto.ephemeralMessage;
return message ? func(message) : null;
}
if (proto.viewOnceMessage) {
const { message } = proto.viewOnceMessage;
return message ? func(message) : null;
}
if (proto.templateMessage?.hydratedTemplate) { if (proto.templateMessage?.hydratedTemplate) {
return func(proto.templateMessage.hydratedTemplate); return func(proto.templateMessage.hydratedTemplate);
} }
return func(...args); return func(...args);
}); });
@@ -365,45 +357,44 @@ exports.LoadUtils = () => {
return returnObject; return returnObject;
} }
if (typeof buttonsOptions.useTemplateButtons === 'undefined' || buttonsOptions.useTemplateButtons === null) {
buttonsOptions.useTemplateButtons = buttonsOptions.buttons.some((button) => {
return 'callButton' in button || 'urlButton' in button;
});
}
returnObject.title = buttonsOptions.title; returnObject.title = buttonsOptions.title;
returnObject.footer = buttonsOptions.footer; returnObject.footer = buttonsOptions.footer;
if (buttonsOptions.useTemplateButtons) { if (buttonsOptions.useTemplateButtons) {
returnObject.isFromTemplate = true; returnObject.isFromTemplate = true;
returnObject.hydratedButtons = buttonsOptions.buttons; returnObject.hydratedButtons = buttonsOptions.buttons;
returnObject.buttons = new window.Store.TemplateButtonCollection; returnObject.buttons = new window.Store.TemplateButtonCollection();
message.buttons.add(
message.hydratedButtons.map((button, index) => {
const i = `${null != button.index ? button.index : index}`;
returnObject.buttons.add(returnObject.hydratedButtons.map((button, index) => {
const buttonIndex = button.index ? button.index : index;
if (button.urlButton) { if (button.urlButton) {
return new window.Store.TemplateButtonModel({ return new window.Store.TemplateButtonModel({
id: buttonIndex, id: i,
displayText: button.urlButton?.displayText || '', displayText: button.urlButton?.displayText,
url: button.urlButton?.url, url: button.urlButton?.url,
subtype: 'url' subtype: 'url',
});
} else if (button.callButton) {
return new window.Store.TemplateButtonModel({
id: buttonIndex,
displayText: button.callButton?.displayText,
phoneNumber: button.callButton?.phoneNumber,
subtype: 'call'
});
} else {
return new window.Store.TemplateButtonModel({
id: buttonIndex,
displayText: button.quickReplyButton?.displayText,
selectionId: button.quickReplyButton?.id,
subtype: 'quick_reply'
}); });
} }
}));
if (button.callButton) {
return new window.Store.TemplateButtonModel({
id: i,
displayText: button.callButton.displayText,
phoneNumber: button.callButton.phoneNumber,
subtype: 'call',
});
}
return new window.Store.TemplateButtonModel({
id: i,
displayText: button.quickReplyButton?.displayText,
selectionId: button.quickReplyButton?.id,
subtype: 'quick_reply',
});
})
);
} }
else { else {
returnObject.isDynamicReplyButtonsMsg = true; returnObject.isDynamicReplyButtonsMsg = true;