2012-12-11 10:47:11 +09:00
|
|
|
Session.set('initialLoad', true);
|
2013-04-06 15:17:09 +09:00
|
|
|
Session.set('currentDate', new Date());
|
2012-12-11 10:47:11 +09:00
|
|
|
|
2012-12-31 18:18:46 +01:00
|
|
|
l=function(s){
|
|
|
|
console.log(s);
|
|
|
|
}
|
|
|
|
|
2012-10-19 19:20:14 +09:00
|
|
|
// HELPERS
|
2012-10-24 11:04:42 +09:00
|
|
|
getSetting = function(setting){
|
|
|
|
var settings=Settings.find().fetch()[0];
|
|
|
|
if(settings){
|
|
|
|
return settings[setting];
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
2012-11-19 12:03:06 +09:00
|
|
|
clearSeenErrors = function(){
|
|
|
|
Errors.update({seen:true}, {$set: {show:false}}, {multi:true});
|
|
|
|
}
|
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-12-11 10:47:11 +09:00
|
|
|
analyticsInit();
|
2012-11-21 14:31:58 +09:00
|
|
|
Session.set('settingsLoaded',true);
|
2012-10-10 07:28:44 +09:00
|
|
|
});
|
|
|
|
|
2013-02-18 13:38:15 +09:00
|
|
|
// ** Categories **
|
|
|
|
|
|
|
|
Categories = new Meteor.Collection('categories');
|
|
|
|
Meteor.subscribe('categories');
|
|
|
|
|
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
|
|
|
|
|
2013-04-05 11:58:37 +09:00
|
|
|
Notifications = new Meteor.Collection('notifications');
|
2013-02-22 15:41:15 +09:00
|
|
|
if(Meteor.userId() != null){
|
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
|
|
|
|
2013-04-06 14:27:01 +09:00
|
|
|
Meteor.autorun(function() {
|
|
|
|
Meteor.subscribe('singlePost', Session.get('selectedPostId'));
|
|
|
|
});
|
2013-02-18 13:38:15 +09:00
|
|
|
|
2012-10-24 11:04:42 +09:00
|
|
|
STATUS_PENDING=1;
|
|
|
|
STATUS_APPROVED=2;
|
|
|
|
STATUS_REJECTED=3;
|
2013-02-18 13:38:15 +09:00
|
|
|
|
2013-04-06 14:27:01 +09:00
|
|
|
// build find query object
|
2013-04-06 15:17:09 +09:00
|
|
|
findPosts = function(properties){
|
2013-02-18 13:38:15 +09:00
|
|
|
var find = {};
|
|
|
|
|
2013-04-06 14:27:01 +09:00
|
|
|
// Status
|
|
|
|
if(properties.status)
|
|
|
|
find = _.extend(find, {status: properties.status});
|
2013-02-18 13:38:15 +09:00
|
|
|
|
2013-04-06 14:27:01 +09:00
|
|
|
// Slug
|
|
|
|
if(properties.slug)
|
2013-04-06 14:50:23 +09:00
|
|
|
find = _.extend(find, {'categories.slug': properties.slug});
|
2013-04-06 14:27:01 +09:00
|
|
|
|
2013-04-06 14:50:23 +09:00
|
|
|
// Date
|
|
|
|
if(properties.date){
|
|
|
|
find = _.extend(find, {submitted:
|
|
|
|
{
|
|
|
|
$gte: moment(properties.date).startOf('day').valueOf(),
|
|
|
|
$lt: moment(properties.date).endOf('day').valueOf()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-04-06 14:27:01 +09:00
|
|
|
console.log("*"+properties.name+"* find query: --------------")
|
|
|
|
console.log(find)
|
2013-02-18 13:59:48 +09:00
|
|
|
|
2013-02-18 13:38:15 +09:00
|
|
|
return find;
|
|
|
|
}
|
|
|
|
|
2013-04-06 15:42:33 +09:00
|
|
|
// build sort query object
|
2013-04-06 15:17:09 +09:00
|
|
|
sortPosts = function(sortProperty){
|
2013-04-05 12:41:50 +09:00
|
|
|
var sort = {sort: {sticky: -1}};
|
|
|
|
sort.sort[sortProperty] = -1;
|
|
|
|
return sort;
|
2013-02-18 13:50:24 +09:00
|
|
|
}
|
2012-11-25 19:10:10 +11:00
|
|
|
|
2013-04-06 15:42:33 +09:00
|
|
|
// put it all together with pagination
|
2013-04-06 15:17:09 +09:00
|
|
|
postListSubscription = function(find, options, per_page) {
|
2013-04-06 14:50:23 +09:00
|
|
|
// console.log('calling postListSubscription')
|
2013-03-23 16:58:28 +11:00
|
|
|
var handle = Meteor.subscribeWithPagination('paginatedPosts', find, options, per_page);
|
2012-12-13 17:06:13 +11:00
|
|
|
handle.fetch = function() {
|
|
|
|
return limitDocuments(Posts.find(find, options), handle.loaded());
|
|
|
|
}
|
|
|
|
return handle;
|
2012-10-09 12:02:37 +09:00
|
|
|
}
|
2013-04-06 15:17:09 +09:00
|
|
|
|
2013-04-06 15:42:33 +09:00
|
|
|
|
|
|
|
var findTop = function() {
|
|
|
|
return findPosts({name: 'top', status: STATUS_APPROVED, slug: Session.get('categorySlug')});
|
|
|
|
}
|
|
|
|
var findNew = function() {
|
|
|
|
return findPosts({name: 'new', status: STATUS_APPROVED, slug: Session.get('categorySlug')});
|
|
|
|
}
|
|
|
|
var findBest = function() {
|
|
|
|
return findPosts({name: 'best', status: STATUS_APPROVED, slug: Session.get('categorySlug')});
|
|
|
|
}
|
|
|
|
var findPending = function() {
|
|
|
|
return findPosts({name: 'pending', status: STATUS_PENDING, slug: Session.get('categorySlug')});
|
|
|
|
}
|
|
|
|
var findDigest = function() {
|
|
|
|
return findPosts({name: 'digest', status: STATUS_APPROVED, date: Session.get('currentDate') });
|
|
|
|
}
|
|
|
|
|
2013-04-06 15:17:09 +09:00
|
|
|
// note: the "name" property is for internal debugging purposes only
|
2013-04-06 15:42:33 +09:00
|
|
|
topPostsHandle = postListSubscription(findTop, sortPosts('score'), 10);
|
2013-04-06 15:17:09 +09:00
|
|
|
|
2013-04-06 15:42:33 +09:00
|
|
|
newPostsHandle = postListSubscription(findNew, sortPosts('submitted'), 10);
|
2013-04-06 15:17:09 +09:00
|
|
|
|
2013-04-06 15:42:33 +09:00
|
|
|
bestPostsHandle = postListSubscription(findBest, sortPosts('baseScore'), 10);
|
2013-04-06 15:17:09 +09:00
|
|
|
|
2013-04-06 15:42:33 +09:00
|
|
|
pendingPostsHandle = postListSubscription(findPending, sortPosts('createdAt'), 10);
|
2013-04-06 15:17:09 +09:00
|
|
|
|
2013-04-06 15:42:33 +09:00
|
|
|
digestHandle = postListSubscription(findDigest, sortPosts('score'), 10);
|
2013-04-06 15:17:09 +09:00
|
|
|
|
2012-12-13 18:12:08 +11:00
|
|
|
// digest subscriptions
|
2013-04-06 14:50:23 +09:00
|
|
|
// DIGEST_PRELOADING = 3;
|
|
|
|
// var digestHandles = {}
|
|
|
|
// var dateHash = function(mDate) {
|
|
|
|
// return mDate.format('DD-MM-YYYY');
|
|
|
|
// }
|
|
|
|
// var currentMDateForDigest = function() {
|
|
|
|
// return moment(Session.get('currentDate')).startOf('day');
|
|
|
|
// }
|
|
|
|
// currentDigestHandle = function() {
|
|
|
|
// return digestHandles[dateHash(currentMDateForDigest())];
|
|
|
|
// }
|
2012-12-13 18:12:08 +11:00
|
|
|
|
|
|
|
// we use autorun here, because we DON'T want meteor to automatically
|
|
|
|
// unsubscribe for us
|
2013-04-06 14:27:01 +09:00
|
|
|
// Meteor.autorun(function() {
|
|
|
|
// var daySubscription = function(mDate) {
|
|
|
|
// console.log('calling daySubscription')
|
|
|
|
// var find = _.extend({
|
|
|
|
// submitted: {
|
|
|
|
// $gte: mDate.startOf('day').valueOf(),
|
|
|
|
// $lt: mDate.endOf('day').valueOf()
|
|
|
|
// }
|
|
|
|
// }, findPosts({status: STATUS_APPROVED}));
|
|
|
|
// // note: the digest is ranked by baseScore and not score because we want the posts with the most votes of the day
|
|
|
|
// // independantly of age
|
|
|
|
// var options = {sort: {baseScore: -1}};
|
2013-03-15 20:21:05 +11:00
|
|
|
|
2013-04-06 14:27:01 +09:00
|
|
|
// // we aren't ever going to paginate this sub, but we'll use pSub
|
|
|
|
// // so we have a reactive loading() function
|
|
|
|
// // (grr... https://github.com/meteor/meteor/pull/273)
|
|
|
|
// return postListSubscription(find, options, 50);
|
|
|
|
// };
|
2013-03-15 20:21:05 +11:00
|
|
|
|
2013-04-06 14:27:01 +09:00
|
|
|
// // take it to the start of the day.
|
|
|
|
// var mDate = currentMDateForDigest();
|
|
|
|
// var firstDate = moment(mDate).subtract('days', DIGEST_PRELOADING);
|
|
|
|
// var lastDate = moment(mDate).add('days', DIGEST_PRELOADING);
|
2013-03-15 20:21:05 +11:00
|
|
|
|
2013-04-06 14:27:01 +09:00
|
|
|
// // first unsubscribe all the subscriptions that fall outside of our current range
|
|
|
|
// _.each(digestHandles, function(handle, hash) {
|
|
|
|
// var mDate = moment(hash, 'DD-MM-YYYY');
|
|
|
|
// if (mDate < firstDate || mDate > lastDate) {
|
|
|
|
// // console.log('unsubscribing digest for ' + mDate.toString())
|
|
|
|
// handle.stop();
|
|
|
|
// delete digestHandles[dateHash(mDate)];
|
|
|
|
// }
|
|
|
|
// });
|
2013-03-15 20:21:05 +11:00
|
|
|
|
2013-04-06 14:27:01 +09:00
|
|
|
// // set up a sub for each day for the DIGEST_PRELOADING days before and after
|
|
|
|
// // but we want to be smart about it --
|
|
|
|
// for (mDate = firstDate; mDate < lastDate; mDate.add('days',1 )) {
|
|
|
|
// if (! digestHandles[dateHash(mDate)]) {
|
|
|
|
// // console.log('subscribing digest for ' + mDate.toString());
|
|
|
|
// digestHandles[dateHash(mDate)] = daySubscription(mDate);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// });
|
2012-10-18 12:01:17 +11:00
|
|
|
|
2012-08-30 21:35:48 -04:00
|
|
|
|
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-11-19 11:26:21 +09:00
|
|
|
Session.set('commentReady', true);
|
2012-10-10 07:57:34 +09:00
|
|
|
});
|
2012-12-17 08:16:09 +01:00
|
|
|
});
|