Changed Posts.getSubParams to Posts.parameters.get; Posts.parameters.get now iterates over the postsParameters callback hook to build parameters object.

This commit is contained in:
Sacha Greif 2015-09-03 14:22:51 +09:00
parent 88b5afc650
commit ffada3baa0
11 changed files with 72 additions and 40 deletions

4
.gitignore vendored
View file

@ -31,4 +31,6 @@ scratch
.meteor/meteorite
mup.json
packages_update.py
packages_update.py
versions

View file

@ -1,3 +1,6 @@
* Changed `Posts.getSubParams` to `Posts.parameters.get`.
* `Posts.parameters.get` now iterates over the `postsParameters` callback hook to build parameters object.
## v0.24 “SubScope2”
* [BREAKING] Modules data context must now be passed on explicitely using the `moduleData` attribute.

View file

@ -10,7 +10,7 @@ getCampaignPosts = function (postsCount) {
var lastWeek = moment().subtract(7, 'days').toDate();
var after = (typeof lastCampaign !== 'undefined') ? lastCampaign.finishedAt : lastWeek
var params = Posts.getSubParams({
var params = Posts.parameters.get({
view: 'campaign',
limit: postsCount,
after: after

View file

@ -87,7 +87,7 @@ Template.posts_list_controller.helpers({
var postsReady = instance.ready.get(); // ⚡ reactive ⚡
var postsLimit = terms.limit;
var parameters = Posts.getSubParams(terms);
var parameters = Posts.parameters.get(terms);
var postsCursor = Posts.find(parameters.find, parameters.options);
var data = {

View file

@ -1,15 +1,19 @@
/**
* Gives an object containing the appropriate find
* 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
* and options arguments for the subscriptions's Posts.find()
* @param {Object} terms
*/
Posts.getSubParams = function (terms) {
Posts.parameters.get = function (terms) {
// add this to ensure all post publications pass audit-arguments-check
check(terms, Match.Any);
var maxLimit = 200;
// console.log(terms)
// note: using jquery's extend() with "deep" parameter set to true instead of shallow _.extend()
@ -25,30 +29,43 @@ Posts.getSubParams = function (terms) {
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
if(!parameters.options.limit || parameters.options.limit === 0 || parameters.options.limit > maxLimit) {
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()};
// filter by category if category _id is provided (unless categories parameter already specificed)
// NOTE: this is a temporary fix because views cannot currently be combined
if (!!terms.category && !parameters.find.categories) {
var categoryId = Categories.findOne({slug: terms.category})._id;
parameters.find.categories = {$in: [categoryId]};
}
// 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);
// limit the number of posts that can be requested at once
function limitPosts (parameters, terms) {
var maxLimit = 200;
// 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
if(!parameters.options.limit || parameters.options.limit === 0 || parameters.options.limit > maxLimit) {
parameters.options.limit = maxLimit;
}
return parameters;
}
Telescope.callbacks.add("postsParameters", limitPosts);
// 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) {
parameters.find.postedAt = {$lte: new Date()};
}
return parameters;
}
Telescope.callbacks.add("postsParameters", hideFuturePosts);

View file

@ -4,7 +4,7 @@ Posts._ensureIndex({"status": 1, "postedAt": 1});
Meteor.publish('postsList', function(terms) {
if(Users.can.viewById(this.userId)){
var parameters = Posts.getSubParams(terms),
var parameters = Posts.parameters.get(terms),
posts = Posts.find(parameters.find, parameters.options);
return posts;
@ -17,7 +17,7 @@ Meteor.publish('postsList', function(terms) {
Meteor.publish('postsListUsers', function(terms) {
if(Users.can.viewById(this.userId)){
var parameters = Posts.getSubParams(terms),
var parameters = Posts.parameters.get(terms),
posts = Posts.find(parameters.find, parameters.options),
userIds = _.pluck(posts.fetch(), 'userId');

View file

@ -18,7 +18,7 @@ servePostRSS = function(view, url, category) {
if (category) {
terms.category = category;
};
var params = Posts.getSubParams(terms);
var params = Posts.parameters.get(terms);
delete params['options']['sort']['sticky'];
Posts.find(params.find, params.options).forEach(function(post) {

View file

@ -8,7 +8,7 @@ Meteor.startup(function() {
if (!_.isFunction(postView)) return null;
var params = Posts.getSubParams(postView(terms));
var params = Posts.parameters.get(postView(terms));
var post = Posts.findOne(params.find, {
'fields': {'postedAt': 1},
'sort': params.options.sort
@ -43,7 +43,7 @@ Meteor.startup(function() {
var postPages = {};
_.each(["top", "new", "best"], function(key) {
var siteUrl = Telescope.utils.getSiteUrl();
var params = Posts.getSubParams(Posts.views[key]());
var params = Posts.parameters.get(Posts.views[key]());
var posts = Posts.find(params.find, {
fields: {postedAt: 1, slug: 1, _id: 1},
limit: 100,

View file

@ -1,5 +1,5 @@
Meteor.publish('userSubscribedPosts', function(terms) {
var parameters = Posts.getSubParams(terms);
var parameters = Posts.parameters.get(terms);
var posts = Posts.find(parameters.find, parameters.options);
return posts;
});

View file

@ -1,4 +1,3 @@
// category post list parameters
Posts.views.add("category", function (terms) {
var category = Categories.findOne({slug: terms.category});
@ -8,4 +7,15 @@ Posts.views.add("category", function (terms) {
find: {'categories': {$in: categoriesIds}} ,
options: {sort: {sticky: -1, score: -1}} // for now categories views default to the "top" view
};
});
});
// add category parameter to publications/subscriptions
function addCategoryParameter (parameters, terms) {
// filter by category if category _id is provided (unless categories parameter already specificed)
if (!!terms.category && !parameters.find.categories) {
var categoryId = Categories.findOne({slug: terms.category})._id;
parameters.find.categories = {$in: [categoryId]};
}
return parameters;
}
Telescope.callbacks.add("postsParameters", addCategoryParameter);

View file

@ -11,19 +11,19 @@ Meteor.publish('singleUser', function(idOrSlug) {
});
Meteor.publish('userPosts', function(terms) {
var parameters = Posts.getSubParams(terms);
var parameters = Posts.parameters.get(terms);
var posts = Posts.find(parameters.find, parameters.options);
return posts;
});
Meteor.publish('userUpvotedPosts', function(terms) {
var parameters = Posts.getSubParams(terms);
var parameters = Posts.parameters.get(terms);
var posts = Posts.find(parameters.find, parameters.options);
return posts;
});
Meteor.publish('userDownvotedPosts', function(terms) {
var parameters = Posts.getSubParams(terms);
var parameters = Posts.parameters.get(terms);
var posts = Posts.find(parameters.find, parameters.options);
return posts;
});