From e384b6fcc3b6cd32e1c17941a54411ca1f320715 Mon Sep 17 00:00:00 2001 From: Talon Date: Fri, 30 Aug 2024 17:22:59 +0200 Subject: [PATCH] Message deleting --- backend/src/services/message-service.ts | 2 +- frontend/src/dialogs/message.ts | 9 ++++++--- frontend/src/views/main.ts | 18 ++++++++++++++++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/backend/src/services/message-service.ts b/backend/src/services/message-service.ts index f320436..f27cf38 100644 --- a/backend/src/services/message-service.ts +++ b/backend/src/services/message-service.ts @@ -40,7 +40,7 @@ export const deleteMessage = async (messageId: string) => { // Remove from FTS table if enabled if (FTS5Enabled) { const query2 = db.prepare(`DELETE FROM messages_fts WHERE rowid = $rowId`); - const result2 = query.run({ rowId: messageId }); + const result2 = query2.run({ rowId: messageId }); } events.emit('message-deleted', messageId); return result; diff --git a/frontend/src/dialogs/message.ts b/frontend/src/dialogs/message.ts index de7846d..c42b862 100644 --- a/frontend/src/dialogs/message.ts +++ b/frontend/src/dialogs/message.ts @@ -4,7 +4,8 @@ import { Button, Container, TextInput} from "../ui"; import { Dialog } from "../ui/dialog"; import { Text } from "../ui"; import { MultilineInput } from "../ui/multiline-input"; -export class MessageDialog extends Dialog { +import { state } from "../state"; +export class MessageDialog extends Dialog { private message: IMessage; private messageText: MultilineInput; private deleteButton: Button; @@ -19,10 +20,12 @@ export class MessageDialog extends Dialog { this.deleteButton = new Button("Delete"); this.deleteButton.setPosition(10, 90, 80, 10); - this.deleteButton.onClick(() => { - return; + this.deleteButton.onClick(async () => { + await API.deleteMessage(state.currentChannel!.id.toString(), this.message.id.toString()); + this.choose(null); }); this.add(this.messageText); + this.add(this.deleteButton); if (this.message.fileId !== null) { this.fileInfoContainer = new Container("File info"); this.fileInfoContainer.setPosition(10, 50, 30, 80); diff --git a/frontend/src/views/main.ts b/frontend/src/views/main.ts index 94b7056..ab5c716 100644 --- a/frontend/src/views/main.ts +++ b/frontend/src/views/main.ts @@ -226,7 +226,7 @@ export class MainView extends View { const itm = new ListItem(`${message.content}; ${this.convertIsoTimeStringToFriendly(message.createdAt)}`); itm.setUserData(message.id); itm.onClick(() => { - new MessageDialog(message).open(); + this.openMessageDialog(message); }) return itm; } @@ -581,7 +581,6 @@ export class MainView extends View { } private handleHotkey(e: KeyboardEvent) { - if (e.ctrlKey && e.shiftKey) { const action = this.hotkeyMap.get(e.key.toLowerCase()); if (action) { @@ -590,4 +589,19 @@ export class MainView extends View { } } } + + private async openMessageDialog(message: IMessage) { + const d = new MessageDialog(message); + const msg = await d.open(); + console.log(msg); + if (!msg || msg === null) { + state.currentChannel?.removeMessage(message.id); + const node = this.messageElementMap.get(message.id); + if (node) { + this.messageList.remove(node); + this.messageElementMap.delete(message.id); + } + state.save(); + } + } } \ No newline at end of file