feat: support guest session (#919)
This commit is contained in:
parent
24cb2ce19b
commit
9279135355
34 changed files with 741 additions and 288 deletions
|
|
@ -3,6 +3,7 @@ import 'server-only';
|
|||
import {
|
||||
and,
|
||||
asc,
|
||||
count,
|
||||
desc,
|
||||
eq,
|
||||
gt,
|
||||
|
|
@ -27,6 +28,7 @@ import {
|
|||
type Chat,
|
||||
} from './schema';
|
||||
import type { ArtifactKind } from '@/components/artifact';
|
||||
import { generateUUID } from '../utils';
|
||||
import { generateHashedPassword } from './utils';
|
||||
|
||||
// Optionally, if not using email/pass login, you can
|
||||
|
|
@ -57,6 +59,21 @@ export async function createUser(email: string, password: string) {
|
|||
}
|
||||
}
|
||||
|
||||
export async function createGuestUser() {
|
||||
const email = `guest-${Date.now()}`;
|
||||
const password = generateHashedPassword(generateUUID());
|
||||
|
||||
try {
|
||||
return await db.insert(user).values({ email, password }).returning({
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failed to create guest user in database');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveChat({
|
||||
id,
|
||||
userId,
|
||||
|
|
@ -422,3 +439,34 @@ export async function updateChatVisiblityById({
|
|||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getMessageCountByUserId({
|
||||
id,
|
||||
differenceInHours,
|
||||
}: { id: string; differenceInHours: number }) {
|
||||
try {
|
||||
const twentyFourHoursAgo = new Date(
|
||||
Date.now() - differenceInHours * 60 * 60 * 1000,
|
||||
);
|
||||
|
||||
const [stats] = await db
|
||||
.select({ count: count(message.id) })
|
||||
.from(message)
|
||||
.innerJoin(chat, eq(message.chatId, chat.id))
|
||||
.where(
|
||||
and(
|
||||
eq(chat.userId, id),
|
||||
gte(message.createdAt, twentyFourHoursAgo),
|
||||
eq(message.role, 'user'),
|
||||
),
|
||||
)
|
||||
.execute();
|
||||
|
||||
return stats?.count ?? 0;
|
||||
} catch (error) {
|
||||
console.error(
|
||||
'Failed to get message count by user id for the last 24 hours from database',
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue