From 8712adb6b9d4c93b7133b08fddb2be4b4b957eda Mon Sep 17 00:00:00 2001 From: DerpgonCz Date: Sun, 26 Jun 2016 18:46:20 +0200 Subject: [PATCH 1/5] Serverside implementation LevelDB implemented --- socketserver/db_mysql.js | 6 ++++ socketserver/socketserver.js | 70 ++++++++++++++++++++++++++++++++++++ socketserver/user.js | 41 +++++++++++++++++++++ 3 files changed, 117 insertions(+) diff --git a/socketserver/db_mysql.js b/socketserver/db_mysql.js index 580d40b..491d934 100644 --- a/socketserver/db_mysql.js +++ b/socketserver/db_mysql.js @@ -139,6 +139,12 @@ var MysqlDB = function(){ `time` DATETIME\ );\ \ + CREATE TABLE IF NOT EXISTS `user_blocks` (\ + `from` INTEGER UNSINGED NOT NULL,\ + `to` INTEGER UNSIGNED NOT NULL,\ + PRIMARY KEY(`from`, `to`)\ + );\ + \ UPDATE `users` SET `lastdj` = false;\ ", null, function(err, res){ if(err) throw new Error(err); diff --git a/socketserver/socketserver.js b/socketserver/socketserver.js index 48e3eb3..3b2ed49 100644 --- a/socketserver/socketserver.js +++ b/socketserver/socketserver.js @@ -308,6 +308,8 @@ var SocketServer = function(server){ var ip = (socket.upgradeReq.headers['x-forwarded-for'] || socket.upgradeReq.connection.remoteAddress); log.debug(ip + " sent: " + data); + if(socket.user) + socket.user.blocked.push("a"); try { data = JSON.parse(data); @@ -3078,6 +3080,74 @@ var SocketServer = function(server){ else DB.getUserByUid(~~(data.data.uid), { getPlaylists: false }, cb); break; + case 'blockUser': + /* + Expects { + type: 'blockUser', + data: { + uid: uid, + } + } + */ + + //Check for props + if (!(data.data.uid = +data.data.uid)){ + returnObj.data = { + error: 'WrongProps' + }; + socket.sendJSON(returnObj); + break; + } + + //Add blocked user + socket.user.addBlockedUser(data.data.uid, function(err) { + if(err) { + returnObj.data = { + error: err + } + } else { + returnObj.data = { + success: true + } + } + + socket.sendJSON(returnObj); + }); + break; + case 'unblockUser': + /* + Expects { + type: 'unblockUser', + data: { + uid: uid, + } + } + */ + + //Check for props + if (!(data.data.uid = +data.data.uid)){ + returnObj.data = { + error: 'WrongProps' + }; + socket.sendJSON(returnObj); + break; + } + + //Remove blocked user + socket.user.removeBlockedUser(data.data.uid, function(err) { + if(err) { + returnObj.data = { + error: err + } + } else { + returnObj.data = { + success: true + } + } + + socket.sendJSON(returnObj); + }); + break; } }); }); diff --git a/socketserver/user.js b/socketserver/user.js index 2d47a6b..a37a2c9 100644 --- a/socketserver/user.js +++ b/socketserver/user.js @@ -23,6 +23,7 @@ var defaultObj = function(){ temp_uptime: Date.now(), lastdj: false, salt: '', + blocked: [], }; }; @@ -39,6 +40,7 @@ var fieldsNotSent = [ 'created', 'lastdj', 'salt', + 'blocked', ]; // These fields (key from defaultObj) are not saved in the db @@ -180,6 +182,35 @@ User.prototype.removePlaylist = function (pid, callback) { callback('PlaylistNotFound'); }; +User.prototype.addBlockedUser = function(uid, callback) { + var index = this.data.blocked.indexOf(uid); + + if(index != -1) { + callback('UserAlreadyBlocked'); + + } else if(this.data.uid == uid) { + callback('CannotBlockSelf'); + + } else { + this.data.blocked.push(uid); + this.updateUser(); + callback(null, true); + } +}; + +User.prototype.removeBlockedUser = function(uid, callback) { + var index = this.data.blocked.indexOf(uid); + + if(index == -1) { + callback('UserNotBlocked'); + + } else { + this.data.blocked.splice(index, 1); + this.updateUser(); + callback(null, true); + } +} + /** * getClientObj() Returns public information to be sent to clients * @@ -361,4 +392,14 @@ Object.defineProperty( User.prototype, 'salt', { } }); +Object.defineProperty( User.prototype, 'blocked', { + get: function() { + return this.data.blocked; + }, + set: function(val) { + this.data.blocked = val; + this.updateUser(); + } +}); + module.exports = User; \ No newline at end of file From 9e9401e7520fd268f2e60d596820ede026018798 Mon Sep 17 00:00:00 2001 From: DerpgonCz Date: Sun, 26 Jun 2016 21:05:54 +0200 Subject: [PATCH 2/5] Front-end implementation Does not show messages from blocked users API functions added - API.user.block(uid[, callback]), API.user.unblock(uid[, callback]), API.user.isBlocked(uid) --- socketserver/socketserver.js | 2 - webserver/public/lib/js/app.js | 93 +++++++++++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 4 deletions(-) diff --git a/socketserver/socketserver.js b/socketserver/socketserver.js index 3b2ed49..e67927f 100644 --- a/socketserver/socketserver.js +++ b/socketserver/socketserver.js @@ -308,8 +308,6 @@ var SocketServer = function(server){ var ip = (socket.upgradeReq.headers['x-forwarded-for'] || socket.upgradeReq.connection.remoteAddress); log.debug(ip + " sent: " + data); - if(socket.user) - socket.user.blocked.push("a"); try { data = JSON.parse(data); diff --git a/webserver/public/lib/js/app.js b/webserver/public/lib/js/app.js index f38032c..03b5379 100644 --- a/webserver/public/lib/js/app.js +++ b/webserver/public/lib/js/app.js @@ -261,6 +261,7 @@ historylimit: 50, lastdj: false, description: '', + blockedusers: [], }, isOnWaitlist: function(uid){ if (MP.session.queue.currentdj && MP.session.queue.currentdj.uid == uid) return true; @@ -1580,6 +1581,54 @@ $('#creds-back').hide(); $('.dash, #app-left, #app-right').show(); }, + user: { + isBlocked: function(uid) { + return MP.session.blockedusers.indexOf(uid) != -1; + }, + block: function(uid, callback) { + if(!(uid = +uid)) + return false + + var obj = { + type: 'blockUser', + data: { + uid: uid + } + } + + obj.id = MP.addCallback(obj.type, function(err, data) { + MP.session.blockedusers.push(uid); + + if(callback) + callback(err, data); + }); + + socket.sendJSON(obj); + }, + unblock: function(uid, callback) { + if(!(uid = +uid)) + return false + + var obj = { + type: 'unblockUser', + data: { + uid: uid + } + } + + obj.id = MP.addCallback(obj.type, function(err, data) { + var index = MP.session.blockedusers.indexOf(uid); + + if(index != -1) + MP.session.blockedusers.splice(index, 1); + + if(callback) + callback(err, data); + }); + + socket.sendJSON(obj); + } + }, }, mediaPreview : { isOpened: function(){return MP.session.mediaPreview.player != null;}, @@ -1923,6 +1972,9 @@ } var user = data.user || MP.findUser(data.uid); + if(MP.api.user.isBlocked(user.uid)) + return; + var queue_pos = MP.findPosInWaitlist(); var mention = ''; @@ -4256,7 +4308,7 @@