perf: quote modal

This commit is contained in:
archer
2023-07-23 22:24:14 +08:00
parent 1ffe1be562
commit ea35ad2144
10 changed files with 188 additions and 209 deletions

View File

@@ -1,52 +0,0 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { connectToDatabase, Chat } from '@/service/mongo';
import { authUser } from '@/service/utils/auth';
import { Types } from 'mongoose';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const { chatId, contentId } = req.query as {
chatId: string;
contentId: string;
};
await connectToDatabase();
const { userId } = await authUser({ req, authToken: true });
if (!chatId || !contentId) {
throw new Error('params is error');
}
const history = await Chat.aggregate([
{
$match: {
_id: new Types.ObjectId(chatId),
userId: new Types.ObjectId(userId)
}
},
{
$unwind: '$content'
},
{
$match: {
'content._id': new Types.ObjectId(contentId)
}
},
{
$project: {
// [rawSearchKey]: `$content.${rawSearchKey}`
}
}
]);
jsonRes(res, {
// data: history[0]?.[rawSearchKey] || []
});
} catch (err) {
jsonRes(res, {
code: 500,
error: err
});
}
}

View File

@@ -1,6 +1,6 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { connectToDatabase, Chat, App } from '@/service/mongo';
import { connectToDatabase, Chat } from '@/service/mongo';
import type { InitChatResponse } from '@/api/response/chat';
import { authUser } from '@/service/utils/auth';
import { ChatItemType } from '@/types/chat';
@@ -59,7 +59,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
{
$project: {
content: {
$slice: ['$content', -50] // 返回 content 数组的最后50个元素
$slice: ['$content', -30] // 返回 content 数组的最后 30 个元素
}
}
},

View File

@@ -36,7 +36,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
export async function getChatHistory({
chatId,
userId,
limit = 50
limit = 20
}: Props & { userId: string }): Promise<Response> {
if (!chatId) {
return { history: [] };
@@ -47,7 +47,7 @@ export async function getChatHistory({
{
$project: {
content: {
$slice: ['$content', -limit] // 返回 content 数组的最后50个元素
$slice: ['$content', -limit] // 返回 content 数组的最后20个元素
}
}
},