From f939712635dcbcbe5160095320e1057b4cd35aa2 Mon Sep 17 00:00:00 2001 From: dangered wolf Date: Mon, 21 Aug 2023 02:00:25 -0400 Subject: [PATCH] Minor improvements --- src/api/status.ts | 2 +- src/embed/status.ts | 19 +++++++++------ src/render/instantview.ts | 49 +++++++++++++++++++-------------------- src/strings.ts | 4 ++-- 4 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/api/status.ts b/src/api/status.ts index 869482d..65bace7 100644 --- a/src/api/status.ts +++ b/src/api/status.ts @@ -117,7 +117,7 @@ const populateTweetProperties = async ( } /* Handle photos and mosaic if available */ - if ((apiTweet.media?.photos?.length || 0) > 1) { + if ((apiTweet?.media?.photos?.length || 0) > 1) { const mosaic = await handleMosaic(apiTweet.media?.photos || [], tweet.rest_id); if (typeof apiTweet.media !== 'undefined' && mosaic !== null) { apiTweet.media.mosaic = mosaic; diff --git a/src/embed/status.ts b/src/embed/status.ts index d25a018..bb110fc 100644 --- a/src/embed/status.ts +++ b/src/embed/status.ts @@ -38,7 +38,7 @@ export const handleStatus = async ( const isTelegram = (userAgent || '').indexOf('Telegram') > -1; /* Should sensitive posts be allowed Instant View? */ - const useIV = + let useIV = isTelegram /*&& !tweet.possibly_sensitive*/ && !flags?.direct && !flags?.api && @@ -130,12 +130,17 @@ export const handleStatus = async ( } if (useIV) { - const instructions = renderInstantView({ tweet: tweet, text: newText }); - headers.push(...instructions.addHeaders); - if (instructions.authorText) { - authorText = instructions.authorText; + try { + const instructions = renderInstantView({ tweet: tweet, text: newText }); + headers.push(...instructions.addHeaders); + if (instructions.authorText) { + authorText = instructions.authorText; + } + ivbody = instructions.text || ''; + } catch (e) { + console.log('Error rendering Instant View', e); + useIV = false; } - ivbody = instructions.text || ''; } /* This Tweet has a translation attached to it, so we'll render it. */ @@ -365,7 +370,7 @@ export const handleStatus = async ( lang: `lang="${lang}"`, headers: headers.join(''), body: ivbody - }), + }).replace(/>(\s+)<'), // Remove whitespace between tags cacheControl: cacheControl }; }; diff --git a/src/render/instantview.ts b/src/render/instantview.ts index c8caf4f..1dcb66b 100644 --- a/src/render/instantview.ts +++ b/src/render/instantview.ts @@ -21,13 +21,13 @@ const generateTweetMedia = (tweet: APITweet): string => { tweet.media.all.forEach(mediaItem => { switch (mediaItem.type) { case 'photo': - media += `${tweet.author.name}'s photo`; + media += `${tweet.author.name}'s photo`; break; case 'video': - media += `