Add message actions (#482)

This commit is contained in:
Jeremy 2024-11-05 17:15:51 +03:00 committed by GitHub
parent 94f563f179
commit 171914941e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 1011 additions and 150 deletions

View file

@ -1,11 +1,20 @@
"server-only";
'server-only';
import { genSaltSync, hashSync } from "bcrypt-ts";
import { and, asc, desc, eq, gt } from "drizzle-orm";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { genSaltSync, hashSync } from 'bcrypt-ts';
import { and, asc, desc, eq, gt } from 'drizzle-orm';
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import { user, chat, User, document, Suggestion } from "./schema";
import {
user,
chat,
User,
document,
Suggestion,
Message,
message,
vote,
} from './schema';
// Optionally, if not using email/pass login, you can
// use the Drizzle adapter for Auth.js / NextAuth
@ -17,7 +26,7 @@ export async function getUser(email: string): Promise<Array<User>> {
try {
return await db.select().from(user).where(eq(user.email, email));
} catch (error) {
console.error("Failed to get user from database");
console.error('Failed to get user from database');
throw error;
}
}
@ -29,40 +38,29 @@ export async function createUser(email: string, password: string) {
try {
return await db.insert(user).values({ email, password: hash });
} catch (error) {
console.error("Failed to create user in database");
console.error('Failed to create user in database');
throw error;
}
}
export async function saveChat({
id,
messages,
userId,
title,
}: {
id: string;
messages: any;
userId: string;
title: string;
}) {
try {
const selectedChats = await db.select().from(chat).where(eq(chat.id, id));
if (selectedChats.length > 0) {
return await db
.update(chat)
.set({
messages: JSON.stringify(messages),
})
.where(eq(chat.id, id));
}
return await db.insert(chat).values({
id,
createdAt: new Date(),
messages: JSON.stringify(messages),
userId,
title,
});
} catch (error) {
console.error("Failed to save chat in database");
console.error('Failed to save chat in database');
throw error;
}
}
@ -71,7 +69,7 @@ export async function deleteChatById({ id }: { id: string }) {
try {
return await db.delete(chat).where(eq(chat.id, id));
} catch (error) {
console.error("Failed to delete chat by id from database");
console.error('Failed to delete chat by id from database');
throw error;
}
}
@ -84,7 +82,7 @@ export async function getChatsByUserId({ id }: { id: string }) {
.where(eq(chat.userId, id))
.orderBy(desc(chat.createdAt));
} catch (error) {
console.error("Failed to get chats by user from database");
console.error('Failed to get chats by user from database');
throw error;
}
}
@ -94,7 +92,71 @@ export async function getChatById({ id }: { id: string }) {
const [selectedChat] = await db.select().from(chat).where(eq(chat.id, id));
return selectedChat;
} catch (error) {
console.error("Failed to get chat by id from database");
console.error('Failed to get chat by id from database');
throw error;
}
}
export async function saveMessages({ messages }: { messages: Array<Message> }) {
try {
return await db.insert(message).values(messages);
} catch (error) {
console.error('Failed to save messages in database', error);
throw error;
}
}
export async function getMessagesByChatId({ id }: { id: string }) {
try {
return await db
.select()
.from(message)
.where(eq(message.chatId, id))
.orderBy(asc(message.createdAt));
} catch (error) {
console.error('Failed to get messages by chat id from database', error);
throw error;
}
}
export async function voteMessage({
chatId,
messageId,
type,
}: {
chatId: string;
messageId: string;
type: 'up' | 'down';
}) {
try {
const [existingVote] = await db
.select()
.from(vote)
.where(and(eq(vote.messageId, messageId)));
if (existingVote) {
return await db
.update(vote)
.set({ isUpvoted: type === 'up' ? true : false })
.where(and(eq(vote.messageId, messageId), eq(vote.chatId, chatId)));
} else {
return await db.insert(vote).values({
chatId,
messageId,
isUpvoted: type === 'up' ? true : false,
});
}
} catch (error) {
console.error('Failed to upvote message in database', error);
throw error;
}
}
export async function getVotesByChatId({ id }: { id: string }) {
try {
return await db.select().from(vote).where(eq(vote.chatId, id));
} catch (error) {
console.error('Failed to get votes by chat id from database', error);
throw error;
}
}
@ -119,7 +181,7 @@ export async function saveDocument({
createdAt: new Date(),
});
} catch (error) {
console.error("Failed to save document in database");
console.error('Failed to save document in database');
throw error;
}
}
@ -134,7 +196,7 @@ export async function getDocumentsById({ id }: { id: string }) {
return documents;
} catch (error) {
console.error("Failed to get document by id from database");
console.error('Failed to get document by id from database');
throw error;
}
}
@ -149,7 +211,7 @@ export async function getDocumentById({ id }: { id: string }) {
return selectedDocument;
} catch (error) {
console.error("Failed to get document by id from database");
console.error('Failed to get document by id from database');
throw error;
}
}
@ -167,7 +229,7 @@ export async function deleteDocumentsByIdAfterTimestamp({
.where(and(eq(document.id, id), gt(document.createdAt, timestamp)));
} catch (error) {
console.error(
"Failed to delete documents by id after timestamp from database",
'Failed to delete documents by id after timestamp from database'
);
throw error;
}
@ -181,7 +243,7 @@ export async function saveSuggestions({
try {
return await db.insert(Suggestion).values(suggestions);
} catch (error) {
console.error("Failed to save suggestions in database");
console.error('Failed to save suggestions in database');
throw error;
}
}
@ -198,7 +260,7 @@ export async function getSuggestionsByDocumentId({
.where(and(eq(Suggestion.documentId, documentId)));
} catch (error) {
console.error(
"Failed to get suggestions by document version from database",
'Failed to get suggestions by document version from database'
);
throw error;
}