Fix incoming messages not showing previews.

Linting.
pull/186/head
Mikunj 6 years ago
parent ae695fe32f
commit 27c6062351

@ -21,13 +21,17 @@
if (base64ImageCache[url]) return base64ImageCache[url]; if (base64ImageCache[url]) return base64ImageCache[url];
// Set the cache and return the value // Set the cache and return the value
const contentType = image.contentType || 'image/jpeg'; try {
const base64 = dcodeIO.ByteBuffer.wrap(image.data).toString('base64'); const contentType = image.contentType || 'image/jpeg';
const base64 = dcodeIO.ByteBuffer.wrap(image.data).toString('base64');
const data = `data:${contentType};base64, ${base64}`; const data = `data:${contentType};base64, ${base64}`;
base64ImageCache[url] = data; base64ImageCache[url] = data;
return data; return data;
} catch (e) {
return null;
}
} }
async function makeChunkedRequest(url) { async function makeChunkedRequest(url) {
@ -147,5 +151,5 @@
window.Signal.LinkPreviews.helper = { window.Signal.LinkPreviews.helper = {
getPreview, getPreview,
getBase64Image, getBase64Image,
} };
})(); })();

@ -8,7 +8,6 @@
/* global Signal: false */ /* global Signal: false */
/* global textsecure: false */ /* global textsecure: false */
/* global Whisper: false */ /* global Whisper: false */
/* global dcodeIO: false */
/* eslint-disable more/no-then */ /* eslint-disable more/no-then */
@ -113,16 +112,29 @@
return !!(this.get('flags') & flag); return !!(this.get('flags') & flag);
}, },
async updatePreviews() { async updatePreviews() {
if (this.updatingPreview) return; // Don't generate link previews if user has turned them off
if (!storage.get('linkPreviews', false)) {
return;
}
if (this.updatingPreview) {
return;
}
// Only update the preview if we don't have any set // Only update the preview if we don't have any set
const preview = this.get('preview'); const preview = this.get('preview');
if (!_.isEmpty(preview)) return; if (!_.isEmpty(preview)) {
return;
}
// Make sure we have links we can preview // Make sure we have links we can preview
const links = Signal.LinkPreviews.findLinks(this.get('body')); const links = Signal.LinkPreviews.findLinks(this.get('body'));
const firstLink = links.find(link => Signal.LinkPreviews.isLinkInWhitelist(link)); const firstLink = links.find(link =>
if (!firstLink) return; Signal.LinkPreviews.isLinkInWhitelist(link)
);
if (!firstLink) {
return;
}
this.updatingPreview = true; this.updatingPreview = true;
@ -139,8 +151,8 @@
return; return;
} }
// We don't want to save the base64 url in the message as it will increase the size of it // We don't want to save the base64 url in the message as
// Rather we fetch the base64 later // it will increase the size of it, Rather we fetch the base64 later
this.set({ preview: [result] }); this.set({ preview: [result] });
} catch (e) { } catch (e) {
window.log.warn(`Failed to load previews for message: ${this.id}`); window.log.warn(`Failed to load previews for message: ${this.id}`);
@ -653,6 +665,11 @@
}); });
}, },
getPropsForPreview() { getPropsForPreview() {
// Don't generate link previews if user has turned them off
if (!storage.get('linkPreviews', false)) {
return null;
}
const previews = this.get('preview') || []; const previews = this.get('preview') || [];
return previews.map(preview => { return previews.map(preview => {
@ -672,7 +689,7 @@
image = { image = {
...preview.image, ...preview.image,
url: preview.image.url || url, url: preview.image.url || url,
} };
} }
} }
} }
@ -1369,6 +1386,9 @@
schemaVersion: dataMessage.schemaVersion, schemaVersion: dataMessage.schemaVersion,
}); });
// Update the previews if we need to
message.updatePreviews();
if (type === 'outgoing') { if (type === 'outgoing') {
const receipts = Whisper.DeliveryReceipts.forMessage( const receipts = Whisper.DeliveryReceipts.forMessage(
conversation, conversation,

Loading…
Cancel
Save