Implement channel history clearing on the server

This commit is contained in:
Pavel Djundik 2020-01-30 10:52:29 +02:00
parent 6f04216af5
commit eb7f9ab298
4 changed files with 74 additions and 4 deletions

View File

@ -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) { Client.prototype.open = function(socketId, target) {
// Due to how socket.io works internally, normal events may arrive later than // 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, // the disconnect event, and because we can't control this timing precisely,

View File

@ -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. * Load messages for given channel on a given network and resolve a promise with loaded messages.
* *

View File

@ -100,11 +100,35 @@ class TextFileMessageStorage {
line += "\n"; line += "\n";
fs.appendFile(path.join(logPath, `${cleanFilename(channel.name)}.log`), line, (e) => { fs.appendFile(
path.join(logPath, TextFileMessageStorage.getChannelFileName(channel)),
line,
(e) => {
if (e) { if (e) {
log.error("Failed to write user log", 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() { getMessages() {
@ -125,6 +149,10 @@ class TextFileMessageStorage {
return `${networkName}-${network.uuid.substring(networkName.length + 1)}`; return `${networkName}-${network.uuid.substring(networkName.length + 1)}`;
} }
static getChannelFileName(channel) {
return `${cleanFilename(channel.name)}.log`;
}
} }
module.exports = TextFileMessageStorage; module.exports = TextFileMessageStorage;

View File

@ -420,6 +420,12 @@ function initializeClient(socket, client, token, lastMessage, openChannel) {
network.edit(client, data); network.edit(client, data);
}); });
socket.on("history:clear", (data) => {
if (typeof data === "object") {
client.clearHistory(data);
}
});
if (!Helper.config.public && !Helper.config.ldap.enable) { if (!Helper.config.public && !Helper.config.ldap.enable) {
socket.on("change-password", (data) => { socket.on("change-password", (data) => {
if (typeof data === "object") { if (typeof data === "object") {