2013-08-31 21:47:03 +00:00
|
|
|
var mongoose = require("mongoose");
|
|
|
|
|
var Schema = mongoose.Schema;
|
|
|
|
|
var helpers = require('habitrpg-shared/script/helpers');
|
|
|
|
|
var _ = require('lodash');
|
2013-08-29 05:45:19 +00:00
|
|
|
|
2013-08-31 21:47:03 +00:00
|
|
|
var GroupSchema = new Schema({
|
|
|
|
|
_id: {type: String, 'default': helpers.uuid},
|
2013-08-29 05:45:19 +00:00
|
|
|
name: String,
|
|
|
|
|
description: String,
|
2013-10-28 05:27:07 +00:00
|
|
|
leader: {type: String, ref: 'User'},
|
|
|
|
|
members: [{type: String, ref: 'User'}],
|
|
|
|
|
invites: [{type: String, ref: 'User'}],
|
|
|
|
|
type: {type: String, "enum": ['guild', 'party']},
|
|
|
|
|
privacy: {type: String, "enum": ['private', 'public']},
|
|
|
|
|
_v: {Number: Number,'default': 0},
|
2013-08-31 21:47:03 +00:00
|
|
|
websites: Array,
|
2013-08-29 05:45:19 +00:00
|
|
|
chat: Array,
|
|
|
|
|
/*
|
|
|
|
|
# [{
|
|
|
|
|
# timestamp: Date
|
|
|
|
|
# user: String
|
|
|
|
|
# text: String
|
|
|
|
|
# contributor: String
|
|
|
|
|
# uuid: String
|
|
|
|
|
# id: String
|
|
|
|
|
# }]
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
balance: Number,
|
|
|
|
|
logo: String,
|
2013-10-27 00:24:45 +00:00
|
|
|
leaderMessage: String,
|
|
|
|
|
challenges: [{type:'String', ref:'Challenge'}]
|
2013-10-27 09:55:05 +00:00
|
|
|
}, {
|
2013-10-27 00:24:45 +00:00
|
|
|
strict: 'throw',
|
2013-10-27 09:55:43 +00:00
|
|
|
minimize: false // So empty objects are returned
|
2013-10-27 09:55:05 +00:00
|
|
|
});
|
2013-08-29 05:45:19 +00:00
|
|
|
|
2013-08-31 21:47:03 +00:00
|
|
|
/**
|
|
|
|
|
* Derby duplicated stuff. This is a temporary solution, once we're completely off derby we'll run an mongo migration
|
|
|
|
|
* to remove duplicates, then take these fucntions out
|
|
|
|
|
*/
|
|
|
|
|
function removeDuplicates(doc){
|
|
|
|
|
// Remove duplicate members
|
|
|
|
|
if (doc.members) {
|
|
|
|
|
var uniqMembers = _.uniq(doc.members);
|
|
|
|
|
if (uniqMembers.length != doc.members.length) {
|
|
|
|
|
doc.members = uniqMembers;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doc.websites) {
|
|
|
|
|
var uniqWebsites = _.uniq(doc.websites);
|
|
|
|
|
if (uniqWebsites.length != doc.websites.length) {
|
|
|
|
|
doc.websites = uniqWebsites;
|
|
|
|
|
}
|
|
|
|
|
console.log(doc.websites);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GroupSchema.pre('save', function(next){
|
|
|
|
|
removeDuplicates(this);
|
|
|
|
|
next();
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
GroupSchema.methods.toJSON = function(){
|
|
|
|
|
var doc = this.toObject();
|
|
|
|
|
removeDuplicates(doc);
|
|
|
|
|
return doc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports.schema = GroupSchema;
|
2013-08-29 05:45:19 +00:00
|
|
|
module.exports.model = mongoose.model("Group", GroupSchema);
|