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 ]: Revoke rights for a certain user #84

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions whiteboard/WhiteboardApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class WhiteboardApp extends App implements IUIKitInteractionHandler {
\`/whiteboard delete <board name>\` - Delete a whiteboard
\`/whiteboard help\` - Display helper message
\`/whiteboard list\` - List all the board names in the room
\`/whiteboard deny <user name> of <board name>\` - Deny a user to edit the board
You can use \`Create Whiteboard\` Action Button to create a new whiteboard as well \n
Refer https://github.com/RocketChat/Apps.Whiteboard documentation for more details 🚀`,
persistence
Expand Down
24 changes: 2 additions & 22 deletions whiteboard/blocks/UtilityBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,6 @@ export async function buildHeaderBlock(
ButtonStyle.PRIMARY
);

// const settingButton = getButton(
// "Settings",
// UtilityEnum.PREVIEW_BLOCK_ID,
// UtilityEnum.SETTINGS_BUTTON_ACTION_ID,
// appId,
// "Settings",
// undefined
// );

// const deleteButton = getDeleteButton(
// "Delete board",
// UtilityEnum.PREVIEW_BLOCK_ID,
// UtilityEnum.DELETE_BUTTON_ACTION_ID,
// appId,
// "Delete",
// ButtonStyle.DANGER
// );

let markdownBlock: SectionBlock;
if (boardname == undefined) {
markdownBlock = getMarkdownBlock(
Expand All @@ -53,9 +35,7 @@ export async function buildHeaderBlock(
}

const actionBlock = getActionsBlock(UtilityEnum.PREVIEW_BLOCK_ID, [
// settingButton,
openbutton,
// deleteButton,
openbutton
]);
block.push(markdownBlock);
block.push(actionBlock);
Expand Down Expand Up @@ -176,7 +156,7 @@ export async function permissionHeaderBlock(

let markdownBlock: SectionBlock;
markdownBlock = getMarkdownBlock(
`${userForBoardPermission} wants to edit *${boardname}*`
`*${userForBoardPermission}* wants to edit *${boardname}*`
);

const actionBlock = getActionsBlock(UtilityEnum.PERMISSION_BLOCK_ID, [
Expand Down
36 changes: 8 additions & 28 deletions whiteboard/handlers/ExecuteViewSubmitHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export class ExecuteViewSubmitHandler {
.getAppUser()) as IUser;
const appId = AppSender.appId;
try {
console.log("View Id: ", view);
switch (view.id) {
// This case is used to handle the submit interaction from the settings modal
case UtilityEnum.SETTINGS_MODAL_ID:
Expand Down Expand Up @@ -400,7 +399,6 @@ export class ExecuteViewSubmitHandler {
const messageId =
this.context.getInteractionData().view.submit
?.value;
// console.log("MessageId inside Delete Modal ID", messageId)
if (messageId) {
// Board data is deleted from database
const boardName = await deleteBoardByMessageId(
Expand Down Expand Up @@ -445,46 +443,32 @@ export class ExecuteViewSubmitHandler {
.getInteractionResponder()
.successResponse();

// case UtilityEnum.EDIT_MODAL_ID:
// const boardURL = this.context.getInteractionData().view.submit?.url
// console.log("boardURL", boardURL)
// if(boardURL){
// this.http.get(boardURL)
// }
// return this.context
// .getInteractionResponder()
// .successResponse();

case UtilityEnum.PERMISSION_MODAL_ID:
const boardMessageId =
this.context.getInteractionData().view.submit
?.value;

// console.log("MessageId inside Permission Modal ID", messageId)
if(boardMessageId){

const room = await this.read
.getMessageReader()
.getRoom(boardMessageId);
if(room){
// console.log("Checking interaction data", this.context.getInteractionData().view.submit?.value)

const boardData = await getBoardRecordByMessageId(this.read.getPersistenceReader(), boardMessageId)
// console.log("Board data", boardData)

for(let i=0; i<boardData.boardOwner.length; i++){
const userBoardOwner = boardData.boardOwner[i]
if(userBoardOwner && appId){
const boardName = boardData.title;
// const message = await this.modify
// .getUpdater()
// .message(messageId, AppSender);

const message = this.modify.getCreator().startMessage();
const userForBoardPermission = this.context.getInteractionData().user;

// Some message configurations
message.setEditor(userBoardOwner).setRoom(room);

// console.log("board_permission_kaun_le_rha_viewsubmithandler.ts", this.context.getInteractionData())
// Permission header block as other user is trying to edit the board

const permissionBlock =
await permissionHeaderBlock(
userBoardOwner.username,
Expand All @@ -494,20 +478,16 @@ export class ExecuteViewSubmitHandler {
);

message.setBlocks(permissionBlock);
// message.removeAttachment(0);


await this.modify.getNotifier().notifyUser(userBoardOwner, message.getMessage());


// const message:IMessage = {text:"Hello", room: room, sender: this.context.getInteractionData().user}
// this.modify.getNotifier().notifyUser(user, message)

}
}
}


// const message:IMessage = {text:"Hello", room: room, sender: this.context.getInteractionData().user}
// this.modify.getNotifier().notifyUser(user, message)
}

}
return this.context.getInteractionResponder().successResponse();

Expand Down
108 changes: 59 additions & 49 deletions whiteboard/lib/commandUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { WhiteboardApp } from "../WhiteboardApp";
import {
handleListCommand,
helperMessage,
removeUserFromBoardOwner,
sendMessage,
sendMessageWithAttachment,
} from "./messages";
Expand All @@ -26,12 +27,7 @@ import {
import { randomId } from "./utilts";
import { defaultPreview } from "../assets/defaultPreview";

import {
checkBoardNameByRoomId,
getMessageIdByBoardName,
deleteBoardByMessageId,
} from "../persistence/boardInteraction";
import { IMessage } from "@rocket.chat/apps-engine/definition/messages";
import {checkBoardNameByRoomId, getMessageIdByBoardName, deleteBoardByMessageId, getBoardRecordByRoomId} from '../persistence/boardInteraction';
//CommandUtility is used to handle the commands

export class CommandUtility implements ExecutorProps {
Expand Down Expand Up @@ -82,8 +78,6 @@ export class CommandUtility implements ExecutorProps {
for(const user of users){
if(user.roles.includes('admin') || user.roles.includes('owner') || user.roles.includes('moderator')){
if(sender.username != user.username){

// console.log("here again", user, sender.username)
boardOwner.push(user)
}

Expand Down Expand Up @@ -157,24 +151,6 @@ export class CommandUtility implements ExecutorProps {
headerBlock
);

console.log("MessageId", messageId);

// const headerBlockAfterPermission = await buildHeaderBlockAfterPermission(
// sender.username,
// boardURL,
// appId,
// name
// )
// const message = await this.modify
// .getUpdater()
// .message(messageId, sender);
// message.setBlocks(headerBlockAfterPermission)
// for(let user of boardOwner){

// await this.modify.getNotifier().notifyUser(user, message.getMessage());
// // await this.modify.getUpdater().finish(message);
// }

storeBoardRecord(
persistence,
room.id,
Expand Down Expand Up @@ -339,26 +315,60 @@ export class CommandUtility implements ExecutorProps {
}
}

// private async checkCommand() {
// 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;
// const attachments = [
// {
// collapsed: true,
// color: "#00000000",
// imageUrl: defaultPreview,
// },
// ];

// const message:IMessage = {text:"Board Here", room:room, sender:appSender, pinned:true, attachments:attachments}

// await this.read.getNotifier().notifyRoom(room, message);
// }
// denyUser is used to handle the /whiteboard deny {userName} of {boardName} command
private async denyUser() {

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"
const requiredParams = params.slice(1)
const index = requiredParams.indexOf('of')
const userName = requiredParams.slice(0,index).join(" ").trim()
const boardName = requiredParams.slice(index+1).join(" ").trim()

// Get the board data from the database
const boardData = await getBoardRecordByRoomId(this.read.getPersistenceReader(), room.id)
let requiredBoardData;
for (let i = 0; i < boardData.length; i++) {
if (boardData[i].title == boardName) {
requiredBoardData = boardData[i]
break;
}
}
// If board not found
if(!requiredBoardData){
const message = {room: room, sender: appSender, text: "Board not found"}
return this.read.getNotifier().notifyUser(user, message);
}
// If board found
else{
// check whether the user is admin or board Owner or not
const match = requiredBoardData.boardOwner.find(obj => obj.id === user.id)
if(match){
// if the user is admin or board Owner
const response = await removeUserFromBoardOwner(this.room, this.persistence, userName, requiredBoardData)
if(response !== undefined){
const message = {room: room, sender: appSender, text: `**${userName}** has been removed from the rights of **${boardName}**.`}
return this.read.getNotifier().notifyUser(user, message);
}
else{
const message = {room: room, sender: appSender, text: "Some error occured!"}
return this.read.getNotifier().notifyUser(user, message);
}

}
else{
// if the user is not admin or board Owner
const message = {room: room, sender: appSender, text: "You are not authorized to perform this action"}
return this.read.getNotifier().notifyUser(user, message);
}
}
}

public async resolveCommand(context: WhiteboardSlashCommandContext) {
switch (this.command[0]) {
Expand All @@ -374,9 +384,9 @@ export class CommandUtility implements ExecutorProps {
case "delete":
await this.deleteBoardCommand();
break;
// case "check":
// await this.checkCommand();
// break;
case "deny":
await this.denyUser();
break;
default:
const appSender: IUser = (await this.read
.getUserReader()
Expand Down
37 changes: 37 additions & 0 deletions whiteboard/lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export async function helperMessage(
\`/whiteboard delete <board name>\` - Delete a whiteboard
\`/whiteboard help\` - Display helper message
\`/whiteboard list\` - List all the board names in the room
\`/whiteboard deny <user name> of <board name>\` - Deny a user to edit the board
You can use \`Create Whiteboard\` Action Button to create a new whiteboard as well \n
Refer https://github.com/RocketChat/Apps.Whiteboard for more details 🚀
`;
Expand Down Expand Up @@ -356,3 +357,39 @@ export async function addUsertoBoardOwner(
return undefined
}
}


// function to remove user from boardRights
export const removeUserFromBoardOwner = async (
room: IRoom,
persistance: IPersistence,
userName: string,
board: any,
) => {

// Add the user to the boardOwner
const boardOwners = board.boardOwner

// Filter the user from the boardOwner
const boardOwnerArray = boardOwners.filter((boardOwner) => boardOwner.username !== userName)

if(boardOwnerArray === boardOwners){
return undefined
}
else{
// Update the boardData in the database
await storeBoardRecord(
persistance,
room.id,
board.id,
board.boardData,
board.messageId,
board.cover,
board.title,
board.privateMessageId,
board.status,
boardOwnerArray
)
return boardOwnerArray
}
}
1 change: 0 additions & 1 deletion whiteboard/persistence/boardInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,6 @@ export const getMessageIdByBoardName = async (

for (const board of boardData) {
if (board.title === boardName) {
console.log("Board name mil gaya!");
let messageId = board.messageId;
return messageId;
}
Expand Down