feat: format param for json feed

This commit is contained in:
DIYgod
2023-04-30 11:41:18 +01:00
parent d1a6c225c8
commit 9beab8c7b6
13 changed files with 183 additions and 119 deletions
+43 -7
View File
@@ -32,24 +32,30 @@ export const SEOHead: React.FC<{
<link rel="icon" href={icon || `${SITE_URL}/assets/logo.svg`}></link>
{site ? (
<>
<link
rel="alternate"
href="/feed/xml"
title={siteName}
type="application/rss+xml"
></link>
<link
rel="alternate"
href="/feed"
title={siteName}
type="application/feed+json"
></link>
<link
rel="alternate"
href="/feed?format=xml"
title={siteName}
type="application/rss+xml"
></link>
<link
rel="alternate"
href="/feed/comments"
title={`Comments on ${siteName}`}
type="application/feed+json"
></link>
<link
rel="alternate"
href="/feed/comments?format=xml"
title={`Comments on ${siteName}`}
type="application/rss+xml"
></link>
<link rel="manifest" href="/manifest.json" />
</>
) : (
@@ -60,30 +66,60 @@ export const SEOHead: React.FC<{
title="xLog Latest"
type="application/feed+json"
></link>
<link
rel="alternate"
href="/latest?format=xml"
title="xLog Latest"
type="application/rss+xml"
></link>
<link
rel="alternate"
href="/feed/hottest/0"
title="xLog Hot"
type="application/feed+json"
></link>
<link
rel="alternate"
href="/feed/hottest/0?format=xml"
title="xLog Hot"
type="application/rss+xml"
></link>
<link
rel="alternate"
href="/feed/hottest/1"
title="xLog Daily Hot"
type="application/feed+json"
></link>
<link
rel="alternate"
href="/feed/hottest/1?format=xml"
title="xLog Daily Hot"
type="application/rss+xml"
></link>
<link
rel="alternate"
href="/feed/hottest/7"
title="xLog Weekly Hot"
type="application/feed+json"
></link>
<link
rel="alternate"
href="/feed/hottest/7?format=xml"
title="xLog Weekly Hot"
type="application/rss+xml"
></link>
<link
rel="alternate"
href="/feed/hottest/30"
title="xLog Monthly Hottest"
title="xLog Monthly Hot"
type="application/feed+json"
></link>
<link
rel="alternate"
href="/feed/hottest/30?format=xml"
title="xLog Monthly Hot"
type="application/rss+xml"
></link>
</>
)}
</Head>
+1 -1
View File
@@ -116,7 +116,7 @@ export const SiteHeader: React.FC<{
{
text: "Subscribe to RSS",
icon: <RssIcon className="w-full h-full text-[#ee832f]" />,
url: `/feed/xml`,
url: `/feed?format=xml`,
out: true,
},
{
+17 -7
View File
@@ -73,10 +73,18 @@ export const getJsonFeed = async (domainOrSubdomain: string, path: string) => {
}
}
export const parsePost = (post: ExpandedNote) => {
export const parsePost = (post: ExpandedNote, withTwitter?: boolean) => {
let twitter
if (withTwitter) {
twitter = post.character?.metadata?.content?.connected_accounts
?.find((account) => account?.endsWith?.("@twitter"))
?.match(/csb:\/\/account:([^@]+)@twitter/)?.[1]
}
return {
id: `${post.characterId}-${post.noteId}`,
title: post.metadata?.content?.title,
title: `${post.metadata?.content?.title}${
withTwitter && twitter ? ` by @${twitter}` : ""
}`,
summary: post.metadata?.content?.summary,
url: `${SITE_URL}/api/redirection?characterId=${post.characterId}&noteId=${post.noteId}`,
image: post.metadata?.content?.cover,
@@ -86,16 +94,18 @@ export const parsePost = (post: ExpandedNote) => {
author: {
name: post.character?.metadata?.content?.name || post.character?.handle,
url: `${SITE_URL}/api/redirection?characterId=${post.characterId}`,
twitter: post.character?.metadata?.content?.connected_accounts
?.find((account) => account?.endsWith?.("@twitter"))
?.match(/csb:\/\/account:([^@]+)@twitter/)?.[1],
handle: post.character?.handle,
},
}
}
export const setHeader = (ctx: GetServerSidePropsContext) => {
ctx.res.setHeader("Content-Type", "application/feed+json; charset=utf-8")
export const setHeader = (ctx: GetServerSidePropsContext, isXml?: boolean) => {
ctx.res.setHeader(
"Content-Type",
ctx.query.format === "xml" || isXml
? "application/xml; charset=utf-8"
: "application/feed+json; charset=utf-8",
)
ctx.res.setHeader("Access-Control-Allow-Methods", "GET")
ctx.res.setHeader("Access-Control-Allow-Origin", "*")
ctx.res.setHeader("Cache-Control", "public, max-age=1800")
+6 -2
View File
@@ -32,9 +32,13 @@ export default async function middleware(req: NextRequest) {
}
}
if (pathname === "/feed.xml" || pathname === "/atom.xml") {
if (
pathname === "/feed.xml" ||
pathname === "/atom.xml" ||
pathname === "/feed/xml"
) {
return NextResponse.redirect(
`https://${req.headers.get("host")}/feed/xml`,
`https://${req.headers.get("host")}/feed?format=xml`,
301,
)
}
+46 -43
View File
@@ -1,16 +1,17 @@
// @ts-ignore
import jsonfeedToRSS from "jsonfeed-to-rss"
import { GetServerSideProps } from "next"
import { QueryClient } from "@tanstack/react-query"
import { getSiteLink } from "~/lib/helpers"
import { setHeader } from "~/lib/json-feed"
import { renderPageContent } from "~/markdown"
import { fetchGetComments, fetchGetSite } from "~/queries/site.server"
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const queryClient = new QueryClient()
ctx.res.setHeader("Content-Type", "application/feed+json; charset=utf-8")
ctx.res.setHeader("Access-Control-Allow-Methods", "GET")
ctx.res.setHeader("Access-Control-Allow-Origin", "*")
setHeader(ctx)
const domainOrSubdomain = ctx.params!.site as string
const site = await fetchGetSite(domainOrSubdomain, queryClient)
@@ -24,49 +25,51 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
const link = getSiteLink({
subdomain: site.username || "",
})
ctx.res.write(
JSON.stringify({
version: "https://jsonfeed.org/version/1.1",
title: "Comments on " + site.name,
description: site.description,
icon: site.avatars?.[0],
home_page_url: link,
feed_url: `${link}/feed/notifications`,
items: comments?.list?.map((comment) => {
const type = comment.toNote?.metadata?.content?.tags?.[0]
let toTitle
if (type === "post" || type === "page") {
toTitle = comment.toNote?.metadata?.content?.title
} else {
if ((comment.toNote?.metadata?.content?.content?.length || 0) > 30) {
toTitle =
comment.toNote?.metadata?.content?.content?.slice(0, 30) + "..."
} else {
toTitle = comment.toNote?.metadata?.content?.content
}
}
const name =
comment?.character?.metadata?.content?.name ||
`@${comment?.character?.handle}`
return {
id: comment.characterId + "-" + comment.noteId,
title: `${name}: ${comment.metadata?.content?.content}`,
content_html: `${name} commented on ${type} ${toTitle}: ${
renderPageContent(comment.metadata?.content?.content || "")
.contentHTML
}`,
url: `${link}/${
comment.toNote?.metadata?.content?.attributes?.find(
(attribute: any) => attribute.trait_type === "xlog_slug",
)?.value ||
comment.toNote?.characterId + "-" + comment.toNote?.noteId
}`,
date_published: comment.createdAt,
date_modified: comment.updatedAt,
const data = {
version: "https://jsonfeed.org/version/1",
title: "Comments on " + site.name,
description: site.description,
icon: site.avatars?.[0],
home_page_url: link,
feed_url: `${link}/feed/notifications`,
items: comments?.list?.map((comment) => {
const type = comment.toNote?.metadata?.content?.tags?.[0]
let toTitle
if (type === "post" || type === "page") {
toTitle = comment.toNote?.metadata?.content?.title
} else {
if ((comment.toNote?.metadata?.content?.content?.length || 0) > 30) {
toTitle =
comment.toNote?.metadata?.content?.content?.slice(0, 30) + "..."
} else {
toTitle = comment.toNote?.metadata?.content?.content
}
}),
}
const name =
comment?.character?.metadata?.content?.name ||
`@${comment?.character?.handle}`
return {
id: comment.characterId + "-" + comment.noteId,
title: `${name}: ${comment.metadata?.content?.content}`,
content_html: `${name} commented on ${type} ${toTitle}: ${
renderPageContent(comment.metadata?.content?.content || "")
.contentHTML
}`,
url: `${link}/${
comment.toNote?.metadata?.content?.attributes?.find(
(attribute: any) => attribute.trait_type === "xlog_slug",
)?.value || comment.toNote?.characterId + "-" + comment.toNote?.noteId
}`,
date_published: comment.createdAt,
date_modified: comment.updatedAt,
}
}),
}
ctx.res.write(
ctx.query.format === "xml" ? jsonfeedToRSS(data) : JSON.stringify(data),
)
ctx.res.end()
+7 -1
View File
@@ -1,3 +1,5 @@
// @ts-ignore
import jsonfeedToRSS from "jsonfeed-to-rss"
import { GetServerSideProps } from "next"
import { getJsonFeed, setHeader } from "~/lib/json-feed"
@@ -6,7 +8,11 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
setHeader(ctx)
const domainOrSubdomain = ctx.params!.site as string
ctx.res.write(JSON.stringify(await getJsonFeed(domainOrSubdomain, "/feed")))
const data = await getJsonFeed(domainOrSubdomain, "/feed")
ctx.res.write(
ctx.query.format === "xml" ? jsonfeedToRSS(data) : JSON.stringify(data),
)
ctx.res.end()
return {
-23
View File
@@ -1,23 +0,0 @@
// @ts-ignore
import jsonfeedToRSS from "jsonfeed-to-rss"
import { GetServerSideProps } from "next"
import { getJsonFeed, setHeader } from "~/lib/json-feed"
export const getServerSideProps: GetServerSideProps = async (ctx) => {
setHeader(ctx)
const domainOrSubdomain = ctx.params!.site as string
ctx.res.write(
jsonfeedToRSS(await getJsonFeed(domainOrSubdomain, "/feed/xml")),
)
ctx.res.end()
return {
props: {},
}
}
const SiteFeed: React.FC = () => null
export default SiteFeed
+14 -8
View File
@@ -1,3 +1,5 @@
// @ts-ignore
import jsonfeedToRSS from "jsonfeed-to-rss"
import { GetServerSideProps } from "next"
import { SITE_URL } from "~/lib/env"
@@ -13,15 +15,19 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
daysInterval: 0,
})
const data = {
version: "https://jsonfeed.org/version/1",
title: "xLog Hot",
icon: "https://ipfs.4everland.xyz/ipfs/bafkreigxdnr5lvtjxqin5upquomrti2s77hlgtjy5zaeu43uhpny75rbga",
home_page_url: `${SITE_URL}/activities`,
feed_url: `${SITE_URL}/feed/hottest`,
items: feed?.list?.map((post: ExpandedNote) =>
parsePost(post, !!ctx.query.withTwitter),
),
}
ctx.res.write(
JSON.stringify({
version: "https://jsonfeed.org/version/1.1",
title: "xLog Hot",
icon: "https://ipfs.4everland.xyz/ipfs/bafkreigxdnr5lvtjxqin5upquomrti2s77hlgtjy5zaeu43uhpny75rbga",
home_page_url: `${SITE_URL}/activities`,
feed_url: `${SITE_URL}/feed/hottest`,
items: feed?.list?.map((post: ExpandedNote) => parsePost(post)),
}),
ctx.query.format === "xml" ? jsonfeedToRSS(data) : JSON.stringify(data),
)
ctx.res.end()
+14 -8
View File
@@ -1,3 +1,5 @@
// @ts-ignore
import jsonfeedToRSS from "jsonfeed-to-rss"
import { GetServerSideProps } from "next"
import { SITE_URL } from "~/lib/env"
@@ -13,15 +15,19 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
daysInterval: 1,
})
const data = {
version: "https://jsonfeed.org/version/1",
title: "xLog Daily Hot",
icon: "https://ipfs.4everland.xyz/ipfs/bafkreigxdnr5lvtjxqin5upquomrti2s77hlgtjy5zaeu43uhpny75rbga",
home_page_url: `${SITE_URL}/activities`,
feed_url: `${SITE_URL}/feed/hottest`,
items: feed?.list?.map((post: ExpandedNote) =>
parsePost(post, !!ctx.query.withTwitter),
),
}
ctx.res.write(
JSON.stringify({
version: "https://jsonfeed.org/version/1.1",
title: "xLog Daily Hot",
icon: "https://ipfs.4everland.xyz/ipfs/bafkreigxdnr5lvtjxqin5upquomrti2s77hlgtjy5zaeu43uhpny75rbga",
home_page_url: `${SITE_URL}/activities`,
feed_url: `${SITE_URL}/feed/hottest`,
items: feed?.list?.map((post: ExpandedNote) => parsePost(post)),
}),
ctx.query.format === "xml" ? jsonfeedToRSS(data) : JSON.stringify(data),
)
ctx.res.end()
+14 -8
View File
@@ -1,3 +1,5 @@
// @ts-ignore
import jsonfeedToRSS from "jsonfeed-to-rss"
import { GetServerSideProps } from "next"
import { SITE_URL } from "~/lib/env"
@@ -13,15 +15,19 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
daysInterval: 30,
})
const data = {
version: "https://jsonfeed.org/version/1",
title: "xLog Monthly Hot",
icon: "https://ipfs.4everland.xyz/ipfs/bafkreigxdnr5lvtjxqin5upquomrti2s77hlgtjy5zaeu43uhpny75rbga",
home_page_url: `${SITE_URL}/activities`,
feed_url: `${SITE_URL}/feed/hottest`,
items: feed?.list?.map((post: ExpandedNote) =>
parsePost(post, !!ctx.query.withTwitter),
),
}
ctx.res.write(
JSON.stringify({
version: "https://jsonfeed.org/version/1.1",
title: "xLog Monthly Hot",
icon: "https://ipfs.4everland.xyz/ipfs/bafkreigxdnr5lvtjxqin5upquomrti2s77hlgtjy5zaeu43uhpny75rbga",
home_page_url: `${SITE_URL}/activities`,
feed_url: `${SITE_URL}/feed/hottest`,
items: feed?.list?.map((post: ExpandedNote) => parsePost(post)),
}),
ctx.query.format === "xml" ? jsonfeedToRSS(data) : JSON.stringify(data),
)
ctx.res.end()
+14 -8
View File
@@ -1,3 +1,5 @@
// @ts-ignore
import jsonfeedToRSS from "jsonfeed-to-rss"
import { GetServerSideProps } from "next"
import { SITE_URL } from "~/lib/env"
@@ -13,15 +15,19 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
daysInterval: 7,
})
const data = {
version: "https://jsonfeed.org/version/1",
title: "xLog Weekly Hot",
icon: "https://ipfs.4everland.xyz/ipfs/bafkreigxdnr5lvtjxqin5upquomrti2s77hlgtjy5zaeu43uhpny75rbga",
home_page_url: `${SITE_URL}/activities`,
feed_url: `${SITE_URL}/feed/hottest`,
items: feed?.list?.map((post: ExpandedNote) =>
parsePost(post, !!ctx.query.withTwitter),
),
}
ctx.res.write(
JSON.stringify({
version: "https://jsonfeed.org/version/1.1",
title: "xLog Weekly Hot",
icon: "https://ipfs.4everland.xyz/ipfs/bafkreigxdnr5lvtjxqin5upquomrti2s77hlgtjy5zaeu43uhpny75rbga",
home_page_url: `${SITE_URL}/activities`,
feed_url: `${SITE_URL}/feed/hottest`,
items: feed?.list?.map((post: ExpandedNote) => parsePost(post)),
}),
ctx.query.format === "xml" ? jsonfeedToRSS(data) : JSON.stringify(data),
)
ctx.res.end()
+6 -2
View File
@@ -1,3 +1,5 @@
// @ts-ignore
import jsonfeedToRSS from "jsonfeed-to-rss"
import { GetServerSideProps } from "next"
import { SITE_URL } from "~/lib/env"
@@ -13,7 +15,7 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
})
const data = {
version: "https://jsonfeed.org/version/1.1",
version: "https://jsonfeed.org/version/1",
title: "xLog Latest",
icon: "https://ipfs.4everland.xyz/ipfs/bafkreigxdnr5lvtjxqin5upquomrti2s77hlgtjy5zaeu43uhpny75rbga",
home_page_url: `${SITE_URL}/activities`,
@@ -21,7 +23,9 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
items: feed?.list?.map((post: ExpandedNote) => parsePost(post)),
}
ctx.res.write(JSON.stringify(data))
ctx.res.write(
ctx.query.format === "xml" ? jsonfeedToRSS(data) : JSON.stringify(data),
)
ctx.res.end()
return {
+1 -1
View File
@@ -141,7 +141,7 @@ function Home() {
url:
getSiteLink({
subdomain: "xlog",
}) + "/feed/xml",
}) + "/feed?format=xml",
},
{
name: "JSON Feed",