Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat log, PM's and other new Features! #49

Merged
merged 16 commits into from
Apr 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 151 additions & 1 deletion socketserver/db_level.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var DBUtils = require('./database_util');
//Variables
var currentPID = 0;
var currentUID = 0;
var currentCID = 0;
var expires = 1000 * 60 * 60 * 24 * config.loginExpire;
var usernames = [];

Expand Down Expand Up @@ -118,6 +119,46 @@ function LevelDB(callback) {
return false;
});
});

//ChatDB
if(!this.ChatDB)
this.ChatDB = setupDB(dbdir + '/chat',

//If new DB is created
function(newdb) {
currentCID = 1;
log.debug('CIDCOUNTER set to 1');
newdb.put('CIDCOUNTER', 1);
},

//Callback
function(err, newdb) {
if (err) {
throw new Error('Could not open ChatDB: ' + err);
}
if (currentCID != 0) return;

newdb.get('CIDCOUNTER', function(err, val) {
if (err) {
throw new Error('Cannot get CIDCOUNTER from PmDB. Might be corrupt');
}
currentCID = parseInt(val);
});
});

//PmDB
if(!this.PmDB)
this.PmDB = setupDB(dbdir + '/pm',

//If new DB is created
function(newdb) {},

//Callback
function(err, newdb) {
if (err) {
throw new Error('Could not open PmDB: ' + err);
}
});
}

function setupDB(dir, setup, callback){
Expand Down Expand Up @@ -583,4 +624,113 @@ LevelDB.prototype.userEmailExists = function(key, callback) {
});
};

module.exports = new LevelDB();
//ChatDB
LevelDB.prototype.logChat = function(uid, msg, special, callback) {
this.putJSON(this.ChatDB, currentCID, { uid: uid, msg: msg, special: special });
callback(currentCID++);
};

//PmDB
LevelDB.prototype.logPM = function(from, to, msg, callback) {
var that = this;
var key = Math.min(from, to) + ":" + Math.max(from, to);

this.getJSON(this.PmDB, key, function(err, res){
var out = [];

if(!err) out = res;

out.push({
message: msg,
time: new Date(),
from: from,
unread: true,
});

that.putJSON(that.PmDB, key, out);
});
};

LevelDB.prototype.getConversation = function(from, to, callback) {
var key = Math.min(from, to) + ":" + Math.max(from, to);

this.getJSON(this.PmDB, key, function(err, res){
if(err){
callback(null, []);
} else {
callback(null, res);
}
});
};

LevelDB.prototype.getConversations = function(uid, callback) {
var that = this;

var out = {};
var uids;
uid = uid.toString();

this.PmDB.createReadStream()
.on('data', function(data) {
if (data.key.indexOf(':') == -1 || (uids = data.key.split(':')).indexOf(uid) == -1) return;

try {
var convo = JSON.parse(data.value);
} catch (e) {
return;
}

var unread = 0;
convo.map(function(e){
if(e.unread && e.from != uid) unread++;
return {
messages: e.messages,
time: e.time,
from: e.from,
};
});

out[uids[(uids.indexOf(uid) + 1) % 2]] = {
user: null,
messages: [ convo.pop() ],
unread: unread,
};
})
.on('end', function() {
var uids = Object.keys(out).map(function(e){ return parseInt(e); });

if (uids.length > 0) {
that.getUserByUid(uids, function(err, result){
if (err) {
callback(err);
} else {
for (var id in result) {
out[id].user = result[id].getClientObj();
}
callback(null, out);
}
});
} else {
callback(null, out);
}
return false;
});
};

LevelDB.prototype.markConversationRead = function(uid, uid2, time) {
var that = this;
var key = Math.min(uid, uid2) + ":" + Math.max(uid, uid2);

this.getJSON(this.PmDB, key, function(err, res) {
if(err) return;

res.map(function(e){
if(e.from == uid2 && new Date(e.time) < new Date(time)) e.unread = false;
return e;
});

that.putJSON(that.PmDB, key, res);
});
};

module.exports = new LevelDB();
Loading