This commit is contained in:
dangered wolf 2023-05-31 16:35:38 -04:00
parent d90b39ab11
commit 68266afc6c
No known key found for this signature in database
GPG key ID: 41E4D37680ED8B58
5 changed files with 64 additions and 30 deletions

View file

@ -71,13 +71,12 @@ const populateTweetProperties = async (
tweet.extended_entities?.media || tweet.entities?.media || [] tweet.extended_entities?.media || tweet.entities?.media || []
); );
console.log('tweet', JSON.stringify(tweet)) console.log('tweet', JSON.stringify(tweet));
/* Populate this Tweet's media */ /* Populate this Tweet's media */
mediaList.forEach(media => { mediaList.forEach(media => {
const mediaObject = processMedia(media); const mediaObject = processMedia(media);
if (mediaObject) { if (mediaObject) {
apiTweet.media = apiTweet.media || {}; apiTweet.media = apiTweet.media || {};
apiTweet.media.all = apiTweet.media?.all || []; apiTweet.media.all = apiTweet.media?.all || [];
apiTweet.media.all.push(mediaObject); apiTweet.media.all.push(mediaObject);

View file

@ -80,7 +80,7 @@ export const handleStatus = async (
} else if (all.length > 0) { } else if (all.length > 0) {
redirectUrl = all[0].url; redirectUrl = all[0].url;
} }
if (redirectUrl) { if (redirectUrl) {
return { response: Response.redirect(redirectUrl, 302) }; return { response: Response.redirect(redirectUrl, 302) };
} }
@ -150,7 +150,16 @@ export const handleStatus = async (
switch (overrideMedia.type) { switch (overrideMedia.type) {
case 'photo': case 'photo':
/* This Tweet has a photo to render. */ /* This Tweet has a photo to render. */
instructions = renderPhoto( {tweet: tweet, authorText: authorText, engagementText: engagementText, userAgent: userAgent, isOverrideMedia: true }, overrideMedia as APIPhoto ); instructions = renderPhoto(
{
tweet: tweet,
authorText: authorText,
engagementText: engagementText,
userAgent: userAgent,
isOverrideMedia: true
},
overrideMedia as APIPhoto
);
headers.push(...instructions.addHeaders); headers.push(...instructions.addHeaders);
if (instructions.authorText) { if (instructions.authorText) {
authorText = instructions.authorText; authorText = instructions.authorText;
@ -160,7 +169,10 @@ export const handleStatus = async (
} }
break; break;
case 'video': case 'video':
instructions = renderVideo( {tweet: tweet, userAgent: userAgent, text: newText, isOverrideMedia: true }, overrideMedia as APIVideo ); instructions = renderVideo(
{ tweet: tweet, userAgent: userAgent, text: newText, isOverrideMedia: true },
overrideMedia as APIVideo
);
headers.push(...instructions.addHeaders); headers.push(...instructions.addHeaders);
if (instructions.authorText) { if (instructions.authorText) {
authorText = instructions.authorText; authorText = instructions.authorText;
@ -172,10 +184,21 @@ export const handleStatus = async (
break; break;
} }
} else if (tweet.media?.mosaic) { } else if (tweet.media?.mosaic) {
const instructions = renderPhoto( {tweet: tweet, authorText: authorText, engagementText: engagementText, userAgent: userAgent }, tweet.media?.mosaic ); const instructions = renderPhoto(
{
tweet: tweet,
authorText: authorText,
engagementText: engagementText,
userAgent: userAgent
},
tweet.media?.mosaic
);
headers.push(...instructions.addHeaders); headers.push(...instructions.addHeaders);
} else if (tweet.media?.videos) { } else if (tweet.media?.videos) {
const instructions = renderVideo( {tweet: tweet, userAgent: userAgent, text: newText }, tweet.media?.videos[0] ); const instructions = renderVideo(
{ tweet: tweet, userAgent: userAgent, text: newText },
tweet.media?.videos[0]
);
headers.push(...instructions.addHeaders); headers.push(...instructions.addHeaders);
if (instructions.authorText) { if (instructions.authorText) {
authorText = instructions.authorText; authorText = instructions.authorText;

View file

@ -1,37 +1,45 @@
import { Constants } from "../constants"; import { Constants } from '../constants';
import { Strings } from "../strings"; import { Strings } from '../strings';
export const renderPhoto = (properties: RenderProperties, photo: APIPhoto | APIMosaicPhoto): ResponseInstructions => { export const renderPhoto = (
const { tweet, engagementText, authorText, isOverrideMedia, userAgent } = properties; properties: RenderProperties,
photo: APIPhoto | APIMosaicPhoto
): ResponseInstructions => {
const { tweet, engagementText, authorText, isOverrideMedia, userAgent } = properties;
const instructions: ResponseInstructions = { addHeaders: [] }; const instructions: ResponseInstructions = { addHeaders: [] };
if (!tweet.media?.mosaic || isOverrideMedia) { if (!tweet.media?.mosaic || isOverrideMedia) {
photo = photo as APIPhoto; photo = photo as APIPhoto;
const all = tweet.media?.all as APIMedia[]; const all = tweet.media?.all as APIMedia[];
const baseString = all.length === tweet.media?.photos?.length ? Strings.PHOTO_COUNT : Strings.MEDIA_COUNT; const baseString =
all.length === tweet.media?.photos?.length
? Strings.PHOTO_COUNT
: Strings.MEDIA_COUNT;
const photoCounter = baseString.format({ const photoCounter = baseString.format({
number: String(all.indexOf(photo) + 1), number: String(all.indexOf(photo) + 1),
total: String(all.length) total: String(all.length)
}); });
console.log('Telegram', userAgent?.indexOf('Telegram')) const isTelegram = (userAgent?.indexOf('Telegram') ?? 0) > -1;
if (authorText === Strings.DEFAULT_AUTHOR_TEXT || (userAgent?.indexOf('Telegram') ?? 0) > -1) {
if (authorText === Strings.DEFAULT_AUTHOR_TEXT || isTelegram) {
instructions.authorText = photoCounter; instructions.authorText = photoCounter;
} else { } else {
instructions.authorText = `${authorText}${authorText ? ' ― ' : ''}${photoCounter}`; instructions.authorText = `${authorText}${
authorText ? ' ― ' : ''
}${photoCounter}`;
} }
if (engagementText && (userAgent?.indexOf('Telegram') ?? 0) === -1) { if (engagementText && !isTelegram) {
instructions.siteName = `${Constants.BRANDING_NAME} - ${engagementText} - ${photoCounter}`; instructions.siteName = `${Constants.BRANDING_NAME} - ${engagementText} - ${photoCounter}`;
} else { } else {
instructions.siteName = `${Constants.BRANDING_NAME} - ${photoCounter}`; instructions.siteName = `${Constants.BRANDING_NAME} - ${photoCounter}`;
} }
} }
if (photo.type === 'mosaic_photo' && !isOverrideMedia) { if (photo.type === 'mosaic_photo' && !isOverrideMedia) {
console.log('Mosaic object:', tweet.media?.mosaic);
instructions.addHeaders = [ instructions.addHeaders = [
`<meta property="twitter:image" content="${tweet.media?.mosaic?.formats.jpeg}"/>`, `<meta property="twitter:image" content="${tweet.media?.mosaic?.formats.jpeg}"/>`,
`<meta property="og:image" content="${tweet.media?.mosaic?.formats.jpeg}"/>` `<meta property="og:image" content="${tweet.media?.mosaic?.formats.jpeg}"/>`
@ -50,4 +58,4 @@ export const renderPhoto = (properties: RenderProperties, photo: APIPhoto | APIM
console.log('Photo render instructions', JSON.stringify(instructions)); console.log('Photo render instructions', JSON.stringify(instructions));
return instructions; return instructions;
} };

View file

@ -1,7 +1,10 @@
import { Constants } from "../constants"; import { Constants } from '../constants';
import { Strings } from "../strings"; import { Strings } from '../strings';
export const renderVideo = (properties: RenderProperties, video: APIVideo): ResponseInstructions => { export const renderVideo = (
properties: RenderProperties,
video: APIVideo
): ResponseInstructions => {
const { tweet, userAgent, text } = properties; const { tweet, userAgent, text } = properties;
const instructions: ResponseInstructions = { addHeaders: [] }; const instructions: ResponseInstructions = { addHeaders: [] };
@ -27,7 +30,10 @@ export const renderVideo = (properties: RenderProperties, video: APIVideo): Resp
/* Like photos when picking a specific one (not using mosaic), /* Like photos when picking a specific one (not using mosaic),
we'll put an indicator if there are more than one video */ we'll put an indicator if there are more than one video */
if (all && all.length > 1 && (userAgent?.indexOf('Telegram') ?? 0) > -1) { if (all && all.length > 1 && (userAgent?.indexOf('Telegram') ?? 0) > -1) {
const baseString = all.length === tweet.media?.videos?.length ? Strings.VIDEO_COUNT : Strings.MEDIA_COUNT; const baseString =
all.length === tweet.media?.videos?.length
? Strings.VIDEO_COUNT
: Strings.MEDIA_COUNT;
const videoCounter = baseString.format({ const videoCounter = baseString.format({
number: String(all.indexOf(video) + 1), number: String(all.indexOf(video) + 1),
total: String(all.length) total: String(all.length)
@ -41,9 +47,7 @@ export const renderVideo = (properties: RenderProperties, video: APIVideo): Resp
/* Push the raw video-related headers */ /* Push the raw video-related headers */
instructions.addHeaders = [ instructions.addHeaders = [
`<meta property="twitter:player:stream:content_type" content="${video.format}"/>`, `<meta property="twitter:player:stream:content_type" content="${video.format}"/>`,
`<meta property="twitter:player:height" content="${ `<meta property="twitter:player:height" content="${video.height * sizeMultiplier}"/>`,
video.height * sizeMultiplier
}"/>`,
`<meta property="twitter:player:width" content="${video.width * sizeMultiplier}"/>`, `<meta property="twitter:player:width" content="${video.width * sizeMultiplier}"/>`,
`<meta property="og:video" content="${video.url}"/>`, `<meta property="og:video" content="${video.url}"/>`,
`<meta property="og:video:secure_url" content="${video.url}"/>`, `<meta property="og:video:secure_url" content="${video.url}"/>`,
@ -54,4 +58,4 @@ export const renderVideo = (properties: RenderProperties, video: APIVideo): Resp
]; ];
return instructions; return instructions;
} };

View file

@ -165,7 +165,7 @@ interface APITweet {
lang: string | null; lang: string | null;
possibly_sensitive: boolean; possibly_sensitive: boolean;
replying_to: string | null; replying_to: string | null;
replying_to_status: string | null; replying_to_status: string | null;