Skip to content

Commit

Permalink
fix: long descriptions exceeding 256 character limit #15
Browse files Browse the repository at this point in the history
  • Loading branch information
okdargy committed Sep 2, 2024
1 parent fec9a58 commit 32e2f32
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/templates/pages/VideoResponse.tsx
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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') } : {})
}
)}
</>
Expand Down
20 changes: 0 additions & 20 deletions src/tests/main.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/tests/video.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ describe('GET /generate/video/:videoId', () => {

expect(res.status).toBe(500)
})
})
})
15 changes: 13 additions & 2 deletions src/util/generateAlternate.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Context } from 'hono'
import { Buffer } from 'node:buffer';

export default function generateAlternate(c: Context): {
version: string
Expand All @@ -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}`,
}
}
1 change: 1 addition & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -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" }

Expand Down

0 comments on commit 32e2f32

Please sign in to comment.