Skip to content

Commit 488b3b6

Browse files
committed
feat: implement delete message functionality using API client
1 parent cdba8cb commit 488b3b6

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

src/lib/components/chat/Alternatives.svelte

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import CarbonChevronRight from "~icons/carbon/chevron-right";
66
77
import { createEventDispatcher } from "svelte";
8-
import { base } from "$app/paths";
98
import { page } from "$app/state";
109
import { error } from "$lib/stores/errors";
1110
import { invalidate } from "$app/navigation";
1211
import { UrlDependency } from "$lib/types/UrlDependency";
12+
import { handleResponse, useAPIClient } from "$lib/APIClient";
1313
1414
interface Props {
1515
message: Message;
@@ -24,6 +24,8 @@
2424
const dispatch = createEventDispatcher<{
2525
showAlternateMsg: { id: Message["id"] };
2626
}>();
27+
28+
const client = useAPIClient();
2729
</script>
2830

2931
<div
@@ -54,15 +56,18 @@
5456
class="hidden group-hover/navbranch:block"
5557
onclick={() => {
5658
if (confirm("Are you sure you want to delete this branch?")) {
57-
fetch(`${base}/api/conversation/${page.params.id}/message/${message.id}`, {
58-
method: "DELETE",
59-
}).then(async (r) => {
60-
if (r.ok) {
59+
client
60+
.conversations({ id: page.params.id })
61+
.message({ messageId: message.id })
62+
.delete()
63+
.then(handleResponse)
64+
.then(async () => {
6165
await invalidate(UrlDependency.Conversation);
62-
} else {
63-
$error = (await r.json()).message;
64-
}
65-
});
66+
})
67+
.catch((err) => {
68+
console.error(err);
69+
$error = String(err);
70+
});
6671
}
6772
}}
6873
>

src/lib/server/api/routes/groups/conversations.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,49 @@ export const conversationGroup = new Elysia().use(authPlugin).group("/conversati
456456
model: t.Optional(t.String()),
457457
}),
458458
}
459+
)
460+
.delete(
461+
"/message/:messageId",
462+
async ({ locals, params, conversation }) => {
463+
if (!conversation.messages.map((m) => m.id).includes(params.messageId)) {
464+
throw new Error("Message not found");
465+
}
466+
467+
const filteredMessages = conversation.messages
468+
.filter(
469+
(message) =>
470+
// not the message AND the message is not in ancestors
471+
!(message.id === params.messageId) &&
472+
message.ancestors &&
473+
!message.ancestors.includes(params.messageId)
474+
)
475+
.map((message) => {
476+
// remove the message from children if it's there
477+
if (message.children && message.children.includes(params.messageId)) {
478+
message.children = message.children.filter(
479+
(child) => child !== params.messageId
480+
);
481+
}
482+
return message;
483+
});
484+
485+
const res = await collections.conversations.updateOne(
486+
{ _id: new ObjectId(conversation._id), ...authCondition(locals) },
487+
{ $set: { messages: filteredMessages } }
488+
);
489+
490+
if (res.modifiedCount === 0) {
491+
throw new Error("Deleting message failed");
492+
}
493+
494+
return { success: true };
495+
},
496+
{
497+
params: t.Object({
498+
id: t.String(),
499+
messageId: t.String(),
500+
}),
501+
}
459502
);
460503
}
461504
);

0 commit comments

Comments
 (0)