2014-09-29 15:42:35 +08:00
|
|
|
// getPostsParameters gives an object containing the appropriate find and options arguments for the subscriptions's Posts.find()
|
2013-10-26 11:41:53 +09:00
|
|
|
|
2015-01-06 11:26:44 +09:00
|
|
|
getPostsParameters = function (terms) {
|
2014-12-27 11:56:27 +09:00
|
|
|
|
|
|
|
var maxLimit = 200;
|
2013-11-16 14:01:00 +09:00
|
|
|
|
2014-09-29 16:04:09 +08:00
|
|
|
// console.log(terms)
|
2013-11-17 08:01:56 +09:00
|
|
|
|
|
|
|
// note: using jquery's extend() with "deep" parameter set to true instead of shallow _.extend()
|
2013-11-16 14:01:00 +09:00
|
|
|
// see: http://api.jquery.com/jQuery.extend/
|
2013-10-26 11:41:53 +09:00
|
|
|
|
2014-12-08 16:39:10 +09:00
|
|
|
// initialize parameters by extending baseParameters object, to avoid passing it by reference
|
|
|
|
var parameters = deepExtend(true, {}, viewParameters.baseParameters);
|
|
|
|
|
|
|
|
// if view is not defined, default to "top"
|
|
|
|
var view = !!terms.view ? dashToCamel(terms.view) : 'top';
|
2013-10-26 11:41:53 +09:00
|
|
|
|
2014-06-23 12:28:40 +09:00
|
|
|
// get query parameters according to current view
|
2014-12-08 16:39:10 +09:00
|
|
|
if (typeof viewParameters[view] !== 'undefined')
|
|
|
|
parameters = deepExtend(true, parameters, viewParameters[view](terms));
|
2014-06-23 12:08:01 +09:00
|
|
|
|
2014-12-08 16:39:10 +09:00
|
|
|
// extend sort to sort posts by _id to break ties
|
2014-05-06 20:15:48 -07:00
|
|
|
deepExtend(true, parameters, {options: {sort: {_id: -1}}});
|
2013-10-26 11:41:53 +09:00
|
|
|
|
2015-01-06 14:45:10 +09:00
|
|
|
// 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)});
|
|
|
|
|
2015-01-06 11:26:44 +09:00
|
|
|
// limit to "maxLimit" posts at most when limit is undefined, equal to 0, or superior to maxLimit
|
|
|
|
if(!parameters.options.limit || parameters.options.limit == 0 || parameters.options.limit > maxLimit) {
|
2014-12-27 11:56:27 +09:00
|
|
|
parameters.options.limit = maxLimit;
|
|
|
|
}
|
|
|
|
|
2014-12-10 09:48:24 +09:00
|
|
|
// hide future scheduled posts unless "showFuture" is set to true or postedAt is already defined
|
|
|
|
if (!parameters.showFuture && !parameters.find.postedAt)
|
2014-12-08 16:39:10 +09:00
|
|
|
parameters.find.postedAt = {$lte: new Date()};
|
|
|
|
|
|
|
|
// console.log(parameters);
|
2014-06-23 12:31:59 +09:00
|
|
|
|
2013-10-26 11:41:53 +09:00
|
|
|
return parameters;
|
2014-05-06 20:15:48 -07:00
|
|
|
};
|
2013-10-26 11:41:53 +09:00
|
|
|
|
2013-11-08 09:47:23 +09:00
|
|
|
getUsersParameters = function(filterBy, sortBy, limit) {
|
|
|
|
var find = {},
|
|
|
|
sort = {createdAt: -1};
|
|
|
|
|
|
|
|
switch(filterBy){
|
2013-11-04 22:05:19 +09:00
|
|
|
case 'invited':
|
2013-11-08 09:47:23 +09:00
|
|
|
// consider admins as invited
|
2014-10-03 16:21:06 -06:00
|
|
|
find = {$or: [{isInvited: true}, adminMongoQuery]};
|
2013-11-04 22:05:19 +09:00
|
|
|
break;
|
|
|
|
case 'uninvited':
|
2014-10-03 16:21:06 -06:00
|
|
|
find = {$and: [{isInvited: false}, notAdminMongoQuery]};
|
2013-11-04 22:05:19 +09:00
|
|
|
break;
|
|
|
|
case 'admin':
|
2014-10-03 16:21:06 -06:00
|
|
|
find = adminMongoQuery;
|
2013-11-04 22:05:19 +09:00
|
|
|
break;
|
|
|
|
}
|
2013-11-08 09:47:23 +09:00
|
|
|
|
|
|
|
switch(sortBy){
|
|
|
|
case 'username':
|
|
|
|
sort = {username: 1};
|
|
|
|
break;
|
|
|
|
case 'karma':
|
|
|
|
sort = {karma: -1};
|
|
|
|
break;
|
2013-11-08 11:10:23 +09:00
|
|
|
case 'postCount':
|
|
|
|
sort = {postCount: -1};
|
|
|
|
break;
|
|
|
|
case 'commentCount':
|
|
|
|
sort = {commentCount: -1};
|
2013-11-14 10:49:37 +09:00
|
|
|
case 'invitedCount':
|
|
|
|
sort = {invitedCount: -1};
|
2013-11-08 09:47:23 +09:00
|
|
|
}
|
2013-11-04 22:05:19 +09:00
|
|
|
return {
|
|
|
|
find: find,
|
2013-11-08 09:47:23 +09:00
|
|
|
options: {sort: sort, limit: limit}
|
2013-11-04 22:05:19 +09:00
|
|
|
};
|
2014-09-29 15:42:35 +08:00
|
|
|
};
|