10 minutes chat history, limit of 20 messages

This commit is contained in:
Kasper Rynning-Tønnesen
2017-10-13 13:47:13 +02:00
parent 3b50d84af8
commit d8c1abfba9
7 changed files with 60 additions and 6 deletions

View File

@@ -1,3 +1,28 @@
function get_history(channel, all, socket) {
var query = {};
if(all) {
query = {
all: true,
};
} else {
query = {
all: false,
channel: channel,
};
}
db.collection("chat_logs").find(query, {
from: 1,
createdAt: 1,
all: 1,
channel: 1,
msg: 1,
icon: 1,
_id: 0
}).sort({createdAt: 1}).limit(20, function(err, docs) {
socket.emit("chat_history", {all: all, data: docs});
});
}
function chat(msg, guid, offline, socket) {
if(typeof(msg) !== 'object' && !msg.hasOwnProperty('data') && !msg.hasOwnProperty('channel') && !msg.hasOwnProperty('pass')) {
@@ -18,6 +43,7 @@ function chat(msg, guid, offline, socket) {
if(n.length > 0 && n[0].icon) {
icon = n[0].icon;
}
db.collection("chat_logs").insert({ "createdAt": new Date(), all: false, channel: coll, from: docs[0].name, msg: ": " + data, icon: icon });
io.to(coll).emit('chat', {from: docs[0].name, msg: ": " + data, icon: icon});
});
} else if(docs.length == 0){
@@ -49,6 +75,7 @@ function all_chat(msg, guid, offline, socket) {
if(n.length > 0 && n[0].icon) {
icon = n[0].icon;
}
db.collection("chat_logs").insert({ "createdAt": new Date(), all: true, channel: coll, from: docs[0].name, msg: ": " + data, icon: icon }, function(err, docs) {});
io.sockets.emit('chat.all', {from: docs[0].name, msg: ": " + data, channel: coll, icon: icon});
});
} else if(docs.length == 0) {
@@ -163,6 +190,7 @@ function get_name(guid, announce_payload) {
})
}
module.exports.get_history = get_history;
module.exports.chat = chat;
module.exports.all_chat = all_chat;
module.exports.namechange = namechange;

View File

@@ -2,6 +2,8 @@ var mongo_db_cred = {config: 'mydb'};
var mongojs = require('mongojs');
var db = mongojs(mongo_db_cred.config);
db.collection("chat_logs").createIndex({ "createdAt": 1 }, { expireAfterSeconds: 600 });
db.on('connected', function(err) {
console.log("connected");
})

View File

@@ -131,6 +131,10 @@ module.exports = function() {
}
});
socket.on('get_history', function(msg) {
Chat.get_history(msg.channel, msg.all, socket);
});
socket.on('chat', function (msg) {
Chat.chat(msg, guid, offline, socket);
});