bug fixes, and performance improvements, and drawer
This commit is contained in:
@@ -63,62 +63,106 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
}
|
||||
|
||||
const rootMessage = messagesMap.get(messageId);
|
||||
if (!rootMessage) {
|
||||
const targetMessage = messagesMap.get(messageId);
|
||||
if (!targetMessage) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Invalid message',
|
||||
});
|
||||
}
|
||||
|
||||
if (rootMessage.role === 'user') {
|
||||
await deeplyDeleteMessage(rootMessage.id, topicId, userId);
|
||||
if (targetMessage.role === 'user') {
|
||||
await deeplyDeleteMessage(targetMessage.id, topicId, userId);
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
if (rootMessage.deleted === true) {
|
||||
const childMessage = rootMessage.activeChildId
|
||||
? rootMessage.children.find(c => c.id === rootMessage.activeChildId)
|
||||
: rootMessage.children[0];
|
||||
if (!childMessage) {
|
||||
console.error('Message not found');
|
||||
return { ok: false };
|
||||
if (targetMessage.parentMessageId && messagesMap.has(targetMessage.parentMessageId)) {
|
||||
const parent = messagesMap.get(targetMessage.parentMessageId)!;
|
||||
await deeplyDeleteMessage(targetMessage.id, topicId, userId);
|
||||
|
||||
const remainingChildren = parent.children.filter((child) => {
|
||||
return child.id !== targetMessage.id;
|
||||
});
|
||||
|
||||
if (parent.deleted === true && remainingChildren.length === 0) {
|
||||
await deeplyDeleteMessage(parent.id, topicId, userId);
|
||||
} else if (parent.activeChildId === targetMessage.id) {
|
||||
const nextActiveChildId = remainingChildren.length > 0
|
||||
? (parent.deleted ? remainingChildren[0]!.id : remainingChildren[remainingChildren.length - 1]!.id)
|
||||
: null;
|
||||
await db.update(messages).set({ activeChildId: nextActiveChildId }).where(sql`${messages.id} = ${parent.id} AND ${messages.userId} = ${userId}`);
|
||||
topicEvents.emit(topicId, {
|
||||
type: 'MESSAGE_UPDATED',
|
||||
payload: { ...parent, activeChildId: nextActiveChildId },
|
||||
});
|
||||
}
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
await deeplyDeleteMessage(childMessage.id, topicId, userId);
|
||||
if (targetMessage.deleted === true) {
|
||||
const childMessage = targetMessage.activeChildId
|
||||
? targetMessage.children.find((c) => { return c.id === targetMessage.activeChildId; })
|
||||
: targetMessage.children[0];
|
||||
|
||||
const remainingChildren = rootMessage.children.filter(child => child!.id !== childMessage.id);
|
||||
if (remainingChildren.length === 0) {
|
||||
await deeplyDeleteMessage(rootMessage.id, topicId, userId);
|
||||
if (childMessage) {
|
||||
await deeplyDeleteMessage(childMessage.id, topicId, userId);
|
||||
const remainingChildren = targetMessage.children.filter((child) => {
|
||||
return child.id !== childMessage.id;
|
||||
});
|
||||
if (remainingChildren.length === 0) {
|
||||
await deeplyDeleteMessage(targetMessage.id, topicId, userId);
|
||||
} else if (targetMessage.activeChildId === childMessage.id) {
|
||||
const nextActiveChildId = remainingChildren[0]!.id;
|
||||
await db.update(messages).set({ activeChildId: nextActiveChildId }).where(sql`${messages.id} = ${targetMessage.id} AND ${messages.userId} = ${userId}`);
|
||||
topicEvents.emit(topicId, {
|
||||
type: 'MESSAGE_UPDATED',
|
||||
payload: { ...targetMessage, activeChildId: nextActiveChildId },
|
||||
});
|
||||
}
|
||||
} else {
|
||||
await deeplyDeleteMessage(targetMessage.id, topicId, userId);
|
||||
}
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
if (
|
||||
rootMessage.activeChildId
|
||||
&& rootMessage.children.length > 0
|
||||
targetMessage.activeChildId
|
||||
&& targetMessage.children.length > 0
|
||||
) {
|
||||
const childMessage = rootMessage.children.find(c => c.id === rootMessage.activeChildId);
|
||||
if (!childMessage) {
|
||||
console.error('Message not found');
|
||||
return { ok: false };
|
||||
const childMessage = targetMessage.children.find((c) => {
|
||||
return c.id === targetMessage.activeChildId;
|
||||
});
|
||||
if (childMessage) {
|
||||
await deeplyDeleteMessage(childMessage.id, topicId, userId);
|
||||
const remainingChildren = targetMessage.children.filter((child) => {
|
||||
return child.id !== childMessage.id;
|
||||
});
|
||||
const nextActiveChildId = remainingChildren.length > 0
|
||||
? remainingChildren[remainingChildren.length - 1]!.id
|
||||
: null;
|
||||
await db.update(messages).set({ activeChildId: nextActiveChildId }).where(sql`${messages.id} = ${targetMessage.id} AND ${messages.userId} = ${userId}`);
|
||||
topicEvents.emit(topicId, {
|
||||
type: 'MESSAGE_UPDATED',
|
||||
payload: { ...targetMessage, activeChildId: nextActiveChildId },
|
||||
});
|
||||
return { ok: true };
|
||||
}
|
||||
}
|
||||
|
||||
await deeplyDeleteMessage(childMessage.id, topicId, userId);
|
||||
|
||||
if (targetMessage.children.length === 0) {
|
||||
await deeplyDeleteMessage(targetMessage.id, topicId, userId);
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
if (rootMessage.children.length === 0) {
|
||||
await deeplyDeleteMessage(rootMessage.id, topicId, userId);
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
await db.update(messages).set({ deleted: true }).where(sql`${messages.id} = ${rootMessage.id} AND ${messages.userId} = ${userId}`);
|
||||
const nextActiveChildId = targetMessage.children[0]?.id ?? null;
|
||||
await db.update(messages).set({
|
||||
deleted: true,
|
||||
activeChildId: nextActiveChildId,
|
||||
}).where(sql`${messages.id} = ${targetMessage.id} AND ${messages.userId} = ${userId}`);
|
||||
|
||||
topicEvents.emit(topicId, {
|
||||
type: 'MESSAGE_UPDATED',
|
||||
payload: { ...rootMessage, deleted: true },
|
||||
payload: { ...targetMessage, deleted: true, activeChildId: nextActiveChildId },
|
||||
});
|
||||
|
||||
return { ok: true };
|
||||
|
||||
Reference in New Issue
Block a user