refactored a bunch of methods so that you can pass in an author argument in querystring to see posts by just that author, #2355

v1.18.x
Julian Lam 10 years ago
parent 3272ca34ee
commit a12907512f

@ -67,26 +67,32 @@ var db = require('./database'),
db.isSortedSetMember('categories:cid', cid, callback);
};
Categories.getCategoryById = function(cid, start, end, uid, callback) {
Categories.getCategories([cid], uid, function(err, categories) {
Categories.getCategoryById = function(data, callback) {
Categories.getCategories([data.cid], data.uid, function(err, categories) {
if (err || !Array.isArray(categories) || !categories[0]) {
return callback(err || new Error('[[error:invalid-cid]]'));
}
var category = categories[0];
if (parseInt(uid, 10)) {
Categories.markAsRead([cid], uid);
if (parseInt(data.uid, 10)) {
Categories.markAsRead([data.cid], data.uid);
}
async.parallel({
topics: function(next) {
Categories.getCategoryTopics(cid, start, end, uid, next);
Categories.getCategoryTopics({
cid: data.cid,
start: data.start,
stop: data.end,
uid: data.uid,
targetUid: data.targetUid
}, next);
},
pageCount: function(next) {
Categories.getPageCount(cid, uid, next);
Categories.getPageCount(data.cid, data.uid, next);
},
isIgnored: function(next) {
Categories.isIgnored([cid], uid, next);
Categories.isIgnored([data.cid], data.uid, next);
}
}, function(err, results) {
if(err) {
@ -99,20 +105,20 @@ var db = require('./database'),
category.isIgnored = results.isIgnored[0];
category.topic_row_size = 'col-md-9';
plugins.fireHook('filter:category.get', category, uid, callback);
plugins.fireHook('filter:category.get', category, data.uid, callback);
});
});
};
Categories.getCategoryTopics = function(cid, start, stop, uid, callback) {
Categories.getCategoryTopics = function(data, callback) {
var tids;
async.waterfall([
function(next) {
Categories.getTopicIds(cid, start, stop, next);
Categories.getTopicIds(data.targetUid ? 'cid:' + data.cid + ':uid:' + data.targetUid + ':tid' : 'categories:' + data.cid + ':tid', data.start, data.stop, next);
},
function(topicIds, next) {
tids = topicIds;
topics.getTopicsByTids(tids, uid, next);
topics.getTopicsByTids(tids, data.uid, next);
},
function(topics, next) {
if (!Array.isArray(topics) || !topics.length) {
@ -125,7 +131,7 @@ var db = require('./database'),
var indices = {},
i = 0;
for(i=0; i<tids.length; ++i) {
indices[tids[i]] = start + i;
indices[tids[i]] = data.start + i;
}
for(i=0; i<topics.length; ++i) {
@ -134,7 +140,7 @@ var db = require('./database'),
next(null, {
topics: topics,
nextStart: stop + 1
nextStart: data.stop + 1
});
}
], callback);
@ -153,8 +159,8 @@ var db = require('./database'),
});
};
Categories.getTopicIds = function(cid, start, stop, callback) {
db.getSortedSetRevRange('categories:' + cid + ':tid', start, stop, callback);
Categories.getTopicIds = function(set, start, stop, callback) {
db.getSortedSetRevRange(set, start, stop, callback);
};
Categories.getTopicIndex = function(tid, callback) {

@ -101,7 +101,8 @@ categoriesController.unreadTotal = function(req, res, next) {
categoriesController.get = function(req, res, next) {
var cid = req.params.category_id,
page = req.query.page || 1,
uid = req.user ? req.user.uid : 0;
uid = req.user ? req.user.uid : 0,
userPrivileges;
async.waterfall([
function(next) {
@ -133,6 +134,7 @@ categoriesController.get = function(req, res, next) {
return categoriesController.notAllowed(req, res);
}
userPrivileges = results.privileges;
var settings = results.userSettings;
var topicIndex = 0;
@ -146,21 +148,37 @@ categoriesController.get = function(req, res, next) {
var start = (page - 1) * settings.topicsPerPage + topicIndex,
end = start + settings.topicsPerPage - 1;
categories.getCategoryById(cid, start, end, uid, function (err, categoryData) {
next(null, {
cid: cid,
start: start,
end: end,
uid: uid
});
},
function(payload, next) {
// If a userslug was specified, add a targetUid
if (req.query.author) {
user.getUidByUserslug(req.query.author, function(err, uid) {
payload.targetUid = uid;
next(err, payload);
});
} else {
next(null, payload);
}
},
categories.getCategoryById,
function(categoryData, uid, next) {
categories.getRecentTopicReplies(categoryData.children, uid, function(err) {
if (err) {
return next(err);
}
categories.getRecentTopicReplies(categoryData.children, uid, function(err) {
if (err) {
return next(err);
}
categoryData.privileges = results.privileges;
next(null, categoryData);
});
next(null, categoryData);
});
},
function (categoryData, next) {
categoryData.privileges = userPrivileges;
res.locals.metaTags = [
{
name: 'title',

@ -97,7 +97,12 @@ function generateForTopic(req, res, next) {
function generateForCategory(req, res, next) {
var cid = req.params.category_id;
var uid = req.user ? req.user.uid : 0;
categories.getCategoryById(cid, 0, 25, uid, function (err, categoryData) {
categories.getCategoryById({
cid: cid,
start: 0,
end: 25,
uid: uid
}, function (err, categoryData) {
if (err) {
return next(err);
}

@ -52,7 +52,12 @@ SocketCategories.loadMore = function(socket, data, callback) {
var start = parseInt(data.after, 10),
end = start + results.settings.topicsPerPage - 1;
categories.getCategoryTopics(data.cid, start, end, socket.uid, function(err, data) {
categories.getCategoryTopics({
cid: data.cid,
start: start,
end: end,
uid: socket.uid
}, function(err, data) {
if (err) {
return callback(err);
}

@ -383,7 +383,7 @@ SocketTopics.moveAll = function(socket, data, callback) {
return callback(err || new Error('[[error:no-privileges]]'));
}
categories.getTopicIds(data.currentCid, 0, -1, function(err, tids) {
categories.getTopicIds('categories:' + data.currentCid + ':tid', 0, -1, function(err, tids) {
if (err) {
return callback(err);
}

@ -62,7 +62,7 @@ module.exports = function(Topics) {
async.parallel([
function(next) {
db.sortedSetsAdd(['topics:tid', 'categories:' + cid + ':tid'], timestamp, tid, next);
db.sortedSetsAdd(['topics:tid', 'categories:' + cid + ':tid', 'cid:' + cid + ':uid:' + uid + ':tid'], timestamp, tid, next);
},
function(next) {
user.addTopicIdToUser(uid, tid, timestamp, next);

@ -31,7 +31,12 @@ describe('Categories', function() {
describe('.getCategoryById', function() {
it('should retrieve a newly created category by its ID', function(done) {
Categories.getCategoryById(categoryObj.cid, 0, -1, 0, function(err, categoryData) {
Categories.getCategoryById({
cid: categoryObj.cid,
start: 0,
end: -1,
uid: 0
}, function(err, categoryData) {
assert(categoryData);
assert.equal(categoryObj.name, categoryData.name);
assert.equal(categoryObj.description, categoryData.description);
@ -43,7 +48,12 @@ describe('Categories', function() {
describe('.getCategoryTopics', function() {
it('should return a list of topics', function(done) {
Categories.getCategoryTopics(categoryObj.cid, 0, 10, 0, function(err, result) {
Categories.getCategoryTopics({
cid: categoryObj.cid,
start: 0,
stop: 10,
uid: 0
}, function(err, result) {
assert(Array.isArray(result.topics));
assert(result.topics.every(function(topic) {
return topic instanceof Object;
@ -52,6 +62,23 @@ describe('Categories', function() {
done();
});
});
it('should return a list of topics by a specific user', function(done) {
Categories.getCategoryTopics({
cid: categoryObj.cid,
start: 0,
stop: 10,
uid: 0,
targetUid: 1
}, function(err, result) {
assert(Array.isArray(result.topics));
assert(result.topics.every(function(topic) {
return topic instanceof Object && topic.uid === '1';
}));
done();
});
});
});
after(function() {

Loading…
Cancel
Save