2012-10-19 19:20:14 +09:00
|
|
|
// HELPERS
|
2012-10-18 14:24:36 +09:00
|
|
|
|
2012-10-19 19:20:14 +09:00
|
|
|
// Workaround for the fact that you cannot store objects in
|
|
|
|
// Session variables. Is used by app.js so needs to come first.
|
2012-10-10 07:28:44 +09:00
|
|
|
|
2012-10-09 12:02:37 +09:00
|
|
|
sessionSetObject=function(name, value){
|
2012-10-10 07:57:34 +09:00
|
|
|
Session.set(name, JSON.stringify(value));
|
2012-10-09 12:02:37 +09:00
|
|
|
}
|
|
|
|
sessionGetObject=function(name){
|
2012-10-18 13:07:10 +11:00
|
|
|
var data = Session.get(name);
|
|
|
|
return data && JSON.parse(data);
|
2012-10-09 12:02:37 +09:00
|
|
|
}
|
2012-10-24 11:04:42 +09:00
|
|
|
getSetting = function(setting){
|
|
|
|
var settings=Settings.find().fetch()[0];
|
|
|
|
if(settings){
|
|
|
|
return settings[setting];
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
2012-10-19 19:20:14 +09:00
|
|
|
// SUBSCRIPTIONS
|
2012-10-10 07:28:44 +09:00
|
|
|
|
|
|
|
// ** Errors **
|
|
|
|
// Local (client-only) collection
|
2012-10-09 12:02:37 +09:00
|
|
|
|
2012-10-08 10:44:13 +09:00
|
|
|
Errors = new Meteor.Collection(null);
|
|
|
|
|
2012-10-10 07:28:44 +09:00
|
|
|
// ** Settings **
|
|
|
|
|
|
|
|
Settings = new Meteor.Collection('settings');
|
|
|
|
Meteor.subscribe('settings', function(){
|
|
|
|
|
2012-10-10 07:57:34 +09:00
|
|
|
// runs once on site load
|
2012-10-10 07:28:44 +09:00
|
|
|
|
2012-10-10 07:57:34 +09:00
|
|
|
window.settingsLoaded=true;
|
2012-10-10 07:28:44 +09:00
|
|
|
|
2012-10-10 07:57:34 +09:00
|
|
|
window.Backbone.history.start({pushState: true});
|
2012-10-10 07:28:44 +09:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2012-11-17 13:24:19 +09:00
|
|
|
// ** Users **
|
|
|
|
|
|
|
|
Meteor.subscribe('currentUser');
|
|
|
|
Meteor.subscribe('allUsers');
|
|
|
|
|
|
|
|
|
2012-10-10 07:28:44 +09:00
|
|
|
// ** Notifications **
|
|
|
|
// Only load if user is logged in
|
|
|
|
|
2012-10-18 13:07:10 +11:00
|
|
|
var Notifications = new Meteor.Collection('notifications');
|
2012-10-10 07:28:44 +09:00
|
|
|
if(Meteor.user()){
|
2012-10-10 07:57:34 +09:00
|
|
|
Meteor.subscribe('notifications');
|
2012-10-10 07:28:44 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// ** Posts **
|
2012-10-18 13:07:10 +11:00
|
|
|
// We have a few subscriptions here, for the various ways we load posts
|
|
|
|
//
|
|
|
|
// The advantage is that
|
|
|
|
// a) we can change pages a lot quicker
|
|
|
|
// XXX: and we can animate between them (todo)
|
|
|
|
// b) we know when an individual page is ready
|
2012-10-10 07:28:44 +09:00
|
|
|
|
2012-08-22 21:27:22 -04:00
|
|
|
Posts = new Meteor.Collection('posts');
|
2012-10-09 12:02:37 +09:00
|
|
|
|
2012-10-24 11:04:42 +09:00
|
|
|
STATUS_PENDING=1;
|
|
|
|
STATUS_APPROVED=2;
|
|
|
|
STATUS_REJECTED=3;
|
|
|
|
FIND_APPROVED={$or: [{status: {$exists : false}}, {status: STATUS_APPROVED}]};
|
|
|
|
|
2012-10-18 13:07:10 +11:00
|
|
|
// TOP page
|
|
|
|
TOP_PAGE_PER_PAGE = 10;
|
|
|
|
TOP_PAGE_SORT = {score: -1};
|
|
|
|
Session.set('topPageLimit', TOP_PAGE_PER_PAGE);
|
|
|
|
Meteor.autosubscribe(function() {
|
2012-11-02 13:16:24 +09:00
|
|
|
Session.set('topPostsReady', false);
|
2012-10-24 11:04:42 +09:00
|
|
|
Meteor.subscribe('posts', FIND_APPROVED, {
|
2012-10-18 13:07:10 +11:00
|
|
|
sort: TOP_PAGE_SORT,
|
|
|
|
limit: Session.get('topPageLimit')
|
|
|
|
}, function() {
|
|
|
|
Session.set('topPostsReady', true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
var topPosts = function() {
|
2012-10-24 11:04:42 +09:00
|
|
|
var orderedPosts = Posts.find(FIND_APPROVED, {sort: TOP_PAGE_SORT});
|
2012-10-18 13:07:10 +11:00
|
|
|
return limitDocuments(orderedPosts, Session.get('topPageLimit'));
|
2012-10-09 12:02:37 +09:00
|
|
|
}
|
|
|
|
|
2012-10-18 13:07:10 +11:00
|
|
|
// NEW page
|
|
|
|
NEW_PAGE_PER_PAGE = 10;
|
|
|
|
NEW_PAGE_SORT = {submitted: -1};
|
|
|
|
Session.set('newPageLimit', NEW_PAGE_PER_PAGE);
|
2012-10-08 16:12:47 +02:00
|
|
|
Meteor.autosubscribe(function() {
|
2012-11-02 13:16:24 +09:00
|
|
|
Session.set('newPostsReady', false);
|
2012-10-30 13:33:52 +09:00
|
|
|
// note: should use FIND_APPROVED, but this is a temporary workaround because of bug
|
|
|
|
Meteor.subscribe('posts', {userId:{$exists: true},$or: [{status: {$exists : false}}, {status: STATUS_APPROVED}]}, {
|
2012-10-18 13:07:10 +11:00
|
|
|
sort: NEW_PAGE_SORT,
|
|
|
|
limit: Session.get('newPageLimit')
|
|
|
|
}, function() {
|
|
|
|
Session.set('newPostsReady', true);
|
2012-10-10 07:57:34 +09:00
|
|
|
});
|
2012-10-08 16:12:47 +02:00
|
|
|
});
|
2012-10-18 13:07:10 +11:00
|
|
|
var newPosts = function() {
|
2012-10-24 11:04:42 +09:00
|
|
|
var orderedPosts = Posts.find(FIND_APPROVED, {sort: NEW_PAGE_SORT});
|
|
|
|
return limitDocuments(orderedPosts, Session.get('newPageLimit'));
|
|
|
|
}
|
|
|
|
|
|
|
|
PENDING_FIND = {$or: [{status: STATUS_PENDING}, {status: STATUS_REJECTED}]};
|
|
|
|
// PENDING_FIND = {};
|
|
|
|
// PENDING page
|
|
|
|
Meteor.autosubscribe(function() {
|
2012-11-02 13:16:24 +09:00
|
|
|
Session.set('pendingPostsReady', false);
|
2012-10-24 11:04:42 +09:00
|
|
|
Meteor.subscribe('posts', PENDING_FIND, {
|
|
|
|
sort: NEW_PAGE_SORT,
|
|
|
|
limit: Session.get('newPageLimit')
|
|
|
|
}, function() {
|
|
|
|
Session.set('pendingPostsReady', true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
var pendingPosts = function() {
|
|
|
|
var orderedPosts = Posts.find( PENDING_FIND, {sort: NEW_PAGE_SORT});
|
2012-10-18 13:07:10 +11:00
|
|
|
return limitDocuments(orderedPosts, Session.get('newPageLimit'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// DIGEST page
|
|
|
|
DIGEST_PAGE_PER_PAGE = 5;
|
|
|
|
DIGEST_PAGE_SORT = {score: -1};
|
2012-10-19 15:35:46 +11:00
|
|
|
var digestPageFind = function(mDate) {
|
2012-10-24 11:04:42 +09:00
|
|
|
var find = {
|
|
|
|
submitted: {
|
|
|
|
$gte: mDate.startOf('day').valueOf(),
|
|
|
|
$lt: mDate.endOf('day').valueOf()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
find=_.extend(find, FIND_APPROVED);
|
|
|
|
return find;
|
2012-10-18 13:07:10 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
Session.set('digestPageLimit', DIGEST_PAGE_PER_PAGE);
|
|
|
|
Meteor.autosubscribe(function() {
|
2012-11-02 13:16:24 +09:00
|
|
|
Session.set('digestPostsReady', false);
|
2012-10-19 15:35:46 +11:00
|
|
|
|
|
|
|
var mDate = moment(sessionGetObject('currentDate'));
|
|
|
|
// start yesterday, and subscribe to 3 days
|
|
|
|
mDate.add('days', -1);
|
|
|
|
for (var i = 0; i < 3; i++) {
|
|
|
|
Meteor.subscribe('posts', digestPageFind(mDate), {
|
|
|
|
sort: DIGEST_PAGE_SORT,
|
|
|
|
limit: Session.get('digestPageLimit')
|
|
|
|
}, function() {
|
|
|
|
Session.set('digestPostsReady', true);
|
|
|
|
});
|
|
|
|
mDate.add('days', 1);
|
|
|
|
}
|
2012-10-18 13:07:10 +11:00
|
|
|
});
|
|
|
|
var digestPosts = function() {
|
2012-10-19 15:35:46 +11:00
|
|
|
var mDate = moment(sessionGetObject('currentDate'))
|
|
|
|
var orderedPosts = Posts.find(digestPageFind(mDate), {sort: DIGEST_PAGE_SORT});
|
2012-10-18 13:07:10 +11:00
|
|
|
return limitDocuments(orderedPosts, Session.get('digestPageLimit'));
|
|
|
|
}
|
2012-08-22 21:27:22 -04:00
|
|
|
|
2012-10-18 13:07:10 +11:00
|
|
|
// SINGLE post, e.g. post_page
|
2012-10-18 12:01:17 +11:00
|
|
|
Meteor.autosubscribe(function() {
|
|
|
|
Session.set('postReady', false);
|
|
|
|
Meteor.subscribe('post', Session.get('selectedPostId'), function() {
|
|
|
|
Session.set('postReady', true);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2012-10-10 07:28:44 +09:00
|
|
|
// ** Categories **
|
2012-08-30 21:35:48 -04:00
|
|
|
|
2012-10-10 07:28:44 +09:00
|
|
|
Categories = new Meteor.Collection('categories');
|
|
|
|
Meteor.subscribe('categories');
|
2012-10-01 12:23:35 +09:00
|
|
|
|
|
|
|
|
2012-10-10 07:28:44 +09:00
|
|
|
// ** Comments **
|
2012-10-10 11:03:09 +09:00
|
|
|
// Collection depends on selectedPostId and selectedCommentId session variable
|
2012-10-08 16:49:01 +09:00
|
|
|
|
2012-10-10 07:28:44 +09:00
|
|
|
Session.set('selectedPostId', null);
|
|
|
|
Comments = new Meteor.Collection('comments');
|
|
|
|
Meteor.autosubscribe(function() {
|
2012-10-10 11:03:09 +09:00
|
|
|
var query = { $or : [ { post : Session.get('selectedPostId') } , { _id : Session.get('selectedCommentId') } ] };
|
|
|
|
Meteor.subscribe('comments', query, function() {
|
2012-10-10 07:57:34 +09:00
|
|
|
//
|
|
|
|
});
|
2012-10-04 14:54:26 +09:00
|
|
|
});
|