File size: 2,662 Bytes
c3e8f3d
 
 
 
 
bfbf1a7
84c9f51
 
 
c3e8f3d
a86b547
bfbf1a7
c3e8f3d
f80b091
 
 
 
 
c3e8f3d
f80b091
 
 
c3e8f3d
bfbf1a7
 
d553ae5
 
 
bfbf1a7
d553ae5
bfbf1a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3e8f3d
bfbf1a7
f80b091
 
 
c3e8f3d
 
d0a1b70
bfbf1a7
d0a1b70
c3e8f3d
38448fc
a86b547
 
 
f80b091
c3e8f3d
 
84c9f51
bfbf1a7
 
84c9f51
 
 
 
 
 
 
 
 
 
 
 
973f0d8
84c9f51
 
 
 
 
 
 
 
 
 
 
bfbf1a7
84c9f51
d553ae5
84c9f51
 
d553ae5
 
c3e8f3d
d553ae5
f80b091
 
 
 
c3e8f3d
d553ae5
 
 
 
c3e8f3d
d553ae5
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
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, authEmail } 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 authEmail();

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

  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 authEmail();
  const chat = await kv.hgetall<ChatEntity>(`chat:${id}`);

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

  return chat;
}

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

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

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

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

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