File tree Expand file tree Collapse file tree 2 files changed +57
-9
lines changed Expand file tree Collapse file tree 2 files changed +57
-9
lines changed Original file line number Diff line number Diff line change 5
5
import CarbonChevronRight from " ~icons/carbon/chevron-right" ;
6
6
7
7
import { createEventDispatcher } from " svelte" ;
8
- import { base } from " $app/paths" ;
9
8
import { page } from " $app/state" ;
10
9
import { error } from " $lib/stores/errors" ;
11
10
import { invalidate } from " $app/navigation" ;
12
11
import { UrlDependency } from " $lib/types/UrlDependency" ;
12
+ import { handleResponse , useAPIClient } from " $lib/APIClient" ;
13
13
14
14
interface Props {
15
15
message: Message ;
24
24
const dispatch = createEventDispatcher <{
25
25
showAlternateMsg: { id: Message [" id" ] };
26
26
}>();
27
+
28
+ const client = useAPIClient ();
27
29
</script >
28
30
29
31
<div
54
56
class =" hidden group-hover/navbranch:block"
55
57
onclick ={() => {
56
58
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 () => {
61
65
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
+ });
66
71
}
67
72
}}
68
73
>
Original file line number Diff line number Diff line change @@ -456,6 +456,49 @@ export const conversationGroup = new Elysia().use(authPlugin).group("/conversati
456
456
model : t . Optional ( t . String ( ) ) ,
457
457
} ) ,
458
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
+ }
459
502
) ;
460
503
}
461
504
) ;
You can’t perform that action at this time.
0 commit comments