Subdocs & script templates. Migrate the API from User.tasks =>
User.habits/dailys/todos/rewards. Move /#/tasks & /#/options page loading from server-sent html to everything loaded in the page as script templates (including necessary fixes for adsense). NOTE: this commit won't work, it depends a bit on the *next* commit with Challenges functionality, but I wanted to separate it out a bit for clarity
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
// User.js
|
||||
// =======
|
||||
// Defines the user data model (schema) for use via the API.
|
||||
|
||||
// Dependencies
|
||||
// ------------
|
||||
var mongoose = require("mongoose");
|
||||
var Schema = mongoose.Schema;
|
||||
var helpers = require('habitrpg-shared/script/helpers');
|
||||
var _ = require('lodash');
|
||||
|
||||
// Task Schema
|
||||
// -----------
|
||||
|
||||
var TaskSchema = new Schema({
|
||||
history: [{date:Date, value:Number}],
|
||||
_id:{type: String,'default': helpers.uuid},
|
||||
text: String,
|
||||
notes: {type: String, 'default': ''},
|
||||
tags: Schema.Types.Mixed, //{ "4ddf03d9-54bd-41a3-b011-ca1f1d2e9371" : true },
|
||||
type: {type:String, 'default': 'habit'}, // habit, daily
|
||||
up: {type: Boolean, 'default': true},
|
||||
down: {type: Boolean, 'default': true},
|
||||
value: {type: Number, 'default': 0},
|
||||
completed: {type: Boolean, 'default': false},
|
||||
priority: {type: String, 'default': '!'}, //'!!' // FIXME this should be a number or something
|
||||
repeat: {type: Schema.Types.Mixed, 'default': {m:1, t:1, w:1, th:1, f:1, s:1, su:1} },
|
||||
streak: {type: Number, 'default': 0}
|
||||
});
|
||||
|
||||
TaskSchema.methods.toJSON = function() {
|
||||
var doc = this.toObject();
|
||||
doc.id = doc._id;
|
||||
return doc;
|
||||
}
|
||||
TaskSchema.virtual('id').get(function(){
|
||||
return this._id;
|
||||
})
|
||||
|
||||
module.exports.schema = TaskSchema;
|
||||
module.exports.model = mongoose.model("Task", TaskSchema);
|
||||
+23
-34
@@ -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
|
||||
// -----------
|
||||
@@ -204,26 +205,12 @@ var UserSchema = new Schema({
|
||||
}
|
||||
],
|
||||
|
||||
// ### Tasks Definition
|
||||
// We can't define `tasks` until we move off Derby, since Derby requires dictionary of objects. When we're off, migrate
|
||||
// to array of subdocs
|
||||
challenges: [{type: 'String', ref:'Challenge'}],
|
||||
|
||||
tasks: Schema.Types.Mixed
|
||||
/*
|
||||
# history: {date, value}
|
||||
# id
|
||||
# notes
|
||||
# tags { "4ddf03d9-54bd-41a3-b011-ca1f1d2e9371" : true },
|
||||
# text
|
||||
# type
|
||||
# up
|
||||
# down
|
||||
# value
|
||||
# completed
|
||||
# priority: '!!'
|
||||
# repeat {m: true, t: true}
|
||||
# streak
|
||||
*/
|
||||
habits: [TaskSchema],
|
||||
dailys: [TaskSchema],
|
||||
todos: [TaskSchema],
|
||||
rewards: [TaskSchema],
|
||||
|
||||
}, {
|
||||
strict: true,
|
||||
@@ -237,7 +224,8 @@ var UserSchema = new Schema({
|
||||
// the underlying data will be modified too - so when we save back to the database, it saves it in the way Derby likes.
|
||||
// This will go away after the rewrite is complete
|
||||
|
||||
function transformTaskLists(doc) {
|
||||
//FIXME use this in migration
|
||||
/*function transformTaskLists(doc) {
|
||||
_.each(['habit', 'daily', 'todo', 'reward'], function(type) {
|
||||
// we use _.transform instead of a simple _.where in order to maintain sort-order
|
||||
doc[type + "s"] = _.reduce(doc[type + "Ids"], function(m, tid) {
|
||||
@@ -247,36 +235,37 @@ function transformTaskLists(doc) {
|
||||
return m;
|
||||
}, []);
|
||||
});
|
||||
}
|
||||
|
||||
UserSchema.post('init', function(doc) {
|
||||
transformTaskLists(doc);
|
||||
});
|
||||
}*/
|
||||
|
||||
UserSchema.methods.toJSON = function() {
|
||||
var doc = this.toObject();
|
||||
doc.id = doc._id;
|
||||
transformTaskLists(doc); // we need to also transform for our server-side routes
|
||||
|
||||
// FIXME? Is this a reference to `doc.filters` or just disabled code? Remove?
|
||||
/*
|
||||
// Remove some unecessary data as far as client consumers are concerned
|
||||
//_.each(['habit', 'daily', 'todo', 'reward'], function(type) {
|
||||
// delete doc["#{type}Ids"]
|
||||
//});
|
||||
//delete doc.tasks
|
||||
*/
|
||||
doc.filters = {};
|
||||
doc._tmp = this._tmp; // be sure to send down drop notifs
|
||||
|
||||
// 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;
|
||||
};
|
||||
|
||||
UserSchema.virtual('tasks').get(function () {
|
||||
var tasks = this.habits.concat(this.dailys).concat(this.todos).concat(this.rewards);
|
||||
var tasks = _.object(_.pluck(tasks,'id'), tasks);
|
||||
return tasks;
|
||||
});
|
||||
|
||||
// FIXME - since we're using special @post('init') above, we need to flag when the original path was modified.
|
||||
// Custom setter/getter virtuals?
|
||||
|
||||
UserSchema.pre('save', function(next) {
|
||||
this.markModified('tasks');
|
||||
//this.markModified('tasks'); //FIXME
|
||||
//our own version incrementer
|
||||
this._v++;
|
||||
next();
|
||||
|
||||
Reference in New Issue
Block a user