storesData.stores.UsersWorkspaces.Store.GetQuery( { filters: { userId: self._req.session.userId, workspaceId: headersWorkspaceId } }, function( err, uwDocs){ if( err ){ done( err ); } else { if( uwDocs.length == 0 ){ hotplate.log("No access -- user needs to (re?)login!"); done( new self.UnauthorizedError() ); } else { hotplate.log("OK, access is cleared, creating the tab for the user..."); hotplate.log( self._req.session.userId ); stores.Tabs.Post( { userId: self._req.session.userId }, {}, function( err, tab ){ if( err ){ done( err ); } else { hotplate.log("...and ALSO returning the workspace configuration for that userId"); hotCoreStoreConfig.getConfigRecords( headersWorkspaceId, self._req.session.userId, function( err, storeRecords ){ if( err ){ done( err ); } else { done( null, { messages: [ { fromUserId: self._req.session.userId, message: { type: 'resetStores', tabId: tab.id, storeRecords: storeRecords } } ] } ); }; }); } }); // Tabs.Post() } } });//UsersWorkspaces.GetQuer ()
Object.keys( element.result ).forEach( function( k ){ var Store = element.result[ k ]; try { var store = new Store(); } catch ( e ){ hotplate.log("AN ERROR HAPPENED WHILE INSTANCING A STORE: " ); hotplate.log( e ); hotplate.log( e.stack ); hotplate.log( element ); hotplate.log( Store ); } // If the store was created (no exception thrown...) if( store ){ // Add the module to the store registry res.stores[ store.storeName ] = { Store: Store, storeObject: store }; // Add the module to the collection registry if( store.collectionName ){ res.collections[ store.collectionName ] = res.collections[ store.collectionName ] || []; res.collections[ store.collectionName ].push( { Store: Store, storeObject: store } ); } } });
stores.Tabs.Post( { userId: self._req.session.userId }, {}, function( err, tab ){ if( err ){ done( err ); } else { hotplate.log("...and ALSO returning the workspace configuration for that userId"); hotCoreStoreConfig.getConfigRecords( headersWorkspaceId, self._req.session.userId, function( err, storeRecords ){ if( err ){ done( err ); } else { done( null, { messages: [ { fromUserId: self._req.session.userId, message: { type: 'resetStores', tabId: tab.id, storeRecords: storeRecords } } ] } ); }; }); } }); // Tabs.Post()
setInterval( function(){ if( cleaningUp ) return; cleaningUp = true; hotplate.log("Cleaning up unused tabs..."); //hotplate.log( "Cleaning up expired tabs and tab messages..." ); storesData.stores.Tabs.storeObject.dbLayer.delete( { conditions: { and: [ { field: 'lastSync', type: 'lte', value: new Date( new Date() - TABLIFESPAN ) } ] } }, { multi: true }, function( err, howMany ){ // hotplate.log( 'Tabs: error and howMany: ', err, howMany ); //TODO: Log error storesData.stores.TabMessages.storeObject.dbLayer.delete( { conditions: { and: [ { field: 'added', type: 'lte', value: new Date( new Date() - TABLIFESPAN ) } ] } }, { multi: true }, function( err, howMany ){ //// hotplate.log( 'TabMessages: error and howMany: ', err, howMany ); // TODO: Log error cleaningUp = false; }); }); }, CLEANUPINTERVAL );
stores.Tabs.GetQuery( { filters: { id: params.tabId, userId: self._req.session.userId, fromLastSync: new Date() - TABLIFESPAN } }, function( err, tab ){ if( err ){ done( err ); } else { hotplate.log("Returned:"); hotplate.log( tab ); if( tab.length == 0 ){ hotplate.log("Tab was NOT present. Trying to understand if I should create one"); // At this point, the tab wasn't found. If workspaceId was passed via headers, // the person will be returned the configuration for that workspace. We need to check // that the user actually has access to that workspaceId. hotCoreStoreRegistry.getAllStores( function( err, storesData ){ if( err ){ done( err ); } else { hotplate.log("Checking that user has access to the workspaceId she is trying to register for"); storesData.stores.UsersWorkspaces.Store.GetQuery( { filters: { userId: self._req.session.userId, workspaceId: headersWorkspaceId } }, function( err, uwDocs){ if( err ){ done( err ); } else { if( uwDocs.length == 0 ){ hotplate.log("No access -- user needs to (re?)login!"); done( new self.UnauthorizedError() ); } else { hotplate.log("OK, access is cleared, creating the tab for the user..."); hotplate.log( self._req.session.userId ); stores.Tabs.Post( { userId: self._req.session.userId }, {}, function( err, tab ){ if( err ){ done( err ); } else { hotplate.log("...and ALSO returning the workspace configuration for that userId"); hotCoreStoreConfig.getConfigRecords( headersWorkspaceId, self._req.session.userId, function( err, storeRecords ){ if( err ){ done( err ); } else { done( null, { messages: [ { fromUserId: self._req.session.userId, message: { type: 'resetStores', tabId: tab.id, storeRecords: storeRecords } } ] } ); }; }); } }); // Tabs.Post() } } });//UsersWorkspaces.GetQuer () } }); } else { // Write the new access time onto the tab's record; tab = tab[ 0 ]; tab.lastSync = new Date(); stores.Tabs.Put( tab.id, tab, function( err, tab ){ if( err ){ done( err ); } else { // Return all messages for that tab, REMOVING after fetching stores.TabMessages.GetQuery( { filters: { tabId: params.tabId, fromAdded: new Date() - TABLIFESPAN }, delete: true }, function( err, tabMessages ){ if( err ){ done( err ); } else { tabMessages.forEach( function( tabMessage ){ // delete tabMessage._\id; delete tabMessage.messageId; delete tabMessage.tabId; messages.push( tabMessage); }); done( null, { messages: messages } ); } }); } }); } }; });
execAllDbFetch: function( params, body, options, done ){ var messages = []; var self = this; // If it's not a remote call, always return empty result if( ! self.remote ) return done( null, { messages: [] } ); var headersWorkspaceId = self._req.headers[ 'x-hotplate-workspaceid' ]; // User is not logged in -- goodbye if( ! self._req.session.loggedIn || ! self._req.session.userId ){ hotplate.log("A non-logged in user tried to fetch tabId %s for workspaceId %s, denying...", params.tabId, headersWorkspaceId ); return done( new self.UnauthorizedError() ); } hotplate.log("Looking for tab %s owned by user %s, x-workspaceId is %s", params.tabId, self._req.session.userId, headersWorkspaceId ); // Return all messages for that tab, REMOVING after fetching stores.Tabs.GetQuery( { filters: { id: params.tabId, userId: self._req.session.userId, fromLastSync: new Date() - TABLIFESPAN } }, function( err, tab ){ if( err ){ done( err ); } else { hotplate.log("Returned:"); hotplate.log( tab ); if( tab.length == 0 ){ hotplate.log("Tab was NOT present. Trying to understand if I should create one"); // At this point, the tab wasn't found. If workspaceId was passed via headers, // the person will be returned the configuration for that workspace. We need to check // that the user actually has access to that workspaceId. hotCoreStoreRegistry.getAllStores( function( err, storesData ){ if( err ){ done( err ); } else { hotplate.log("Checking that user has access to the workspaceId she is trying to register for"); storesData.stores.UsersWorkspaces.Store.GetQuery( { filters: { userId: self._req.session.userId, workspaceId: headersWorkspaceId } }, function( err, uwDocs){ if( err ){ done( err ); } else { if( uwDocs.length == 0 ){ hotplate.log("No access -- user needs to (re?)login!"); done( new self.UnauthorizedError() ); } else { hotplate.log("OK, access is cleared, creating the tab for the user..."); hotplate.log( self._req.session.userId ); stores.Tabs.Post( { userId: self._req.session.userId }, {}, function( err, tab ){ if( err ){ done( err ); } else { hotplate.log("...and ALSO returning the workspace configuration for that userId"); hotCoreStoreConfig.getConfigRecords( headersWorkspaceId, self._req.session.userId, function( err, storeRecords ){ if( err ){ done( err ); } else { done( null, { messages: [ { fromUserId: self._req.session.userId, message: { type: 'resetStores', tabId: tab.id, storeRecords: storeRecords } } ] } ); }; }); } }); // Tabs.Post() } } });//UsersWorkspaces.GetQuer () } }); } else { // Write the new access time onto the tab's record; tab = tab[ 0 ]; tab.lastSync = new Date(); stores.Tabs.Put( tab.id, tab, function( err, tab ){ if( err ){ done( err ); } else { // Return all messages for that tab, REMOVING after fetching stores.TabMessages.GetQuery( { filters: { tabId: params.tabId, fromAdded: new Date() - TABLIFESPAN }, delete: true }, function( err, tabMessages ){ if( err ){ done( err ); } else { tabMessages.forEach( function( tabMessage ){ // delete tabMessage._\id; delete tabMessage.messageId; delete tabMessage.tabId; messages.push( tabMessage); }); done( null, { messages: messages } ); } }); } }); } }; }); },