mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
port challenge model
This commit is contained in:
@@ -6,6 +6,7 @@ const SERVER_FILES = [
|
|||||||
'./website/src/models/user.js',
|
'./website/src/models/user.js',
|
||||||
'./website/src/models/task.js',
|
'./website/src/models/task.js',
|
||||||
'./website/src/models/group.js',
|
'./website/src/models/group.js',
|
||||||
|
'./website/src/models/challenge.js',
|
||||||
'./website/src/models/tag.js',
|
'./website/src/models/tag.js',
|
||||||
'./website/src/models/emailUnsubscription.js',
|
'./website/src/models/emailUnsubscription.js',
|
||||||
'./website/src/server.js',
|
'./website/src/server.js',
|
||||||
|
|||||||
@@ -1,55 +1,55 @@
|
|||||||
var mongoose = require("mongoose");
|
import mongoose from 'mongoose';
|
||||||
var Schema = mongoose.Schema;
|
import Q from 'q';
|
||||||
var shared = require('../../../common');
|
import validator from 'validator';
|
||||||
var _ = require('lodash');
|
import baseModel from '../libs/api-v3/baseModel';
|
||||||
var TaskSchemas = require('./task');
|
import _ from 'lodash';
|
||||||
|
import * as Tasks from './task';
|
||||||
|
|
||||||
var ChallengeSchema = new Schema({
|
let Schema = mongoose.Schema;
|
||||||
_id: {type: String, 'default': shared.uuid},
|
|
||||||
name: String,
|
let schema = new Schema({
|
||||||
shortName: String,
|
name: {type: String, required: true},
|
||||||
|
shortName: {type: String, required: true}, // TODO what is it?
|
||||||
description: String,
|
description: String,
|
||||||
official: {type: Boolean,'default':false},
|
official: {type: Boolean, default: false},
|
||||||
//habits: [TaskSchemas.HabitSchema],
|
tasksOrder: {
|
||||||
//dailys: [TaskSchemas.DailySchema],
|
habits: [{type: String, ref: 'Task'}],
|
||||||
//todos: [TaskSchemas.TodoSchema],
|
dailys: [{type: String, ref: 'Task'}],
|
||||||
//rewards: [TaskSchemas.RewardSchema],
|
todos: [{type: String, ref: 'Task'}],
|
||||||
leader: {type: String, ref: 'User'},
|
rewards: [{type: String, ref: 'Task'}],
|
||||||
group: {type: String, ref: 'Group'},
|
},
|
||||||
timestamp: {type: Date, 'default': Date.now},
|
leader: {type: String, ref: 'User', validate: [validator.isUUID, 'Invalid uuid.'], required: true},
|
||||||
members: [{type: String, ref: 'User'}],
|
group: {type: String, ref: 'Group', validate: [validator.isUUID, 'Invalid uuid.'], required: true},
|
||||||
memberCount: {type: Number, 'default': 0},
|
timestamp: {type: Date, default: Date.now, required: true}, // TODO what is this? use timestamps from plugin?
|
||||||
prize: {type: Number, 'default': 0}
|
memberCount: {type: Number, default: 0},
|
||||||
|
prize: {type: Number, default: 0, required: true},
|
||||||
});
|
});
|
||||||
|
|
||||||
ChallengeSchema.virtual('tasks').get(function () {
|
schema.plugin(baseModel, {
|
||||||
var tasks = this.habits.concat(this.dailys).concat(this.todos).concat(this.rewards);
|
noSet: ['_id', 'memberCount', 'tasksOrder'],
|
||||||
var tasks = _.object(_.pluck(tasks,'id'), tasks);
|
toJSONTransform: function userToJSON (doc) {
|
||||||
return tasks;
|
// TODO fixme
|
||||||
|
// TODO this works?
|
||||||
|
doc._isMember = this._isMember;
|
||||||
|
|
||||||
|
return doc;
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
ChallengeSchema.methods.toJSON = function(){
|
|
||||||
var doc = this.toObject();
|
|
||||||
doc._isMember = this._isMember;
|
|
||||||
return doc;
|
|
||||||
}
|
|
||||||
|
|
||||||
// --------------
|
|
||||||
// Syncing logic
|
// Syncing logic
|
||||||
// --------------
|
|
||||||
|
|
||||||
function syncableAttrs(task) {
|
function _syncableAttrs (task) {
|
||||||
var t = (task.toObject) ? task.toObject() : task; // lodash doesn't seem to like _.omit on EmbeddedDocument
|
let t = task.toObject(); // lodash doesn't seem to like _.omit on EmbeddedDocument
|
||||||
// only sync/compare important attrs
|
// only sync/compare important attrs
|
||||||
var omitAttrs = 'challenge history tags completed streak notes'.split(' ');
|
let omitAttrs = ['userId', 'challenge', 'history', 'tags', 'completed', 'streak', 'notes']; // TODO use whitelist instead of blacklist?
|
||||||
if (t.type != 'reward') omitAttrs.push('value');
|
if (t.type !== 'reward') omitAttrs.push('value');
|
||||||
return _.omit(t, omitAttrs);
|
return _.omit(t, omitAttrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// TODO redo
|
||||||
* Compare whether any changes have been made to tasks. If so, we'll want to sync those changes to subscribers
|
// Compare whether any changes have been made to tasks. If so, we'll want to sync those changes to subscribers
|
||||||
*/
|
/* function comparableData(obj) {
|
||||||
function comparableData(obj) {
|
|
||||||
return JSON.stringify(
|
return JSON.stringify(
|
||||||
_(obj.habits.concat(obj.dailys).concat(obj.todos).concat(obj.rewards))
|
_(obj.habits.concat(obj.dailys).concat(obj.todos).concat(obj.rewards))
|
||||||
.sortBy('id') // we don't want to update if they're sort-order is different
|
.sortBy('id') // we don't want to update if they're sort-order is different
|
||||||
@@ -59,62 +59,86 @@ function comparableData(obj) {
|
|||||||
.value())
|
.value())
|
||||||
}
|
}
|
||||||
|
|
||||||
ChallengeSchema.methods.isOutdated = function(newData) {
|
ChallengeSchema.methods.isOutdated = function isChallengeOutdated (newData) {
|
||||||
return comparableData(this) !== comparableData(newData);
|
return comparableData(this) !== comparableData(newData);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
/**
|
// Syncs all new tasks, deleted tasks, etc to the user object
|
||||||
* Syncs all new tasks, deleted tasks, etc to the user object
|
schema.methods.syncToUser = function syncChallengeToUser (user) {
|
||||||
* @param user
|
if (!user) throw new Error('User required.');
|
||||||
* @return nothing, user is modified directly. REMEMBER to save the user!
|
|
||||||
*/
|
let challenge = this;
|
||||||
ChallengeSchema.methods.syncToUser = function(user, cb) {
|
challenge.shortName = challenge.shortName || challenge.name;
|
||||||
if (!user) return;
|
|
||||||
var self = this;
|
|
||||||
self.shortName = self.shortName || self.name;
|
|
||||||
|
|
||||||
// Add challenge to user.challenges
|
// Add challenge to user.challenges
|
||||||
if (!_.contains(user.challenges, self._id)) {
|
if (!_.contains(user.challenges, challenge._id)) user.challenges.push(challenge._id);
|
||||||
user.challenges.push(self._id);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sync tags
|
// Sync tags
|
||||||
var tags = user.tags || [];
|
let userTags = user.tags;
|
||||||
var i = _.findIndex(tags, {id: self._id})
|
let i = _.findIndex(userTags, {_id: challenge._id});
|
||||||
if (~i) {
|
|
||||||
if (tags[i].name !== self.shortName) {
|
if (i !== -1) {
|
||||||
|
if (userTags[i].name !== challenge.shortName) {
|
||||||
// update the name - it's been changed since
|
// update the name - it's been changed since
|
||||||
user.tags[i].name = self.shortName;
|
userTags[i].name = challenge.shortName;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
user.tags.push({
|
userTags.push({
|
||||||
id: self._id,
|
_id: challenge._id,
|
||||||
name: self.shortName,
|
name: challenge.shortName,
|
||||||
challenge: true
|
challenge: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sync new tasks and updated tasks
|
// Sync new tasks and updated tasks
|
||||||
_.each(self.tasks, function(task){
|
return Q.all([
|
||||||
var list = user[task.type+'s'];
|
// Find original challenge tasks
|
||||||
var userTask = user.tasks[task.id] || (list.push(syncableAttrs(task)), list[list.length-1]);
|
Tasks.Task.find({
|
||||||
if (!userTask.notes) userTask.notes = task.notes; // don't override the notes, but provide it if not provided
|
userId: {$exists: false},
|
||||||
userTask.challenge = {id:self._id};
|
'challenge.id': challenge._id,
|
||||||
userTask.tags = userTask.tags || {};
|
}).exec(),
|
||||||
userTask.tags[self._id] = true;
|
// Find user's tasks linked to this challenge
|
||||||
_.merge(userTask, syncableAttrs(task));
|
Tasks.Task.find({
|
||||||
})
|
userId: user._id,
|
||||||
|
'challenge.id': challenge._id,
|
||||||
|
}).exec(),
|
||||||
|
])
|
||||||
|
.then(results => {
|
||||||
|
let challengeTasks = results[0];
|
||||||
|
let userTasks = results[1];
|
||||||
|
let toSave = []; // An array of things to save
|
||||||
|
|
||||||
// Flag deleted tasks as "broken"
|
challengeTasks.forEach(chalTask => {
|
||||||
_.each(user.tasks, function(task){
|
let matchingTask = _.find(userTasks, userTask => userTask.challenge.taskId === chalTask._id);
|
||||||
if (task.challenge && task.challenge.id==self._id && !self.tasks[task.id]) {
|
|
||||||
task.challenge.broken = 'TASK_DELETED';
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
user.save(cb);
|
if (!matchingTask) { // If the task is new, create it
|
||||||
|
matchingTask = new Tasks[chalTask.type](Tasks.Task.sanitizeCreate(_syncableAttrs(chalTask)));
|
||||||
|
matchingTask.challenge = {taskId: chalTask._id, id: challenge._id};
|
||||||
|
matchingTask.userId = user._id;
|
||||||
|
user.tasksOrder[`${chalTask.type}s`].push(matchingTask._id);
|
||||||
|
} else {
|
||||||
|
_.merge(matchingTask, _syncableAttrs(chalTask));
|
||||||
|
// Make sure the task is in user.tasksOrder TODO necessary?
|
||||||
|
let orderList = user.tasksOrder[`${chalTask.type}s`];
|
||||||
|
if (orderList.indexOf(matchingTask._id) === -1 && (matchingTask.type !== 'todo' || !matchingTask.completed)) orderList.push(matchingTask._id);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!matchingTask.notes) matchingTask.notes = chalTask.notes; // don't override the notes, but provide it if not provided
|
||||||
|
if (matchingTask.tags.indexOf(challenge._id) === -1) matchingTask.tags.push(challenge._id); // add tag if missing
|
||||||
|
toSave.push(matchingTask.save());
|
||||||
|
});
|
||||||
|
|
||||||
|
// Flag deleted tasks as "broken"
|
||||||
|
userTasks.forEach(userTask => {
|
||||||
|
if (!_.find(challengeTasks, chalTask => chalTask._id === userTask.challenge.taskId)) {
|
||||||
|
userTask.challenge.broken = 'TASK_DELETED';
|
||||||
|
toSave.push(userTask.save());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
toSave.push(user.save());
|
||||||
|
return Q.all(toSave);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export let model = mongoose.model('Challenge', schema);
|
||||||
module.exports.schema = ChallengeSchema;
|
|
||||||
module.exports.model = mongoose.model("Challenge", ChallengeSchema);
|
|
||||||
|
|||||||
@@ -24,12 +24,12 @@ export let TaskSchema = new Schema({
|
|||||||
value: {type: Number, default: 0}, // redness or cost for rewards
|
value: {type: Number, default: 0}, // redness or cost for rewards
|
||||||
priority: {type: Number, default: 1, required: true}, // TODO enum?
|
priority: {type: Number, default: 1, required: true}, // TODO enum?
|
||||||
attribute: {type: String, default: 'str', enum: ['str', 'con', 'int', 'per']},
|
attribute: {type: String, default: 'str', enum: ['str', 'con', 'int', 'per']},
|
||||||
userId: {type: String, ref: 'User'}, // When null it belongs to a challenge
|
userId: {type: String, ref: 'User', validate: [validator.isUUID, 'Invalid uuid.']}, // When not set it belongs to a challenge
|
||||||
|
|
||||||
challenge: {
|
challenge: {
|
||||||
id: {type: String, ref: 'Challenge'},
|
id: {type: String, ref: 'Challenge', validate: [validator.isUUID, 'Invalid uuid.']},
|
||||||
taskId: {type: String, ref: 'Task'}, // When null but challenge.id defined it's the original task
|
taskId: {type: String, ref: 'Task', validate: [validator.isUUID, 'Invalid uuid.']}, // When not set but challenge.id defined it's the original task
|
||||||
broken: String, // CHALLENGE_DELETED, TASK_DELETED, UNSUBSCRIBED, CHALLENGE_CLOSED TODO enum
|
broken: {type: String, enum: ['CHALLENGE_DELETED', 'TASK_DELETED', 'UNSUBSCRIBED', 'CHALLENGE_CLOSED']},
|
||||||
winner: String, // user.profile.name TODO necessary?
|
winner: String, // user.profile.name TODO necessary?
|
||||||
},
|
},
|
||||||
}, _.defaults({
|
}, _.defaults({
|
||||||
|
|||||||
@@ -481,6 +481,7 @@ schema.plugin(baseModel, {
|
|||||||
private: ['auth.local.hashed_password', 'auth.local.salt'],
|
private: ['auth.local.hashed_password', 'auth.local.salt'],
|
||||||
toJSONTransform: function userToJSON (doc) {
|
toJSONTransform: function userToJSON (doc) {
|
||||||
// FIXME? Is this a reference to `doc.filters` or just disabled code? Remove?
|
// FIXME? Is this a reference to `doc.filters` or just disabled code? Remove?
|
||||||
|
// TODO this works?
|
||||||
doc.filters = {};
|
doc.filters = {};
|
||||||
doc._tmp = this._tmp; // be sure to send down drop notifs
|
doc._tmp = this._tmp; // be sure to send down drop notifs
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user