Skip to content
This repository has been archived by the owner on Feb 13, 2020. It is now read-only.

Commit

Permalink
cleaning up subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
SachaG committed Feb 18, 2014
1 parent 8906d96 commit e622c11
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 47 deletions.
37 changes: 8 additions & 29 deletions client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,13 @@ Session.set('initialLoad', true);
Session.set('today', new Date());
Session.set('view', 'top');
Session.set('postsLimit', getSetting('postsPerPage', 10));
Session.set('settingsLoaded', false);
Session.set('sessionId', Meteor.default_connection._lastSessionId);

// Subscriptions

// note: here we only subscribe to subscriptions that we need to be available all the time.
// For subscriptions depending on specific pages, see the router.

// TODO: add session variable that tracks when all global subscriptions have loaded

// Settings
Meteor.subscribe('settings', function(){
// runs once after settings have loaded
Session.set('settingsLoaded',true);
analyticsInit();
});

// Categories
Meteor.subscribe('categories');
STATUS_PENDING=1;
STATUS_APPROVED=2;
STATUS_REJECTED=3;

// Current User
// We need to subscribe to the currentUser subscription because by itself,
// Meteor doesn't send all the user properties that we need
Meteor.subscribe('currentUser');

// Subscribe to all users for now to make user selection autocomplete work
Meteor.subscribe('allUsersAdmin');

// Notifications - only load if user is logged in
// Not mandatory, because server won't publish anything even if we try to load.
Expand All @@ -38,10 +18,9 @@ Deps.autorun(function() {
// userId() can be changed before user(), because loading profile takes time
if(Meteor.userId()) {
Meteor.subscribe('notifications');
if(isAdmin(Meteor.user())){
// Subscribe to all users for now to make user selection autocomplete work
Meteor.subscribe('allUsersAdmin');
}
}
})

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

});
4 changes: 2 additions & 2 deletions lib/analytics.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
analyticsInit = function() {
analyticsInit = _.once(function() {

// Mixpanel
if (mixpanelId=getSetting("mixpanelId")){
Expand Down Expand Up @@ -69,7 +69,7 @@ analyticsInit = function() {

}

}
});

analyticsRequest = function() {

Expand Down
2 changes: 0 additions & 2 deletions lib/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ canPost = function(user, returnError){
var user=(typeof user === 'undefined') ? Meteor.user() : user;

// console.log('canPost', user, action, getSetting('requirePostInvite'));
if(Meteor.isClient && !Session.get('settingsLoaded'))
return false;

if(!user){
return returnError ? "no_account" : false;
Expand Down
25 changes: 12 additions & 13 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,23 @@ RSS
*/

// uncomment to disable FastRender
// var FastRender = {RouteController: RouteController, onAllRoutes: function() {}};

//--------------------------------------------------------------------------------------------------//
//--------------------------------------------- Config ---------------------------------------------//
//--------------------------------------------------------------------------------------------------//

var preloadSubscriptions = ['categories', 'settings', 'currentUser'];

Router.configure({
layoutTemplate: 'layout',
loadingTemplate: 'loading',
notFoundTemplate: 'not_found',
waitOn: function() {
return Meteor.subscribe('categories');
waitOn: function () {
return _.map(preloadSubscriptions, function(sub){
Meteor.subscribe(sub);
});
}
});

Expand Down Expand Up @@ -137,7 +142,6 @@ var filters = {
},

isAdmin: function() {
this.subscribe('currentUser').wait();
if(!this.ready()) return;
if(!isAdmin()){
throwError(i18n.t("Sorry, you have to be an admin to view this page."))
Expand All @@ -147,8 +151,6 @@ var filters = {
},

canView: function() {
this.subscribe('currentUser').wait();
this.subscribe('settings').wait();
if(!this.ready()) return;
if(!canView()){
console.log('cannot view')
Expand All @@ -158,8 +160,6 @@ var filters = {
},

canPost: function () {
this.subscribe('currentUser').wait();
this.subscribe('settings').wait();
if(!this.ready()) return;
if(!canPost()){
throwError(i18n.t("Sorry, you don't have permissions to add new items."))
Expand All @@ -169,7 +169,6 @@ var filters = {
},

canEditPost: function() {
this.subscribe('currentUser').wait();
if(!this.ready()) return;
// Already subscribed to this post by route({waitOn: ...})
var post = Posts.findOne(this.params._id);
Expand All @@ -181,7 +180,6 @@ var filters = {
},

canEditComment: function() {
this.subscribe('currentUser').wait();
if(!this.ready()) return;
// Already subscribed to this commit by CommentPageController
var comment = Comments.findOne(this.params._id);
Expand All @@ -193,7 +191,6 @@ var filters = {
},

hasCompletedProfile: function() {
this.subscribe('currentUser').wait();
if(!this.ready()) return;
var user = Meteor.user();
if (user && ! userProfileComplete(user)){
Expand Down Expand Up @@ -255,6 +252,7 @@ if(Meteor.isClient){

Router.after(filters.resetScroll, {except:['posts_top', 'posts_new', 'posts_best', 'posts_pending', 'posts_category', 'all-users']});
Router.after( function () {
analyticsInit(); // will only run once thanks to _.once()
analyticsRequest() // log this request with mixpanel, etc
});

Expand Down Expand Up @@ -704,8 +702,9 @@ Router.map(function() {
// it might have considerable amount of docs
if(Meteor.isServer) {
FastRender.onAllRoutes(function() {
this.subscribe('categories');
this.subscribe('settings');
this.subscribe('currentUser');
var router = this;
_.each(preloadSubscriptions, function(sub){
router.subscribe(sub);
});
});
}
21 changes: 21 additions & 0 deletions license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) [year] [fullname]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 1 addition & 1 deletion server/publications.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var privacyOptions = { // false means private
// Publish the current user

Meteor.publish('currentUser', function() {
var user = Meteor.users.find(this.userId);
var user = Meteor.users.find({_id: this.userId});
return user;
});

Expand Down

0 comments on commit e622c11

Please sign in to comment.