From c893d7d8709b9ce531dfee47b19621f695ceed27 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 17 Mar 2020 01:03:47 +0100 Subject: [PATCH 1/3] Added redis. Chat io msg to redis list. --- api/redis.js | 39 +++++++++++++++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 40 insertions(+) create mode 100644 api/redis.js diff --git a/api/redis.js b/api/redis.js new file mode 100644 index 0000000..e688baa --- /dev/null +++ b/api/redis.js @@ -0,0 +1,39 @@ +const redis = require("redis") +const client = redis.createClient() + + +const addMessage = message => { + const json = JSON.stringify(message); + client.rpush("messages", json) + + return message +} + +const history = (skip=0, take=20) => { + skip = (1 + skip) * -1 // negate to get FIFO + return new Promise((resolve, reject) => client.lrange("messages", (skip * take), skip, (err, data) => { + if (err) { + console.log(err); + reject(err); + } + + data = data.map(data => JSON.parse(data)); + resolve(data); + })) +} + +const clearHistory = () => { + return new Promise((resolve, reject) => client.del("messages", (err, success) => { + if (err) { + console.log(err); + reject(err); + } + resolve(success == 1 ? true : false); + })) +} + +module.exports = { + addMessage, + history, + clearHistory +}; \ No newline at end of file diff --git a/package.json b/package.json index 9f7d0ed..81d6102 100644 --- a/package.json +++ b/package.json @@ -62,6 +62,7 @@ "mini-css-extract-plugin": "~0.5", "optimize-css-assets-webpack-plugin": "~3.2", "pm2": "^4.2.3", + "redis": "^3.0.2", "sass-loader": "~7.1", "uglifyjs-webpack-plugin": "~1.2", "url-loader": "^2.2.0", -- 2.34.1 From cda945ead82e5fc19cc9c69744decd9adea6cfe5 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Tue, 17 Mar 2020 01:05:26 +0100 Subject: [PATCH 2/3] Messages from io gets saved to redis in message list. Endpoint for clearing, should be called when a lottery is ended. --- api/chat.js | 8 +++++++- api/chatHistory.js | 33 +++++++++++++++++++++++++++++++++ server.js | 2 ++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 api/chatHistory.js diff --git a/api/chat.js b/api/chat.js index e319d4a..03be510 100644 --- a/api/chat.js +++ b/api/chat.js @@ -1,4 +1,7 @@ -module.exports = io => { +const path = require("path"); +const { addMessage } = require(path.join(__dirname + "/redis.js")); + +const io = (io) => { io.on("connection", socket => { let username = null; @@ -19,7 +22,10 @@ module.exports = io => { socket.on("chat", msg => { msg.username = username; msg.timestamp = new Date().getTime(); + addMessage(msg); io.emit("chat", msg); }); }); }; + +module.exports = io; diff --git a/api/chatHistory.js b/api/chatHistory.js new file mode 100644 index 0000000..dc1862c --- /dev/null +++ b/api/chatHistory.js @@ -0,0 +1,33 @@ +const express = require("express"); +const path = require("path"); +const router = express.Router(); + +const { history, clearHistory } = require(path.join(__dirname + "/../api/redis")); + +router.use((req, res, next) => { + next(); +}); + +router.route("/chat/history").get(async (req, res) => { + let { skip, take } = req.query; + skip = !isNaN(skip) ? Number(skip) : undefined; + take = !isNaN(take) ? Number(take) : undefined; + + try { + const messages = await history(skip, take); + res.json(messages) + } catch(error) { + res.status(500).send(error); + } +}); + +router.route("/chat/history").delete(async (req, res) => { + try { + const messages = await clearHistory(); + res.json(messages) + } catch(error) { + res.status(500).send(error); + } +}); + +module.exports = router; diff --git a/server.js b/server.js index 9aa428f..f727fd4 100644 --- a/server.js +++ b/server.js @@ -15,6 +15,7 @@ const virtualApi = require(path.join(__dirname + "/api/virtualLottery")); //This is required for the chat to work const chat = require(path.join(__dirname + "/api/chat"))(io); +const chatHistory = require(path.join(__dirname + "/api/chatHistory")); const bodyParser = require("body-parser"); @@ -89,6 +90,7 @@ app.use("/", loginApi); app.use("/api/", updateApi); app.use("/api/", retrieveApi); app.use("/api/", wineinfoApi); +app.use("/api/", chatHistory); app.use("/api/virtual/", virtualApi(io)); app.use("/subscription", subscriptionApi); -- 2.34.1 From 278d252e1e21e5d80b2979b4bc6c15f9309e94ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20Midb=C3=B8e?= Date: Tue, 17 Mar 2020 10:02:06 +0100 Subject: [PATCH 3/3] When loading VirtualLottery we fetch default size of messages from /api/chat/history and append to local chatHistory. --- src/api.js | 16 +++++++++++++++- src/components/VirtualLotteryPage.vue | 6 +++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/api.js b/src/api.js index ebd9173..0b02ef0 100644 --- a/src/api.js +++ b/src/api.js @@ -222,6 +222,19 @@ const register = (username, password) => { }) } + +const getChatHistory = (skip=null, take=null) => { + const url = new URL("/api/chat/history", BASE_URL); + if (!isNaN(skip)) + url.searchParams.append("skip", skip); + if (!isNaN(take)) + url.searchParams.append("take", take); + + return fetch(url.href) + .then(resp => resp.json()) + +} + export { statistics, colorStatistics, @@ -243,5 +256,6 @@ export { winners, winnersSecure, deleteWinners, - deleteAttendees + deleteAttendees, + getChatHistory }; diff --git a/src/components/VirtualLotteryPage.vue b/src/components/VirtualLotteryPage.vue index 95ac450..fc19330 100644 --- a/src/components/VirtualLotteryPage.vue +++ b/src/components/VirtualLotteryPage.vue @@ -35,7 +35,7 @@