mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-05 13:36:01 +01:00
refactored a bunch of methods so that you can pass in an author argument in querystring to see posts by just that author, #2355
This commit is contained in:
@@ -67,26 +67,32 @@ var db = require('./database'),
|
|||||||
db.isSortedSetMember('categories:cid', cid, callback);
|
db.isSortedSetMember('categories:cid', cid, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
Categories.getCategoryById = function(cid, start, end, uid, callback) {
|
Categories.getCategoryById = function(data, callback) {
|
||||||
Categories.getCategories([cid], uid, function(err, categories) {
|
Categories.getCategories([data.cid], data.uid, function(err, categories) {
|
||||||
if (err || !Array.isArray(categories) || !categories[0]) {
|
if (err || !Array.isArray(categories) || !categories[0]) {
|
||||||
return callback(err || new Error('[[error:invalid-cid]]'));
|
return callback(err || new Error('[[error:invalid-cid]]'));
|
||||||
}
|
}
|
||||||
var category = categories[0];
|
var category = categories[0];
|
||||||
|
|
||||||
if (parseInt(uid, 10)) {
|
if (parseInt(data.uid, 10)) {
|
||||||
Categories.markAsRead([cid], uid);
|
Categories.markAsRead([data.cid], data.uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
async.parallel({
|
async.parallel({
|
||||||
topics: function(next) {
|
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) {
|
pageCount: function(next) {
|
||||||
Categories.getPageCount(cid, uid, next);
|
Categories.getPageCount(data.cid, data.uid, next);
|
||||||
},
|
},
|
||||||
isIgnored: function(next) {
|
isIgnored: function(next) {
|
||||||
Categories.isIgnored([cid], uid, next);
|
Categories.isIgnored([data.cid], data.uid, next);
|
||||||
}
|
}
|
||||||
}, function(err, results) {
|
}, function(err, results) {
|
||||||
if(err) {
|
if(err) {
|
||||||
@@ -99,20 +105,20 @@ var db = require('./database'),
|
|||||||
category.isIgnored = results.isIgnored[0];
|
category.isIgnored = results.isIgnored[0];
|
||||||
category.topic_row_size = 'col-md-9';
|
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;
|
var tids;
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
function(next) {
|
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) {
|
function(topicIds, next) {
|
||||||
tids = topicIds;
|
tids = topicIds;
|
||||||
topics.getTopicsByTids(tids, uid, next);
|
topics.getTopicsByTids(tids, data.uid, next);
|
||||||
},
|
},
|
||||||
function(topics, next) {
|
function(topics, next) {
|
||||||
if (!Array.isArray(topics) || !topics.length) {
|
if (!Array.isArray(topics) || !topics.length) {
|
||||||
@@ -125,7 +131,7 @@ var db = require('./database'),
|
|||||||
var indices = {},
|
var indices = {},
|
||||||
i = 0;
|
i = 0;
|
||||||
for(i=0; i<tids.length; ++i) {
|
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) {
|
for(i=0; i<topics.length; ++i) {
|
||||||
@@ -134,7 +140,7 @@ var db = require('./database'),
|
|||||||
|
|
||||||
next(null, {
|
next(null, {
|
||||||
topics: topics,
|
topics: topics,
|
||||||
nextStart: stop + 1
|
nextStart: data.stop + 1
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
], callback);
|
], callback);
|
||||||
@@ -153,8 +159,8 @@ var db = require('./database'),
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Categories.getTopicIds = function(cid, start, stop, callback) {
|
Categories.getTopicIds = function(set, start, stop, callback) {
|
||||||
db.getSortedSetRevRange('categories:' + cid + ':tid', start, stop, callback);
|
db.getSortedSetRevRange(set, start, stop, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
Categories.getTopicIndex = function(tid, callback) {
|
Categories.getTopicIndex = function(tid, callback) {
|
||||||
|
|||||||
@@ -101,7 +101,8 @@ categoriesController.unreadTotal = function(req, res, next) {
|
|||||||
categoriesController.get = function(req, res, next) {
|
categoriesController.get = function(req, res, next) {
|
||||||
var cid = req.params.category_id,
|
var cid = req.params.category_id,
|
||||||
page = req.query.page || 1,
|
page = req.query.page || 1,
|
||||||
uid = req.user ? req.user.uid : 0;
|
uid = req.user ? req.user.uid : 0,
|
||||||
|
userPrivileges;
|
||||||
|
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
function(next) {
|
function(next) {
|
||||||
@@ -133,6 +134,7 @@ categoriesController.get = function(req, res, next) {
|
|||||||
return categoriesController.notAllowed(req, res);
|
return categoriesController.notAllowed(req, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
userPrivileges = results.privileges;
|
||||||
var settings = results.userSettings;
|
var settings = results.userSettings;
|
||||||
|
|
||||||
var topicIndex = 0;
|
var topicIndex = 0;
|
||||||
@@ -146,21 +148,37 @@ categoriesController.get = function(req, res, next) {
|
|||||||
var start = (page - 1) * settings.topicsPerPage + topicIndex,
|
var start = (page - 1) * settings.topicsPerPage + topicIndex,
|
||||||
end = start + settings.topicsPerPage - 1;
|
end = start + settings.topicsPerPage - 1;
|
||||||
|
|
||||||
categories.getCategoryById(cid, start, end, uid, function (err, categoryData) {
|
next(null, {
|
||||||
if (err) {
|
cid: cid,
|
||||||
return next(err);
|
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) {
|
categories.getRecentTopicReplies(categoryData.children, uid, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
categoryData.privileges = results.privileges;
|
|
||||||
next(null, categoryData);
|
next(null, categoryData);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
},
|
},
|
||||||
function (categoryData, next) {
|
function (categoryData, next) {
|
||||||
|
categoryData.privileges = userPrivileges;
|
||||||
|
|
||||||
res.locals.metaTags = [
|
res.locals.metaTags = [
|
||||||
{
|
{
|
||||||
name: 'title',
|
name: 'title',
|
||||||
|
|||||||
@@ -97,7 +97,12 @@ function generateForTopic(req, res, next) {
|
|||||||
function generateForCategory(req, res, next) {
|
function generateForCategory(req, res, next) {
|
||||||
var cid = req.params.category_id;
|
var cid = req.params.category_id;
|
||||||
var uid = req.user ? req.user.uid : 0;
|
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) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,12 @@ SocketCategories.loadMore = function(socket, data, callback) {
|
|||||||
var start = parseInt(data.after, 10),
|
var start = parseInt(data.after, 10),
|
||||||
end = start + results.settings.topicsPerPage - 1;
|
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) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -383,7 +383,7 @@ SocketTopics.moveAll = function(socket, data, callback) {
|
|||||||
return callback(err || new Error('[[error:no-privileges]]'));
|
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) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ module.exports = function(Topics) {
|
|||||||
|
|
||||||
async.parallel([
|
async.parallel([
|
||||||
function(next) {
|
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) {
|
function(next) {
|
||||||
user.addTopicIdToUser(uid, tid, timestamp, next);
|
user.addTopicIdToUser(uid, tid, timestamp, next);
|
||||||
|
|||||||
@@ -31,7 +31,12 @@ describe('Categories', function() {
|
|||||||
|
|
||||||
describe('.getCategoryById', function() {
|
describe('.getCategoryById', function() {
|
||||||
it('should retrieve a newly created category by its ID', function(done) {
|
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(categoryData);
|
||||||
assert.equal(categoryObj.name, categoryData.name);
|
assert.equal(categoryObj.name, categoryData.name);
|
||||||
assert.equal(categoryObj.description, categoryData.description);
|
assert.equal(categoryObj.description, categoryData.description);
|
||||||
@@ -43,7 +48,12 @@ describe('Categories', function() {
|
|||||||
|
|
||||||
describe('.getCategoryTopics', function() {
|
describe('.getCategoryTopics', function() {
|
||||||
it('should return a list of topics', function(done) {
|
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(Array.isArray(result.topics));
|
||||||
assert(result.topics.every(function(topic) {
|
assert(result.topics.every(function(topic) {
|
||||||
return topic instanceof Object;
|
return topic instanceof Object;
|
||||||
@@ -52,6 +62,23 @@ describe('Categories', function() {
|
|||||||
done();
|
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() {
|
after(function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user