From eb7f9ab2985f303b051a491c54e362a813f2d997 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Thu, 30 Jan 2020 10:52:29 +0200 Subject: [PATCH] Implement channel history clearing on the server --- src/client.js | 22 +++++++++++++++++ src/plugins/messageStorage/sqlite.js | 14 +++++++++++ src/plugins/messageStorage/text.js | 36 ++++++++++++++++++++++++---- src/server.js | 6 +++++ 4 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/client.js b/src/client.js index b188fafa..ee5f8de5 100644 --- a/src/client.js +++ b/src/client.js @@ -501,6 +501,28 @@ Client.prototype.more = function(data) { }; }; +Client.prototype.clearHistory = function(data) { + const client = this; + const target = client.find(data.target); + + if (!target) { + return; + } + + target.chan.messages = []; + target.chan.unread = 0; + target.chan.highlight = 0; + target.chan.firstUnread = 0; + + if (!target.chan.isLoggable()) { + return; + } + + for (const messageStorage of this.messageStorage) { + messageStorage.deleteChannel(target.network, target.chan); + } +}; + Client.prototype.open = function(socketId, target) { // Due to how socket.io works internally, normal events may arrive later than // the disconnect event, and because we can't control this timing precisely, diff --git a/src/plugins/messageStorage/sqlite.js b/src/plugins/messageStorage/sqlite.js index a1c56cd9..f54dba93 100644 --- a/src/plugins/messageStorage/sqlite.js +++ b/src/plugins/messageStorage/sqlite.js @@ -144,6 +144,20 @@ class MessageStorage { ); } + deleteChannel(network, channel) { + if (!this.isEnabled) { + return; + } + + this.database.serialize(() => + this.database.run( + "DELETE FROM messages WHERE network = ? AND channel = ?", + network.uuid, + channel.name.toLowerCase() + ) + ); + } + /** * Load messages for given channel on a given network and resolve a promise with loaded messages. * diff --git a/src/plugins/messageStorage/text.js b/src/plugins/messageStorage/text.js index 0feebe8a..90044468 100644 --- a/src/plugins/messageStorage/text.js +++ b/src/plugins/messageStorage/text.js @@ -100,11 +100,35 @@ class TextFileMessageStorage { line += "\n"; - fs.appendFile(path.join(logPath, `${cleanFilename(channel.name)}.log`), line, (e) => { - if (e) { - log.error("Failed to write user log", e); + fs.appendFile( + path.join(logPath, TextFileMessageStorage.getChannelFileName(channel)), + line, + (e) => { + if (e) { + log.error("Failed to write user log", e); + } } - }); + ); + } + + deleteChannel() { + /* TODO: Truncating text logs is disabled, until we figure out some UI for it + if (!this.isEnabled) { + return; + } + + const logPath = path.join( + Helper.getUserLogsPath(), + this.client.name, + TextFileMessageStorage.getNetworkFolderName(network), + TextFileMessageStorage.getChannelFileName(channel) + ); + + fs.truncate(logPath, 0, (e) => { + if (e) { + log.error("Failed to truncate user log", e); + } + });*/ } getMessages() { @@ -125,6 +149,10 @@ class TextFileMessageStorage { return `${networkName}-${network.uuid.substring(networkName.length + 1)}`; } + + static getChannelFileName(channel) { + return `${cleanFilename(channel.name)}.log`; + } } module.exports = TextFileMessageStorage; diff --git a/src/server.js b/src/server.js index 6827daa7..6b5281bd 100644 --- a/src/server.js +++ b/src/server.js @@ -420,6 +420,12 @@ function initializeClient(socket, client, token, lastMessage, openChannel) { network.edit(client, data); }); + socket.on("history:clear", (data) => { + if (typeof data === "object") { + client.clearHistory(data); + } + }); + if (!Helper.config.public && !Helper.config.ldap.enable) { socket.on("change-password", (data) => { if (typeof data === "object") {