This commit is contained in:
Barış Soner Uşaklı
2015-02-19 15:56:04 -05:00
parent 4ba6d44858
commit 743cd20441
10 changed files with 229 additions and 85 deletions

View File

@@ -29,7 +29,7 @@ module.exports = function(Posts) {
removeFromCategoryRecentPosts(pid, postData.tid, next);
},
function(next) {
db.sortedSetRemove('posts:flagged', pid, next);
Posts.dismissFlags(pid, next);
}
], function(err) {
callback(err, postData);
@@ -125,6 +125,9 @@ module.exports = function(Posts) {
},
function(next) {
db.sortedSetsRemove(['posts:pid', 'posts:flagged'], pid, next);
},
function(next) {
Posts.dismissFlags(pid, next);
}
], function(err) {
if (err) {

View File

@@ -3,22 +3,52 @@
'use strict';
var async = require('async'),
db = require('../database');
db = require('../database'),
user = require('../user');
module.exports = function(Posts) {
Posts.flag = function(pid, callback) {
Posts.exists(pid, function(err, exists) {
if (err || !exists) {
Posts.flag = function(post, uid, callback) {
async.parallel({
hasFlagged: async.apply(hasFlagged, post.pid, uid),
exists: async.apply(Posts.exists, post.pid)
}, function(err, results) {
if (err || !results.exists) {
return callback(err || new Error('[[error:no-post]]'));
}
if (results.hasFlagged) {
return callback(new Error('[[error:already-flagged]]'));
}
var now = Date.now();
async.parallel([
function(next) {
db.sortedSetAdd('posts:flagged', Date.now(), pid, next);
db.sortedSetAdd('posts:flagged', now, post.pid, next);
},
function(next) {
db.incrObjectField('post:' + pid, 'flags', next);
db.sortedSetIncrBy('posts:flags:count', 1, post.pid, next);
},
function(next) {
db.incrObjectField('post:' + post.pid, 'flags', next);
},
function(next) {
db.sortedSetAdd('pid:' + post.pid + ':flag:uids', now, uid, next);
},
function(next) {
if (parseInt(post.uid, 10)) {
db.sortedSetAdd('uid:' + post.uid + ':flag:pids', now, post.pid, next);
} else {
next();
}
},
function(next) {
if (parseInt(post.uid, 10)) {
db.setAdd('uid:' + post.uid + ':flagged_by', uid, next);
} else {
next();
}
}
], function(err, results) {
callback(err);
@@ -26,14 +56,31 @@ module.exports = function(Posts) {
});
};
function hasFlagged(pid, uid, callback) {
db.isSortedSetMember('pid:' + pid + ':flag:uids', uid, callback);
}
Posts.dismissFlag = function(pid, callback) {
async.parallel([
function(next) {
db.sortedSetRemove('posts:flagged', pid, next);
db.getObjectField('post:' + pid, 'uid', function(err, uid) {
if (err) {
return next(err);
}
db.sortedSetsRemove([
'posts:flagged',
'posts:flags:count',
'uid:' + uid + ':flag:pids'
], pid, next);
});
},
function(next) {
db.deleteObjectField('post:' + pid, 'flags', next);
}
},
function(next) {
db.delete('pid:' + pid + ':flag:uids', next);
}
], function(err, results) {
callback(err);
});
@@ -43,8 +90,8 @@ module.exports = function(Posts) {
db.delete('posts:flagged', callback);
};
Posts.getFlags = function(uid, start, end, callback) {
db.getSortedSetRevRange('posts:flagged', start, end, function(err, pids) {
Posts.getFlags = function(set, uid, start, end, callback) {
db.getSortedSetRevRange(set, start, end, function(err, pids) {
if (err) {
return callback(err);
}
@@ -52,4 +99,29 @@ module.exports = function(Posts) {
Posts.getPostSummaryByPids(pids, uid, {stripTags: false, extraFields: ['flags']}, callback);
});
};
Posts.getUserFlags = function(byUsername, sortBy, callerUID, start, end, callback) {
async.waterfall([
function(next) {
user.getUidByUsername(byUsername, next);
},
function(uid, next) {
if (!uid) {
return next(null, []);
}
db.getSortedSetRevRange('uid:' + uid + ':flag:pids', 0, -1, next);
},
function(pids, next) {
Posts.getPostSummaryByPids(pids, callerUID, {stripTags: false, extraFields: ['flags']}, next);
},
function(posts, next) {
if (sortBy === 'count') {
posts.sort(function(a, b) {
return b.flags - a.flags;
});
}
next(null, posts.slice(start, end));
}
], callback);
};
};