File size: 2,484 Bytes
c3e8f3d
 
 
 
 
 
84c9f51
 
 
c3e8f3d
a86b547
f80b091
 
84c9f51
 
 
 
a86b547
 
 
 
c3e8f3d
f80b091
 
 
 
 
c3e8f3d
f80b091
 
 
c3e8f3d
f80b091
c3e8f3d
d0a1b70
f80b091
 
 
c3e8f3d
 
d0a1b70
38448fc
d0a1b70
c3e8f3d
38448fc
a86b547
 
 
f80b091
c3e8f3d
 
84c9f51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3e8f3d
f80b091
c3e8f3d
f80b091
 
 
 
 
c3e8f3d
f80b091
 
c3e8f3d
f80b091
 
 
 
 
c3e8f3d
f80b091
 
c3e8f3d
84c9f51
f80b091
c3e8f3d
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
'use server';

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

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

async function authCheck() {
  const session = await auth();
  const email = session?.user?.email;
  // if (!email) {
  //   redirect('/');
  // }
  return { email, isAdmin: !!email?.endsWith('landing.ai') };
}

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

  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();

    return results as ChatEntity[];
  } catch (error) {
    return [];
  }
}

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

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

  return chat;
}

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

  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', '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],
  });
  revalidatePath('/chat/layout', 'layout');
}

export async function removeKVChat({ id, path }: { id: string; path: string }) {
  const session = await auth();

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

  //Convert uid to string for consistent comparison with session.user.id
  const uid = String(await kv.hget(`chat:${id}`, 'userId'));

  if (uid !== session?.user?.id) {
    return {
      error: 'Unauthorized',
    };
  }

  await kv.del(`chat:${id}`);
  await kv.zrem(`user:chat:${session.user.id}`, `chat:${id}`);

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