Fix link preview (#2681)

* Fix link preview

* Fix typo

* Fix preview options

* Add example for preview
This commit is contained in:
Shir Serlui 2023-12-12 20:56:44 +02:00 • committed by GitHub
commit ac0a50082f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 24 deletions

View file

@ -66,6 +66,9 @@ client.on('message', async msg => {
} else if (msg.body.startsWith('!echo ')) { } else if (msg.body.startsWith('!echo ')) {
// Replies with the same message // Replies with the same message
msg.reply(msg.body.slice(6)); msg.reply(msg.body.slice(6));
} else if (msg.body.startsWith('!preview ')) {
const text = msg.body.slice(9);
msg.reply(text, null, { linkPreview: true });
} else if (msg.body.startsWith('!desc ')) { } else if (msg.body.startsWith('!desc ')) {
// Change the group description // Change the group description
let chat = await msg.getChat(); let chat = await msg.getChat();

View file

@ -50,6 +50,7 @@ exports.ExposeStore = (moduleRaidStr) => {
window.Store.EphemeralFields = window.mR.findModule('getEphemeralFields')[0]; window.Store.EphemeralFields = window.mR.findModule('getEphemeralFields')[0];
window.Store.MsgActionChecks = window.mR.findModule('canSenderRevokeMsg')[0]; window.Store.MsgActionChecks = window.mR.findModule('canSenderRevokeMsg')[0];
window.Store.QuotedMsg = window.mR.findModule('getQuotedMsgObj')[0]; window.Store.QuotedMsg = window.mR.findModule('getQuotedMsgObj')[0];
window.Store.LinkPreview = window.mR.findModule('getLinkPreview')[0];
window.Store.Socket = window.mR.findModule('deprecatedSendIq')[0]; window.Store.Socket = window.mR.findModule('deprecatedSendIq')[0];
window.Store.SocketWap = window.mR.findModule('wap')[0]; window.Store.SocketWap = window.mR.findModule('wap')[0];
window.Store.SearchContext = window.mR.findModule('getSearchContext')[0].getSearchContext; window.Store.SearchContext = window.mR.findModule('getSearchContext')[0].getSearchContext;
@ -107,12 +108,6 @@ exports.ExposeStore = (moduleRaidStr) => {
// eslint-disable-next-line no-undef // 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; 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');
if (_linkPreview && _linkPreview[0] && _linkPreview[0].default) {
window.Store.Wap = _linkPreview[0].default;
}
const _isMDBackend = window.mR.findModule('isMDBackend'); const _isMDBackend = window.mR.findModule('isMDBackend');
if(_isMDBackend && _isMDBackend[0] && _isMDBackend[0].isMDBackend) { if(_isMDBackend && _isMDBackend[0] && _isMDBackend[0].isMDBackend) {
window.Store.MDBackend = _isMDBackend[0].isMDBackend(); window.Store.MDBackend = _isMDBackend[0].isMDBackend();
@ -271,15 +266,14 @@ exports.LoadUtils = () => {
if (options.linkPreview) { if (options.linkPreview) {
delete options.linkPreview; delete options.linkPreview;
const link = window.Store.Validators.findLink(content);
// Not supported yet by WhatsApp Web on MD if (link) {
if(!window.Store.MDBackend) { let preview = await window.Store.LinkPreview.getLinkPreview(link);
const link = window.Store.Validators.findLink(content); if (preview && preview.data) {
if (link) { preview = preview.data;
const preview = await window.Store.Wap.queryLinkPreview(link.url);
preview.preview = true; preview.preview = true;
preview.subtype = 'url'; preview.subtype = 'url';
options = { ...options, ...preview }; options = {...options, ...preview};
} }
} }
} }
@ -379,17 +373,13 @@ exports.LoadUtils = () => {
} }
if (options.linkPreview) { if (options.linkPreview) {
options.linkPreview = null; delete options.linkPreview;
const link = window.Store.Validators.findLink(content);
// Not supported yet by WhatsApp Web on MD if (link) {
if(!window.Store.MDBackend) { const preview = await window.Store.LinkPreview.getLinkPreview(link);
const link = window.Store.Validators.findLink(content); preview.preview = true;
if (link) { preview.subtype = 'url';
const preview = await window.Store.Wap.queryLinkPreview(link.url); options = { ...options, ...preview };
preview.preview = true;
preview.subtype = 'url';
options = { ...options, ...preview };
}
} }
} }