Merge pull request #14 from KevinMidboe/chat/history
Chat/history
This commit was merged in pull request #14.
This commit is contained in:
@@ -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 => {
|
io.on("connection", socket => {
|
||||||
let username = null;
|
let username = null;
|
||||||
|
|
||||||
@@ -19,7 +22,10 @@ module.exports = io => {
|
|||||||
socket.on("chat", msg => {
|
socket.on("chat", msg => {
|
||||||
msg.username = username;
|
msg.username = username;
|
||||||
msg.timestamp = new Date().getTime();
|
msg.timestamp = new Date().getTime();
|
||||||
|
addMessage(msg);
|
||||||
io.emit("chat", msg);
|
io.emit("chat", msg);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports = io;
|
||||||
|
|||||||
33
api/chatHistory.js
Normal file
33
api/chatHistory.js
Normal 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;
|
||||||
39
api/redis.js
Normal file
39
api/redis.js
Normal file
@@ -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
|
||||||
|
};
|
||||||
@@ -62,6 +62,7 @@
|
|||||||
"mini-css-extract-plugin": "~0.5",
|
"mini-css-extract-plugin": "~0.5",
|
||||||
"optimize-css-assets-webpack-plugin": "~3.2",
|
"optimize-css-assets-webpack-plugin": "~3.2",
|
||||||
"pm2": "^4.2.3",
|
"pm2": "^4.2.3",
|
||||||
|
"redis": "^3.0.2",
|
||||||
"sass-loader": "~7.1",
|
"sass-loader": "~7.1",
|
||||||
"uglifyjs-webpack-plugin": "~1.2",
|
"uglifyjs-webpack-plugin": "~1.2",
|
||||||
"url-loader": "^2.2.0",
|
"url-loader": "^2.2.0",
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ const virtualApi = require(path.join(__dirname + "/api/virtualLottery"));
|
|||||||
|
|
||||||
//This is required for the chat to work
|
//This is required for the chat to work
|
||||||
const chat = require(path.join(__dirname + "/api/chat"))(io);
|
const chat = require(path.join(__dirname + "/api/chat"))(io);
|
||||||
|
const chatHistory = require(path.join(__dirname + "/api/chatHistory"));
|
||||||
|
|
||||||
const bodyParser = require("body-parser");
|
const bodyParser = require("body-parser");
|
||||||
|
|
||||||
@@ -89,6 +90,7 @@ app.use("/", loginApi);
|
|||||||
app.use("/api/", updateApi);
|
app.use("/api/", updateApi);
|
||||||
app.use("/api/", retrieveApi);
|
app.use("/api/", retrieveApi);
|
||||||
app.use("/api/", wineinfoApi);
|
app.use("/api/", wineinfoApi);
|
||||||
|
app.use("/api/", chatHistory);
|
||||||
app.use("/api/virtual/", virtualApi(io));
|
app.use("/api/virtual/", virtualApi(io));
|
||||||
app.use("/subscription", subscriptionApi);
|
app.use("/subscription", subscriptionApi);
|
||||||
|
|
||||||
|
|||||||
16
src/api.js
16
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 {
|
export {
|
||||||
statistics,
|
statistics,
|
||||||
colorStatistics,
|
colorStatistics,
|
||||||
@@ -243,5 +256,6 @@ export {
|
|||||||
winners,
|
winners,
|
||||||
winnersSecure,
|
winnersSecure,
|
||||||
deleteWinners,
|
deleteWinners,
|
||||||
deleteAttendees
|
deleteAttendees,
|
||||||
|
getChatHistory
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { page, event } from "vue-analytics";
|
import { page, event } from "vue-analytics";
|
||||||
import { attendees, winners } from "@/api";
|
import { attendees, winners, getChatHistory } from "@/api";
|
||||||
import Chat from "@/ui/Chat";
|
import Chat from "@/ui/Chat";
|
||||||
import Vipps from "@/ui/Vipps";
|
import Vipps from "@/ui/Vipps";
|
||||||
import Attendees from "@/ui/Attendees";
|
import Attendees from "@/ui/Attendees";
|
||||||
@@ -61,6 +61,10 @@ export default {
|
|||||||
emitUsernameOnConnect: false
|
emitUsernameOnConnect: false
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
created() {
|
||||||
|
getChatHistory()
|
||||||
|
.then(messages => this.chatHistory = messages)
|
||||||
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
this.track();
|
this.track();
|
||||||
this.getAttendees();
|
this.getAttendees();
|
||||||
|
|||||||
Reference in New Issue
Block a user