fix: twitter preview (#4545)

This commit is contained in:
Alex Yang 2023-09-30 01:19:54 -05:00 committed by GitHub
parent f59a35d8d2
commit 19646a97af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ import { app, BrowserWindow, nativeTheme } from 'electron';
import { getLinkPreview } from 'link-preview-js';
import { isMacOS } from '../../shared/utils';
import { logger } from '../logger';
import type { NamespaceHandlers } from '../type';
import { getGoogleOauthCode } from './google-auth';
@ -45,10 +46,39 @@ export const uiHandlers = {
return getGoogleOauthCode();
},
getBookmarkDataByLink: async (_, link: string) => {
if (
(link.startsWith('https://x.com/') ||
link.startsWith('https://www.x.com/') ||
link.startsWith('https://www.twitter.com/') ||
link.startsWith('https://twitter.com/')) &&
link.includes('/status/')
) {
// use api.fxtwitter.com
link =
'https://api.fxtwitter.com/status/' + /\/status\/(.*)/.exec(link)?.[1];
try {
const { tweet } = await fetch(link).then(res => res.json());
return {
title: tweet.author.name,
icon: tweet.author.avatar_url,
description: tweet.text,
image: tweet.media?.photos[0].url || tweet.author.banner_url,
};
} catch (err) {
logger.error('getBookmarkDataByLink', err);
return {
title: undefined,
description: undefined,
icon: undefined,
image: undefined,
};
}
} else {
const previewData = (await getLinkPreview(link, {
timeout: 6000,
headers: {
'user-agent': 'googlebot',
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
},
followRedirects: 'follow',
}).catch(() => {
@ -69,5 +99,6 @@ export const uiHandlers = {
icon: previewData.favicons[0],
image: previewData.images[0],
};
}
},
} satisfies NamespaceHandlers;