Spaces:
Runtime error
Runtime error
feat: implement delete message functionality using API client
Browse files
src/lib/components/chat/Alternatives.svelte
CHANGED
|
@@ -5,11 +5,11 @@
|
|
| 5 |
import CarbonChevronRight from "~icons/carbon/chevron-right";
|
| 6 |
|
| 7 |
import { createEventDispatcher } from "svelte";
|
| 8 |
-
import { base } from "$app/paths";
|
| 9 |
import { page } from "$app/state";
|
| 10 |
import { error } from "$lib/stores/errors";
|
| 11 |
import { invalidate } from "$app/navigation";
|
| 12 |
import { UrlDependency } from "$lib/types/UrlDependency";
|
|
|
|
| 13 |
|
| 14 |
interface Props {
|
| 15 |
message: Message;
|
|
@@ -24,6 +24,8 @@
|
|
| 24 |
const dispatch = createEventDispatcher<{
|
| 25 |
showAlternateMsg: { id: Message["id"] };
|
| 26 |
}>();
|
|
|
|
|
|
|
| 27 |
</script>
|
| 28 |
|
| 29 |
<div
|
|
@@ -54,15 +56,18 @@
|
|
| 54 |
class="hidden group-hover/navbranch:block"
|
| 55 |
onclick={() => {
|
| 56 |
if (confirm("Are you sure you want to delete this branch?")) {
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
await invalidate(UrlDependency.Conversation);
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
}
|
| 67 |
}}
|
| 68 |
>
|
|
|
|
| 5 |
import CarbonChevronRight from "~icons/carbon/chevron-right";
|
| 6 |
|
| 7 |
import { createEventDispatcher } from "svelte";
|
|
|
|
| 8 |
import { page } from "$app/state";
|
| 9 |
import { error } from "$lib/stores/errors";
|
| 10 |
import { invalidate } from "$app/navigation";
|
| 11 |
import { UrlDependency } from "$lib/types/UrlDependency";
|
| 12 |
+
import { handleResponse, useAPIClient } from "$lib/APIClient";
|
| 13 |
|
| 14 |
interface Props {
|
| 15 |
message: Message;
|
|
|
|
| 24 |
const dispatch = createEventDispatcher<{
|
| 25 |
showAlternateMsg: { id: Message["id"] };
|
| 26 |
}>();
|
| 27 |
+
|
| 28 |
+
const client = useAPIClient();
|
| 29 |
</script>
|
| 30 |
|
| 31 |
<div
|
|
|
|
| 56 |
class="hidden group-hover/navbranch:block"
|
| 57 |
onclick={() => {
|
| 58 |
if (confirm("Are you sure you want to delete this branch?")) {
|
| 59 |
+
client
|
| 60 |
+
.conversations({ id: page.params.id })
|
| 61 |
+
.message({ messageId: message.id })
|
| 62 |
+
.delete()
|
| 63 |
+
.then(handleResponse)
|
| 64 |
+
.then(async () => {
|
| 65 |
await invalidate(UrlDependency.Conversation);
|
| 66 |
+
})
|
| 67 |
+
.catch((err) => {
|
| 68 |
+
console.error(err);
|
| 69 |
+
$error = String(err);
|
| 70 |
+
});
|
| 71 |
}
|
| 72 |
}}
|
| 73 |
>
|
src/lib/server/api/routes/groups/conversations.ts
CHANGED
|
@@ -456,6 +456,49 @@ export const conversationGroup = new Elysia().use(authPlugin).group("/conversati
|
|
| 456 |
model: t.Optional(t.String()),
|
| 457 |
}),
|
| 458 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
);
|
| 460 |
}
|
| 461 |
);
|
|
|
|
| 456 |
model: t.Optional(t.String()),
|
| 457 |
}),
|
| 458 |
}
|
| 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 |
+
}
|
| 502 |
);
|
| 503 |
}
|
| 504 |
);
|