Skip to content

Commit

Permalink
add option to delete board if the last user left
Browse files Browse the repository at this point in the history
resolves Delete Board as the last user leaves lovasoa#199
  • Loading branch information
DanielHabenicht authored Jun 2, 2021
1 parent 9127340 commit 93608f2
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
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
20 changes: 20 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,14 @@ Tools.connect = function () {
});
});

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

this.socket.on("reconnect", function onReconnection() {
Tools.socket.emit('joinboard', Tools.boardName);
});
Expand Down Expand Up @@ -704,4 +713,15 @@ 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) {
// TODO: Add config from other PR
// if(Tools.metadata.saved === true) {
return 'The board will be deleted after you leave, make sure you saved the content!';
}
return undefined;
}
}
})()
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",
};
12 changes: 11 additions & 1 deletion server/sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function socketConnection(socket) {

var board = await getBoard(name);
board.users.add(socket.id);
broadcastMetaData(socket, board.name, { users: board.users.size })
log("board joined", { board: board.name, users: board.users.size });
return board;
}
Expand Down Expand Up @@ -141,15 +142,24 @@ 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.broadcast.to(boardName).emit("metadata", metaData);
}

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

0 comments on commit 93608f2

Please sign in to comment.