mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 01:51:40 -05:00
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:
parent
88b5afc650
commit
ffada3baa0
11 changed files with 72 additions and 40 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -32,3 +32,5 @@ scratch
|
||||||
mup.json
|
mup.json
|
||||||
|
|
||||||
packages_update.py
|
packages_update.py
|
||||||
|
|
||||||
|
versions
|
|
@ -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”
|
## v0.24 “SubScope2”
|
||||||
|
|
||||||
* [BREAKING] Modules data context must now be passed on explicitely using the `moduleData` attribute.
|
* [BREAKING] Modules data context must now be passed on explicitely using the `moduleData` attribute.
|
||||||
|
|
|
@ -10,7 +10,7 @@ getCampaignPosts = function (postsCount) {
|
||||||
var lastWeek = moment().subtract(7, 'days').toDate();
|
var lastWeek = moment().subtract(7, 'days').toDate();
|
||||||
var after = (typeof lastCampaign !== 'undefined') ? lastCampaign.finishedAt : lastWeek
|
var after = (typeof lastCampaign !== 'undefined') ? lastCampaign.finishedAt : lastWeek
|
||||||
|
|
||||||
var params = Posts.getSubParams({
|
var params = Posts.parameters.get({
|
||||||
view: 'campaign',
|
view: 'campaign',
|
||||||
limit: postsCount,
|
limit: postsCount,
|
||||||
after: after
|
after: after
|
||||||
|
|
|
@ -87,7 +87,7 @@ Template.posts_list_controller.helpers({
|
||||||
var postsReady = instance.ready.get(); // ⚡ reactive ⚡
|
var postsReady = instance.ready.get(); // ⚡ reactive ⚡
|
||||||
|
|
||||||
var postsLimit = terms.limit;
|
var postsLimit = terms.limit;
|
||||||
var parameters = Posts.getSubParams(terms);
|
var parameters = Posts.parameters.get(terms);
|
||||||
var postsCursor = Posts.find(parameters.find, parameters.options);
|
var postsCursor = Posts.find(parameters.find, parameters.options);
|
||||||
|
|
||||||
var data = {
|
var data = {
|
||||||
|
|
|
@ -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()
|
* and options arguments for the subscriptions's Posts.find()
|
||||||
* @param {Object} terms
|
* @param {Object} terms
|
||||||
*/
|
*/
|
||||||
Posts.getSubParams = function (terms) {
|
Posts.parameters.get = function (terms) {
|
||||||
|
|
||||||
// add this to ensure all post publications pass audit-arguments-check
|
// add this to ensure all post publications pass audit-arguments-check
|
||||||
check(terms, Match.Any);
|
check(terms, Match.Any);
|
||||||
|
|
||||||
var maxLimit = 200;
|
|
||||||
|
|
||||||
// console.log(terms)
|
// console.log(terms)
|
||||||
|
|
||||||
// note: using jquery's extend() with "deep" parameter set to true instead of shallow _.extend()
|
// 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')
|
if (typeof Posts.views[view] !== 'undefined')
|
||||||
parameters = Telescope.utils.deepExtend(true, parameters, Posts.views[view](terms));
|
parameters = Telescope.utils.deepExtend(true, parameters, Posts.views[view](terms));
|
||||||
|
|
||||||
// extend sort to sort posts by _id to break ties
|
// iterate over postsParameters callbacks
|
||||||
Telescope.utils.deepExtend(true, parameters, {options: {sort: {_id: -1}}});
|
parameters = Telescope.callbacks.run("postsParameters", parameters, terms);
|
||||||
|
|
||||||
// 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]};
|
|
||||||
}
|
|
||||||
|
|
||||||
// console.log(parameters);
|
// console.log(parameters);
|
||||||
|
|
||||||
return 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);
|
||||||
|
|
|
@ -4,7 +4,7 @@ Posts._ensureIndex({"status": 1, "postedAt": 1});
|
||||||
|
|
||||||
Meteor.publish('postsList', function(terms) {
|
Meteor.publish('postsList', function(terms) {
|
||||||
if(Users.can.viewById(this.userId)){
|
if(Users.can.viewById(this.userId)){
|
||||||
var parameters = Posts.getSubParams(terms),
|
var parameters = Posts.parameters.get(terms),
|
||||||
posts = Posts.find(parameters.find, parameters.options);
|
posts = Posts.find(parameters.find, parameters.options);
|
||||||
|
|
||||||
return posts;
|
return posts;
|
||||||
|
@ -17,7 +17,7 @@ Meteor.publish('postsList', function(terms) {
|
||||||
|
|
||||||
Meteor.publish('postsListUsers', function(terms) {
|
Meteor.publish('postsListUsers', function(terms) {
|
||||||
if(Users.can.viewById(this.userId)){
|
if(Users.can.viewById(this.userId)){
|
||||||
var parameters = Posts.getSubParams(terms),
|
var parameters = Posts.parameters.get(terms),
|
||||||
posts = Posts.find(parameters.find, parameters.options),
|
posts = Posts.find(parameters.find, parameters.options),
|
||||||
userIds = _.pluck(posts.fetch(), 'userId');
|
userIds = _.pluck(posts.fetch(), 'userId');
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ servePostRSS = function(view, url, category) {
|
||||||
if (category) {
|
if (category) {
|
||||||
terms.category = category;
|
terms.category = category;
|
||||||
};
|
};
|
||||||
var params = Posts.getSubParams(terms);
|
var params = Posts.parameters.get(terms);
|
||||||
delete params['options']['sort']['sticky'];
|
delete params['options']['sort']['sticky'];
|
||||||
|
|
||||||
Posts.find(params.find, params.options).forEach(function(post) {
|
Posts.find(params.find, params.options).forEach(function(post) {
|
||||||
|
|
|
@ -8,7 +8,7 @@ Meteor.startup(function() {
|
||||||
|
|
||||||
if (!_.isFunction(postView)) return null;
|
if (!_.isFunction(postView)) return null;
|
||||||
|
|
||||||
var params = Posts.getSubParams(postView(terms));
|
var params = Posts.parameters.get(postView(terms));
|
||||||
var post = Posts.findOne(params.find, {
|
var post = Posts.findOne(params.find, {
|
||||||
'fields': {'postedAt': 1},
|
'fields': {'postedAt': 1},
|
||||||
'sort': params.options.sort
|
'sort': params.options.sort
|
||||||
|
@ -43,7 +43,7 @@ Meteor.startup(function() {
|
||||||
var postPages = {};
|
var postPages = {};
|
||||||
_.each(["top", "new", "best"], function(key) {
|
_.each(["top", "new", "best"], function(key) {
|
||||||
var siteUrl = Telescope.utils.getSiteUrl();
|
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, {
|
var posts = Posts.find(params.find, {
|
||||||
fields: {postedAt: 1, slug: 1, _id: 1},
|
fields: {postedAt: 1, slug: 1, _id: 1},
|
||||||
limit: 100,
|
limit: 100,
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
Meteor.publish('userSubscribedPosts', function(terms) {
|
Meteor.publish('userSubscribedPosts', function(terms) {
|
||||||
var parameters = Posts.getSubParams(terms);
|
var parameters = Posts.parameters.get(terms);
|
||||||
var posts = Posts.find(parameters.find, parameters.options);
|
var posts = Posts.find(parameters.find, parameters.options);
|
||||||
return posts;
|
return posts;
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
// category post list parameters
|
// category post list parameters
|
||||||
Posts.views.add("category", function (terms) {
|
Posts.views.add("category", function (terms) {
|
||||||
var category = Categories.findOne({slug: terms.category});
|
var category = Categories.findOne({slug: terms.category});
|
||||||
|
@ -9,3 +8,14 @@ Posts.views.add("category", function (terms) {
|
||||||
options: {sort: {sticky: -1, score: -1}} // for now categories views default to the "top" view
|
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);
|
|
@ -11,19 +11,19 @@ Meteor.publish('singleUser', function(idOrSlug) {
|
||||||
});
|
});
|
||||||
|
|
||||||
Meteor.publish('userPosts', function(terms) {
|
Meteor.publish('userPosts', function(terms) {
|
||||||
var parameters = Posts.getSubParams(terms);
|
var parameters = Posts.parameters.get(terms);
|
||||||
var posts = Posts.find(parameters.find, parameters.options);
|
var posts = Posts.find(parameters.find, parameters.options);
|
||||||
return posts;
|
return posts;
|
||||||
});
|
});
|
||||||
|
|
||||||
Meteor.publish('userUpvotedPosts', function(terms) {
|
Meteor.publish('userUpvotedPosts', function(terms) {
|
||||||
var parameters = Posts.getSubParams(terms);
|
var parameters = Posts.parameters.get(terms);
|
||||||
var posts = Posts.find(parameters.find, parameters.options);
|
var posts = Posts.find(parameters.find, parameters.options);
|
||||||
return posts;
|
return posts;
|
||||||
});
|
});
|
||||||
|
|
||||||
Meteor.publish('userDownvotedPosts', function(terms) {
|
Meteor.publish('userDownvotedPosts', function(terms) {
|
||||||
var parameters = Posts.getSubParams(terms);
|
var parameters = Posts.parameters.get(terms);
|
||||||
var posts = Posts.find(parameters.find, parameters.options);
|
var posts = Posts.find(parameters.find, parameters.options);
|
||||||
return posts;
|
return posts;
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue