Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 30 additions & 41 deletions common.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ noUserIdError = function () {
UserSession = {
set: function (key, value, userId) {
// Set a new variable in the user session
if (Meteor.userId() || Meteor.isServer) {
// If the user is logged in, update the variable in the collection
if (typeof userId === 'undefined') {
if (Meteor.isClient) userId = Meteor.userId();
else if (Meteor.isServer) {
noUserIdError();
return undefined;
}
}
if (Meteor.isServer && undefined !== userId || Meteor.isClient && (undefined !== userId || Meteor.userId()) ) {
// If the user is logged in and no userId is passed, use the logged in user ID
if (undefined === userId && Meteor.isClient) {
userId = Meteor.userId();
}

var existing = UserSessionCollection.findOne({ key: key, userId: userId});
var sv = { key: key, value: value, userId: userId };
if (existing) UserSessionCollection.update({ _id: existing._id }, { $set: sv });
Expand All @@ -39,14 +36,12 @@ UserSession = {
},
get: function (key, userId) {
// Get the value of a user session variable
if (Meteor.userId() || Meteor.isServer) {
if (typeof userId === 'undefined') {
if (Meteor.isClient) userId = Meteor.userId();
else if (Meteor.isServer) {
noUserIdError();
return undefined;
}
}
if (Meteor.isServer && undefined !== userId || Meteor.isClient && (undefined !== userId || Meteor.userId()) ) {
// If the user is logged in and no userId is passed, use the logged in user ID
if (undefined === userId && Meteor.isClient) {
userId = Meteor.userId();
}

var existing = UserSessionCollection.findOne({ key: key, userId: userId});
if (existing) return existing.value;
} else {
Expand All @@ -55,14 +50,12 @@ UserSession = {
},
delete: function (key, userId) {
// Delete a user session variable, if it exists
if (Meteor.userId() || Meteor.isServer) {
if (typeof userId === 'undefined') {
if (Meteor.isClient) userId = Meteor.userId();
else if (Meteor.isServer) {
noUserIdError();
return undefined;
}
}
if (Meteor.isServer && undefined !== userId || Meteor.isClient && (undefined !== userId || Meteor.userId()) ) {
// If the user is logged in and no userId is passed, use the logged in user ID
if (undefined === userId && Meteor.isClient) {
userId = Meteor.userId();
}

var existing = UserSessionCollection.findOne({ key: key, userId: userId});
if (existing) UserSessionCollection.remove(existing._id);
} else {
Expand All @@ -71,14 +64,12 @@ UserSession = {
},
equals: function (key, value, userId) {
// Test if a user session variable is equal to a value
if (Meteor.userId() || Meteor.isServer) {
if (typeof userId === 'undefined') {
if (Meteor.isClient) userId = Meteor.userId();
else if (Meteor.isServer) {
noUserIdError();
return undefined;
}
}
if (Meteor.isServer && undefined !== userId || Meteor.isClient && (undefined !== userId || Meteor.userId()) ) {
// If the user is logged in and no userId is passed, use the logged in user ID
if (undefined === userId && Meteor.isClient) {
userId = Meteor.userId();
}

var existing = UserSessionCollection.findOne({ key: key, userId: userId});
if (existing) return existing.value == value; //XXX Should this be ===
} else {
Expand All @@ -87,14 +78,12 @@ UserSession = {
},
list: function (userId) {
// Get all the user session variables as an object
if (Meteor.userId() || Meteor.isServer) {
if (typeof userId === 'undefined') {
if (Meteor.isClient) userId = Meteor.userId();
else if (Meteor.isServer) {
noUserIdError();
return undefined;
}
}
if (Meteor.isServer && undefined !== userId || Meteor.isClient && (undefined !== userId || Meteor.userId()) ) {
// If the user is logged in and no userId is passed, use the logged in user ID
if (undefined === userId && Meteor.isClient) {
userId = Meteor.userId();
}

var existing = UserSessionCollection.findOne({ userId: userId});
if (existing) {
var list = {};
Expand Down