Messages from io gets saved to redis in message list. Endpoint for clearing, should be called when a lottery is ended.

This commit is contained in:
2020-03-17 01:05:26 +01:00
parent c893d7d870
commit cda945ead8
3 changed files with 42 additions and 1 deletions

View File

@@ -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;

33
api/chatHistory.js Normal file
View File

@@ -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;

View File

@@ -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);