Skip to content

Commit

Permalink
Chat log, PM's and other new Features! (#49)
Browse files Browse the repository at this point in the history
* Public chat logging

* Initial Commit of PM Update

* LevelDB support for PMs

* Changed PM Chat notification to default to 'Off'

Changed due to new PM interface.

* Fix for PM counter not increasing in certain situations

* Fix for PM unread counter not increasing in certain situations

* Fix for client not marking read messages

* Update app.js

* Added ability to send a PM to an offline user

* Added mention filter option.

* Improved look of the New PM Modal

* Removed logging and undefined error, added loading animation

* Cleaned up look of PM buttons

* Fix for buttons being in the wrong place on new-pm modal
  • Loading branch information
TheBanHammer committed Apr 22, 2016
1 parent f5415d0 commit 772f666
Show file tree
Hide file tree
Showing 8 changed files with 1,115 additions and 123 deletions.
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

0 comments on commit 772f666

Please sign in to comment.