src/favourites.js

v1.18.x
barisusakli 10 years ago
parent 9a16a359a8
commit bd388f727c

@ -1,3 +1,5 @@
"use strict";
var async = require('async'), var async = require('async'),
winston = require('winston'), winston = require('winston'),
db = require('./database'), db = require('./database'),
@ -7,7 +9,6 @@ var async = require('async'),
meta = require('./meta'); meta = require('./meta');
(function (Favourites) { (function (Favourites) {
"use strict";
function vote(type, unvote, pid, uid, callback) { function vote(type, unvote, pid, uid, callback) {
uid = parseInt(uid, 10); uid = parseInt(uid, 10);
@ -239,54 +240,56 @@ var async = require('async'),
}; };
function toggleFavourite(type, pid, uid, callback) { function toggleFavourite(type, pid, uid, callback) {
if (uid === 0) { if (!parseInt(uid, 10)) {
return callback(new Error('[[error:not-logged-in]]')); return callback(new Error('[[error:not-logged-in]]'));
} }
var isFavouriting = type === 'favourite'; var isFavouriting = type === 'favourite';
posts.getPostFields(pid, ['pid', 'uid', 'timestamp'], function (err, postData) {
async.parallel({
postData: function(next) {
posts.getPostFields(pid, ['pid', 'uid'], next);
},
hasFavourited: function(next) {
Favourites.hasFavourited(pid, uid, next);
}
}, function(err, results) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
Favourites.hasFavourited(pid, uid, function (err, hasFavourited) { if (isFavouriting && results.hasFavourited) {
if (err) { return callback(new Error('[[error:already-favourited]]'));
return callback(err); }
}
if (isFavouriting && hasFavourited) {
return callback(new Error('[[error:already-favourited]]'));
}
if (!isFavouriting && !hasFavourited) {
return callback(new Error('[[error:alrady-unfavourited]]'));
}
if (isFavouriting) {
db.sortedSetAdd('uid:' + uid + ':favourites', postData.timestamp, pid);
} else {
db.sortedSetRemove('uid:' + uid + ':favourites', pid);
}
if (!isFavouriting && !results.hasFavourited) {
return callback(new Error('[[error:alrady-unfavourited]]'));
}
db[isFavouriting ? 'setAdd' : 'setRemove']('pid:' + pid + ':users_favourited', uid, function(err) { async.waterfall([
if (err) { function(next) {
return callback(err); if (isFavouriting) {
db.sortedSetAdd('uid:' + uid + ':favourites', Date.now(), pid, next);
} else {
db.sortedSetRemove('uid:' + uid + ':favourites', pid, next);
} }
},
db.setCount('pid:' + pid + ':users_favourited', function(err, count) { function(next) {
if (err) { db[isFavouriting ? 'setAdd' : 'setRemove']('pid:' + pid + ':users_favourited', uid, next);
return callback(err); },
} function(next) {
postData.reputation = count; db.setCount('pid:' + pid + ':users_favourited', next);
posts.setPostField(pid, 'reputation', count, function(err) { },
callback(err, { function(count, next) {
post: postData, results.postData.reputation = count;
isFavourited: isFavouriting posts.setPostField(pid, 'reputation', count, next);
}); },
}); function(next) {
next(null, {
post: results.postData,
isFavourited: isFavouriting
}); });
}); }
}); ], callback);
}); });
} }
@ -310,13 +313,6 @@ var async = require('async'),
db.isMemberOfSets(sets, uid, callback); db.isMemberOfSets(sets, uid, callback);
}; };
Favourites.getFavouritedUidsByPids = function(pids, callback) {
var sets = pids.map(function(pid) {
return 'pid:' + pid + ':users_favourited';
});
db.getSetsMembers(sets, callback);
};
Favourites.getUpvotedUidsByPids = function(pids, callback) { Favourites.getUpvotedUidsByPids = function(pids, callback) {
var sets = pids.map(function(pid) { var sets = pids.map(function(pid) {
return 'pid:' + pid + ':upvote'; return 'pid:' + pid + ':upvote';

Loading…
Cancel
Save