diff --git a/website/src/models/user.js b/website/src/models/user.js index 7266e0c1b7..61fe6bd140 100644 --- a/website/src/models/user.js +++ b/website/src/models/user.js @@ -481,68 +481,7 @@ UserSchema.pre('save', function(next) { // Populate new users with default content if (this.isNew){ - //TODO for some reason this doesn't work here: `_.merge(this, shared.content.userDefaults);` - var self = this; - var taskTypes; - if (self.registeredThrough === "habitica-web") { - taskTypes = ['habits', 'dailys', 'todos', 'rewards', 'tags']; - } else { - taskTypes = ['todos', 'tags'] - } - _.each(taskTypes, function(taskType){ - self[taskType] = _.map(shared.content.userDefaults[taskType], function(task){ - var newTask = _.cloneDeep(task); - - // Render task's text and notes in user's language - if(taskType === 'tags'){ - // tasks automatically get id=helpers.uuid() from TaskSchema id.default, but tags are Schema.Types.Mixed - so we need to manually invoke here - newTask.id = shared.uuid(); - newTask.name = newTask.name(self.preferences.language); - }else{ - newTask.text = newTask.text(self.preferences.language); - if(newTask.notes) { - newTask.notes = newTask.notes(self.preferences.language); - } - - if(newTask.checklist){ - newTask.checklist = _.map(newTask.checklist, function(checklistItem){ - checklistItem.text = checklistItem.text(self.preferences.language); - return checklistItem; - }); - } - } - - return newTask; - }); - }); - if (self.registeredThrough === "habitica-web") { - this.flags.tutorial.common.habits = true; - this.flags.tutorial.common.dailies = true; - this.flags.tutorial.common.todos = true; - this.flags.tutorial.common.rewards = true; - this.flags.tutorial.common.party = true; - this.flags.tutorial.common.pets = true; - this.flags.tutorial.common.gems = true; - this.flags.tutorial.common.skills = true; - this.flags.tutorial.common.classes = true; - this.flags.tutorial.common.tavern = true; - this.flags.tutorial.common.equipment = true; - this.flags.tutorial.common.items = true; - } else { - self.flags.showTour = false; - self.flags.tour.intro = -2; - self.flags.tour.classes = -2; - self.flags.tour.stats = -2; - self.flags.tour.tavern = -2; - self.flags.tour.party = -2; - self.flags.tour.guilds = -2; - self.flags.tour.challenges = -2; - self.flags.tour.market = -2; - self.flags.tour.pets = -2; - self.flags.tour.mounts = -2; - self.flags.tour.hall = -2; - self.flags.tour.equipment = -2; - } + _populateDefaultsForNewUser(this); } //this.markModified('tasks'); @@ -635,6 +574,57 @@ UserSchema.methods.unlink = function(options, cb) { self.save(cb); } +function _populateDefaultsForNewUser(user) { + var taskTypes; + + if (user.registeredThrough === "habitica-web") { + taskTypes = ['habits', 'dailys', 'todos', 'rewards', 'tags']; + + _.each(user.flags.tutorial.common, function(value, section) { + user.flags.tutorial.common[section] = true; + }); + } else { + taskTypes = ['todos', 'tags'] + + user.flags.showTour = false; + + _.each(user.flags.tour, function(value, section) { + user.flags.tour[section] = -2; + }); + } + + _populateDefaultTasks(user, taskTypes); +} + +function _populateDefaultTasks (user, taskTypes) { + _.each(taskTypes, function(taskType){ + user[taskType] = _.map(shared.content.userDefaults[taskType], function(task){ + var newTask = _.cloneDeep(task); + + // Render task's text and notes in user's language + if(taskType === 'tags'){ + // tasks automatically get id=helpers.uuid() from TaskSchema id.default, but tags are Schema.Types.Mixed - so we need to manually invoke here + newTask.id = shared.uuid(); + newTask.name = newTask.name(user.preferences.language); + }else{ + newTask.text = newTask.text(user.preferences.language); + if(newTask.notes) { + newTask.notes = newTask.notes(user.preferences.language); + } + + if(newTask.checklist){ + newTask.checklist = _.map(newTask.checklist, function(checklistItem){ + checklistItem.text = checklistItem.text(user.preferences.language); + return checklistItem; + }); + } + } + + return newTask; + }); + }); +} + module.exports.schema = UserSchema; module.exports.model = mongoose.model("User", UserSchema); // Initially export an empty object so external requires will get