formatted code

This commit is contained in:
Yaman Qalieh 2016-08-12 12:13:26 -04:00
parent 4f67ff8194
commit 513e19046c

View File

@ -1,159 +1,218 @@
import { Meteor } from 'meteor/meteor'; import {
import { Mongo } from 'meteor/mongo'; Meteor
} from 'meteor/meteor';
import {
Mongo
} from 'meteor/mongo';
_uuid4 = function(cc) { _uuid4 = function(cc) {
var rr = Math.random() * 16 | 0; var rr = Math.random() * 16 | 0;
return (cc === 'x' ? rr : (rr & 0x3 | 0x8)).toString(16); return (cc === 'x' ? rr : (rr & 0x3 | 0x8)).toString(16);
}; };
worktype = ["test", "quiz", "project", "normal"]; worktype = ["test", "quiz", "project", "normal"];
Meteor.methods({ Meteor.methods({
'genCode': function() { 'genCode': function() {
return 'xxxxxx'.replace(/[x]/g, _uuid4); return 'xxxxxx'.replace(/[x]/g, _uuid4);
}, },
'createSchool': function(schoolname) { 'createSchool': function(schoolname) {
// if superadmin, no need for approval // if superadmin, no need for approval
if (Meteor.user() !== null && if (Meteor.user() !== null &&
schools.findOne({name:input.school}) !== null && schools.findOne({
schools.findOne({status: false, creator: Meteor.userId()}) !== null) { name: input.school
}) !== null &&
schools.findOne({
status: false,
creator: Meteor.userId()
}) !== null) {
schools.insert({name: schoolname, status: false, creator: Meteor.userId()}); schools.insert({
} name: schoolname,
}, status: false,
'deleteSchool': function(schoolid) { creator: Meteor.userId()
// alanning:roles implementation here });
schools.remove({_id: schoolid}); }
}, },
'createClass': function(input) { 'deleteSchool': function(schoolid) {
// if superadmin, no need for approval // alanning:roles implementation here
classes.schema.validate(input); schools.remove({
if(Meteor.user() !== null && _id: schoolid
classes.find({status:false, admin:Meteor.userId()}).fetch().length < 5 &&
schools.findOne({name:input.school}) !== null) {
input.status = false;
input.subscribers = 0;
input.admin = Meteor.userId();
if (input.privacy) {
Meteor.call('genCode', function(error, result) {
input.code = result;
}); });
} else { },
input.code = ""; 'createClass': function(input) {
} // if superadmin, no need for approval
if (input.category != "class" && input.category != "club") { classes.schema.validate(input);
input.category = "other"; if (Meteor.user() !== null &&
} classes.find({
input.moderators = []; status: false,
input.banned = []; admin: Meteor.userId()
input.blockEdit = []; }).fetch().length < 5 &&
classes.insert(input); schools.findOne({
Meteor.call('joinClass',classes.findOne(input)._id, input.code, function(error,result){}); name: input.school
return 1; }) !== null) {
} else {
return 0;
}
},
'deleteClass': function(classid) {
found = classes.findOne({_id: classid});
// Add roles
if (Meteor.user() !== null && found !== null && found.admin === Meteor.user()._id) {
classes.remove({_id: classid});
}
},
'createWork': function(input) {
ref = new Date();
month = ref.getMonth + 1;
ref = new Date(ref.getFullYear()+ "-" + month.toString() + "-" + ref.getDate()).getTime();
work.schema.validate(input);
found = Meteor.findOne({_id: input.class});
if (Meteor.user() !== null && input.status = false;
found !== null && input.subscribers = 0;
found.subscribers.indexOf(Meteor.userId()) != -1 && input.admin = Meteor.userId();
found.banned.indexOf(Meteor.userId()) === -1 && if (input.privacy) {
found.blockEdit.indexOf(Meteor.userId()) === -1 && Meteor.call('genCode', function(error, result) {
input.dueDate.getTime() >= ref && worktype.indexOf(type) != -1 && input.code = result;
input.name.length <= 50) { });
} else {
input.submittor = Meteor.userId(); input.code = "";
input.confirmations = [Meteor.userId()]; }
input.reports = []; if (input.category != "class" && input.category != "club") {
input.done = []; input.category = "other";
input.numberdone = 0; }
work.insert(input); input.moderators = [];
} input.banned = [];
input.blockEdit = [];
}, classes.insert(input);
'deleteWork': function(workid) { Meteor.call('joinClass', classes.findOne(input)._id, input.code, function(error, result) {});
// Add security here
work.remove({_id: workid});
},
'editProfile': function(change) {
current = Meteor.user().profile;
current.school = change.school;
current.grade = change.grade;
current.description = change.description;
current.avatar = change.avatar;
current.banner = change.banner;
current.preferences = change.preferences;
if (schools.findOne({name:current.school}) !== null &&
Number.isInteger(current.grade) &&
current.grade >= 9 && current.grade <= 12 &&
current.description.length <= 50) {
Meteor.users.update({_id: Meteor.userId()}, {$set: {profile: current}});
return 1;
} else {
return 0;
}
},
'joinClass': function(input) {
change = input[0];
pass = input[1];
if(Meteor.user().profile.classes === undefined) {
curr = Meteor.user().profile;
curr.classes = [];
Meteor.users.update({_id: Meteor.userId()}, {$set: {profile: curr}});
}
prof = Meteor.user().profile;
found = classes.findOne({_id: change, status: true});
if (Meteor.user() !== null &&
found !== null &&
pass === found.code &&
!found.banned.includes(Meteor.userId()) &&
!prof.classes.includes(change)) {
current = Meteor.user().profile;
current.classes.append(change);
Meteor.users.update({_id: Meteor.userId()}, {$set: {profile: current}});
return 1;
} else {
return 0;
}
},
'leaveClass': function(change) {
if (Meteor.user() !== null) {
profile = Meteor.user().profile;
index = profile.classes.indexOf(change);
if (index >= 0) {
if (classes.findOne({_id: change}).admin != Meteor.userId()) {
profile.classes.splice(index, 1);
Meteor.users.update({_id: Meteor.userId()}, {$set: {profile: current}});
return 1; return 1;
} else { } else {
throw "You are currently the admin of this class. Transfer ownership in order to leave this class."; return 0;
} }
} },
'deleteClass': function(classid) {
found = classes.findOne({
_id: classid
});
// Add roles
if (Meteor.user() !== null && found !== null && found.admin === Meteor.user()._id) {
classes.remove({
_id: classid
});
}
},
'createWork': function(input) {
ref = new Date();
month = ref.getMonth + 1;
ref = new Date(ref.getFullYear() + "-" + month.toString() + "-" + ref.getDate()).getTime();
work.schema.validate(input);
found = Meteor.findOne({
_id: input.class
});
if (Meteor.user() !== null &&
found !== null &&
found.subscribers.indexOf(Meteor.userId()) != -1 &&
found.banned.indexOf(Meteor.userId()) === -1 &&
found.blockEdit.indexOf(Meteor.userId()) === -1 &&
input.dueDate.getTime() >= ref && worktype.indexOf(type) != -1 &&
input.name.length <= 50) {
input.submittor = Meteor.userId();
input.confirmations = [Meteor.userId()];
input.reports = [];
input.done = [];
input.numberdone = 0;
work.insert(input);
}
},
'deleteWork': function(workid) {
// Add security here
work.remove({
_id: workid
});
},
'editProfile': function(change) {
current = Meteor.user().profile;
current.school = change.school;
current.grade = change.grade;
current.description = change.description;
current.avatar = change.avatar;
current.banner = change.banner;
current.preferences = change.preferences;
if (schools.findOne({
name: current.school
}) !== null &&
Number.isInteger(current.grade) &&
current.grade >= 9 && current.grade <= 12 &&
current.description.length <= 50) {
Meteor.users.update({
_id: Meteor.userId()
}, {
$set: {
profile: current
}
});
return 1;
} else {
return 0;
}
},
'joinClass': function(input) {
change = input[0];
pass = input[1];
if (Meteor.user().profile.classes === undefined) {
curr = Meteor.user().profile;
curr.classes = [];
Meteor.users.update({
_id: Meteor.userId()
}, {
$set: {
profile: curr
}
});
}
prof = Meteor.user().profile;
found = classes.findOne({
_id: change,
status: true
});
if (Meteor.user() !== null &&
found !== null &&
pass === found.code &&
!found.banned.includes(Meteor.userId()) &&
!prof.classes.includes(change)) {
current = Meteor.user().profile;
current.classes.append(change);
Meteor.users.update({
_id: Meteor.userId()
}, {
$set: {
profile: current
}
});
return 1;
} else {
return 0;
}
},
'leaveClass': function(change) {
if (Meteor.user() !== null) {
profile = Meteor.user().profile;
index = profile.classes.indexOf(change);
if (index >= 0) {
if (classes.findOne({
_id: change
}).admin != Meteor.userId()) {
profile.classes.splice(index, 1);
Meteor.users.update({
_id: Meteor.userId()
}, {
$set: {
profile: current
}
});
return 1;
} else {
throw "You are currently the admin of this class. Transfer ownership in order to leave this class.";
}
}
}
} }
}
}); });
function has(array, has) { function has(array, has) {
for(var i = 0; i < array.length; i++) { for (var i = 0; i < array.length; i++) {
if(array[i] === has) return true; if (array[i] === has) return true;
} }
return false; return false;
} }