perf: 输入超长提示

This commit is contained in:
archer
2023-03-28 00:36:26 +08:00
parent 7a6d0ea650
commit c3ccbcb7f6
5 changed files with 45 additions and 12 deletions

View File

@@ -9,13 +9,31 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if (!chatId || !index) {
throw new Error('缺少参数');
}
console.log(index);
await connectToDatabase();
const chatRecord = await Chat.findById(chatId);
if (!chatRecord) {
throw new Error('找不到对话');
}
// 重新计算 index跳过已经被删除的内容
let unDeleteIndex = +index;
let deletedIndex = 0;
for (deletedIndex = 0; deletedIndex < chatRecord.content.length; deletedIndex++) {
if (!chatRecord.content[deletedIndex].deleted) {
unDeleteIndex--;
if (unDeleteIndex < 0) {
break;
}
}
}
// 删除最一条数据库记录, 也就是预发送的那一条
await Chat.findByIdAndUpdate(chatId, {
$set: {
[`content.${index}.deleted`]: true,
[`content.${deletedIndex}.deleted`]: true,
updateTime: Date.now()
}
});