2015-05-06 17:38:19 +09:00
|
|
|
/**
|
2015-09-03 14:22:51 +09:00
|
|
|
* Parameter callbacks let you add parameters to subscriptions
|
|
|
|
* @namespace Posts.parameters
|
|
|
|
*/
|
|
|
|
Posts.parameters = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a set of terms, and translates them into a `parameter` object containing the appropriate find
|
2015-05-06 17:38:19 +09:00
|
|
|
* and options arguments for the subscriptions's Posts.find()
|
|
|
|
* @param {Object} terms
|
|
|
|
*/
|
2015-09-03 14:22:51 +09:00
|
|
|
Posts.parameters.get = function (terms) {
|
2015-05-06 17:38:19 +09:00
|
|
|
|
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
|
|
|
// 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));
|
|
|
|
|
2015-09-03 14:22:51 +09:00
|
|
|
// iterate over postsParameters callbacks
|
|
|
|
parameters = Telescope.callbacks.run("postsParameters", parameters, terms);
|
|
|
|
|
|
|
|
// console.log(parameters);
|
|
|
|
|
|
|
|
return parameters;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Parameter callbacks
|
|
|
|
|
|
|
|
// extend sort to sort posts by _id to break ties
|
|
|
|
function breakTies (parameters, terms) {
|
|
|
|
return Telescope.utils.deepExtend(true, parameters, {options: {sort: {_id: -1}}});
|
|
|
|
}
|
|
|
|
Telescope.callbacks.add("postsParameters", breakTies);
|
2015-05-06 17:38:19 +09:00
|
|
|
|
2015-09-03 14:30:35 +09:00
|
|
|
// limit the number of items that can be requested at once
|
2015-09-03 14:22:51 +09:00
|
|
|
function limitPosts (parameters, terms) {
|
|
|
|
var maxLimit = 200;
|
2015-05-06 17:38:19 +09:00
|
|
|
// if a limit was provided with the terms, add it too (note: limit=0 means "no limit")
|
2015-09-03 14:22:51 +09:00
|
|
|
if (typeof terms.limit !== 'undefined') {
|
2015-05-06 17:38:19 +09:00
|
|
|
_.extend(parameters.options, {limit: parseInt(terms.limit)});
|
2015-09-03 14:22:51 +09:00
|
|
|
}
|
2015-05-06 17:38:19 +09:00
|
|
|
|
2015-09-03 14:30:35 +09:00
|
|
|
// limit to "maxLimit" items 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;
|
|
|
|
}
|
2015-09-03 14:22:51 +09:00
|
|
|
return parameters;
|
|
|
|
}
|
|
|
|
Telescope.callbacks.add("postsParameters", limitPosts);
|
2015-05-06 17:38:19 +09:00
|
|
|
|
2015-09-03 14:22:51 +09:00
|
|
|
// hide future scheduled posts unless "showFuture" is set to true or postedAt is already defined
|
|
|
|
function hideFuturePosts (parameters, terms) {
|
|
|
|
if (!parameters.showFuture && !parameters.find.postedAt) {
|
2015-05-06 17:38:19 +09:00
|
|
|
parameters.find.postedAt = {$lte: new Date()};
|
2015-05-18 16:42:11 +09:00
|
|
|
}
|
2015-05-06 17:38:19 +09:00
|
|
|
return parameters;
|
2015-09-03 14:22:51 +09:00
|
|
|
}
|
|
|
|
Telescope.callbacks.add("postsParameters", hideFuturePosts);
|
2015-09-04 09:35:01 +09:00
|
|
|
|
|
|
|
// if options are not provided, default to "top" sort
|
|
|
|
function defaultSort (parameters, terms) {
|
|
|
|
if (_.isEmpty(parameters.options.sort)) {
|
|
|
|
parameters.options.sort = {sort: {sticky: -1, score: -1}};
|
|
|
|
}
|
|
|
|
return parameters;
|
|
|
|
}
|
|
|
|
Telescope.callbacks.add("postsParameters", defaultSort);
|