|
|
@ -8,74 +8,109 @@ var async = require('async'),
|
|
|
|
(function (Favourites) {
|
|
|
|
(function (Favourites) {
|
|
|
|
"use strict";
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
|
|
function vote(type, postData, pid, room_id, uid, socket) {
|
|
|
|
function vote(type, unvote, pid, room_id, uid, socket, callback) {
|
|
|
|
var websockets = require('./socket.io');
|
|
|
|
var websockets = require('./socket.io');
|
|
|
|
|
|
|
|
|
|
|
|
if (uid === 0) {
|
|
|
|
if (uid === 0) {
|
|
|
|
return socket.emit('event:alert', {
|
|
|
|
return socket.emit('event:alert', {
|
|
|
|
alert_id: 'post_vote',
|
|
|
|
alert_id: 'post_vote',
|
|
|
|
title: '[[vote.not_logged_in.title]]',
|
|
|
|
title: '[[topic:vote.not_logged_in.title]]',
|
|
|
|
message: '[[vote.not_logged_in.message]]',
|
|
|
|
message: '[[topic:vote.not_logged_in.message]]',
|
|
|
|
type: 'danger',
|
|
|
|
|
|
|
|
timeout: 5000
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else if (uid === postData.uid) {
|
|
|
|
|
|
|
|
return socket.emit('event:alert', {
|
|
|
|
|
|
|
|
alert_id: 'post_vote',
|
|
|
|
|
|
|
|
title: '[[vote.cant_vote_self.title]]',
|
|
|
|
|
|
|
|
message: '[[vote.cant_vote_self.message]]',
|
|
|
|
|
|
|
|
type: 'danger',
|
|
|
|
type: 'danger',
|
|
|
|
timeout: 5000
|
|
|
|
timeout: 5000
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Favourites.hasVoted(type, pid, uid, function (err, hasVoted) {
|
|
|
|
posts.getPostFields(pid, ['uid', 'timestamp'], function (err, postData) {
|
|
|
|
// if (!hasVoted) {
|
|
|
|
if (uid === parseInt(postData.uid, 10)) {
|
|
|
|
var notType = (type === 'upvote' ? 'downvote' : 'upvote');
|
|
|
|
socket.emit('event:alert', {
|
|
|
|
|
|
|
|
alert_id: 'post_vote',
|
|
|
|
|
|
|
|
title: '[[topic:vote.cant_vote_self.title]]',
|
|
|
|
|
|
|
|
message: '[[topic:vote.cant_vote_self.message]]',
|
|
|
|
|
|
|
|
type: 'danger',
|
|
|
|
|
|
|
|
timeout: 5000
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
db[type === 'upvote' ? 'sortedSetAdd' : 'sortedSetRemove']('uid:' + uid + ':upvote', postData.timestamp, pid);
|
|
|
|
if (callback) {
|
|
|
|
db[type === 'upvote' ? 'sortedSetRemove' : 'sortedSetAdd']('uid:' + uid + ':downvote', postData.timestamp, pid);
|
|
|
|
callback(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
user[type === 'upvote' ? 'incrementUserFieldBy' : 'decrementUserFieldBy'](postData.uid, 'reputation', 1, function (err, newreputation) {
|
|
|
|
db[type === 'upvote' || !unvote ? 'sortedSetAdd' : 'sortedSetRemove']('uid:' + uid + ':upvote', postData.timestamp, pid);
|
|
|
|
db.sortedSetAdd('users:reputation', newreputation, postData.uid);
|
|
|
|
db[type === 'upvote' || unvote ? 'sortedSetRemove' : 'sortedSetAdd']('uid:' + uid + ':downvote', postData.timestamp, pid);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.setAdd('pid:' + pid + ':' + type, uid, function(err) {
|
|
|
|
user[type === 'upvote' ? 'incrementUserFieldBy' : 'decrementUserFieldBy'](postData.uid, 'reputation', 1, function (err, newreputation) {
|
|
|
|
db.setCount('pid:' + pid + ':' + type, function(err, count) {
|
|
|
|
db.sortedSetAdd('users:reputation', newreputation, postData.uid);
|
|
|
|
posts.setPostField(pid, type, count);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.setRemove('pid:' + pid + ':' + notType, uid, function(err) {
|
|
|
|
if (room_id) {
|
|
|
|
db.setCount('pid:' + pid + ':' + notType, function(err, count) {
|
|
|
|
websockets.in(room_id).emit('event:' + (type === 'upvote' ? 'rep_up' : 'rep_down'), {
|
|
|
|
posts.setPostField(pid, notType, count);
|
|
|
|
uid: postData.uid,
|
|
|
|
});
|
|
|
|
pid: pid
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (room_id) {
|
|
|
|
socket.emit('posts.' + (unvote ? 'unvote' : type), {
|
|
|
|
websockets.in(room_id).emit('event:' + (type === 'upvote' ? 'rep_up' : 'rep_down'), {
|
|
|
|
pid: pid
|
|
|
|
uid: postData.uid,
|
|
|
|
});
|
|
|
|
pid: pid
|
|
|
|
|
|
|
|
});
|
|
|
|
adjustPostVotes(pid, uid, type, unvote, function() {
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
socket.emit('posts.' + type, {
|
|
|
|
function adjustPostVotes(pid, uid, type, unvote, callback) {
|
|
|
|
pid: pid
|
|
|
|
var notType = (type === 'upvote' ? 'downvote' : 'upvote');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async.series([
|
|
|
|
|
|
|
|
function(next) {
|
|
|
|
|
|
|
|
if (unvote) {
|
|
|
|
|
|
|
|
db.setRemove('pid:' + pid + ':' + type, uid, function(err) {
|
|
|
|
|
|
|
|
next(err);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
db.setAdd('pid:' + pid + ':' + type, uid, function(err) {
|
|
|
|
|
|
|
|
next(err);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
function(next) {
|
|
|
|
|
|
|
|
db.setRemove('pid:' + pid + ':' + notType, uid, function(err) {
|
|
|
|
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
//});
|
|
|
|
], function(err) {
|
|
|
|
|
|
|
|
async.parallel({
|
|
|
|
|
|
|
|
upvotes: function(next) {
|
|
|
|
|
|
|
|
db.setCount('pid:' + pid + ':upvote', next);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
downvotes: function(next) {
|
|
|
|
|
|
|
|
db.setCount('pid:' + pid + ':downvote', next);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}, function(err, results) {
|
|
|
|
|
|
|
|
posts.setPostField(pid, 'votes', parseInt(results.upvotes, 10) - parseInt(results.downvotes, 10));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Favourites.upvote = function(pid, room_id, uid, socket) {
|
|
|
|
Favourites.upvote = function(pid, room_id, uid, socket) {
|
|
|
|
Favourites.unvote(pid, room_id, uid, socket, function(err, postData) {
|
|
|
|
Favourites.unvote(pid, room_id, uid, socket, function(err) {
|
|
|
|
vote('upvote', postData, pid, room_id, uid, socket);
|
|
|
|
vote('upvote', false, pid, room_id, uid, socket);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Favourites.downvote = function(pid, room_id, uid, socket) {
|
|
|
|
Favourites.downvote = function(pid, room_id, uid, socket) {
|
|
|
|
Favourites.unvote(pid, room_id, uid, socket, function(err, postData) {
|
|
|
|
Favourites.unvote(pid, room_id, uid, socket, function(err) {
|
|
|
|
vote('downvote', postData, pid, room_id, uid, socket);
|
|
|
|
vote('downvote', false, pid, room_id, uid, socket);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -83,79 +118,49 @@ var async = require('async'),
|
|
|
|
var websockets = require('./socket.io');
|
|
|
|
var websockets = require('./socket.io');
|
|
|
|
|
|
|
|
|
|
|
|
Favourites.hasVoted(pid, uid, function(err, voteStatus) {
|
|
|
|
Favourites.hasVoted(pid, uid, function(err, voteStatus) {
|
|
|
|
posts.getPostFields(pid, ['uid', 'timestamp'], function (err, postData) {
|
|
|
|
if (voteStatus.upvoted || voteStatus.downvoted) {
|
|
|
|
if (voteStatus === 'upvoted') {
|
|
|
|
socket.emit('posts.unvote', {
|
|
|
|
db.sortedSetRemove('uid:' + uid + ':upvote');
|
|
|
|
pid: pid
|
|
|
|
|
|
|
|
});
|
|
|
|
db.setRemove('pid:' + pid + ':upvote', uid, function(err) {
|
|
|
|
|
|
|
|
db.setCount('pid:' + pid + ':upvote', function(err, count) {
|
|
|
|
|
|
|
|
posts.setPostField(pid, 'upvote', count);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
user.decrementUserFieldBy(postData.uid, 'reputation', 1, function (err, newreputation) {
|
|
|
|
|
|
|
|
db.sortedSetAdd('users:reputation', newreputation, postData.uid);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (room_id) {
|
|
|
|
|
|
|
|
websockets.in(room_id).emit('event:rep_down', {
|
|
|
|
|
|
|
|
uid: postData.uid,
|
|
|
|
|
|
|
|
pid: pid
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (voteStatus === 'downvoted') {
|
|
|
|
|
|
|
|
db.sortedSetRemove('uid:' + uid + ':downvote');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.setRemove('pid:' + pid + ':downvote', uid, function(err) {
|
|
|
|
|
|
|
|
db.setCount('pid:' + pid + ':downvote', function(err, count) {
|
|
|
|
|
|
|
|
posts.setPostField(pid, 'downvote', count);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
user.incrementUserFieldBy(postData.uid, 'reputation', 1, function (err, newreputation) {
|
|
|
|
|
|
|
|
db.sortedSetAdd('users:reputation', newreputation, postData.uid);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (room_id) {
|
|
|
|
return vote(voteStatus.upvoted ? 'downvote' : 'upvote', true, pid, room_id, uid, socket, function() {
|
|
|
|
websockets.in(room_id).emit('event:rep_up', {
|
|
|
|
if (callback) {
|
|
|
|
uid: postData.uid,
|
|
|
|
callback(err);
|
|
|
|
pid: pid
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
if (voteStatus) {
|
|
|
|
|
|
|
|
socket.emit('posts.unvote', {
|
|
|
|
|
|
|
|
pid: pid
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
if (callback) {
|
|
|
|
callback(err, postData);
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Favourites.hasVoted = function(pid, uid, callback) {
|
|
|
|
Favourites.hasVoted = function(pid, uid, callback) {
|
|
|
|
async.parallel({
|
|
|
|
async.parallel({
|
|
|
|
upvoted: function(each) {
|
|
|
|
upvoted: function(next) {
|
|
|
|
db.isSetMember('pid:' + pid + ':upvote', uid, each);
|
|
|
|
db.isSetMember('pid:' + pid + ':upvote', uid, next);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
downvoted: function(each) {
|
|
|
|
downvoted: function(next) {
|
|
|
|
db.isSetMember('pid:' + pid + ':downvote', uid, each);
|
|
|
|
db.isSetMember('pid:' + pid + ':downvote', uid, next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, function(err, results) {
|
|
|
|
}, function(err, results) {
|
|
|
|
var voteStatus = "";
|
|
|
|
callback(err, results)
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (results.upvoted) {
|
|
|
|
Favourites.getVoteStatusByPostIDs = function(pids, uid, callback) {
|
|
|
|
voteStatus = "upvoted";
|
|
|
|
var data = {};
|
|
|
|
} else if (results.downvoted) {
|
|
|
|
|
|
|
|
voteStatus = "downvoted";
|
|
|
|
function iterator(pid, next) {
|
|
|
|
}
|
|
|
|
Favourites.hasVoted(pid, uid, function(err, voteStatus) {
|
|
|
|
|
|
|
|
data[pid] = voteStatus;
|
|
|
|
|
|
|
|
next()
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
callback(err, voteStatus)
|
|
|
|
async.each(pids, iterator, function(err) {
|
|
|
|
|
|
|
|
callback(data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|