Skip to content

Commit

Permalink
Merged branch feature-user_block into develop
Browse files Browse the repository at this point in the history
Fixes #61
  • Loading branch information
DerpgonCz committed Jun 27, 2016
2 parents 250f470 + 3556dbb commit c11998f
Show file tree
Hide file tree
Showing 4 changed files with 260 additions and 8 deletions.
34 changes: 28 additions & 6 deletions socketserver/db_mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ var MysqlDB = function(){
`time` DATETIME\
);\
\
CREATE TABLE IF NOT EXISTS `user_blocks` (\
`from` INTEGER UNSIGNED 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);
Expand Down Expand Up @@ -476,12 +482,16 @@ MysqlDB.prototype.getUserNoLogin = function(uid, callback){
un: res.un,
uid: res.id,
salt: res.salt,
blocked: [],
}

that.execute("SELECT `id` FROM `playlists` WHERE `owner` = ?;", [ uid ], function(err, res) {
that.execute("SELECT `id` FROM `playlists` WHERE ?; SELECT `to` FROM `user_blocks` WHERE ?", [ { owner: uid, }, { from: uid,} ], function(err, res) {

for(var ind in res)
data.playlists.push(res[ind].id);
for(var ind in res[0])
data.playlists.push(res[0][ind].id);

for(var ind in res[1])
data.blocked.push(res[1][ind].to);

callback(null, data);
});
Expand Down Expand Up @@ -634,11 +644,15 @@ MysqlDB.prototype.loginUser = function(obj, callback) {
};

MysqlDB.prototype.putUser = function(email, data, callback) {
var that = this;

callback = callback || function(){};

var newData = {};
util._extend(newData, data);

var blocked = newData.blocked.map(function(x) { return [data.uid, x]; });

newData.badge_bottom = newData.badge.bottom;
newData.badge_top = newData.badge.top;
newData.recovery_timeout = new Date(newData.recovery.timeout);
Expand All @@ -649,9 +663,17 @@ MysqlDB.prototype.putUser = function(email, data, callback) {
delete newData.uid;
delete newData.badge;
delete newData.playlists;
this.execute("INSERT INTO `users`(??) VALUES(?) ON DUPLICATE KEY UPDATE ?;", [ Object.keys(newData), _.values(newData), newData ], function(err, res) {
if(err) callback(err);
else callback(null, res.insertId);
delete newData.blocked;

this.execute("INSERT INTO `users`(??) VALUES(?) ON DUPLICATE KEY UPDATE ?; DELETE FROM `user_blocks` WHERE ?;", [ Object.keys(newData), _.values(newData), newData, { from: data.uid } ], function(err, res) {
if(err)
callback(err);
else
callback(null, res.insertId);

if(blocked.length) {
that.execute("INSERT INTO `user_blocks`(??) VALUES ?;", [ [ 'from', 'to' ], blocked ]);
}
});
};

Expand Down
68 changes: 68 additions & 0 deletions socketserver/socketserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3078,6 +3078,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) || uid < 0){
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;
}
});
});
Expand Down
41 changes: 41 additions & 0 deletions socketserver/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var defaultObj = function(){
temp_uptime: Date.now(),
lastdj: false,
salt: '',
blocked: [],
};
};

Expand All @@ -39,6 +40,7 @@ var fieldsNotSent = [
'created',
'lastdj',
'salt',
'blocked',
];

// These fields (key from defaultObj) are not saved in the db
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
Loading

0 comments on commit c11998f

Please sign in to comment.