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

121 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-03-23 16:27:59 +09:00
import Users from 'meteor/vulcan:users';
2016-06-23 12:17:39 +09:00
import Posts from './collection.js'
2016-06-15 11:07:10 +09:00
2015-05-10 13:37:42 +09:00
/**
* @summary Base parameters that will be common to all other view unless specific properties are overwritten
2015-05-10 13:37:42 +09:00
*/
2017-03-05 10:33:34 +00:00
Posts.addDefaultView(terms => ({
selector: {
status: Posts.config.STATUS_APPROVED,
isFuture: {$ne: true} // match both false and undefined
}
2017-03-05 10:33:34 +00:00
}));
2015-04-27 10:35:06 +09:00
2015-05-10 13:37:42 +09:00
/**
* @summary Top view
2015-05-10 13:37:42 +09:00
*/
2017-03-05 10:33:34 +00:00
Posts.addView("top", terms => ({
options: {
sort: {sticky: -1, score: -1}
}
}));
2015-04-27 10:35:06 +09:00
2015-05-10 13:37:42 +09:00
/**
* @summary New view
2015-05-10 13:37:42 +09:00
*/
2017-03-05 10:33:34 +00:00
Posts.addView("new", terms => ({
options: {
sort: {sticky: -1, postedAt: -1}
}
}));
2015-04-27 10:35:06 +09:00
2015-05-10 13:37:42 +09:00
/**
* @summary Best view
2015-05-10 13:37:42 +09:00
*/
2017-03-05 10:33:34 +00:00
Posts.addView("best", terms => ({
options: {
sort: {sticky: -1, baseScore: -1}
}
}));
2015-04-27 10:35:06 +09:00
2015-05-10 13:37:42 +09:00
/**
* @summary Pending view
2015-05-10 13:37:42 +09:00
*/
2017-03-05 10:33:34 +00:00
Posts.addView("pending", terms => ({
selector: {
status: Posts.config.STATUS_PENDING
},
options: {
sort: {createdAt: -1}
}
}));
/**
* @summary Rejected view
*/
2017-03-05 10:33:34 +00:00
Posts.addView("rejected", terms => ({
selector: {
status: Posts.config.STATUS_REJECTED
},
options: {
sort: {createdAt: -1}
}
}));
2015-04-27 10:35:06 +09:00
2015-05-10 13:37:42 +09:00
/**
* @summary Scheduled view
2015-05-10 13:37:42 +09:00
*/
2017-03-05 10:33:34 +00:00
Posts.addView("scheduled", terms => ({
selector: {
status: Posts.config.STATUS_APPROVED,
isFuture: true
},
options: {
sort: {postedAt: -1}
}
}));
2015-04-27 10:35:06 +09:00
2015-05-10 13:37:42 +09:00
/**
* @summary User posts view
2015-05-10 13:37:42 +09:00
*/
2017-03-05 10:33:34 +00:00
Posts.addView("userPosts", terms => ({
selector: {
userId: terms.userId,
status: Posts.config.STATUS_APPROVED,
isFuture: {$ne: true}
},
options: {
limit: 5,
sort: {
postedAt: -1
}
2017-03-05 10:33:34 +00:00
}
}));
2015-04-27 10:35:06 +09:00
2015-05-10 13:37:42 +09:00
/**
* @summary User upvoted posts view
2015-05-10 13:37:42 +09:00
*/
2017-03-05 10:33:34 +00:00
Posts.addView("userUpvotedPosts", (terms, apolloClient) => {
var user = apolloClient ? Users.findOneInStore(apolloClient.store, terms.userId) : Users.findOne(terms.userId);
var postsIds = _.pluck(user.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
/**
* @summary User downvoted posts view
2015-05-10 13:37:42 +09:00
*/
2017-03-05 10:33:34 +00:00
Posts.addView("userDownvotedPosts", (terms, apolloClient) => {
var user = apolloClient ? Users.findOneInStore(apolloClient.store, terms.userId) : Users.findOne(terms.userId);
var postsIds = _.pluck(user.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
});