File size: 2,961 Bytes
5ec491a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// 'use server';

// import { revalidatePath } from 'next/cache';
// import { kv } from '@vercel/kv';

// import { auth, sessionUser } from '@/auth';
// import { ChatEntity, MessageBase } from '@/lib/types';
// import { notFound, redirect } from 'next/navigation';
// import { nanoid } from '../utils';

// export async function getKVChats() {
//   const { email } = await sessionUser();

//   try {
//     const pipeline = kv.pipeline();
//     const chats: string[] = await kv.zrange(`user:chat:${email}`, 0, -1, {
//       rev: true,
//     });

//     for (const chat of chats) {
//       pipeline.hgetall(chat);
//     }

//     const results = (await pipeline.exec()) as ChatEntity[];

//     return results
//       .filter(r => !!r)
//       .sort((r1, r2) => r2.updatedAt - r1.updatedAt);
//   } catch (error) {
//     console.error('getKVChats error:', error);
//     return [];
//   }
// }

// export async function adminGetAllKVChats() {
//   const { isAdmin } = await sessionUser();

//   if (!isAdmin) {
//     notFound();
//   }

//   try {
//     const pipeline = kv.pipeline();
//     const chats: string[] = await kv.zrange(`user:chat:all`, 0, -1, {
//       rev: true,
//     });

//     for (const chat of chats) {
//       pipeline.hgetall(chat);
//     }

//     const results = (await pipeline.exec()) as ChatEntity[];

//     return results.sort((r1, r2) => r2.updatedAt - r1.updatedAt);
//   } catch (error) {
//     return [];
//   }
// }

// export async function getKVChat(id: string) {
//   // const { email, isAdmin } = await sessionUser();
//   const chat = await kv.hgetall<ChatEntity>(`chat:${id}`);

//   if (!chat) {
//     redirect('/');
//   }

//   return chat;
// }

// export async function createKVChat(chat: ChatEntity) {
//   // const { email, isAdmin } = await sessionUser();
//   const { email } = await sessionUser();

//   await kv.hmset(`chat:${chat.id}`, chat);
//   if (email) {
//     await kv.zadd(`user:chat:${email}`, {
//       score: Date.now(),
//       member: `chat:${chat.id}`,
//     });
//   }
//   await kv.zadd('user:chat:all', {
//     score: Date.now(),
//     member: `chat:${chat.id}`,
//   });
//   revalidatePath('/chat', 'layout');
// }

// export async function saveKVChatMessage(id: string, message: MessageBase) {
//   const chat = await kv.hgetall<ChatEntity>(`chat:${id}`);
//   if (!chat) {
//     notFound();
//   }
//   const { messages } = chat;
//   await kv.hmset(`chat:${id}`, {
//     ...chat,
//     messages: [...messages, message],
//     updatedAt: Date.now(),
//   });
//   return revalidatePath('/chat', 'layout');
// }

// export async function removeKVChat(id: string) {
//   const { email } = await sessionUser();

//   if (!email) {
//     return {
//       error: 'Unauthorized',
//     };
//   }

//   await Promise.all([
//     kv.zrem(`user:chat:${email}`, `chat:${id}`),
//     kv.del(`chat:${id}`),
//   ]);

//   return revalidatePath('/chat', 'layout');
// }