Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ Feat ] : Delete slash command #54

Merged
merged 4 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions whiteboard/blocks/UtilityBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand Down
151 changes: 143 additions & 8 deletions whiteboard/lib/commandUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()!;
Expand All @@ -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,
Expand All @@ -90,7 +101,7 @@ export class CommandUtility implements ExecutorProps {
attachments,
headerBlock
);

storeBoardRecord(
persistence,
room.id,
Expand All @@ -106,9 +117,7 @@ export class CommandUtility implements ExecutorProps {
"",
"Public"
);

}

}
}

Expand All @@ -130,6 +139,129 @@ 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(" ") : "";

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 {
const checkBoard = await checkBoardNameByRoomId(
this.read.getPersistenceReader(),
room.id,
deleteBoardName
);
if (checkBoard == 1) {
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);
}
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":
Expand All @@ -141,6 +273,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()
Expand Down
11 changes: 6 additions & 5 deletions whiteboard/lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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*:
Expand All @@ -217,7 +219,6 @@ export async function handleListCommand(
.setParseUrls(true);

return await read.getNotifier().notifyRoom(room, msg.getMessage());

}

const text = `No boards found`;
Expand Down
26 changes: 23 additions & 3 deletions whiteboard/persistence/boardInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,16 @@ export const getAllBoardIds = async (
};

// function to get new board name
export const getBoardName = async (persis: IPersistenceRead, roomId: string): Promise<string> => {
export const getBoardName = async (
persis: IPersistenceRead,
roomId: string
): Promise<string> => {
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}`;
}
Expand Down Expand Up @@ -305,7 +308,7 @@ export const deleteBoardByMessageId = async (
messageId: string
): Promise<string> => {
let records = await getBoardRecordByMessageId(persistenceRead, messageId);
console.log("records", records)
console.log("records", records);
if (!records) {
console.log("No records found for boardname");
return "";
Expand All @@ -328,3 +331,20 @@ export const deleteBoardByMessageId = async (

return records.title;
};

export const getMessageIdByBoardName = async (
persistenceRead: IPersistenceRead,
roomId: string,
boardName: string
): Promise<any> => {
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;
};