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

136 lines
2.7 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
*/
2015-05-17 15:38:02 +09:00
Posts.views.add = 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 = {
selector: {
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
*/
2015-05-17 15:38:02 +09:00
Posts.views.add("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
*/
2015-05-17 15:38:02 +09:00
Posts.views.add("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
*/
2015-05-17 15:38:02 +09:00
Posts.views.add("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
*/
2015-05-17 15:38:02 +09:00
Posts.views.add("pending", function (terms) {
2015-04-27 10:35:06 +09:00
return {
selector: {
status: Posts.config.STATUS_PENDING
},
options: {sort: {createdAt: -1}},
showFuture: true
};
});
/**
* Rejected view
*/
Posts.views.add("rejected", function (terms) {
return {
selector: {
status: Posts.config.STATUS_REJECTED
2015-04-27 10:35:06 +09:00
},
options: {sort: {createdAt: -1}},
showFuture: true
};
});
2015-04-27 10:35:06 +09:00
2015-05-10 13:37:42 +09:00
/**
* Scheduled view
*/
2015-05-17 15:38:02 +09:00
Posts.views.add("scheduled", function (terms) {
2015-04-27 10:35:06 +09:00
return {
selector: {postedAt: {$gte: new Date()}},
options: {sort: {postedAt: -1}},
showFuture: true
2015-04-27 10:35:06 +09:00
};
});
2015-04-27 10:35:06 +09:00
2015-05-10 13:37:42 +09:00
/**
* User posts view
*/
2015-05-17 15:38:02 +09:00
Posts.views.add("userPosts", function (terms) {
2015-04-27 10:35:06 +09:00
return {
selector: {userId: terms.userId},
2015-04-27 10:35:06 +09:00
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
*/
2015-05-17 15:38:02 +09:00
Posts.views.add("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 {
selector: {_id: {$in: postsIds}, userId: {$ne: terms.userId}}, // exclude own posts
2015-04-27 10:35:06 +09:00
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
*/
2015-05-17 15:38:02 +09:00
Posts.views.add("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 {
selector: {_id: {$in: postsIds}},
2015-04-27 10:35:06 +09:00
options: {limit: 5, sort: {postedAt: -1}}
};
2015-09-17 14:51:14 +09:00
});
Posts.views.add("test", function (terms) {
return {
selector: {
title: {$regex: "newsletter", $options: 'i'}
},
options: {sort: {sticky: -1, baseScore: -1}}
};
2016-02-17 17:22:32 +09:00
});