feat: implement sharing
This commit is contained in:
parent
385b31d42c
commit
8cc3fea048
19 changed files with 507 additions and 96 deletions
|
|
@ -5,6 +5,7 @@ import { kv } from '@vercel/kv'
|
|||
|
||||
import { type Chat } from '@/lib/types'
|
||||
import { currentUser } from '@clerk/nextjs'
|
||||
import { nanoid } from '@/lib/utils'
|
||||
|
||||
export async function getChats(userId?: string | null) {
|
||||
if (!userId) {
|
||||
|
|
@ -60,3 +61,54 @@ export async function removeChat({ id, path }: { id: string; path: string }) {
|
|||
revalidatePath('/')
|
||||
revalidatePath(path)
|
||||
}
|
||||
|
||||
export async function clearChats() {
|
||||
const user = await currentUser()
|
||||
|
||||
if (!user) {
|
||||
throw new Error('Unauthorized')
|
||||
}
|
||||
|
||||
const chats: string[] = await kv.zrange(`user:chat:${user.id}`, 0, -1, {
|
||||
rev: true
|
||||
})
|
||||
|
||||
const pipeline = kv.pipeline()
|
||||
|
||||
for (const chat of chats) {
|
||||
pipeline.del(chat)
|
||||
pipeline.zrem(`user:chat:${user.id}`, chat)
|
||||
}
|
||||
|
||||
await pipeline.exec()
|
||||
|
||||
revalidatePath('/')
|
||||
}
|
||||
|
||||
export async function shareChat(chat: Chat) {
|
||||
const user = await currentUser()
|
||||
|
||||
if (!user || chat.userId !== user.id) {
|
||||
throw new Error('Unauthorized')
|
||||
}
|
||||
|
||||
const id = nanoid()
|
||||
const createdAt = Date.now()
|
||||
|
||||
const payload = {
|
||||
id,
|
||||
title: chat.title,
|
||||
userId: user.id,
|
||||
createdAt,
|
||||
path: `/share/${id}`,
|
||||
chat
|
||||
}
|
||||
|
||||
await kv.hmset(`share:${id}`, payload)
|
||||
await kv.zadd(`user:share:${user.id}`, {
|
||||
score: createdAt,
|
||||
member: `share:${id}`
|
||||
})
|
||||
|
||||
return payload
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,11 +43,13 @@ export async function POST(req: Request) {
|
|||
const userId = user.id
|
||||
const id = json.id ?? nanoid()
|
||||
const createdAt = Date.now()
|
||||
const path = `/chat/${id}`
|
||||
const payload = {
|
||||
id,
|
||||
title,
|
||||
userId,
|
||||
createdAt,
|
||||
path,
|
||||
messages: [
|
||||
...messages,
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import { Metadata } from 'next'
|
||||
|
||||
import { ClerkProvider } from '@clerk/nextjs'
|
||||
import { Toaster } from 'react-hot-toast'
|
||||
|
||||
import '@/app/globals.css'
|
||||
import { fontMono, fontSans } from '@/lib/fonts'
|
||||
|
|
@ -42,6 +43,7 @@ export default function RootLayout({ children }: RootLayoutProps) {
|
|||
fontMono.variable
|
||||
)}
|
||||
>
|
||||
<Toaster />
|
||||
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
|
||||
<div className="flex min-h-screen flex-col">
|
||||
{/* @ts-ignore */}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue