2023-06-02 15:33:48 -04:00
|
|
|
'use server'
|
2023-05-19 12:33:56 -04:00
|
|
|
|
2023-06-02 15:33:48 -04:00
|
|
|
import { revalidatePath } from 'next/cache'
|
2023-06-16 21:52:01 +04:00
|
|
|
import { redirect } from 'next/navigation'
|
2023-06-11 00:23:23 +04:00
|
|
|
import { kv } from '@vercel/kv'
|
|
|
|
|
|
2023-06-16 21:52:01 +04:00
|
|
|
import { auth } from '@/auth'
|
2023-06-11 00:23:23 +04:00
|
|
|
import { type Chat } from '@/lib/types'
|
|
|
|
|
|
2023-06-11 11:14:39 -04:00
|
|
|
export async function getChats(userId?: string | null) {
|
2024-08-01 14:33:37 +05:30
|
|
|
const session = await auth()
|
|
|
|
|
|
2023-06-11 11:14:39 -04:00
|
|
|
if (!userId) {
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-01 14:33:37 +05:30
|
|
|
if (userId !== session?.user?.id) {
|
|
|
|
|
return {
|
|
|
|
|
error: 'Unauthorized'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-11 00:23:23 +04:00
|
|
|
try {
|
|
|
|
|
const pipeline = kv.pipeline()
|
2023-06-15 16:29:05 +04:00
|
|
|
const chats: string[] = await kv.zrange(`user:chat:${userId}`, 0, -1, {
|
|
|
|
|
rev: true
|
|
|
|
|
})
|
2023-06-11 00:23:23 +04:00
|
|
|
|
|
|
|
|
for (const chat of chats) {
|
|
|
|
|
pipeline.hgetall(chat)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const results = await pipeline.exec()
|
|
|
|
|
|
|
|
|
|
return results as Chat[]
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getChat(id: string, userId: string) {
|
2024-08-01 14:33:37 +05:30
|
|
|
const session = await auth()
|
|
|
|
|
|
|
|
|
|
if (userId !== session?.user?.id) {
|
|
|
|
|
return {
|
|
|
|
|
error: 'Unauthorized'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-11 00:23:23 +04:00
|
|
|
const chat = await kv.hgetall<Chat>(`chat:${id}`)
|
|
|
|
|
|
2023-06-16 21:52:01 +04:00
|
|
|
if (!chat || (userId && chat.userId !== userId)) {
|
|
|
|
|
return null
|
2023-06-11 00:23:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return chat
|
|
|
|
|
}
|
2023-05-19 12:33:56 -04:00
|
|
|
|
2023-06-13 17:31:15 -04:00
|
|
|
export async function removeChat({ id, path }: { id: string; path: string }) {
|
2023-06-16 12:03:09 -04:00
|
|
|
const session = await auth()
|
2023-06-13 17:31:15 -04:00
|
|
|
|
2023-06-16 11:49:14 -04:00
|
|
|
if (!session) {
|
2023-06-16 21:52:01 +04:00
|
|
|
return {
|
|
|
|
|
error: 'Unauthorized'
|
|
|
|
|
}
|
2023-06-13 17:31:15 -04:00
|
|
|
}
|
2023-05-19 14:13:38 -04:00
|
|
|
|
2024-08-01 14:33:37 +05:30
|
|
|
// Convert uid to string for consistent comparison with session.user.id
|
2023-11-27 22:14:06 +01:00
|
|
|
const uid = String(await kv.hget(`chat:${id}`, 'userId'))
|
2023-06-16 16:18:52 +04:00
|
|
|
|
2023-06-16 11:49:14 -04:00
|
|
|
if (uid !== session?.user?.id) {
|
2023-06-16 21:52:01 +04:00
|
|
|
return {
|
|
|
|
|
error: 'Unauthorized'
|
|
|
|
|
}
|
2023-06-02 15:15:35 -04:00
|
|
|
}
|
2023-06-16 16:18:52 +04:00
|
|
|
|
2023-06-02 15:33:48 -04:00
|
|
|
await kv.del(`chat:${id}`)
|
2023-06-16 11:49:14 -04:00
|
|
|
await kv.zrem(`user:chat:${session.user.id}`, `chat:${id}`)
|
2023-05-19 12:33:56 -04:00
|
|
|
|
2023-06-02 15:33:48 -04:00
|
|
|
revalidatePath('/')
|
2023-06-16 21:52:01 +04:00
|
|
|
return revalidatePath(path)
|
2023-05-19 12:33:56 -04:00
|
|
|
}
|
2023-06-16 15:20:42 +04:00
|
|
|
|
|
|
|
|
export async function clearChats() {
|
2023-06-16 11:49:14 -04:00
|
|
|
const session = await auth()
|
2023-06-16 15:20:42 +04:00
|
|
|
|
2023-06-16 21:52:01 +04:00
|
|
|
if (!session?.user?.id) {
|
|
|
|
|
return {
|
|
|
|
|
error: 'Unauthorized'
|
|
|
|
|
}
|
2023-06-16 11:49:14 -04:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 21:52:01 +04:00
|
|
|
const chats: string[] = await kv.zrange(`user:chat:${session.user.id}`, 0, -1)
|
2023-06-21 02:01:22 +03:00
|
|
|
if (!chats.length) {
|
2023-12-04 12:42:53 -05:00
|
|
|
return redirect('/')
|
2023-06-21 02:01:22 +03:00
|
|
|
}
|
2023-06-16 15:20:42 +04:00
|
|
|
const pipeline = kv.pipeline()
|
|
|
|
|
|
|
|
|
|
for (const chat of chats) {
|
|
|
|
|
pipeline.del(chat)
|
2023-06-16 11:49:14 -04:00
|
|
|
pipeline.zrem(`user:chat:${session.user.id}`, chat)
|
2023-06-16 15:20:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await pipeline.exec()
|
|
|
|
|
|
|
|
|
|
revalidatePath('/')
|
2023-06-16 21:52:01 +04:00
|
|
|
return redirect('/')
|
2023-06-16 15:20:42 +04:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 16:18:52 +04:00
|
|
|
export async function getSharedChat(id: string) {
|
|
|
|
|
const chat = await kv.hgetall<Chat>(`chat:${id}`)
|
|
|
|
|
|
|
|
|
|
if (!chat || !chat.sharePath) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return chat
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-04 12:42:53 -05:00
|
|
|
export async function shareChat(id: string) {
|
2023-06-16 11:49:14 -04:00
|
|
|
const session = await auth()
|
|
|
|
|
|
2023-12-04 12:42:53 -05:00
|
|
|
if (!session?.user?.id) {
|
2023-06-16 21:52:01 +04:00
|
|
|
return {
|
|
|
|
|
error: 'Unauthorized'
|
|
|
|
|
}
|
2023-06-16 15:20:42 +04:00
|
|
|
}
|
|
|
|
|
|
2023-12-04 12:42:53 -05:00
|
|
|
const chat = await kv.hgetall<Chat>(`chat:${id}`)
|
|
|
|
|
|
|
|
|
|
if (!chat || chat.userId !== session.user.id) {
|
|
|
|
|
return {
|
|
|
|
|
error: 'Something went wrong'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 15:20:42 +04:00
|
|
|
const payload = {
|
2023-06-16 16:18:52 +04:00
|
|
|
...chat,
|
|
|
|
|
sharePath: `/share/${chat.id}`
|
2023-06-16 15:20:42 +04:00
|
|
|
}
|
|
|
|
|
|
2023-06-16 16:18:52 +04:00
|
|
|
await kv.hmset(`chat:${chat.id}`, payload)
|
2023-06-16 15:20:42 +04:00
|
|
|
|
|
|
|
|
return payload
|
|
|
|
|
}
|
2024-03-14 20:00:52 +03:00
|
|
|
|
|
|
|
|
export async function saveChat(chat: Chat) {
|
|
|
|
|
const session = await auth()
|
|
|
|
|
|
|
|
|
|
if (session && session.user) {
|
|
|
|
|
const pipeline = kv.pipeline()
|
|
|
|
|
pipeline.hmset(`chat:${chat.id}`, chat)
|
|
|
|
|
pipeline.zadd(`user:chat:${chat.userId}`, {
|
|
|
|
|
score: Date.now(),
|
|
|
|
|
member: `chat:${chat.id}`
|
|
|
|
|
})
|
|
|
|
|
await pipeline.exec()
|
|
|
|
|
} else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function refreshHistory(path: string) {
|
|
|
|
|
redirect(path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getMissingKeys() {
|
|
|
|
|
const keysRequired = ['OPENAI_API_KEY']
|
|
|
|
|
return keysRequired
|
|
|
|
|
.map(key => (process.env[key] ? '' : key))
|
|
|
|
|
.filter(key => key !== '')
|
|
|
|
|
}
|