From 095257ad5a4a469a10a498f8db837dd163d75bab Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 16 May 2023 12:43:05 +0100 Subject: [PATCH] feat: compatible with old non-standard slugs --- next.config.js | 2 +- src/app/api/redirection/route.ts | 12 ++---------- src/app/site/[site]/feed/comments/route.ts | 8 ++------ src/lib/expand-unit.ts | 8 +++----- src/lib/helpers.ts | 8 +++++--- 5 files changed, 13 insertions(+), 25 deletions(-) diff --git a/next.config.js b/next.config.js index c0064bf8..fecc049a 100644 --- a/next.config.js +++ b/next.config.js @@ -7,7 +7,7 @@ const execSync = require("child_process").execSync const cache = require("@ducanh2912/next-pwa").runtimeCaching const withPWA = require("@ducanh2912/next-pwa").default({ - // disable: process.env.NODE_ENV === "development", + disable: process.env.NODE_ENV === "development", dest: "public", publicExcludes: ["*"], buildExcludes: [ diff --git a/src/app/api/redirection/route.ts b/src/app/api/redirection/route.ts index f54f8b90..ea53078c 100644 --- a/src/app/api/redirection/route.ts +++ b/src/app/api/redirection/route.ts @@ -2,8 +2,7 @@ import { NoteEntity } from "crossbell.js" import { redirect } from "next/navigation" import { IS_VERCEL_PREVIEW } from "~/lib/constants" -import { getDefaultSlug } from "~/lib/default-slug" -import { getSiteLink } from "~/lib/helpers" +import { getNoteSlug, getSiteLink } from "~/lib/helpers" import { NextServerResponse, getQuery } from "~/lib/server-helper" import { checkDomainServer } from "~/models/site.model" @@ -63,14 +62,7 @@ export async function GET(req: Request): Promise { }) if (note) { - const slug = - note?.metadata?.content?.attributes?.find( - (a: any) => a.trait_type === "xlog_slug", - )?.value || - getDefaultSlug( - note?.metadata?.content?.title || "", - `${characterId}-${noteId}`, - ) + const slug = getNoteSlug(note) link += `/${encodeURIComponent(slug)}` diff --git a/src/app/site/[site]/feed/comments/route.ts b/src/app/site/[site]/feed/comments/route.ts index 89f13f80..da1e740b 100644 --- a/src/app/site/[site]/feed/comments/route.ts +++ b/src/app/site/[site]/feed/comments/route.ts @@ -1,4 +1,4 @@ -import { getSiteLink } from "~/lib/helpers" +import { getNoteSlug, getSiteLink } from "~/lib/helpers" import { NextServerResponse } from "~/lib/server-helper" import { getCommentsBySite, getSite } from "~/models/site.model" @@ -49,11 +49,7 @@ export async function GET( id: comment.characterId + "-" + comment.noteId, title: `${name}: ${comment.metadata?.content?.content}`, content_html: `${name} commented on ${type} ${toTitle}: ${comment.metadata?.content?.content}`, - url: `${link}/${ - comment.toNote?.metadata?.content?.attributes?.find( - (attribute: any) => attribute.trait_type === "xlog_slug", - )?.value || comment.toNote?.characterId + "-" + comment.toNote?.noteId - }`, + url: `${link}/${comment.toNote ? getNoteSlug(comment.toNote) : ""}`, date_published: comment.createdAt, date_modified: comment.updatedAt, } diff --git a/src/lib/expand-unit.ts b/src/lib/expand-unit.ts index 5ca077bf..e00ee5e3 100644 --- a/src/lib/expand-unit.ts +++ b/src/lib/expand-unit.ts @@ -5,6 +5,8 @@ import { SCORE_API_DOMAIN, SITE_URL } from "~/lib/env" import { toCid, toGateway } from "~/lib/ipfs-parser" import { ExpandedCharacter, ExpandedNote } from "~/lib/types" +import { getNoteSlug } from "./helpers" + export const expandCrossbellNote = async ( note: NoteEntity, useStat?: boolean, @@ -44,11 +46,7 @@ export const expandCrossbellNote = async ( expandedNote.metadata.content.audio = rendered.audio expandedNote.metadata.content.frontMatter = rendered.frontMatter } - expandedNote.metadata.content.slug = encodeURIComponent( - expandedNote.metadata.content.attributes?.find( - (a) => a.trait_type === "xlog_slug", - )?.value || "", - ) + expandedNote.metadata.content.slug = getNoteSlug(expandedNote) if (useStat) { const stat = await ( diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts index e8bc3379..e292bc4f 100644 --- a/src/lib/helpers.ts +++ b/src/lib/helpers.ts @@ -42,12 +42,14 @@ export const getSlugUrl = (slug: string) => { } export const getNoteSlug = (note: NoteEntity) => { - return ( + return encodeURIComponent( note.metadata?.content?.attributes?.find( (a) => a?.trait_type === "xlog_slug", )?.value || - (note.metadata?.content as any)?._xlog_slug || - (note.metadata?.content as any)?._crosslog_slug + (note.metadata?.content as any)?._xlog_slug || + (note.metadata?.content as any)?._crosslog_slug || + note.metadata?.content?.title || + `${note.characterId}-${note.noteId}`, )?.toLowerCase?.() }