v1.18.x
Baris Soner Usakli 11 years ago
parent eb3b240b04
commit de34beaf6d

@ -48,18 +48,22 @@ var db = require('./database'),
}); });
}; };
Categories.getCategoryById = function(category_id, start, end, current_user, callback) { Categories.getCategoryById = function(cid, start, end, uid, callback) {
if(parseInt(uid, 10)) {
Categories.markAsRead(cid, uid);
}
function getCategoryData(next) { function getCategoryData(next) {
Categories.getCategoryData(category_id, next); Categories.getCategoryData(cid, next);
} }
function getTopics(next) { function getTopics(next) {
Categories.getCategoryTopics(category_id, start, end, current_user, next); Categories.getCategoryTopics(cid, start, end, uid, next);
} }
function getPageCount(next) { function getPageCount(next) {
Categories.getPageCount(category_id, current_user, next); Categories.getPageCount(cid, uid, next);
} }
async.parallel({ async.parallel({
@ -77,7 +81,7 @@ var db = require('./database'),
'link': results.category.link, 'link': results.category.link,
'disabled': results.category.disabled, 'disabled': results.category.disabled,
'topic_row_size': 'col-md-9', 'topic_row_size': 'col-md-9',
'category_id': category_id, 'category_id': cid,
'topics': results.topics.topics, 'topics': results.topics.topics,
'nextStart': results.topics.nextStart, 'nextStart': results.topics.nextStart,
'pageCount': results.pageCount, 'pageCount': results.pageCount,
@ -169,29 +173,6 @@ var db = require('./database'),
}); });
}; };
Categories.isTopicsRead = function(cid, uid, callback) {
db.getSortedSetRange('categories:' + cid + ':tid', 0, -1, function(err, tids) {
if(err) {
return callback(err);
}
topics.hasReadTopics(tids, uid, function(err, hasRead) {
if(err) {
return callback(err);
}
var allread = true;
for (var i = 0, ii = tids.length; i < ii; i++) {
if (hasRead[i] === 0) {
allread = false;
break;
}
}
callback(allread);
});
});
};
Categories.markAsRead = function(cid, uid) { Categories.markAsRead = function(cid, uid) {
db.setAdd('cid:' + cid + ':read_by_uid', uid); db.setAdd('cid:' + cid + ':read_by_uid', uid);
}; };

@ -614,11 +614,14 @@
max = (args[1] === '+inf')?Number.MAX_VALUE:args[1], max = (args[1] === '+inf')?Number.MAX_VALUE:args[1],
min = args[2], min = args[2],
start = args[4], start = args[4],
stop = args[5]; count = args[5];
if(parseInt(count, 10) === -1) {
count = 0;
}
db.collection('objects').find({_key:key, score: {$gte:min, $lte:max}}, {fields:{value:1}}) db.collection('objects').find({_key:key, score: {$gte:min, $lte:max}}, {fields:{value:1}})
.limit(stop - start + 1) .limit(count)
.skip(start) .skip(start)
.sort({score: -1}) .sort({score: -1})
.toArray(function(err, data) { .toArray(function(err, data) {

@ -232,7 +232,14 @@ var path = require('path'),
end = start + settings.topicsPerPage - 1; end = start + settings.topicsPerPage - 1;
categoryTools.privileges(req.params.id, uid, function(err, privileges) { categoryTools.privileges(req.params.id, uid, function(err, privileges) {
if (!err && privileges.read) { if (err) {
return next(err);
}
if (!privileges.read) {
return res.send(403);
}
categories.getCategoryById(req.params.id, start, end, uid, function (err, data) { categories.getCategoryById(req.params.id, start, end, uid, function (err, data) {
if(err) { if(err) {
return next(err); return next(err);
@ -246,10 +253,7 @@ var path = require('path'),
} else { } else {
next(); next();
} }
}, req.params.id, uid); });
} else {
res.send(403);
}
}); });
}); });
}); });

