Skip to content

Commit

Permalink
refactor: oembed
Browse files Browse the repository at this point in the history
  • Loading branch information
guanbinrui committed Aug 3, 2024
1 parent c527879 commit 612e61e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/app/api/oembed/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,13 @@ import { createSuccessResponseJSON } from '@/helpers/createSuccessResponseJSON.j
import { getGatewayErrorMessage } from '@/helpers/getGatewayErrorMessage.js';
import { memoizeWithRedis } from '@/helpers/memoizeWithRedis.js';
import { OpenGraphProcessor } from '@/providers/og/Processor.js';
import { savePostLinks } from '@/services/getPostLinksKV.js';

const digestLinkRedis = memoizeWithRedis(OpenGraphProcessor.digestDocumentUrl, {
key: KeyType.DigestOpenGraphLink,
resolver: (link) => link,
});

export async function DELETE(request: Request) {
const { searchParams } = new URL(request.url);

const link = searchParams.get('link');
if (!link) return Response.json({ error: 'Missing link' }, { status: StatusCodes.BAD_REQUEST });

await digestLinkRedis.cache.delete(link);
return createSuccessResponseJSON(null);
}

export async function GET(request: Request) {
const { searchParams } = new URL(request.url);

Expand All @@ -39,17 +30,27 @@ export async function GET(request: Request) {
return Response.json({ error: 'Unsupported' }, { status: StatusCodes.BAD_REQUEST });
}

const linkDigested = await digestLinkRedis(decodeURIComponent(link), request.signal);
if (!linkDigested)
return Response.json({ error: `Unable to digest oembed link = ${link}` }, { status: StatusCodes.BAD_GATEWAY });

try {
const linkDigested = await digestLinkRedis(decodeURIComponent(link), request.signal);
if (!linkDigested)
return Response.json(
{ error: `Unable to digest oembed link = ${link}` },
{ status: StatusCodes.BAD_GATEWAY },
);
return createSuccessResponseJSON(linkDigested);
} catch (error) {
return Response.json(getGatewayErrorMessage(error), {
status: StatusCodes.BAD_GATEWAY,
await savePostLinks(request, {
oembed: linkDigested,
});
} catch (error) {
console.error(`[oembed] Failed to save post links\n%s`, getGatewayErrorMessage(error));
}

return createSuccessResponseJSON(linkDigested);
}

export async function DELETE(request: Request) {
const { searchParams } = new URL(request.url);

const link = searchParams.get('link');
if (!link) return Response.json({ error: 'Missing link' }, { status: StatusCodes.BAD_REQUEST });

await digestLinkRedis.cache.delete(link);
return createSuccessResponseJSON(null);
}
2 changes: 2 additions & 0 deletions src/services/getPostLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export async function getPostOembed(post: Post): Promise<LinkDigested | null> {
const linkDigested = await fetchJSON<ResponseJSON<LinkDigested>>(
urlcat('/api/oembed', {
link: (await resolveTCOLink(url)) ?? url,
source: resolveSourceInURL(post.source),
'post-id': post.postId,
}),
);
return linkDigested.success ? linkDigested.data : null;
Expand Down

0 comments on commit 612e61e

Please sign in to comment.