From 32e2f327ff0a90b0647af6da4438cd354594b187 Mon Sep 17 00:00:00 2001 From: dargy Date: Mon, 2 Sep 2024 14:24:08 -0500 Subject: [PATCH] fix: long descriptions exceeding 256 character limit #15 --- src/templates/pages/VideoResponse.tsx | 3 ++- src/tests/main.test.ts | 20 -------------------- src/tests/video.test.ts | 2 +- src/util/generateAlternate.tsx | 15 +++++++++++++-- wrangler.toml | 1 + 5 files changed, 17 insertions(+), 24 deletions(-) delete mode 100644 src/tests/main.test.ts diff --git a/src/templates/pages/VideoResponse.tsx b/src/templates/pages/VideoResponse.tsx index f2ff783..cace93d 100644 --- a/src/templates/pages/VideoResponse.tsx +++ b/src/templates/pages/VideoResponse.tsx @@ -1,6 +1,7 @@ import MetaHelper from '../../util/metaHelper' import { ItemStruct } from '../../types/Web' import { formatNumber } from '../../util/format' +import { Buffer } from 'node:buffer'; export function VideoResponse(data: ItemStruct, addDesc: Boolean): JSX.Element { let videoUrl = 'https://fxtiktok-rewrite.dargy.workers.dev/generate/video/' + data.id @@ -118,7 +119,7 @@ export function VideoResponse(data: ItemStruct, addDesc: Boolean): JSX.Element { { unique_id: data.author.uniqueId, nickname: data.author.nickname, - ...(addDesc ? { description: data.desc } : {}) + ...(addDesc ? { description: Buffer.from(data.desc).toString('base64') } : {}) } )} diff --git a/src/tests/main.test.ts b/src/tests/main.test.ts deleted file mode 100644 index ad79b75..0000000 --- a/src/tests/main.test.ts +++ /dev/null @@ -1,20 +0,0 @@ -import app from '@/index' - -describe('GET /test/:videoId', () => { - it('should return 200', async () => { - const res = await app.request('/test/7332187682480590112', { - method: 'GET' - }) - - expect(res.status).toBe(200) - }) - - it('should return 500', async () => { - const res = await app.request('/test/123', { - method: 'GET' - }) - - expect(res.status).toBe(500) - expect(await res.text()).toBe('Could not parse video info') - }) -}) diff --git a/src/tests/video.test.ts b/src/tests/video.test.ts index 3bc3752..fc526cc 100644 --- a/src/tests/video.test.ts +++ b/src/tests/video.test.ts @@ -79,4 +79,4 @@ describe('GET /generate/video/:videoId', () => { expect(res.status).toBe(500) }) -}) +}) \ No newline at end of file diff --git a/src/util/generateAlternate.tsx b/src/util/generateAlternate.tsx index 4ecee8f..a2610ac 100644 --- a/src/util/generateAlternate.tsx +++ b/src/util/generateAlternate.tsx @@ -1,4 +1,5 @@ import { Context } from 'hono' +import { Buffer } from 'node:buffer'; export default function generateAlternate(c: Context): { version: string @@ -12,13 +13,23 @@ export default function generateAlternate(c: Context): { const { unique_id, nickname, description } = c.req.query() const showSponsor = Math.random() < 0.01 // 1 in 100 chance to show sponsor message, gotta break even somehow + const decodedDescription = description + ? decodeURIComponent(Buffer.from(decodeURIComponent(description), 'base64').toString('utf-8')) + : ''; + + // Some Discord embed values are limited to 256 characters, truncate if necessary + // See more: https://www.pythondiscord.com/pages/guides/python-guides/discord-embed-limits/ + const truncatedDescription = decodedDescription.length > 256 + ? decodedDescription.substring(0, 253) + '...' + : decodedDescription; + return { version: '1.0', type: 'link', author_name: `${decodeURIComponent(nickname)} (@${decodeURIComponent(unique_id)})`, author_url: `https://www.tiktok.com${unique_id ? '/@' + unique_id : ''}`, - provider_name: `${description ? '\n\n' + decodeURIComponent(description) : showSponsor ? 'Use fxTikTok often? Consider supporting us on GitHub!' : 'fxTikTok - Embed with s/i/n'}`, + provider_name: truncatedDescription || (showSponsor ? 'Use fxTikTok often? Consider supporting us on GitHub!' : 'fxTikTok - Embed with s/i/n'), provider_url: showSponsor ? 'https://github.com/sponsors/okdargy' : 'https://github.com/okdargy/fxTikTok', - title: `TikTok by @${unique_id}` + title: `TikTok by @${unique_id}`, } } diff --git a/wrangler.toml b/wrangler.toml index 5d4d3b0..238cd3b 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -1,6 +1,7 @@ name = "fxtiktok-rewrite" compatibility_date = "2023-01-01" main = "src/index.ts" +node_compat = true vars = { WORKER_ENV = "production", BASE_URL = "https://fxtiktok-rewrite.dargy.workers.dev" }