@ -460,10 +460,7 @@ var async = require('async'),
}); });
} }
Topics.getLatestTopics = function(current_user, start, end, term, callback) { Topics.getLatestTids = function(start, end, term, callback) {
var timestamp = Date.now();
var terms = { var terms = {
day: 86400000, day: 86400000,
week: 604800000, week: 604800000,
@ -475,13 +472,17 @@ var async = require('async'),
since = terms[term]; since = terms[term];
} }
var args = ['topics:recent', '+inf', timestamp - since, 'LIMIT', start, end - start + 1]; var count = parseInt(end, 10) === -1 ? end : end - start + 1;
db.getSortedSetRevRangeByScore(args, function(err, tids) {
db.getSortedSetRevRangeByScore(['topics:recent', '+inf', Date.now() - since, 'LIMIT', start, count], callback);
};
Topics.getLatestTopics = function(uid, start, end, term, callback) {
Topics.getLatestTids(start, end, term, function(err, tids) {
if(err) { if(err) {
return callback(err); return callback(err);
} }
getTopics('topics:recent', uid, tids, callback);
getTopics('topics:recent', current_user, tids, callback);
}); });
}; };
@ -496,75 +497,33 @@ var async = require('async'),
}; };
Topics.getTotalUnread = function(uid, callback) { Topics.getTotalUnread = function(uid, callback) {
Topics.getUnreadTids(uid, 0, 21, function(err, tids) {
var unreadTids = [], callback(err, {count: tids ? tids.length : 0});
start = 0,
stop = 21,
done = false;
async.whilst(
function() {
return unreadTids.length < 21 && !done;
},
function(callback) {
db.getSortedSetRevRange('topics:recent', start, stop, function(err, tids) {
if (err) {
return callback(err);
}
if (tids && !tids.length) {
done = true;
return callback(null);
}
Topics.hasReadTopics(tids, uid, function(err, read) {
if(err) {
return callback(err);
}
var newtids = tids.filter(function(tid, index, self) {
return read[index] === 0;
}); });
unreadTids.push.apply(unreadTids, newtids);
start = stop + 1;
stop = start + 21;
callback(null);
});
});
},
function(err) {
callback(null, {
count: unreadTids.length
});
}
);
}; };
Topics.getUnreadTids = function(uid, start, stop, callback) { Topics.getUnreadTids = function(uid, start, stop, callback) {
var unreadTids = [], var unreadTids = [],
done = false; done = false;
function continueCondition() { uid = parseInt(uid, 10);
return unreadTids.length < 20 && !done; if(uid === 0) {
return callback(null, unreadTids);
} }
async.whilst(continueCondition, function(callback) { async.whilst(function() {
db.getSortedSetRevRange('topics:recent', start, stop, function(err, tids) { return unreadTids.length < 20 && !done;
}, function(callback) {
Topics.getLatestTids(start, stop, 'month', function(err, tids) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
if (tids && !tids.length) { if (tids && !tids.length) {
done = true; done = true;
return callback(null); return callback();
} }
if (uid === 0) {
unreadTids.push.apply(unreadTids, tids);
callback(null);
} else {
Topics.hasReadTopics(tids, uid, function(err, read) { Topics.hasReadTopics(tids, uid, function(err, read) {
if(err) { if(err) {
return callback(err); return callback(err);
@ -573,7 +532,6 @@ var async = require('async'),
return parseInt(read[index], 10) === 0; return parseInt(read[index], 10) === 0;
}); });
async.filter(newtids, function(tid, next) { async.filter(newtids, function(tid, next) {
threadTools.privileges(tid, uid, function(err, privileges) { threadTools.privileges(tid, uid, function(err, privileges) {
next(!err && privileges.read); next(!err && privileges.read);
@ -581,15 +539,12 @@ var async = require('async'),
}, function(newtids) { }, function(newtids) {
unreadTids.push.apply(unreadTids, newtids); unreadTids.push.apply(unreadTids, newtids);
if(continueCondition()) {
start = stop + 1; start = stop + 1;
stop = start + 19; stop = start + 19;
}
callback(null); callback();
}); });
}); });
}
}); });
}, function(err) { }, function(err) {
callback(err, unreadTids); callback(err, unreadTids);
@ -598,7 +553,6 @@ var async = require('async'),
Topics.getUnreadTopics = function(uid, start, stop, callback) { Topics.getUnreadTopics = function(uid, start, stop, callback) {
var unreadTopics = { var unreadTopics = {
'show_sidebar': 'hidden',
'show_markallread_button': 'show', 'show_markallread_button': 'show',
'no_topics_message': 'hidden', 'no_topics_message': 'hidden',
'topics': [] 'topics': []
@ -915,13 +869,13 @@ var async = require('async'),
}; };
Topics.markAllRead = function(uid, callback) { Topics.markAllRead = function(uid, callback) {
db.getSortedSetRange('topics:tid', 0, -1, function(err, tids) { Topics.getLatestTids(0, -1, 'month', function(err, tids) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
if(!tids || !tids.length) { if(!tids || !tids.length) {
return callback(null); return callback();
} }
function markRead(tid, next) { function markRead(tid, next) {
@ -958,12 +912,7 @@ var async = require('async'),
}); });
Topics.getTopicField(tid, 'cid', function(err, cid) { Topics.getTopicField(tid, 'cid', function(err, cid) {
categories.isTopicsRead(cid, uid, function(read) {
if (read) {
categories.markAsRead(cid, uid); categories.markAsRead(cid, uid);
}
});
}); });
user.notifications.getUnreadByUniqueId(uid, 'topic:' + tid, function(err, nids) { user.notifications.getUnreadByUniqueId(uid, 'topic:' + tid, function(err, nids) {

@ -741,7 +741,6 @@ module.exports.server = server;
end = start + settings.topicsPerPage - 1; end = start + settings.topicsPerPage - 1;
categories.getCategoryById(cid, start, end, 0, function (err, categoryData) { categories.getCategoryById(cid, start, end, 0, function (err, categoryData) {
if (categoryData) { if (categoryData) {
if (parseInt(categoryData.disabled, 10) === 1) { if (parseInt(categoryData.disabled, 10) === 1) {
return next(new Error('Category disabled'), null); return next(new Error('Category disabled'), null);

Loading…
Cancel
Save