optimize until (including) trackUserInClass

This commit is contained in:
Yaman Qalieh 2016-08-30 22:50:54 -04:00
parent 2dc29d0f16
commit b421e6a256

View File

@ -14,45 +14,39 @@ var superadmins = [
]; ];
var worktype = ["test", "quiz", "project", "normal", "other"]; var worktype = ["test", "quiz", "project", "normal", "other"];
var possiblelist = ["moderators", "banned"];
// Adds roles to superadmins // Adds roles to superadmins
// Not necessary on every run // Not necessary on every run
// Makes superadmins superadmins // Makes superadmins
for (var i = 0; i < superadmins.length; i++) { for (var i = 0; i < superadmins.length; i++) {
var superadmin = superadmins[i]; superadmin = Meteor.users.findOne({
if (Meteor.users.findOne({ "services.google.email": superadmins[i]
"services.google.email": superadmin });
})) { if (superadmin && !(Roles.userIsInRole(superadmin._id, 'superadmin'))) {
var userId = Meteor.users.findOne({ Roles.addUsersToRoles(superadmin._id, 'superadmin');
"services.google.email": superadmin
})._id;
Roles.addUsersToRoles(userId, ['superadmin']);
} }
} }
//
Meteor.publish('schools', function() { Meteor.publish('schools', function() {
return schools.find(); return schools.find();
}); });
// Returns the code for classes (for debug) // Returns the code for classes (for debug)
Meteor.publish('classes', function() { Meteor.publish('classes', function() {
if (Roles.userIsInRole(this.userId, ['superadmin', 'admin'])) { if (Roles.userIsInRole(this.userId, ['superadmin', 'admin'])) {
return classes.find(); return classes.find();
} else { } else {
// Return user classes (if private) and public classes. // Return user classes and all _public_ classes.
var userclasses = Meteor.users.findOne(this.userId).profile.classes; var userclasses = Meteor.users.findOne(this.userId).profile.classes;
if (userclasses !== undefined) { if (userclasses) {
return classes.find({ return classes.find({
$or: [{ $or: [{
privacy: false privacy: false
}, { }, {
_id: { _id: {
$in: Meteor.users.findOne(this.userId).profile.classes $in: userclasses
} }
}] }]
}, { }, {
@ -72,33 +66,30 @@ Meteor.publish('classes', function() {
} }
}); });
} else { } else {
Meteor.call('createProfile', this.userId); Meteor.call('createProfile', this.userId, function(error, result) {
return classes.find({ return result;
_id: null
}); });
} }
} }
}); });
//Gives everything in work if superadmin // Gives everything in work if superadmin
Meteor.publish('work', function() { Meteor.publish('work', function() {
if (Roles.userIsInRole(this.userId, ['superadmin', 'admin'])) { if (Roles.userIsInRole(this.userId, ['superadmin', 'admin'])) {
return work.find(); return work.find();
} else { } else {
var userclasses = Meteor.users.findOne(this.userId).profile.classes; var userclasses = Meteor.users.findOne(this.userId).profile.classes;
if (userclasses !== undefined) { if (userclasses) {
return work.find({ return work.find({
// Only return work of enrolled classes // Only return work of enrolled classes
class: { class: {
$in: Meteor.users.findOne(this.userId).profile.classes $in: userclasses
} }
}); });
} else { } else {
Meteor.call('createProfile', this.userId, function(error, result) {
Meteor.call('createProfile', this.userId); return result;
return classes.find({
_id: null
}); });
} }
@ -138,22 +129,26 @@ Security.permit(['insert', 'update', 'remove']).collections([schools, classes, w
Meteor.methods({ Meteor.methods({
//Stuff that is accessible in client // Stuff that is accessible in client
//Generates private codes for classes - like google classroom // Generates private codes for classes - like google classroom
'genCode': function() { 'genCode': function(privacy) {
var currcode = Math.random().toString(36).substr(2, 6); if (privacy) {
while (classes.findOne({ var currcode = Math.random().toString(36).substr(2, 6);
while (classes.findOne({
code: currcode code: currcode
}) !== undefined) { })) {
currcode = Math.random().toString(36).substr(2, 6); currcode = Math.random().toString(36).substr(2, 6);
}
return currcode;
} else {
return "";
} }
return currcode;
}, },
// School Functions // School Functions
//Ability to create schools for selections // Ability to create schools for selections
'createSchool': function(schoolname) { 'createSchool': function(schoolname) {
if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin'])) { if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin'])) {
schools.insert({ schools.insert({
@ -163,7 +158,7 @@ Meteor.methods({
throw "Unauthorized"; throw "Unauthorized";
} }
}, },
//Deletes school // Deletes school
'deleteSchool': function(schoolId) { 'deleteSchool': function(schoolId) {
if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin'])) { if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin'])) {
schools.remove({ schools.remove({
@ -177,31 +172,23 @@ Meteor.methods({
// Class Functions // Class Functions
'createClass': function(input) { 'createClass': function(input) {
classes.schema.validate(input); classes.schema.validate(input);
if (Meteor.user() !== null && if (Meteor.user() &&
classes.find({ classes.find({
status: false, status: false,
admin: Meteor.userId() admin: Meteor.userId()
}).fetch().length < 5 && }).fetch().length < 5 &&
schools.findOne({ schools.findOne({
name: input.school name: input.school
}) !== null) { })) {
if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin'])) { input.status = Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin']);
input.status = true;
} else {
input.status = false;
}
input.subscribers = [];
input.admin = Meteor.userId(); input.admin = Meteor.userId();
if (input.privacy) { Meteor.call('genCode', function(error, result) {
Meteor.call('genCode', function(error, result) { input.code = result;
input.code = result; });
});
} else {
input.code = "";
}
if (input.category != "class" && input.category != "club") { if (input.category != "class" && input.category != "club") {
input.category = "other"; input.category = "other";
} }
input.subscribers = [];
input.moderators = []; input.moderators = [];
input.banned = []; input.banned = [];
@ -209,7 +196,6 @@ Meteor.methods({
Meteor.call('joinClass', [result, input.code]); Meteor.call('joinClass', [result, input.code]);
}); });
return 1;
} else { } else {
throw "Unauthorized"; throw "Unauthorized";
} }
@ -226,28 +212,25 @@ Meteor.methods({
} }
}, },
'changeAdmin': function(input) { 'changeAdmin': function(input) {
var userId = input[0];
var classId = input[1];
var found = Meteor.users.find({ var found = Meteor.users.find({
_id: input[0] _id: userId
}); });
var foundclass = classes.find({ var foundclass = classes.find({
_id: input[1] _id: classId
}); });
if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin'])) { if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin']) ||
(found && foundclass && foundclass.admin == Meteor.userId() &&
!_.contains(foundclass.banned, userId) &&
_.contains(foundclass.subscribers, userId)
)) {
classes.update({ classes.update({
_id: input[1] _id: classId
}, { }, {
$set: { $set: {
admin: input[0] admin: userId
}
});
} else if (found && foundclass && foundclass.admin == Meteor.userId() &&
foundclass.banned.indexOf(input[0]) === -1 &&
foundclass.subscribers.indexOf(input[0]) !== -1) {
classes.update({
_id: input[1]
}, {
$set: {
admin: input[0]
} }
}); });
} else { } else {
@ -258,52 +241,31 @@ Meteor.methods({
// Allows someone to manage the class // Allows someone to manage the class
'trackUserInClass': function(input) { 'trackUserInClass': function(input) {
var foundclass = classes.findOne({ var userId = input[0];
_id: input[1] var classId = input[1];
});
var userlist = input[2]; var userlist = input[2];
var index = possiblelist.indexOf(input[2]); var dowhat = input[3];
var set = {}; var foundclass = classes.findOne({
set[userlist] = foundclass[userlist].concat(input[0]); _id: classId
if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin'])) { });
classes.update({ classlist = foundclass[userlist];
_id: input[1] var index = ["moderators", "banned"].indexOf(userlist);
}, { var set = foundclass;
$set: set var presence = false;
}); if (dowhat) {
} else if (foundclass && foundclass.admin == Meteor.userId() && index !== -1 && set[userlist] = set[userlist].concat(userId);
(index === 1 || foundclass.moderators.indexOf(Meteor.userId()) !== -1) && presence = true;
foundclass[userlist].indexOf(input[0]) === -1) {
classes.update({
_id: input[1]
}, {
$set: set
});
} else { } else {
throw "Unauthorized"; set[userlist] = _.without(set[userlist], userId);
} }
},
'untrackUserInClass': function(input) {
var foundclass = classes.findOne({
_id: input[1]
});
var userlist = input[2];
var index = possiblelist.indexOf(input[2]);
var set = {};
foundclass[userlist].splice(foundclass[userlist].indexOf(input[0]), 1);
set[userlist] = foundclass[userlist];
if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin'])) { if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin']) ||
(foundclass && foundclass.admin == Meteor.userId() && index !== -1 &&
(index === 0 ^ _.contains(foundclass.moderators, Meteor.userId())) &&
(!_.contains(classlist, userId) ^ presence))) {
classes.update({ classes.update({
_id: input[1] _id: classId
}, {
$set: set
});
} else if (foundclass && foundclass.admin == Meteor.userId() && index !== -1 &&
(index === 1 || foundclass.moderators.indexOf(Meteor.userId()) !== -1) &&
foundclass[userlist].indexOf(input[0]) !== -1) {
classes.update({
_id: input[1]
}, { }, {
$set: set $set: set
}); });
@ -315,7 +277,7 @@ Meteor.methods({
var found = classes.findOne({ var found = classes.findOne({
_id: classid _id: classid
}); });
if (Meteor.user() !== null && found !== null && if (Meteor.user() && found &&
(found.admin === Meteor.user()._id || Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin']))) { (found.admin === Meteor.user()._id || Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin']))) {
for (var i = 0; i < found.subscribers.length; i++) { for (var i = 0; i < found.subscribers.length; i++) {
var current = Meteor.users.findOne({ var current = Meteor.users.findOne({
@ -350,12 +312,11 @@ Meteor.methods({
_id: input.class _id: input.class
}); });
if (Meteor.user() !== null && if (Meteor.user() &&
found !== null && found && _.contains(Meteor.user().profile.classes, input.class) &&
Meteor.user().profile.classes.indexOf(input.class) !== -1 &&
found.banned.indexOf(Meteor.userId()) === -1 && found.banned.indexOf(Meteor.userId()) === -1 &&
input.dueDate instanceof Date && input.dueDate.getTime() >= ref && input.dueDate instanceof Date && input.dueDate.getTime() >= ref &&
worktype.indexOf(input.type) != -1 && _.contains(worktype, input.type) &&
input.name.length <= 50 && input.description.length <= 150) { input.name.length <= 50 && input.description.length <= 150) {
input.confirmations = [Meteor.userId()]; input.confirmations = [Meteor.userId()];
@ -385,8 +346,8 @@ Meteor.methods({
}, { }, {
$set: change $set: change
}); });
} else if (authorized.indexOf(Meteor.userId()) != -1) { } else if (_.contains(authorized, Meteor.userId())) {
if (change.name.length <= 50 && change.description.length <= 150 && worktype.indexOf(change.type) != -1) { if (change.name.length <= 50 && change.description.length <= 150 && _.contains(worktype, change.type)) {
work.update({ work.update({
_id: change._id _id: change._id
}, { }, {
@ -401,9 +362,9 @@ Meteor.methods({
}); });
} }
} else if (Meteor.userId() === work.findOne({ } else if (Meteor.userId() === work.findOne({
_id: change._id _id: change._id
}).creator) { }).creator) {
if (change.name.length <= 50 && worktype.indexOf(change.type) != -1 && if (change.name.length <= 50 && _.contains(worktype, change.type) &&
change.dueDate instanceof Date && change.dueDate.getTime() >= ref) { change.dueDate instanceof Date && change.dueDate.getTime() >= ref) {
work.update({ work.update({
_id: change._id _id: change._id
@ -432,8 +393,8 @@ Meteor.methods({
var user = Meteor.userId(); var user = Meteor.userId();
var foundsubs = currentclass.subscribers; var foundsubs = currentclass.subscribers;
if (typeof comment === "string" && comment.length <= 200 && if (typeof comment === "string" && comment.length <= 200 &&
foundsubs.indexOf(Meteor.userId()) != -1 && _.contains(foundsubs, Meteor.userId()) &&
currentclass.banned.indexOf(Meteor.userId()) === -1) { _.contains(currentclass.banned, Meteor.userId())) {
var commentInfo = { var commentInfo = {
"comment":input[0], "comment":input[0],
"user":user, "user":user,
@ -459,15 +420,15 @@ Meteor.methods({
var currentclass = classes.findOne({ var currentclass = classes.findOne({
_id: workobject.class _id: workobject.class
}); });
if (currentclass.subscribers.indexOf(Meteor.userId()) != -1 && ["confirmations", "reports", "done"].indexOf(input[1]) != -1) { if (_.contains(currentclass.subscribers, Meteor.userId()) && _.contains(["confirmations", "reports", "done"], input[1])) {
var userindex = workobject[input[1]].indexOf(Meteor.userId()); var userindex = workobject[input[1]].indexOf(Meteor.userId());
if (userindex === -1) { if (userindex === -1) {
workobject[input[1]] = workobject[input[1]].concat(Meteor.userId()); workobject[input[1]] = workobject[input[1]].concat(Meteor.userId());
if (input[1] === "confirmations" && if (input[1] === "confirmations" &&
workobject.reports.indexOf(Meteor.userId()) !== -1) { _.contains(workobject.reports, Meteor.userId())) {
workobject.reports.splice(userindex, 1); workobject.reports.splice(userindex, 1);
} else if (input[1] === "reports" && } else if (input[1] === "reports" &&
workobject.confirmations.indexOf(Meteor.userId()) !== -1) { _.contains(workobject.confirmations, Meteor.userId())) {
workobject.confirmations.splice(userindex, 1); workobject.confirmations.splice(userindex, 1);
} }
} else { } else {
@ -490,7 +451,7 @@ Meteor.methods({
}); });
var authorized = currentclass.moderators.concat(currentclass.admin); var authorized = currentclass.moderators.concat(currentclass.admin);
if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin']) || if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin']) ||
authorized.indexOf(Meteor.userId()) != -1) { _.contains(authorized, Meteor.userId())) {
work.remove({ work.remove({
_id: workId _id: workId
@ -514,8 +475,8 @@ Meteor.methods({
current.banner = change.banner; current.banner = change.banner;
current.preferences = change.preferences; current.preferences = change.preferences;
if (schools.findOne({ if (schools.findOne({
name: current.school name: current.school
}) !== null && }) !== null &&
Number.isInteger(current.grade) && Number.isInteger(current.grade) &&
current.grade >= 9 && current.grade <= 12) { current.grade >= 9 && current.grade <= 12) {
@ -546,6 +507,7 @@ Meteor.methods({
profile: current profile: current
} }
}); });
return classes.find({_id: null});
}, },
'joinClass': function(input) { 'joinClass': function(input) {
var change = input[0]; var change = input[0];
@ -558,7 +520,7 @@ Meteor.methods({
if (Meteor.user() !== null && if (Meteor.user() !== null &&
found !== null && found !== null &&
pass === found.code && pass === found.code &&
prof.classes.indexOf(change) === -1) { !_.contains(prof.classes, change)) {
var foundsubs = found.subscribers; var foundsubs = found.subscribers;
classes.update({ classes.update({
_id: found._id _id: found._id
@ -589,7 +551,7 @@ Meteor.methods({
}); });
var current = Meteor.user().profile; var current = Meteor.user().profile;
if (found !== undefined && input !== undefined && if (found !== undefined && input !== undefined &&
current.classes.indexOf(found._id) === -1) { !_.contains(current.classes, found._id)) {
classes.update({ classes.update({
_id: found._id _id: found._id
}, { }, {
@ -616,8 +578,8 @@ Meteor.methods({
var index = current.classes.indexOf(change); var index = current.classes.indexOf(change);
if (index >= 0) { if (index >= 0) {
if (classes.findOne({ if (classes.findOne({
_id: change _id: change
}).admin != Meteor.userId()) { }).admin != Meteor.userId()) {
current.classes.splice(index, 1); current.classes.splice(index, 1);
Meteor.users.update({ Meteor.users.update({
_id: Meteor.userId() _id: Meteor.userId()