From 2c0e2f08c858146aeb10de421889056763413997 Mon Sep 17 00:00:00 2001 From: Sharon Kennedy Date: Wed, 20 May 2020 12:07:09 -0400 Subject: [PATCH] delete file on bot command or when bot leaves the room --- src/bot.js | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/bot.js b/src/bot.js index b044e5f..efe4f8c 100644 --- a/src/bot.js +++ b/src/bot.js @@ -230,6 +230,9 @@ class OcrccBot { case "transcript please": this.sendTranscript(senderId, roomId); break; + case "delete transcript": + this.deleteTranscript(senderId, roomId); + break; case "hi": const responses = [ "Hi!", @@ -313,6 +316,38 @@ class OcrccBot { } } + deleteTranscript(senderId, roomId) { + const transcriptFile = this.localStorage.getItem(`${roomId}-transcript`) + + if (!transcriptFile) { + return this.sendTextMessage( + roomId, + "There is no transcript for this chat.", + senderId + ); + } + + fs.unlink(transcriptFile, (err) => { + if (err) { + logger.log('error', "UNABLE TO DELETE TRANSCRIPT FILE => " + transcriptFile) + logger.log('error', err) + + return this.sendTextMessage( + roomId, + `There was an error deleting the transcript: ${err}`, + senderId + ); + } + this.localStorage.removeItem(`${roomId}-transcript`) + logger.log('info', "DELETED TRANSCRIPT FILE => " + transcriptFile) + this.sendTextMessage( + roomId, + `The transcript file has been deleted.`, + senderId + ); + }); + } + async deleteOldDevices() { const currentDeviceId = this.client.getDeviceId(); const deviceData = await this.client.getDevices() @@ -429,11 +464,12 @@ class OcrccBot { const memberCount = room.getJoinedMemberCount() - if (memberCount === 1) { // just the bot + if (memberCount === 1) { // just the bot left logger.log("info", `LEAVING EMPTY ROOM ==> ${member.roomId}`); - this.client.leave(member.roomId) + this.deleteTranscript(member.userId, member.roomId); this.localStorage.removeItem(`${member.roomId}-facilitator`) this.localStorage.removeItem(`${member.roomId}-transcript`) + this.client.leave(member.roomId) } } });