|
|
|
@ -2,9 +2,10 @@ var RDB = require('./redis.js'),
|
|
|
|
|
posts = require('./posts.js'),
|
|
|
|
|
user = require('./user.js');
|
|
|
|
|
|
|
|
|
|
(function(Favourites) {
|
|
|
|
|
(function (Favourites) {
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
Favourites.favourite = function(pid, room_id, uid, socket) {
|
|
|
|
|
Favourites.favourite = function (pid, room_id, uid, socket) {
|
|
|
|
|
if (uid === 0) {
|
|
|
|
|
socket.emit('event:alert', {
|
|
|
|
|
alert_id: 'post_favourite',
|
|
|
|
@ -16,9 +17,9 @@ var RDB = require('./redis.js'),
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
posts.getPostFields(pid, ['uid', 'timestamp'], function(postData) {
|
|
|
|
|
posts.getPostFields(pid, ['uid', 'timestamp'], function (postData) {
|
|
|
|
|
|
|
|
|
|
Favourites.hasFavourited(pid, uid, function(hasFavourited) {
|
|
|
|
|
Favourites.hasFavourited(pid, uid, function (hasFavourited) {
|
|
|
|
|
if (hasFavourited === 0) {
|
|
|
|
|
RDB.sadd('pid:' + pid + ':users_favourited', uid);
|
|
|
|
|
RDB.zadd('uid:' + uid + ':favourites', postData.timestamp, pid);
|
|
|
|
@ -26,7 +27,7 @@ var RDB = require('./redis.js'),
|
|
|
|
|
RDB.hincrby('post:' + pid, 'reputation', 1);
|
|
|
|
|
|
|
|
|
|
if (uid !== postData.uid) {
|
|
|
|
|
user.incrementUserFieldBy(postData.uid, 'reputation', 1, function(err, newreputation) {
|
|
|
|
|
user.incrementUserFieldBy(postData.uid, 'reputation', 1, function (err, newreputation) {
|
|
|
|
|
RDB.zadd('users:reputation', newreputation, postData.uid);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
@ -47,7 +48,7 @@ var RDB = require('./redis.js'),
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Favourites.unfavourite = function(pid, room_id, uid, socket) {
|
|
|
|
|
Favourites.unfavourite = function (pid, room_id, uid, socket) {
|
|
|
|
|
if (uid === 0) {
|
|
|
|
|
socket.emit('event:alert', {
|
|
|
|
|
alert_id: 'post_favourite',
|
|
|
|
@ -59,8 +60,8 @@ var RDB = require('./redis.js'),
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
posts.getPostField(pid, 'uid', function(uid_of_poster) {
|
|
|
|
|
Favourites.hasFavourited(pid, uid, function(hasFavourited) {
|
|
|
|
|
posts.getPostField(pid, 'uid', function (uid_of_poster) {
|
|
|
|
|
Favourites.hasFavourited(pid, uid, function (hasFavourited) {
|
|
|
|
|
if (hasFavourited === 1) {
|
|
|
|
|
RDB.srem('pid:' + pid + ':users_favourited', uid);
|
|
|
|
|
RDB.zrem('uid:' + uid + ':favourites', pid);
|
|
|
|
@ -68,7 +69,7 @@ var RDB = require('./redis.js'),
|
|
|
|
|
RDB.hincrby('post:' + pid, 'reputation', -1);
|
|
|
|
|
|
|
|
|
|
if (uid !== uid_of_poster) {
|
|
|
|
|
user.incrementUserFieldBy(uid_of_poster, 'reputation', -1, function(err, newreputation) {
|
|
|
|
|
user.incrementUserFieldBy(uid_of_poster, 'reputation', -1, function (err, newreputation) {
|
|
|
|
|
RDB.zadd('users:reputation', newreputation, uid_of_poster);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
@ -89,26 +90,27 @@ var RDB = require('./redis.js'),
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Favourites.hasFavourited = function(pid, uid, callback) {
|
|
|
|
|
RDB.sismember('pid:' + pid + ':users_favourited', uid, function(err, hasFavourited) {
|
|
|
|
|
Favourites.hasFavourited = function (pid, uid, callback) {
|
|
|
|
|
RDB.sismember('pid:' + pid + ':users_favourited', uid, function (err, hasFavourited) {
|
|
|
|
|
RDB.handle(err);
|
|
|
|
|
|
|
|
|
|
callback(hasFavourited);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Favourites.getFavouritesByPostIDs = function(pids, uid, callback) {
|
|
|
|
|
Favourites.getFavouritesByPostIDs = function (pids, uid, callback) {
|
|
|
|
|
var loaded = 0;
|
|
|
|
|
var data = {};
|
|
|
|
|
|
|
|
|
|
for (var i = 0, ii = pids.length; i < ii; i++) {
|
|
|
|
|
(function(post_id) {
|
|
|
|
|
Favourites.hasFavourited(post_id, uid, function(hasFavourited) {
|
|
|
|
|
(function (post_id) {
|
|
|
|
|
Favourites.hasFavourited(post_id, uid, function (hasFavourited) {
|
|
|
|
|
|
|
|
|
|
data[post_id] = hasFavourited;
|
|
|
|
|
loaded++;
|
|
|
|
|
if (loaded === pids.length)
|
|
|
|
|
if (loaded === pids.length) {
|
|
|
|
|
callback(data);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}(pids[i]));
|
|
|
|
|
}
|
|
|
|
|