64 lines
1.9 KiB
JavaScript
Raw Normal View History

2016-08-01 19:30:32 +08:00
import { Meteor } from 'meteor/meteor';
2016-08-07 00:32:19 -04:00
import { Mongo } from 'meteor/mongo';
2016-08-01 19:30:32 +08:00
2016-08-08 05:39:20 -04:00
_uuid4 = function(cc) {
var rr = Math.random() * 16 | 0;
return (cc === 'x' ? rr : (rr & 0x3 | 0x8)).toString(16);
}
2016-08-07 15:21:39 -04:00
2016-08-08 20:55:02 -04:00
Meteor.methods({
'genCode': function() {
return 'xxxxxx'.replace(/[x]/g, _uuid4);
},
2016-08-08 21:05:25 -04:00
//No Security
2016-08-08 20:55:02 -04:00
'createClass': function(input) {
2016-08-09 16:56:30 -04:00
classes.schema.validate(input);
2016-08-09 17:10:08 -04:00
if(Meteor.user() != null && classes.find({status:false, admin:Meteor.userId()}).fetch().length < 5 &&
schools.find({name:input.school}).fetch().length > 0 && input.status === false) {
2016-08-09 16:56:30 -04:00
input.subscribers = 0;
input.admin = Meteor.userId()
if (input.privacy) {
2016-08-09 17:10:08 -04:00
Meteor.call('genCode', function(error, result) {
input.code = result;
});
2016-08-09 16:56:30 -04:00
} else {
input.code = "";
}
if (input.category != "class" && input.category != "club") {
input.category = "other";
}
input.moderators = []
input.banned = []
input.blockEdit = []
2016-08-08 20:55:02 -04:00
classes.insert(input);
2016-08-09 17:10:08 -04:00
Meteor.call('joinClass',input.name, input.code, function(error,result){});
2016-08-09 16:56:30 -04:00
return 1;
} else {
return 0;
}
2016-08-08 20:55:02 -04:00
},
'editProfile': function(change) {
current = Meteor.user().profile;
current.school = change[0];
current.grade = change[1];
if (schools.find({name:current.school}).fetch().length > 0 && Number.isInteger(current.grade) &&
current.grade >= 9 && current.grade <= 12) {
Meteor.users.update({_id: Meteor.userId()}, {$set: {profile: current}});
return 1;
} else {
return 0;
}
},
'joinClass': function(change, pass) {
2016-08-09 16:56:30 -04:00
found = classes.find({name: change, status: true}).fetch();
2016-08-08 20:55:02 -04:00
if (Meteor.user() != null && found.length > 0 && pass === found[0].code) {
2016-08-08 20:39:15 -04:00
current = Meteor.user().profile;
2016-08-08 20:55:02 -04:00
current.classes.append(change);
Meteor.users.update({_id: Meteor.userId()}, {$set: {profile: current}});
return 1;
} else {
return 0;
2016-08-08 20:09:36 -04:00
}
2016-08-08 20:55:02 -04:00
}
});