2015-05-06 17:38:19 +09:00
|
|
|
/**
|
|
|
|
* Gives an object containing the appropriate find
|
|
|
|
* and options arguments for the subscriptions's Posts.find()
|
|
|
|
* @param {Object} terms
|
|
|
|
*/
|
|
|
|
Posts.getSubParams = function (terms) {
|
|
|
|
|
2015-07-10 11:05:13 +09:00
|
|
|
// add this to ensure all post publications pass audit-arguments-check
|
|
|
|
check(terms, Match.Any);
|
|
|
|
|
2015-05-06 17:38:19 +09:00
|
|
|
var maxLimit = 200;
|
|
|
|
|
|
|
|
// console.log(terms)
|
|
|
|
|
|
|
|
// note: using jquery's extend() with "deep" parameter set to true instead of shallow _.extend()
|
|
|
|
// see: http://api.jquery.com/jQuery.extend/
|
|
|
|
|
|
|
|
// initialize parameters by extending baseParameters object, to avoid passing it by reference
|
|
|
|
var parameters = Telescope.utils.deepExtend(true, {}, Posts.views.baseParameters);
|
|
|
|
|
|
|
|
// if view is not defined, default to "top"
|
|
|
|
var view = !!terms.view ? Telescope.utils.dashToCamel(terms.view) : 'top';
|
|
|
|
|
|
|
|
// get query parameters according to current view
|
|
|
|
if (typeof Posts.views[view] !== 'undefined')
|
|
|
|
parameters = Telescope.utils.deepExtend(true, parameters, Posts.views[view](terms));
|
|
|
|
|
|
|
|
// extend sort to sort posts by _id to break ties
|
|
|
|
Telescope.utils.deepExtend(true, parameters, {options: {sort: {_id: -1}}});
|
|
|
|
|
|
|
|
// if a limit was provided with the terms, add it too (note: limit=0 means "no limit")
|
|
|
|
if (typeof terms.limit !== 'undefined')
|
|
|
|
_.extend(parameters.options, {limit: parseInt(terms.limit)});
|
|
|
|
|
|
|
|
// limit to "maxLimit" posts at most when limit is undefined, equal to 0, or superior to maxLimit
|
2015-06-24 15:38:14 +09:00
|
|
|
if(!parameters.options.limit || parameters.options.limit === 0 || parameters.options.limit > maxLimit) {
|
2015-05-06 17:38:19 +09:00
|
|
|
parameters.options.limit = maxLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// hide future scheduled posts unless "showFuture" is set to true or postedAt is already defined
|
|
|
|
if (!parameters.showFuture && !parameters.find.postedAt)
|
|
|
|
parameters.find.postedAt = {$lte: new Date()};
|
|
|
|
|
2015-08-13 09:59:54 +09:00
|
|
|
// filter by category if category _id is provided (unless categories parameter already specificed)
|
2015-05-18 16:42:11 +09:00
|
|
|
// NOTE: this is a temporary fix because views cannot currently be combined
|
2015-08-13 09:59:54 +09:00
|
|
|
if (!!terms.category && !parameters.find.categories) {
|
2015-05-18 16:42:11 +09:00
|
|
|
var categoryId = Categories.findOne({slug: terms.category})._id;
|
|
|
|
parameters.find.categories = {$in: [categoryId]};
|
|
|
|
}
|
2015-05-13 22:26:42 -07:00
|
|
|
|
2015-05-06 17:38:19 +09:00
|
|
|
// console.log(parameters);
|
|
|
|
|
|
|
|
return parameters;
|
2015-05-13 22:26:42 -07:00
|
|
|
};
|