From 2226eecdf296e574298d013a02a3b051ba54323c Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 4 Nov 2022 22:24:17 +0000 Subject: [PATCH] feat: comment feed --- src/pages/_site/[site]/feed/comments.tsx | 101 ++++++++++++++++++ .../_site/[site]/{feed.tsx => feed/index.tsx} | 0 .../dashboard/[subdomain]/notifications.tsx | 10 +- src/queries/site.server.ts | 17 +++ 4 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 src/pages/_site/[site]/feed/comments.tsx rename src/pages/_site/[site]/{feed.tsx => feed/index.tsx} (100%) diff --git a/src/pages/_site/[site]/feed/comments.tsx b/src/pages/_site/[site]/feed/comments.tsx new file mode 100644 index 00000000..9e6f787a --- /dev/null +++ b/src/pages/_site/[site]/feed/comments.tsx @@ -0,0 +1,101 @@ +import { GetServerSideProps } from "next" +import { fetchGetSite, fetchGetNotifications } from "~/queries/site.server" +import { fetchGetPagesBySite } from "~/queries/page.server" +import { PageVisibilityEnum } from "~/lib/types" +import { getSiteLink } from "~/lib/helpers" +import { QueryClient } from "@tanstack/react-query" +import { renderPageContent } from "~/markdown" + +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", "*") + const domainOrSubdomain = ctx.params!.site as string + + const site = await fetchGetSite(domainOrSubdomain, queryClient) + const notifications = await fetchGetNotifications( + { + siteCId: site?.metadata?.proof, + }, + queryClient, + ) + + const link = getSiteLink({ + subdomain: site.username || "", + }) + ctx.res.write( + JSON.stringify({ + version: "https://jsonfeed.org/version/1.1", + title: site.name, + description: site.description, + icon: site.avatars?.[0], + home_page_url: link, + feed_url: `${link}/feed.json`, + items: notifications + ?.map((notification: any) => { + let key = "" + let character + let message = "" + let url = "" + + switch (notification.type) { + case "notes": + if ( + !notification.toNote.metadata?.content?.sources?.includes( + "xlog", + ) + ) { + return null + } + key = + "notes" + notification.characterId + "-" + notification.noteId + character = notification?.character + message = "commented on" + url = `${link}/${ + notification.toNote?.metadata?.content?.attributes?.find( + (attribute: any) => attribute.trait_type === "xlog_slug", + )?.value || + notification.toNote?.characterId + + "-" + + notification.toNote?.noteId + }` + break + case "backlinks": + key = "backlinks" + notification.fromCharacter.handle + character = notification.fromCharacter + message = "follows you" + url = link + break + } + + return { + id: key, + title: `${ + character.metadata?.content?.name + ? character.metadata?.content?.name + : `@${character.handle}` + } ${message} ${ + notification.toNote?.metadata?.content?.title || "" + }`, + content_html: renderPageContent( + notification.metadata?.content?.content, + ).contentHTML, + url: url, + date_published: notification.createdAt, + date_modified: notification.updatedAt, + } + }) + .filter((item: any) => item), + }), + ) + ctx.res.end() + + return { + props: {}, + } +} + +const SiteFeed: React.FC = () => null + +export default SiteFeed diff --git a/src/pages/_site/[site]/feed.tsx b/src/pages/_site/[site]/feed/index.tsx similarity index 100% rename from src/pages/_site/[site]/feed.tsx rename to src/pages/_site/[site]/feed/index.tsx diff --git a/src/pages/dashboard/[subdomain]/notifications.tsx b/src/pages/dashboard/[subdomain]/notifications.tsx index a11389c2..14bfd290 100644 --- a/src/pages/dashboard/[subdomain]/notifications.tsx +++ b/src/pages/dashboard/[subdomain]/notifications.tsx @@ -66,12 +66,16 @@ export default function SubdomainIndex() { ) { return null } - key = notification.characterId + notification.noteId + key = + "notes" + + notification.characterId + + "-" + + notification.noteId character = notification?.character - message = "commented on your post" + message = "commented on" break case "backlinks": - key = notification.fromCharacter.handle + key = "backlinks" + notification.fromCharacter.handle character = notification.fromCharacter message = "follows you" break diff --git a/src/queries/site.server.ts b/src/queries/site.server.ts index f141d50e..6e112c70 100644 --- a/src/queries/site.server.ts +++ b/src/queries/site.server.ts @@ -48,3 +48,20 @@ export const prefetchGetSites = async ( return cacheGet(key, () => siteModel.getSites(input)) }) } + +export const fetchGetNotifications = async ( + data: { siteCId?: string }, + queryClient: QueryClient, +) => { + const key = ["getNotifications", data] + if (!data.siteCId) { + return null + } + return await queryClient.fetchQuery(key, async () => { + return cacheGet(key, () => + siteModel.getNotifications({ + siteCId: data.siteCId!, + }), + ) + }) +}