Link previews: show full size image less often

This commit is contained in:
Evan Hahn 2021-01-08 13:39:32 -06:00 committed by Scott Nonnenberg
parent 92a35649da
commit 8c25ffd6f5
7 changed files with 220 additions and 53 deletions

View file

@ -0,0 +1,34 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { LinkPreviewType } from '../types/message/LinkPreviews';
import { isImageAttachment } from '../types/Attachment';
const MINIMUM_FULL_SIZE_DIMENSION = 200;
export function shouldUseFullSizeLinkPreviewImage({
isStickerPack,
image,
}: Readonly<LinkPreviewType>): boolean {
if (isStickerPack || !isImageAttachment(image)) {
return false;
}
const { width, height } = image;
return (
isDimensionFullSize(width) &&
isDimensionFullSize(height) &&
!isRoughlySquare(width, height)
);
}
function isDimensionFullSize(dimension: unknown): dimension is number {
return (
typeof dimension === 'number' && dimension >= MINIMUM_FULL_SIZE_DIMENSION
);
}
function isRoughlySquare(width: number, height: number): boolean {
return Math.abs(1 - width / height) < 0.05;
}