From e95ec915f2abc5464fcca98c60e5abeea460bdfe Mon Sep 17 00:00:00 2001 From: dangered wolf Date: Sun, 28 Aug 2022 15:23:15 -0400 Subject: [PATCH] Remove mosaic TypeScript size implementation Will assist in solving #48 faster --- src/helpers/mosaic.ts | 88 ++----------------------------------------- src/status.ts | 13 +++++-- src/types/types.d.ts | 2 - test/index.test.ts | 2 - 4 files changed, 13 insertions(+), 92 deletions(-) diff --git a/src/helpers/mosaic.ts b/src/helpers/mosaic.ts index 541e61d..bc3ad7b 100644 --- a/src/helpers/mosaic.ts +++ b/src/helpers/mosaic.ts @@ -28,94 +28,14 @@ export const handleMosaic = async ( } } - const size = calcSize( - mediaList.map(i => ({ width: i.width, height: i.height } as Size)) - ); return { - height: size.height, - width: size.width, + WARNING: 'height and width of mosaic photos are deprecated and will be removed.', + height: 0, + width: 0, formats: { jpeg: `${baseUrl}jpeg/${id}${path}`, webp: `${baseUrl}webp/${id}${path}` } - } as APIMosaicPhoto; + } as unknown as APIMosaicPhoto; } }; - -/* TypeScript Port of https://github.com/FixTweet/mosaic/blob/feature/size-endpoint/src/mosaic.rs#L236 - We use this to generate accurate mosaic sizes which helps Discord render it correctly */ - -const SPACING_SIZE = 10; -/* This number will be multiplied by the height and weight - if all combined images are over 2000 pixels in height or width. - In my tests setting this to 0.5 increased performance by 4x (at the cost of 50% resolution) - NOTE: This only works for when there's 4 images supplied. */ -const BIG_IMAGE_MULTIPLIER = 1; - -const calcSize = (images: Size[]): Size => { - if (images.length === 1) { - return images[0]; - } else if (images.length === 2) { - const size = calcHorizontalSize(images[0], images[1]); - return size as Size; - } else if (images.length === 3) { - const size = calcHorizontalSize(images[0], images[1]); - const thirdSize = calcVerticalSize(size, images[2]); - - if (thirdSize.secondHeight * 1.5 > size.height) { - return calcHorizontalSize(size, images[2]) as Size; - } else { - return thirdSize as Size; - } - } else if (images.length === 4) { - const top = calcHorizontalSize(images[0], images[1]); - const bottom = calcHorizontalSize(images[2], images[3]); - const all = calcVerticalSize(top, bottom); - - const sizeMult = all.width > 2000 || all.height > 2000 ? BIG_IMAGE_MULTIPLIER : 1; - return { - width: all.width * sizeMult, - height: all.height * sizeMult - }; - } else { - throw new Error('Invalid number of images'); - } -}; - -const calcHorizontalSize = (first: Size, second: Size): HorizontalSize => { - let small = second; - let big = first; - let swapped = false; - if (second.height > first.height) { - small = first; - big = second; - swapped = true; - } - - const smallWidth = Math.round((big.height / small.height) * small.width); - return { - width: smallWidth + SPACING_SIZE + big.width, - height: big.height, - firstWidth: swapped ? smallWidth : big.width, - secondWidth: swapped ? big.width : smallWidth - } as HorizontalSize; -}; - -const calcVerticalSize = (first: Size, second: Size): VerticalSize => { - let small = second; - let big = first; - let swapped = false; - if (second.width > first.width) { - small = first; - big = second; - swapped = true; - } - - const smallHeight = Math.round((big.width / small.width) * small.height); - return { - width: big.width, - height: smallHeight + SPACING_SIZE + big.height, - firstHeight: swapped ? smallHeight : big.height, - secondHeight: swapped ? big.height : smallHeight - } as VerticalSize; -}; diff --git a/src/status.ts b/src/status.ts index 046dc58..c1f178a 100644 --- a/src/status.ts +++ b/src/status.ts @@ -220,12 +220,17 @@ export const handleStatus = async ( /* Push the raw photo-related headers */ headers.push( ``, - ``, - ``, ``, - ``, - `` ); + + if (!tweet.media.mosaic) { + headers.push( + ``, + ``, + ``, + `` + ); + } } /* We have external media available to us (i.e. YouTube videos) */ diff --git a/src/types/types.d.ts b/src/types/types.d.ts index 1d0e7fe..c89ea29 100644 --- a/src/types/types.d.ts +++ b/src/types/types.d.ts @@ -88,8 +88,6 @@ interface APIPhoto { interface APIMosaicPhoto { type: 'mosaic_photo'; - width: number; - height: number; formats: { webp: string; jpeg: string; diff --git a/test/index.test.ts b/test/index.test.ts index 94103c1..d6745dc 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -171,8 +171,6 @@ test('API fetch multi-photo Tweet', async () => { expect(photos[3].height).toEqual(418); expect(tweet.media?.mosaic).toBeTruthy(); const mosaic = tweet.media?.mosaic as APIMosaicPhoto; - expect(mosaic.width).toEqual(1610); - expect(mosaic.height).toEqual(846); expect(mosaic.formats?.jpeg).toEqual( 'https://mosaic.fxtwitter.com/jpeg/1554870933449482240/FZQCeMmXwAAOJTt/FZQCl-lWIAMtoW9/FZQCsQPX0AIbY6H/FZQCxmLXEAMST4q' );