coyotte508 HF staff commited on
Commit
1a14c61
1 Parent(s): fc15a4c

✨ Delete convo (#36)

Browse files
src/lib/types/Conversation.ts CHANGED
@@ -3,7 +3,9 @@ import type { Message } from './Message';
3
 
4
  export interface Conversation {
5
  _id: ObjectId;
6
- sessionId: string;
 
 
7
 
8
  title: string;
9
  messages: Message[];
 
3
 
4
  export interface Conversation {
5
  _id: ObjectId;
6
+
7
+ // Can be undefined for shared convo then deleted
8
+ sessionId?: string;
9
 
10
  title: string;
11
  messages: Message[];
src/routes/+layout.svelte CHANGED
@@ -1,4 +1,6 @@
1
  <script lang="ts">
 
 
2
  import '../styles/main.css';
3
  import type { LayoutData } from './$types';
4
 
@@ -45,6 +47,31 @@
45
  alert('Error while sharing conversation: ' + err);
46
  }
47
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  </script>
49
 
50
  <div
@@ -65,11 +92,20 @@
65
  {#each data.conversations as conv}
66
  <a
67
  href="/conversation/{conv.id}"
68
- class="truncate py-3 px-3 rounded-lg flex-none text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 flex justify-between items-center"
69
  >
70
  {conv.title}
71
 
 
 
 
 
 
 
 
 
72
  <button
 
73
  class="bg-white rounded border-black px-2 py-1 border-[1px] border-solid"
74
  on:click|preventDefault={() => shareConversation(conv.id, conv.title)}
75
  >
 
1
  <script lang="ts">
2
+ import { goto, invalidateAll } from '$app/navigation';
3
+ import { page } from '$app/stores';
4
  import '../styles/main.css';
5
  import type { LayoutData } from './$types';
6
 
 
47
  alert('Error while sharing conversation: ' + err);
48
  }
49
  }
50
+
51
+ async function deleteConversation(id: string) {
52
+ try {
53
+ const res = await fetch(`/conversation/${id}`, {
54
+ method: 'DELETE',
55
+ headers: {
56
+ 'Content-Type': 'application/json'
57
+ }
58
+ });
59
+
60
+ if (!res.ok) {
61
+ alert('Error while deleting conversation: ' + (await res.text()));
62
+ return;
63
+ }
64
+
65
+ if ($page.params.id !== id) {
66
+ await invalidateAll();
67
+ } else {
68
+ await goto(`/`, { invalidateAll: true });
69
+ }
70
+ } catch (err) {
71
+ console.error(err);
72
+ alert('Error while deleting conversation: ' + err);
73
+ }
74
+ }
75
  </script>
76
 
77
  <div
 
92
  {#each data.conversations as conv}
93
  <a
94
  href="/conversation/{conv.id}"
95
+ class="truncate py-3 px-3 rounded-lg flex-none text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 flex items-center"
96
  >
97
  {conv.title}
98
 
99
+ <span class="grow" />
100
+ <button
101
+ type="button"
102
+ class="bg-white rounded border-black px-2 py-1 border-[1px] border-solid"
103
+ on:click|preventDefault={() => deleteConversation(conv.id)}
104
+ >
105
+ Delele
106
+ </button>
107
  <button
108
+ type="button"
109
  class="bg-white rounded border-black px-2 py-1 border-[1px] border-solid"
110
  on:click|preventDefault={() => shareConversation(conv.id, conv.title)}
111
  >
src/routes/conversation/[id]/+server.ts CHANGED
@@ -67,6 +67,28 @@ export async function POST({ request, fetch, locals, params }) {
67
  });
68
  }
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  async function parseGeneratedText(stream: ReadableStream): Promise<string> {
71
  const inputs: Uint8Array[] = [];
72
  for await (const input of streamToAsyncIterable(stream)) {
 
67
  });
68
  }
69
 
70
+ export async function DELETE({ locals, params }) {
71
+ const convId = new ObjectId(params.id);
72
+
73
+ const conv = await collections.conversations.findOne({
74
+ _id: convId,
75
+ sessionId: locals.sessionId
76
+ });
77
+
78
+ if (!conv) {
79
+ throw error(404, 'Conversation not found');
80
+ }
81
+
82
+ if (conv.shares?.length) {
83
+ // Keep the convo, as it's been shared we don't want to invalidate share links
84
+ await collections.conversations.updateOne({ _id: conv._id }, { $unset: { sessionId: 1 } });
85
+ } else {
86
+ await collections.conversations.deleteOne({ _id: conv._id });
87
+ }
88
+
89
+ return new Response();
90
+ }
91
+
92
  async function parseGeneratedText(stream: ReadableStream): Promise<string> {
93
  const inputs: Uint8Array[] = [];
94
  for await (const input of streamToAsyncIterable(stream)) {