diff --git a/public/js/controllers/challengesCtrl.js b/public/js/controllers/challengesCtrl.js index 4123c464ef..636ef0cedb 100644 --- a/public/js/controllers/challengesCtrl.js +++ b/public/js/controllers/challengesCtrl.js @@ -35,7 +35,8 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'User', 'Challenge leader: User.user._id, group: null, timestamp: +(new Date), - members: [] + members: [], + official: false }); }; diff --git a/src/controllers/challenges.js b/src/controllers/challenges.js index 0746af3ae3..c4df7535f4 100644 --- a/src/controllers/challenges.js +++ b/src/controllers/challenges.js @@ -33,10 +33,10 @@ api.list = function(req, res) { {group: 'habitrpg'} // public group ] }) - .select('name leader description group members prize') + .select('name leader description group members prize official') .populate('group', '_id name') .populate('leader', 'profile.name') - .sort('-timestamp') + .sort('-official -timestamp') .exec(cb); } ], function(err, challenges){ @@ -129,6 +129,7 @@ api.create = function(req, res){ waterfall = waterfall.concat([ function(cb) { // if we're dealing with prize above, arguemnts will be `group, numRows, cb` - else `cb` req.body.leader = user._id; + req.body.official = user.contributor.admin && req.body.official; var chal = new Challenge(req.body); // FIXME sanitize chal.members.push(user._id); chal.save(cb) diff --git a/src/models/challenge.js b/src/models/challenge.js index 4cf757860e..ce9dc19d84 100644 --- a/src/models/challenge.js +++ b/src/models/challenge.js @@ -10,6 +10,7 @@ var ChallengeSchema = new Schema({ name: String, shortName: String, description: String, + official: {type: Boolean,'default':false}, habits: [TaskSchemas.HabitSchema], dailys: [TaskSchemas.DailySchema], todos: [TaskSchemas.TodoSchema], diff --git a/test/api.mocha.coffee b/test/api.mocha.coffee index d7a28f10a0..e7d6d2d6de 100644 --- a/test/api.mocha.coffee +++ b/test/api.mocha.coffee @@ -157,6 +157,7 @@ describe 'API', -> todos: [{type:'todo', text:'Challenge Todo', notes:'Challenge Notes'}] rewards: [] habits: [] + official: true }) .end (res) -> expectCode res, 200 @@ -168,6 +169,7 @@ describe 'API', -> expect(_user.dailys[_user.dailys.length-1].text).to.be('Challenge Daily') updateTodo = _user.todos[_user.todos.length-1] expect(updateTodo.text).to.be('Challenge Todo') + expect(challenge.official).to.be false done() it 'User updates challenge notes', (done) -> @@ -198,6 +200,26 @@ describe 'API', -> expect(res.body.todos[res.body.todos.length-1].notes).to.be('User overriden notes') done() + it 'Admin creates a challenge', (done) -> + User.findByIdAndUpdate _id, {$set:{'contributor.admin':true}}, (err,_user) -> + expect(err).to.not.be.ok + + async.parallel [ + (cb)-> + request.post("#{baseURL}/challenges") + .send({group:group._id, dailys: [], todos: [], rewards: [], habits: [], official: false}).end (res) -> + expect(res.body.official).to.be false + cb() + (cb)-> + request.post("#{baseURL}/challenges") + .send({group:group._id, dailys: [], todos: [], rewards: [], habits: [], official: true}).end (res) -> + expect(res.body.official).to.be true + cb() + ], done + + + + ############ # Batch Update ############ diff --git a/views/options/social/challenges.jade b/views/options/social/challenges.jade index a1713870ff..923f5045a5 100644 --- a/views/options/social/challenges.jade +++ b/views/options/social/challenges.jade @@ -108,12 +108,19 @@ script(type='text/ng-template', id='partials/options.social.challenges.html') span(ng-show='newChallenge.group=="habitrpg"').  Minimum 1 Gem for public challenges (helps prevent spam, it really does). + .option-medium(ng-if='user.contributor.admin') + label.checkbox + input(type='checkbox', ng-model='newChallenge.official') + | Official HabitRPG Challenge + habitrpg-tasks(main=false, obj='newChallenge') // Challenges list .accordion-group(ng-repeat='challenge in challenges|filter:filterChallenges') .accordion-heading ul.pull-right.challenge-accordion-header-specs + li(ng-if='challenge.official') + span.label.label-success Official HabitRPG Challenge li {{challenge.group.name}} li | by