feat: remove old notifications

This commit is contained in:
DIYgod
2023-01-18 12:45:40 +00:00
parent 7627978762
commit 2b9db31546
7 changed files with 2 additions and 373 deletions
+1 -21
View File
@@ -11,8 +11,7 @@ import { UniLink } from "../ui/UniLink"
import { DashboardSidebar } from "./DashboardSidebar"
import { useAccountSites } from "~/queries/site"
import { ConnectButton } from "~/components/common/ConnectButton"
import { useGetNotifications, useGetSite, useIsOperators } from "~/queries/site"
import { getStorage } from "~/lib/storage"
import { useGetSite, useIsOperators } from "~/queries/site"
import { toGateway } from "~/lib/ipfs-parser"
import { Avatar } from "~/components/ui/Avatar"
import {
@@ -38,30 +37,11 @@ export function DashboardLayout({
const userSite = useAccountSites()
const notifications = useGetNotifications({
siteCId: site.data?.metadata?.proof,
})
const isOperator = useIsOperators({
characterId: +(site.data?.metadata?.proof || 0),
operator: address,
})
const notificationCreatedAt =
getStorage(`notification-${subdomain}`)?.createdAt || 0
const [notificationCount, setNotificationCount] = useState(0)
useEffect(() => {
if (notifications.data && subdomain) {
const count = notifications.data.filter(
(notification: any) =>
+new Date(notification.createdAt) > +new Date(notificationCreatedAt),
).length
setNotificationCount(count)
}
}, [notifications.data, subdomain, notificationCreatedAt])
useEffect(() => {
if (ssrReady && !isConnected) {
router.push("/")
+1 -1
View File
@@ -62,7 +62,7 @@ export const SiteHeader: React.FC<{
{
text: "View on blockchain explorer",
icon: <BlockchainIcon />,
url: `${CSB_SCAN}/address/${site?.metadata?.owner}/transactions`,
url: `${CSB_SCAN}/address/${site?.metadata?.owner}`,
},
{
text: "JSON Feed",
-41
View File
@@ -454,47 +454,6 @@ export async function unsubscribeFromSite(
)
}
export async function getNotifications(input: { siteCId: string }) {
const [subscriptions, notes] = await Promise.all([
indexer.getBacklinksOfCharacter(input.siteCId, {
limit: 100,
}),
indexer.getNotes({
toCharacterId: input.siteCId,
limit: 100,
includeCharacter: true,
}),
])
return [
...(subscriptions.list.map(
(
item: LinkEntity & {
type?: "backlinks"
},
) => {
item.type = "backlinks"
return item
},
) as any),
]
.concat(
notes.list.map(
(
item: NoteEntity & {
type?: "notes"
},
) => {
item.type = "notes"
return item
},
),
)
.sort((a, b) => {
return b.createdAt > a.createdAt ? 1 : -1
})
}
const xLogOperatorPermissions: CharacterOperatorPermission[] = [
CharacterOperatorPermission.SET_NOTE_URI,
CharacterOperatorPermission.DELETE_NOTE,
@@ -1,99 +0,0 @@
import { GetServerSideProps } from "next"
import { fetchGetSite, fetchGetNotifications } from "~/queries/site.server"
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: "Notifications on " + site.name,
description: site.description,
icon: link + site.avatars?.[0],
home_page_url: link,
feed_url: `${link}/feed/notifications`,
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
@@ -1,183 +0,0 @@
import { useRouter } from "next/router"
import { DashboardLayout } from "~/components/dashboard/DashboardLayout"
import { DashboardMain } from "~/components/dashboard/DashboardMain"
import { UniLink } from "~/components/ui/UniLink"
import { getSiteLink } from "~/lib/helpers"
import { useGetNotifications, useGetSite } from "~/queries/site"
import { CharacterFloatCard } from "~/components/common/CharacterFloatCard"
import { Avatar } from "~/components/ui/Avatar"
import dayjs from "~/lib/date"
import { BlockchainIcon } from "~/components/icons/BlockchainIcon"
import { CSB_SCAN } from "~/lib/env"
import { PageContent } from "~/components/common/PageContent"
import { useEffect, useMemo, useState } from "react"
import { setStorage, getStorage } from "~/lib/storage"
export default function SubdomainIndex() {
const router = useRouter()
const subdomain = router.query.subdomain as string
const site = useGetSite(subdomain)
const notifications = useGetNotifications({
siteCId: site.data?.metadata?.proof,
})
const [lastNotificationCreated, setLastNotificationCreated] = useState(0)
useMemo(() => {
if (subdomain && !lastNotificationCreated) {
setLastNotificationCreated(
+new Date(getStorage(`notification-${subdomain}`)?.createdAt || 1),
)
}
}, [subdomain])
useEffect(() => {
if (notifications.data?.[0].createdAt && subdomain) {
setStorage(`notification-${subdomain}`, {
createdAt: notifications.data[0].createdAt,
})
}
}, [notifications.data, subdomain])
return (
<DashboardLayout title="Dashboard">
<DashboardMain>
<h2 className="text-2xl font-bold">Notifications</h2>
<p className="mt-4">
Subscribing the{" "}
<UniLink
className="text-accent-emphasis"
href={`${getSiteLink({
subdomain: subdomain,
})}/feed/notifications`}
>
notifications feed
</UniLink>{" "}
via RSS Reader is a great way to get notified in a timely manner.
</p>
<div className="xlog-comment">
<div className="prose space-y-4 pt-4">
{notifications.data?.map((notification) => {
let key = ""
let character
let message = ""
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"
break
case "backlinks":
key = "backlinks" + notification.fromCharacter.handle
character = notification.fromCharacter
message = "follows you"
break
}
return (
<div key={key} className="border-t border-dashed pt-4">
<div className="flex group items-center relative">
{+new Date(notification.createdAt) >
+new Date(lastNotificationCreated) && (
<div className="absolute right-full text-xs pr-2 top-1/2 -translate-y-1/2 font-bold text-accent">
New
</div>
)}
<div>
<CharacterFloatCard siteId={character?.handle}>
<div>
<UniLink
href={
character?.handle &&
getSiteLink({
subdomain: character.handle,
})
}
className="block align-middle mr-3 text-[0px]"
>
<Avatar
images={
character?.metadata?.content?.avatars || []
}
name={character?.metadata?.content?.name}
size={35}
/>
</UniLink>
</div>
</CharacterFloatCard>
</div>
<div className="flex-1 flex flex-col rounded-lg">
<div className="mb-1 text-sm">
<UniLink
href={
character?.handle &&
getSiteLink({
subdomain: character.handle,
})
}
className="font-medium text-accent"
>
{character?.metadata?.content?.name ||
character?.handle}
</UniLink>{" "}
{message}{" "}
<UniLink
href={`${getSiteLink({
subdomain: notification.toCharacter?.handle || "",
})}/${
notification.toNote?.metadata?.content?.attributes?.find(
(attribute: any) =>
attribute.trait_type === "xlog_slug",
)?.value ||
notification.toNote?.characterId +
"-" +
notification.toNote?.noteId
}`}
>
{notification.toNote?.metadata?.content?.title}
</UniLink>{" "}
·{" "}
{dayjs
.duration(
dayjs(notification?.createdAt).diff(
dayjs(),
"minute",
),
"minute",
)
.humanize()}{" "}
ago ·{" "}
<UniLink
href={`${CSB_SCAN}/tx/${notification.transactionHash}`}
>
<BlockchainIcon className="w-3 h-3 inline-block" />
</UniLink>
</div>
<PageContent
content={notification.metadata?.content?.content}
></PageContent>
</div>
</div>
</div>
)
})}
</div>
</div>
</DashboardMain>
</DashboardLayout>
)
}
-17
View File
@@ -48,20 +48,3 @@ 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!,
}),
)
})
}
-11
View File
@@ -194,17 +194,6 @@ export function useUnsubscribeFromSite() {
})
}
export const useGetNotifications = (data: { siteCId?: string }) => {
return useQuery(["getNotifications", data], async () => {
if (!data.siteCId) {
return null
}
return siteModel.getNotifications({
siteCId: data.siteCId,
})
})
}
export const useGetOperators = (
data: Parameters<typeof siteModel.getOperators>[0],
) => {