Vulcan/packages/telescope-posts/lib/views.js

111 lines
2.3 KiB
JavaScript
Raw Normal View History

/**
* Post views are filters used for subscribing to and viewing posts
* @namespace Posts.views
*/
Posts.views = {};
/**
* Add a post view
* @param {string} viewName - The name of the view
* @param {function} [viewFunction] - The function used to calculate query terms. Takes terms and baseParameters arguments
*/
Posts.views.register = function (viewName, viewFunction) {
Posts.views[viewName] = viewFunction;
};
2015-04-27 10:35:06 +09:00
2015-05-10 13:37:42 +09:00
/**
* Base parameters that will be common to all other view unless specific properties are overwritten
*/
Posts.views.baseParameters = {
2015-04-27 10:35:06 +09:00
find: {
status: Posts.config.STATUS_APPROVED
2015-04-27 10:35:06 +09:00
},
options: {
limit: 10
}
};
2015-05-10 13:37:42 +09:00
/**
* Top view
*/
Posts.views.register("top", function (terms) {
2015-04-27 10:35:06 +09:00
return {
options: {sort: {sticky: -1, score: -1}}
};
});
2015-04-27 10:35:06 +09:00
2015-05-10 13:37:42 +09:00
/**
* New view
*/
Posts.views.register("new", function (terms) {
2015-04-27 10:35:06 +09:00
return {
options: {sort: {sticky: -1, postedAt: -1}}
};
});
2015-04-27 10:35:06 +09:00
2015-05-10 13:37:42 +09:00
/**
* Best view
*/
Posts.views.register("best", function (terms) {
2015-04-27 10:35:06 +09:00
return {
options: {sort: {sticky: -1, baseScore: -1}}
};
});
2015-04-27 10:35:06 +09:00
2015-05-10 13:37:42 +09:00
/**
* Pending view
*/
Posts.views.register("pending", function (terms) {
2015-04-27 10:35:06 +09:00
return {
find: {
status: 1
},
options: {sort: {createdAt: -1}},
showFuture: true
};
});
2015-04-27 10:35:06 +09:00
2015-05-10 13:37:42 +09:00
/**
* Scheduled view
*/
Posts.views.register("scheduled", function (terms) {
2015-04-27 10:35:06 +09:00
return {
find: {postedAt: {$gte: new Date()}},
options: {sort: {postedAt: -1}}
};
});
2015-04-27 10:35:06 +09:00
2015-05-10 13:37:42 +09:00
/**
* User posts view
*/
Posts.views.register("userPosts", function (terms) {
2015-04-27 10:35:06 +09:00
return {
find: {userId: terms.userId},
options: {limit: 5, sort: {postedAt: -1}}
};
});
2015-04-27 10:35:06 +09:00
2015-05-10 13:37:42 +09:00
/**
* User upvoted posts view
*/
Posts.views.register("userUpvotedPosts", function (terms) {
2015-04-27 10:35:06 +09:00
var user = Meteor.users.findOne(terms.userId);
var postsIds = _.pluck(user.telescope.upvotedPosts, "itemId");
2015-04-27 10:35:06 +09:00
return {
find: {_id: {$in: postsIds}, userId: {$ne: terms.userId}}, // exclude own posts
options: {limit: 5, sort: {postedAt: -1}}
};
});
2015-04-27 10:35:06 +09:00
2015-05-10 13:37:42 +09:00
/**
* User downvoted posts view
*/
Posts.views.register("userDownvotedPosts", function (terms) {
2015-04-27 10:35:06 +09:00
var user = Meteor.users.findOne(terms.userId);
var postsIds = _.pluck(user.telescope.downvotedPosts, "itemId");
2015-04-27 10:35:06 +09:00
// TODO: sort based on votedAt timestamp and not postedAt, if possible
return {
find: {_id: {$in: postsIds}},
options: {limit: 5, sort: {postedAt: -1}}
};
});