<commit_before>Session.set('initialLoad', true);
Session.set('currentDate', new Date());

l=function(s){
  console.log(s);
}

// HELPERS
getSetting = function(setting){
  var settings=Settings.find().fetch()[0];
  if(settings){
    return settings[setting];
  }
  return '';
}
clearSeenErrors = function(){
  Errors.update({seen:true}, {$set: {show:false}}, {multi:true});
}
// SUBSCRIPTIONS

// ** Errors **
// Local (client-only) collection

Errors = new Meteor.Collection(null);

// ** Settings **

Settings = new Meteor.Collection('settings');
Meteor.subscribe('settings', function(){

  // runs once on site load
  analyticsInit();
  Session.set('settingsLoaded',true);
});

// ** Categories **

Categories = new Meteor.Collection('categories');
Meteor.subscribe('categories');

// ** Users **

Meteor.subscribe('currentUser');
Meteor.subscribe('allUsers');


// ** Notifications **
// Only load if user is logged in

Notifications = new Meteor.Collection('notifications');
if(Meteor.userId() != null){
  Meteor.subscribe('notifications');
}

// ** Posts **
// 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

Posts = new Meteor.Collection('posts');

Meteor.autorun(function() {
  Meteor.subscribe('singlePost', Session.get('selectedPostId'));
});

STATUS_PENDING=1;
STATUS_APPROVED=2;
STATUS_REJECTED=3;

// build find query object
findPosts = function(properties){
  var find = {};

  // Status
  if(properties.status)
    find = _.extend(find, {status: properties.status});

  // Slug
  if(properties.slug)
    find = _.extend(find, {'categories.slug': properties.slug});
  
  // Date
  if(properties.date){
    find = _.extend(find, {submitted: 
      {
        $gte: moment(properties.date).startOf('day').valueOf(), 
        $lt: moment(properties.date).endOf('day').valueOf()
      }
    });
  }

  console.log("*"+properties.name+"* find query: --------------")
  console.log(find)

  return find;
}

sortPosts = function(sortProperty){
  var sort = {sort: {sticky: -1}};
  sort.sort[sortProperty] = -1;
  return sort;
}

postListSubscription = function(find, options, per_page) {
  // console.log('calling postListSubscription')
  var handle = Meteor.subscribeWithPagination('paginatedPosts', find, options, per_page);
  handle.fetch = function() {
    return limitDocuments(Posts.find(find, options), handle.loaded());
  }
  return handle;
}

// note: the "name" property is for internal debugging purposes only
Meteor.autorun(function(){
  console.log('autorun: topPostsHandle')
  topPostsHandle = postListSubscription(findPosts({name: 'top', status: STATUS_APPROVED, slug: Session.get('categorySlug')}), sortPosts('score'), 10);
});

Meteor.autorun(function(){
  newPostsHandle = postListSubscription(findPosts({name: 'new', status: STATUS_APPROVED}), sortPosts('submitted'), 10);  
});

Meteor.autorun(function(){
  bestPostsHandle = postListSubscription(findPosts({name: 'best', status: STATUS_APPROVED}), sortPosts('baseScore'), 10);  
});

Meteor.autorun(function(){
  pendingPostsHandle =  postListSubscription(findPosts({name: 'pending', status: STATUS_PENDING}), sortPosts('createdAt'), 10);
});

Meteor.autorun(function(){
  digestHandle = postListSubscription(findPosts({name: 'digest', status: STATUS_APPROVED, date: Session.get('currentDate') }), sortPosts('score'), 10);  
});

// digest subscriptions<commit_msg>passing functions as arguments instead of using autorun blocks<commit_after>Session.set('initialLoad', true);
Session.set('currentDate', new Date());

l=function(s){
  console.log(s);
}

// HELPERS
getSetting = function(setting){
  var settings=Settings.find().fetch()[0];
  if(settings){
    return settings[setting];
  }
  return '';
}
clearSeenErrors = function(){
  Errors.update({seen:true}, {$set: {show:false}}, {multi:true});
}
// SUBSCRIPTIONS

// ** Errors **
// Local (client-only) collection

Errors = new Meteor.Collection(null);

// ** Settings **

Settings = new Meteor.Collection('settings');
Meteor.subscribe('settings', function(){

  // runs once on site load
  analyticsInit();
  Session.set('settingsLoaded',true);
});

// ** Categories **

Categories = new Meteor.Collection('categories');
Meteor.subscribe('categories');

// ** Users **

Meteor.subscribe('currentUser');
Meteor.subscribe('allUsers');


// ** Notifications **
// Only load if user is logged in

Notifications = new Meteor.Collection('notifications');
if(Meteor.userId() != null){
  Meteor.subscribe('notifications');
}

// ** Posts **
// 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

Posts = new Meteor.Collection('posts');

Meteor.autorun(function() {
  Meteor.subscribe('singlePost', Session.get('selectedPostId'));
});

STATUS_PENDING=1;
STATUS_APPROVED=2;
STATUS_REJECTED=3;

// build find query object
findPosts = function(properties){
  var find = {};

  // Status
  if(properties.status)
    find = _.extend(find, {status: properties.status});

  // Slug
  if(properties.slug)
    find = _.extend(find, {'categories.slug': properties.slug});
  
  // Date
  if(properties.date){
    find = _.extend(find, {submitted: 
      {
        $gte: moment(properties.date).startOf('day').valueOf(), 
        $lt: moment(properties.date).endOf('day').valueOf()
      }
    });
  }

  console.log("*"+properties.name+"* find query: --------------")
  console.log(find)

  return find;
}

// build sort query object
sortPosts = function(sortProperty){
  var sort = {sort: {sticky: -1}};
  sort.sort[sortProperty] = -1;
  return sort;
}

// put it all together with pagination
postListSubscription = function(find, options, per_page) {
  // console.log('calling postListSubscription')
  var handle = Meteor.subscribeWithPagination('paginatedPosts', find, options, per_page);
  handle.fetch = function() {
    return limitDocuments(Posts.find(find, options), handle.loaded());
  }
  return handle;
}


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') });
}

// note: the "name" property is for internal debugging purposes only
topPostsHandle = postListSubscription(findTop, sortPosts('score'), 10);

newPostsHandle = postListSubscription(findNew, sortPosts('submitted'), 10);  

bestPostsHandle = postListSubscription(findBest, sortPosts('baseScore'), 10);  

pendingPostsHandle = postListSubscription(findPending, sortPosts('createdAt'), 10);

digestHandle = postListSubscription(findDigest, sortPosts('score'), 10);  

// digest subscriptions
{'fraction_non_alphanumeric': 0.1654166098459021, 'fraction_numerical': 0.004363834719759989, 'mean_word_length': 4.658950617283951, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '173108', 'n_tokens_mistral': 2362, 'n_tokens_neox': 2282, 'n_words': 523}