Skip to content

Commit

Permalink
Better housekeeping for link previews
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn-Signal committed Aug 13, 2021
1 parent 50957a3 commit 2304c39
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions ts/views/conversation_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
autoScale,
handleImageAttachment,
} from '../util/handleImageAttachment';
import type { WhatIsThis } from '../window.d';

type AttachmentOptions = {
messageId: string;
Expand Down Expand Up @@ -3929,7 +3930,7 @@ Whisper.ConversationView = Whisper.View.extend({
message,
attachments,
this.quote,
this.getLinkPreview(),
this.getLinkPreviewForSend(),
undefined, // sticker
mentions,
{
Expand Down Expand Up @@ -4426,7 +4427,7 @@ Whisper.ConversationView = Whisper.View.extend({
);
},

getLinkPreview() {
getLinkPreviewForSend(message: string) {
// Don't generate link previews if user has turned them off
if (!window.storage.get('linkPreviews', false)) {
return [];
Expand All @@ -4436,17 +4437,26 @@ Whisper.ConversationView = Whisper.View.extend({
return [];
}

return this.preview.map((item: any) => {
if (item.image) {
// We eliminate the ObjectURL here, unneeded for send or save
return {
...item,
image: window._.omit(item.image, 'url'),
};
}
const urlsInMessage = new Set<string>(LinkPreview.findLinks(message));

return (
this.preview
// This bullet-proofs against sending link previews for URLs that are no longer in
// the message. This can happen if you have a link preview, then quickly delete
// the link and send the message.
.filter(({ url }: Readonly<{ url: string }>) => urlsInMessage.has(url))
.map((item: WhatIsThis) => {
if (item.image) {
// We eliminate the ObjectURL here, unneeded for send or save
return {
...item,
image: window._.omit(item.image, 'url'),
};
}

return item;
});
return item;
})
);
},

getLinkPreviewWithDomain(): LinkPreviewWithDomain | undefined {
Expand Down

0 comments on commit 2304c39

Please sign in to comment.