Always download link preview image
This commit is contained in:
parent
aa1f96cb6a
commit
235a188291
5 changed files with 63 additions and 29 deletions
|
@ -254,32 +254,36 @@ export function getLinkPreviewForSend(message: string): Array<LinkPreviewType> {
|
||||||
// the message. This can happen if you have a link preview, then quickly delete
|
// the message. This can happen if you have a link preview, then quickly delete
|
||||||
// the link and send the message.
|
// the link and send the message.
|
||||||
.filter(({ url }: Readonly<{ url: string }>) => urlsInMessage.has(url))
|
.filter(({ url }: Readonly<{ url: string }>) => urlsInMessage.has(url))
|
||||||
.map((item: LinkPreviewResult) => {
|
.map(sanitizeLinkPreview)
|
||||||
if (item.image) {
|
|
||||||
// We eliminate the ObjectURL here, unneeded for send or save
|
|
||||||
return {
|
|
||||||
...item,
|
|
||||||
image: omit(item.image, 'url'),
|
|
||||||
title: dropNull(item.title),
|
|
||||||
description: dropNull(item.description),
|
|
||||||
date: dropNull(item.date),
|
|
||||||
domain: LinkPreview.getDomain(item.url),
|
|
||||||
isStickerPack: LinkPreview.isStickerPack(item.url),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
...item,
|
|
||||||
title: dropNull(item.title),
|
|
||||||
description: dropNull(item.description),
|
|
||||||
date: dropNull(item.date),
|
|
||||||
domain: LinkPreview.getDomain(item.url),
|
|
||||||
isStickerPack: LinkPreview.isStickerPack(item.url),
|
|
||||||
};
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function sanitizeLinkPreview(
|
||||||
|
item: LinkPreviewResult | LinkPreviewType
|
||||||
|
): LinkPreviewType {
|
||||||
|
if (item.image) {
|
||||||
|
// We eliminate the ObjectURL here, unneeded for send or save
|
||||||
|
return {
|
||||||
|
...item,
|
||||||
|
image: omit(item.image, 'url'),
|
||||||
|
title: dropNull(item.title),
|
||||||
|
description: dropNull(item.description),
|
||||||
|
date: dropNull(item.date),
|
||||||
|
domain: LinkPreview.getDomain(item.url),
|
||||||
|
isStickerPack: LinkPreview.isStickerPack(item.url),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...item,
|
||||||
|
title: dropNull(item.title),
|
||||||
|
description: dropNull(item.description),
|
||||||
|
date: dropNull(item.date),
|
||||||
|
domain: LinkPreview.getDomain(item.url),
|
||||||
|
isStickerPack: LinkPreview.isStickerPack(item.url),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async function getPreview(
|
async function getPreview(
|
||||||
url: string,
|
url: string,
|
||||||
abortSignal: Readonly<AbortSignal>
|
abortSignal: Readonly<AbortSignal>
|
||||||
|
|
|
@ -47,6 +47,10 @@ export function getStoryDataFromMessageAttributes(
|
||||||
? getAttachmentsForMessage(message)
|
? getAttachmentsForMessage(message)
|
||||||
: [unresolvedAttachment];
|
: [unresolvedAttachment];
|
||||||
|
|
||||||
|
// If a story message has a preview property in its attributes then we
|
||||||
|
// rebuild the textAttachment data structure to contain the all the data it
|
||||||
|
// needs to fully render the text attachment including the link preview and
|
||||||
|
// its image.
|
||||||
let preview: LinkPreviewType | undefined;
|
let preview: LinkPreviewType | undefined;
|
||||||
if (message.preview?.length) {
|
if (message.preview?.length) {
|
||||||
strictAssert(
|
strictAssert(
|
||||||
|
|
|
@ -2053,6 +2053,9 @@ export default class MessageReceiver
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg.textAttachment) {
|
if (msg.textAttachment) {
|
||||||
|
// If a text attachment has a link preview we remove it from the
|
||||||
|
// textAttachment data structure and instead process the preview and add
|
||||||
|
// it as a "preview" property for the message attributes.
|
||||||
const { text, preview: unprocessedPreview } = msg.textAttachment;
|
const { text, preview: unprocessedPreview } = msg.textAttachment;
|
||||||
if (unprocessedPreview) {
|
if (unprocessedPreview) {
|
||||||
preview = processPreview([unprocessedPreview]);
|
preview = processPreview([unprocessedPreview]);
|
||||||
|
|
|
@ -13,6 +13,7 @@ import {
|
||||||
} from 'lodash';
|
} from 'lodash';
|
||||||
import { blobToArrayBuffer } from 'blob-util';
|
import { blobToArrayBuffer } from 'blob-util';
|
||||||
|
|
||||||
|
import type { LinkPreviewType } from './message/LinkPreviews';
|
||||||
import type { LoggerType } from './Logging';
|
import type { LoggerType } from './Logging';
|
||||||
import * as MIME from './MIME';
|
import * as MIME from './MIME';
|
||||||
import { toLogFormat } from './errors';
|
import { toLogFormat } from './errors';
|
||||||
|
@ -99,11 +100,7 @@ export type TextAttachmentType = {
|
||||||
textStyle?: number | null;
|
textStyle?: number | null;
|
||||||
textForegroundColor?: number | null;
|
textForegroundColor?: number | null;
|
||||||
textBackgroundColor?: number | null;
|
textBackgroundColor?: number | null;
|
||||||
preview?: {
|
preview?: LinkPreviewType;
|
||||||
image?: AttachmentType;
|
|
||||||
title?: string | null;
|
|
||||||
url?: string | null;
|
|
||||||
} | null;
|
|
||||||
gradient?: {
|
gradient?: {
|
||||||
startColor?: number | null;
|
startColor?: number | null;
|
||||||
endColor?: number | null;
|
endColor?: number | null;
|
||||||
|
|
|
@ -24,6 +24,21 @@ import { isGroupV2 } from './whatTypeOfConversation';
|
||||||
import { isNotNil } from './isNotNil';
|
import { isNotNil } from './isNotNil';
|
||||||
import { collect } from './iterables';
|
import { collect } from './iterables';
|
||||||
import { DurationInSeconds } from './durations';
|
import { DurationInSeconds } from './durations';
|
||||||
|
import { sanitizeLinkPreview } from '../services/LinkPreview';
|
||||||
|
|
||||||
|
function cleanLinkPreviewIfAny(attachment: AttachmentType): AttachmentType {
|
||||||
|
if (!attachment.textAttachment || !attachment.textAttachment.preview) {
|
||||||
|
return attachment;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...attachment,
|
||||||
|
textAttachment: {
|
||||||
|
...attachment.textAttachment,
|
||||||
|
preview: undefined,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export async function sendStoryMessage(
|
export async function sendStoryMessage(
|
||||||
listIds: Array<string>,
|
listIds: Array<string>,
|
||||||
|
@ -135,7 +150,17 @@ export async function sendStoryMessage(
|
||||||
sendStateByListId.set(distributionList.id, sendStateByConversationId);
|
sendStateByListId.set(distributionList.id, sendStateByConversationId);
|
||||||
});
|
});
|
||||||
|
|
||||||
const attachments: Array<AttachmentType> = [attachment];
|
const cleanedAttachment = cleanLinkPreviewIfAny(attachment);
|
||||||
|
const attachments: Array<AttachmentType> = [cleanedAttachment];
|
||||||
|
|
||||||
|
const linkPreview = attachment?.textAttachment?.preview;
|
||||||
|
const sanitizedLinkPreview = linkPreview
|
||||||
|
? sanitizeLinkPreview(linkPreview)
|
||||||
|
: undefined;
|
||||||
|
// If a text attachment has a link preview we remove it from the
|
||||||
|
// textAttachment data structure and instead process the preview and add
|
||||||
|
// it as a "preview" property for the message attributes.
|
||||||
|
const preview = sanitizedLinkPreview ? [sanitizedLinkPreview] : undefined;
|
||||||
|
|
||||||
// * Gather all the job data we'll be sending to the sendStory job
|
// * Gather all the job data we'll be sending to the sendStory job
|
||||||
// * Create the message for each distribution list
|
// * Create the message for each distribution list
|
||||||
|
@ -161,6 +186,7 @@ export async function sendStoryMessage(
|
||||||
expireTimer: DurationInSeconds.DAY,
|
expireTimer: DurationInSeconds.DAY,
|
||||||
expirationStartTimestamp: Date.now(),
|
expirationStartTimestamp: Date.now(),
|
||||||
id: UUID.generate().toString(),
|
id: UUID.generate().toString(),
|
||||||
|
preview,
|
||||||
readStatus: ReadStatus.Read,
|
readStatus: ReadStatus.Read,
|
||||||
received_at: incrementMessageCounter(),
|
received_at: incrementMessageCounter(),
|
||||||
received_at_ms: timestamp,
|
received_at_ms: timestamp,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue