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

add option to delete board if the last user left #201

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ You can see a list of these variables in [`configuration.js`](./server/configura
Some important environment variables are :
- `WBO_HISTORY_DIR` : configures the directory where the boards are saved. Defaults to `./server-data/`.
- `WBO_MAX_EMIT_COUNT` : the maximum number of messages that a client can send per unit of time. Increase this value if you want smoother drawings, at the expense of being susceptible to denial of service attacks if your server does not have enough processing power. By default, the units of this quantity are messages per 4 seconds, and the default value is `192`.
- `DELETE_ON_LEAVE` : (default: `false` / `true`) whether the board gets deleted if the last user leaves or not

## Troubleshooting

Expand Down
25 changes: 25 additions & 0 deletions client-data/js/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Tools.drawingEvent = true;
Tools.showMarker = true;
Tools.showOtherCursors = true;
Tools.showMyCursor = true;
Tools.metadata = {users: 1};

Tools.isIE = /MSIE|Trident/.test(window.navigator.userAgent);

Expand Down Expand Up @@ -78,6 +79,15 @@ Tools.connect = function () {
});
});

//Receive metadata about the board from server
this.socket.on("metadata", function (msg) {
if (!msg.users) {
Tools.metadata.users = 1;
console.error("Received a badly formatted message (no users). ", msg);
}
Tools.metadata.users = msg.users;
});

this.socket.on("reconnect", function onReconnection() {
Tools.socket.emit('joinboard', Tools.boardName);
});
Expand Down Expand Up @@ -335,6 +345,10 @@ Tools.send = function (data, toolName) {
"board": Tools.boardName,
"data": d
};
if(d.tool !== "Cursor"){
// Reset the downloaded property as the Whiteboard was updated
Tools.metadata.downloaded = false;
}
Tools.socket.emit('broadcast', message);
};

Expand Down Expand Up @@ -397,6 +411,8 @@ function handleMessage(message) {

Tools.unreadMessagesCount = 0;
Tools.newUnreadMessage = function () {
// Reset the downloaded property as the Whiteboard was updated
Tools.metadata.downloaded = false;
Tools.unreadMessagesCount++;
updateDocumentTitle();
};
Expand Down Expand Up @@ -704,4 +720,13 @@ Tools.svg.height.baseVal.value = document.body.clientHeight;
document.removeEventListener("mouseup", menu_mouseup);
}
menu.addEventListener("mousedown", menu_mousedown);
// Ask to save before leave
if(Tools.server_config.DELETE_ON_LEAVE){
window.onbeforeunload = function() {
if(Tools.metadata.users === 1 && Tools.metadata.downloaded === false ) {
return 'The board will be deleted after you leave, make sure you saved the content!';
}
return undefined;
}
}
})()
1 change: 1 addition & 0 deletions client-data/tools/download/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
document.body.removeChild(element);
window.URL.revokeObjectURL(url);
}
Tools.metadata.downloaded = true;
}

Tools.add({ //The new tool
Expand Down
7 changes: 7 additions & 0 deletions server/boardData.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ class BoardData {
}
}

/** Deletes the board from storage
*/
deleteBoard() {
this.board = {}
this.save()
}

/** Load the data in the board from a file.
* @param {string} name - name of the board
*/
Expand Down
1 change: 1 addition & 0 deletions server/client_configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ module.exports = {
MAX_EMIT_COUNT_PERIOD: config.MAX_EMIT_COUNT_PERIOD,
BLOCKED_TOOLS: config.BLOCKED_TOOLS,
AUTO_FINGER_WHITEOUT: config.AUTO_FINGER_WHITEOUT,
DELETE_ON_LEAVE: config.DELETE_ON_LEAVE,
};
4 changes: 3 additions & 1 deletion server/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ module.exports = {
/** Automatically switch to White-out on finger touch after drawing
with Pencil using a stylus. Only supported on iPad with Apple Pencil. */
AUTO_FINGER_WHITEOUT: process.env['AUTO_FINGER_WHITEOUT'] !== "disabled",
};
/** Automatically delete boards as soon as the last user leaves the board */
DELETE_ON_LEAVE: process.env['DELETE_ON_LEAVE'] === "true",
};
13 changes: 12 additions & 1 deletion server/sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function socketConnection(socket) {
var board = await getBoard(name);
board.users.add(socket.id);
log("board joined", { board: board.name, users: board.users.size });
broadcastMetaData(socket, board.name, { users: board.users.size })
return board;
}

Expand Down Expand Up @@ -141,15 +142,25 @@ function socketConnection(socket) {
board.users.delete(socket.id);
var userCount = board.users.size;
log("disconnection", { board: board.name, users: board.users.size });
broadcastMetaData(socket, board.name, { users: board.users.size })
if (userCount === 0) {
board.save();
if(config.DELETE_ON_LEAVE){
board.deleteBoard()
} else {
board.save();
}
delete boards[room];
}
}
});
});
}

function broadcastMetaData(socket, boardName, metaData) {
socket.emit("metadata", metaData);
socket.broadcast.to(boardName).emit("metadata", metaData);
}

function handleMessage(boardName, message, socket) {
if (message.tool === "Cursor") {
message.socket = socket.id;
Expand Down