From 01536a2e29d9e41b6428746523816eebd23eaf8a Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Sat, 2 Nov 2013 12:24:05 -0700 Subject: [PATCH] Revert "[#1711] back to Schema.Types.Mixed for user.tasks, this makes me sad :(" This reverts commit c0292d0842a763d998589df8e3e29ddb0c43ee2a. --- src/controllers/user.js | 8 +++----- src/models/challenge.js | 24 ++++++++++-------------- src/models/user.js | 31 ++++++++++++------------------- 3 files changed, 25 insertions(+), 38 deletions(-) diff --git a/src/controllers/user.js b/src/controllers/user.js index 986b5d36da..1d7414695a 100644 --- a/src/controllers/user.js +++ b/src/controllers/user.js @@ -40,6 +40,7 @@ api.marketBuy = function(req, res, next){ }) } + /* ------------------------------------------------------------------------ Tasks @@ -64,15 +65,12 @@ api.verifyTaskExists = function(req, res, next) { }; function deleteTask(user, task) { - var i = _.findIndex(user[task.type+'s'], {id:task.id}); - if (~i) { - user[task.type+'s'].splice(i, 1) - //user.markModified(task.type+'s') - } + user[task.type+'s'].id(task.id).remove(); }; function addTask(user, task) { task = helpers.taskDefaults(task); + task._id = task.id; user[task.type+'s'].unshift(task); return task; } diff --git a/src/models/challenge.js b/src/models/challenge.js index 497c9093dd..6ad5725168 100644 --- a/src/models/challenge.js +++ b/src/models/challenge.js @@ -2,6 +2,7 @@ var mongoose = require("mongoose"); var Schema = mongoose.Schema; var helpers = require('habitrpg-shared/script/helpers'); var _ = require('lodash'); +var TaskSchema = require('./task').schema; var Group = require('./group').model; var ChallengeSchema = new Schema({ @@ -9,10 +10,10 @@ var ChallengeSchema = new Schema({ name: String, shortName: String, description: String, - habits: [Schema.Types.Mixed], - dailys: [Schema.Types.Mixed], - todos: [Schema.Types.Mixed], - rewards: [Schema.Types.Mixed], + habits: [TaskSchema], + dailys: [TaskSchema], + todos: [TaskSchema], + rewards: [TaskSchema], leader: {type: String, ref: 'User'}, group: {type: String, ref: 'Group'}, timestamp: {type: Date, 'default': Date.now}, @@ -30,11 +31,6 @@ ChallengeSchema.virtual('tasks').get(function () { // FIXME this isn't always triggered, since we sometimes use update() or findByIdAndUpdate() // @see https://github.com/LearnBoost/mongoose/issues/964 ChallengeSchema.pre('save', function(next){ - // @see comment in user.js pre(save) - this.markModified('habits'); - this.markModified('dailys'); - this.markModified('todos'); - this.markModified('rewards'); this.memberCount = _.size(this.members); next() }) @@ -43,11 +39,11 @@ ChallengeSchema.methods.toJSON = function(){ var doc = this.toObject(); doc.memberCount = doc.members ? _.size(doc.members) : doc.memberCount; // @see pre('save') comment above doc._isMember = this._isMember; -// _.each(['habits','dailys','todos','rewards'], function(type){ -// _.each(doc[type],function(task){ -// task.id = task._id || task.id; -// }) -// }) + _.each(['habits','dailys','todos','rewards'], function(type){ + _.each(doc[type],function(task){ + task.id = task._id; + }) + }) return doc; } diff --git a/src/models/user.js b/src/models/user.js index df2396e6b3..48ff54f540 100644 --- a/src/models/user.js +++ b/src/models/user.js @@ -8,6 +8,7 @@ var mongoose = require("mongoose"); var Schema = mongoose.Schema; var helpers = require('habitrpg-shared/script/helpers'); var _ = require('lodash'); +var TaskSchema = require('./task').schema; // User Schema // ----------- @@ -190,10 +191,10 @@ var UserSchema = new Schema({ challenges: [{type: 'String', ref:'Challenge'}], - habits: [Schema.Types.Mixed], - dailys: [Schema.Types.Mixed], - todos: [Schema.Types.Mixed], - rewards: [Schema.Types.Mixed], + habits: [TaskSchema], + dailys: [TaskSchema], + todos: [TaskSchema], + rewards: [TaskSchema], }, { strict: true, @@ -208,11 +209,12 @@ UserSchema.methods.toJSON = function() { doc.filters = {}; doc._tmp = this._tmp; // be sure to send down drop notifs -// _.each(['habits','dailys','todos','rewards'], function(type){ -// _.each(doc[type],function(task){ -// task.id = task._id || task.id; -// }) -// }) + // TODO why isnt' this happening automatically given the TaskSchema.methods.toJSON above? + _.each(['habits','dailys','todos','rewards'], function(type){ + _.each(doc[type],function(task){ + task.id = task._id; + }) + }) return doc; }; @@ -227,16 +229,7 @@ UserSchema.virtual('tasks').get(function () { // Custom setter/getter virtuals? UserSchema.pre('save', function(next) { - // I finally figured out when markModified() is required - when using Schema.Type.Mixed or Array. Let's say you - // have `tasks: [{text:String, notes:String}]`. Because it's a defined schema, Mongoose can track attribute changes. - // But if you have `tasks: [Schema.Types.Mixed]`, Mongoose can't track underlying attr changes - only top-level changes - // (via splice(), push/unshift(), etc). Adding a workaround here, which is very inefficient - we'll want to find - // each location where a task attr is modified and use `user.markModified(task.type+'s')` - // (@see https://github.com/HabitRPG/habitrpg/issues/1711#issuecomment-27626467) - this.markModified('habits'); - this.markModified('dailys'); - this.markModified('todos'); - this.markModified('rewards'); + //this.markModified('tasks'); if (!this.profile.name) { var fb = this.auth.facebook;