From 43c4df87deec741ba54636f18862e14894f4449b Mon Sep 17 00:00:00 2001 From: Sumit Date: Sun, 17 Dec 2023 21:29:51 +0530 Subject: [PATCH 1/3] Delete command done --- whiteboard/blocks/UtilityBlock.ts | 4 +- whiteboard/lib/commandUtility.ts | 157 +++++++++++++++++++-- whiteboard/lib/messages.ts | 11 +- whiteboard/persistence/boardInteraction.ts | 26 +++- 4 files changed, 179 insertions(+), 19 deletions(-) diff --git a/whiteboard/blocks/UtilityBlock.ts b/whiteboard/blocks/UtilityBlock.ts index 0b0578b94c..e82316d03f 100644 --- a/whiteboard/blocks/UtilityBlock.ts +++ b/whiteboard/blocks/UtilityBlock.ts @@ -50,9 +50,7 @@ export async function buildHeaderBlock( `*Untitled Whiteboard* by \`@${username}\`` ); } else { - markdownBlock = getMarkdownBlock( - `*${boardname}* by \`@${username}\`` - ); + markdownBlock = getMarkdownBlock(`*${boardname}* by \`@${username}\``); } const actionBlock = getActionsBlock(UtilityEnum.PREVIEW_BLOCK_ID, [ diff --git a/whiteboard/lib/commandUtility.ts b/whiteboard/lib/commandUtility.ts index 6c0860059a..41ab36c6ba 100644 --- a/whiteboard/lib/commandUtility.ts +++ b/whiteboard/lib/commandUtility.ts @@ -15,12 +15,20 @@ import { sendMessage, sendMessageWithAttachment, } from "./messages"; -import { buildHeaderBlock } from "../blocks/UtilityBlock"; +import { buildHeaderBlock, deletionHeaderBlock } from "../blocks/UtilityBlock"; import { WhiteboardSlashCommandContext } from "../commands/WhiteboardCommand"; -import { getBoardName, storeBoardRecord } from "../persistence/boardInteraction"; +import { + getBoardName, + storeBoardRecord, +} from "../persistence/boardInteraction"; import { randomId } from "./utilts"; import { defaultPreview } from "../assets/defaultPreview"; +import { + checkBoardNameByRoomId, + getMessageIdByBoardName, + deleteBoardByMessageId, +} from "../persistence/boardInteraction"; //CommandUtility is used to handle the commands export class CommandUtility implements ExecutorProps { @@ -54,7 +62,7 @@ export class CommandUtility implements ExecutorProps { read, modify, http, - persistence + persistence, }: WhiteboardSlashCommandContext) { const appUser = (await read.getUserReader().getAppUser())!; const sender = context.getSender()!; @@ -66,9 +74,12 @@ export class CommandUtility implements ExecutorProps { const randomBoardId = randomId(); const boardURL = `${boardEndpoint.computedPath}?id=${randomBoardId}`; - const name = await getBoardName(read.getPersistenceReader(), room.id) + const name = await getBoardName( + read.getPersistenceReader(), + room.id + ); - if(name){ + if (name) { const headerBlock = await buildHeaderBlock( sender.username, boardURL, @@ -90,7 +101,7 @@ export class CommandUtility implements ExecutorProps { attachments, headerBlock ); - + storeBoardRecord( persistence, room.id, @@ -106,9 +117,7 @@ export class CommandUtility implements ExecutorProps { "", "Public" ); - } - } } @@ -130,6 +139,135 @@ export class CommandUtility implements ExecutorProps { await handleListCommand(this.read, this.modify, this.room, appSender); } + // handleListCommand is used to handle the /whiteboard delete command + + private async deleteBoardCommand() { + const appId = this.app.getID(); + const user = this.context.getSender(); + const params = this.context.getArguments(); + const room: IRoom = this.context.getRoom(); + const appSender: IUser = (await this.read + .getUserReader() + .getAppUser()) as IUser; + + // the name specified in command "/whiteboard delete" + let deleteBoardName = + params.length > 1 ? params.slice(1).join(" ") : ""; + console.log(`deleteBoardName :${deleteBoardName}`); + + if (deleteBoardName == "") { + const message = this.modify + .getCreator() + .startMessage() + .setSender(appSender) + .setRoom(room) + .setText( + "Please specify the name of the whiteboard you wish to delete" + ) + .setParseUrls(true); + + await this.read + .getNotifier() + .notifyRoom(room, message.getMessage()); + } else if ( + deleteBoardName == "untitled" || + deleteBoardName == "Untitled" + ) { + const message = this.modify + .getCreator() + .startMessage() + .setSender(appSender) + .setRoom(room) + .setText("Unititled Whiteboard can not be deleted") + .setParseUrls(true); + + await this.read + .getNotifier() + .notifyRoom(room, message.getMessage()); + } else { + console.log("Finding Board :-------------"); + const checkBoard = await checkBoardNameByRoomId( + this.read.getPersistenceReader(), + room.id, + deleteBoardName + ); + if (checkBoard == 1) { + console.log("Board name exist in the room!"); + const messageId = await getMessageIdByBoardName( + this.read.getPersistenceReader(), + room.id, + deleteBoardName + ); + + if (messageId) { + const AppSender: IUser = (await this.read + .getUserReader() + .getAppUser()) as IUser; + + // Board data is deleted from database + await deleteBoardByMessageId( + this.persistence, + this.read.getPersistenceReader(), + messageId + ); + console.log("Board is deleted from database!!!!"); + + // Message is Updated to "Deletion" + const room = await this.read + .getMessageReader() + .getRoom(messageId); + if (room) { + // Extracted the message to be updated + const message = await this.modify + .getUpdater() + .message(messageId, AppSender); + + // Deletion header block as board get deleted + const deleteHeaderBlock = await deletionHeaderBlock( + user.username, + deleteBoardName + ); + + // Some message configurations + message.setEditor(user).setRoom(room); + message.setBlocks(deleteHeaderBlock); + message.removeAttachment(0); + + // Message is finished modified and saved to database + await this.modify.getUpdater().finish(message); + console.log( + "Message is updated after deleting the board" + ); + } + await Promise.all([ + sendMessage( + this.modify, + this.room, + appSender, + `The "${deleteBoardName}" board has been deleted successfully` + ), + ]); + } else { + console.log("MessageId not found"); + } + } else { + const message = this.modify + .getCreator() + .startMessage() + .setSender(appSender) + .setRoom(room) + .setText( + `Oops! The board named "${deleteBoardName}" is not found in this room. Please check the board name and try again` + ) + .setParseUrls(true); + + await this.read + .getNotifier() + .notifyRoom(room, message.getMessage()); + } + } + } + public async resolveCommand(context: WhiteboardSlashCommandContext) { switch (this.command[0]) { case "new": @@ -141,6 +279,9 @@ export class CommandUtility implements ExecutorProps { case "list": await this.handleListCommand(); break; + case "delete": + await this.deleteBoardCommand(); + break; default: const appSender: IUser = (await this.read .getUserReader() diff --git a/whiteboard/lib/messages.ts b/whiteboard/lib/messages.ts index a21b65c590..6104239c24 100644 --- a/whiteboard/lib/messages.ts +++ b/whiteboard/lib/messages.ts @@ -193,15 +193,17 @@ export async function handleListCommand( read: IRead, modify: IModify, room: IRoom, - appUser: IUser, + appUser: IUser ) { - const boardDataArray: string[] = []; - const boardData = await getBoardRecordByRoomId(read.getPersistenceReader(), room.id) + const boardData = await getBoardRecordByRoomId( + read.getPersistenceReader(), + room.id + ); if (boardData !== undefined && boardData.length > 0) { for (let i = 0; i < boardData.length; i++) { - boardDataArray.push(boardData[i].title) + boardDataArray.push(boardData[i].title); } const text = `*All existing boards are*: @@ -217,7 +219,6 @@ export async function handleListCommand( .setParseUrls(true); return await read.getNotifier().notifyRoom(room, msg.getMessage()); - } const text = `No boards found`; diff --git a/whiteboard/persistence/boardInteraction.ts b/whiteboard/persistence/boardInteraction.ts index f9472f14bc..ed9bab1da1 100644 --- a/whiteboard/persistence/boardInteraction.ts +++ b/whiteboard/persistence/boardInteraction.ts @@ -127,13 +127,16 @@ export const getAllBoardIds = async ( }; // function to get new board name -export const getBoardName = async (persis: IPersistenceRead, roomId: string): Promise => { +export const getBoardName = async ( + persis: IPersistenceRead, + roomId: string +): Promise => { const boardArray = await getBoardRecordByRoomId(persis, roomId); let suffix = 1; let newName = `Untitled Whiteboard`; - while (boardArray.some(board => board.title === newName)) { + while (boardArray.some((board) => board.title === newName)) { suffix++; newName = `Untitled Whiteboard ${suffix}`; } @@ -305,7 +308,7 @@ export const deleteBoardByMessageId = async ( messageId: string ): Promise => { let records = await getBoardRecordByMessageId(persistenceRead, messageId); - console.log("records", records) + console.log("records", records); if (!records) { console.log("No records found for boardname"); return ""; @@ -328,3 +331,20 @@ export const deleteBoardByMessageId = async ( return records.title; }; + +export const getMessageIdByBoardName = async ( + persistenceRead: IPersistenceRead, + roomId: string, + boardName: string +): Promise => { + const boardData = await getBoardRecordByRoomId(persistenceRead, roomId); + + for (const board of boardData) { + if (board.title === boardName) { + console.log("Board name mil gaya!"); + let messageId = board.messageId; + return messageId; + } + } + return 0; +}; From 3f978ede68b85e63592369dbf4485302ec6edd7d Mon Sep 17 00:00:00 2001 From: Sumit Date: Sun, 24 Dec 2023 01:51:35 +0530 Subject: [PATCH 2/3] Feat: delete command is implemented --- whiteboard/lib/commandUtility.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/whiteboard/lib/commandUtility.ts b/whiteboard/lib/commandUtility.ts index 41ab36c6ba..7528546729 100644 --- a/whiteboard/lib/commandUtility.ts +++ b/whiteboard/lib/commandUtility.ts @@ -153,7 +153,6 @@ export class CommandUtility implements ExecutorProps { // the name specified in command "/whiteboard delete" let deleteBoardName = params.length > 1 ? params.slice(1).join(" ") : ""; - console.log(`deleteBoardName :${deleteBoardName}`); if (deleteBoardName == "") { const message = this.modify @@ -244,7 +243,7 @@ export class CommandUtility implements ExecutorProps { this.modify, this.room, appSender, - `The "${deleteBoardName}" board has been deleted successfully` + `The *${deleteBoardName}* board has been deleted successfully` ), ]); } else { @@ -257,7 +256,7 @@ export class CommandUtility implements ExecutorProps { .setSender(appSender) .setRoom(room) .setText( - `Oops! The board named "${deleteBoardName}" is not found in this room. Please check the board name and try again` + `Oops! The board named *${deleteBoardName}* is not found in this room. Please check the board name and try again` ) .setParseUrls(true); From 6e268c0490086b27164b67245e98d665bd2ae97d Mon Sep 17 00:00:00 2001 From: Sumit Date: Sun, 24 Dec 2023 01:55:25 +0530 Subject: [PATCH 3/3] Feat: delete command is implemented --- whiteboard/lib/commandUtility.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/whiteboard/lib/commandUtility.ts b/whiteboard/lib/commandUtility.ts index 7528546729..1643972c69 100644 --- a/whiteboard/lib/commandUtility.ts +++ b/whiteboard/lib/commandUtility.ts @@ -184,14 +184,12 @@ export class CommandUtility implements ExecutorProps { .getNotifier() .notifyRoom(room, message.getMessage()); } else { - console.log("Finding Board :-------------"); const checkBoard = await checkBoardNameByRoomId( this.read.getPersistenceReader(), room.id, deleteBoardName ); if (checkBoard == 1) { - console.log("Board name exist in the room!"); const messageId = await getMessageIdByBoardName( this.read.getPersistenceReader(), room.id, @@ -234,9 +232,6 @@ export class CommandUtility implements ExecutorProps { // Message is finished modified and saved to database await this.modify.getUpdater().finish(message); - console.log( - "Message is updated after deleting the board" - ); } await Promise.all([ sendMessage(