Merge branch 'master' of github.com:coralproject/talk into asset-comment-settings

This commit is contained in:
Belen Curcio
2016-12-09 12:28:40 -03:00
20 changed files with 296 additions and 153 deletions
+2 -6
View File
@@ -36,11 +36,7 @@ const AssetSchema = new Schema({
subsection: String,
author: String,
publication_date: Date,
modified_date: Date,
status: {
type: String,
default: 'open'
}
modified_date: Date
}, {
versionKey: false,
timestamps: {
@@ -85,7 +81,7 @@ AssetSchema.statics.rectifySettings = (assetQuery) => Promise.all([
// If the asset exists and has settings then return the merged object.
if (asset && asset.settings) {
return Object.assign({}, settings, asset.settings);
settings.merge(asset.settings);
}
return settings;
+32 -29
View File
@@ -1,5 +1,6 @@
const mongoose = require('../mongoose');
const Schema = mongoose.Schema;
const _ = require('lodash');
const uuid = require('uuid');
const Action = require('./action');
@@ -45,7 +46,7 @@ const CommentSchema = new Schema({
},
asset_id: String,
author_id: String,
status: [StatusSchema],
status_history: [StatusSchema],
parent_id: String
}, {
timestamps: {
@@ -59,22 +60,7 @@ const CommentSchema = new Schema({
* output.
*/
CommentSchema.options.toJSON = {};
CommentSchema.options.toJSON.hide = '_id status';
CommentSchema.options.toJSON.transform = (doc, ret, options) => {
if (options.hide) {
options.hide.split(' ').forEach((prop) => {
delete ret[prop];
});
}
return ret;
};
/**
* toJSON overrides to remove fields from the json
* output.
*/
CommentSchema.options.toJSON = {};
CommentSchema.options.toJSON.virtuals = true;
CommentSchema.options.toJSON.hide = '_id';
CommentSchema.options.toJSON.transform = (doc, ret, options) => {
if (options.hide) {
@@ -86,14 +72,31 @@ CommentSchema.options.toJSON.transform = (doc, ret, options) => {
return ret;
};
/**
* Filters the object for the given user only allowing those with the allowed
* roles/permissions to access particular parameters.
*/
CommentSchema.method('filterForUser', function(user = false) {
if (!user || !user.roles.includes('admin')) {
return _.pick(this.toJSON(), ['id', 'body', 'asset_id', 'author_id', 'parent_id', 'status']);
}
return this.toJSON();
});
/**
* Sets up a virtual getter function on a comment such that when you try and
* access the `comment.last_status` it returns the last status in the array
* of status's on the comment, or `null` if there was no status.
* of status's on the comment, or `null` if there was no status_history.
*/
CommentSchema.virtual('last_status').get(function() {
if (this.status && this.status.length > 0) {
return this.status[this.status.length - 1].type;
CommentSchema.virtual('status').get(function() {
// Here we are taking advantage of the fact that when documents are inserted
// for the new status on a comment that they are always appended to the end
// of the list in the order that they are inserted, hence, the last status
// is always the most recent.
if (this.status_history && this.status_history.length > 0) {
return this.status_history[this.status_history.length - 1].type;
}
return null;
@@ -123,7 +126,7 @@ CommentSchema.statics.publicCreate = (comment) => {
body,
asset_id,
parent_id,
status: status ? [{
status_history: status ? [{
type: status,
created_at: new Date()
}] : [],
@@ -157,7 +160,7 @@ CommentSchema.statics.findByAssetId = (asset_id) => Comment.find({
*/
CommentSchema.statics.findAcceptedByAssetId = (asset_id) => Comment.find({
asset_id,
'status.type': 'accepted'
'status_history.type': 'accepted'
});
/**
@@ -169,10 +172,10 @@ CommentSchema.statics.findAcceptedAndNewByAssetId = (asset_id) => Comment.find({
asset_id,
$or: [
{
'status.type': 'accepted'
'status_history.type': 'accepted'
},
{
status: {
status_history: {
$size: 0
}
}
@@ -202,7 +205,7 @@ CommentSchema.statics.findIdsByActionType = (action_type) => Action
.then((actions) => actions.map(a => a.item_id));
/**
* Find comments by their status.
* Find comments by their status_history.
* @param {String} status the status of the comment to search for
* @return {Promise}
*/
@@ -210,9 +213,9 @@ CommentSchema.statics.findByStatus = (status = false) => {
let q = {};
if (status) {
q['status.type'] = status;
q['status_history.type'] = status;
} else {
q.status = {$size: 0};
q.status_history = {$size: 0};
}
return Comment.find(q);
@@ -269,7 +272,7 @@ CommentSchema.statics.moderationQueue = (moderation, asset_id = false) => {
*/
CommentSchema.statics.pushStatus = (id, status, assigned_by = null) => Comment.update({id}, {
$push: {
status: {
status_history: {
type: status,
created_at: new Date(),
assigned_by
+39 -9
View File
@@ -36,6 +36,42 @@ const SettingSchema = new Schema({
}
});
/**
* toJSON provides settings overrides to this object's serialization methods.
*/
SettingSchema.options.toJSON = {};
SettingSchema.options.toJSON.virtuals = true;
/**
* Merges two settings objects.
*/
SettingSchema.method('merge', function(src) {
SettingSchema.eachPath((path) => {
// Exclude internal fields...
if (['id', '_id', '__v', 'created_at', 'updated_at'].includes(path)) {
return;
}
// If the source object contains the path, shallow copy it.
if (path in src) {
this[path] = src[path];
}
});
});
/**
* Filters the object for the given user only allowing those with the allowed
* roles/permissions to access particular parameters.
*/
SettingSchema.method('filterForUser', function(user = false) {
if (!user || !user.roles.includes('admin')) {
return _.pick(this.toJSON(), ['moderation', 'infoBoxEnable', 'infoBoxContent']);
}
return this.toJSON();
});
/**
* The Mongo Mongoose object.
*/
@@ -62,7 +98,9 @@ const EXPIRY_TIME = 60 * 2;
* Gets the entire settings record and sends it back
* @return {Promise} settings the whole settings record
*/
SettingService.retrieve = () => cache.wrap('settings', EXPIRY_TIME, () => Setting.findOne(selector));
SettingService.retrieve = () => cache.wrap('settings', EXPIRY_TIME, () => {
return Setting.findOne(selector);
}).then((setting) => new Setting(setting));
/**
* This will update the settings object with whatever you pass in
@@ -83,14 +121,6 @@ SettingService.update = (settings) => Setting.findOneAndUpdate(selector, {
.then(() => settings);
});
/**
* Filters the document to ensure that the resulting document is indeed ready
* for non authenticated users.
* @param {Object} settings the source settings object
* @return {Object} the filtered settings object
*/
SettingService.public = (settings) => _.pick(settings, ['moderation', 'infoBoxEnable', 'infoBoxContent']);
/**
* This is run once when the app starts to ensure settings are populated.
* @return {Promise} null initialize the global settings object
+10 -12
View File
@@ -1,5 +1,6 @@
const mongoose = require('../mongoose');
const uuid = require('uuid');
const _ = require('lodash');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
@@ -105,7 +106,8 @@ UserSchema.index({
* output.
*/
UserSchema.options.toJSON = {};
UserSchema.options.toJSON.hide = '_id password profiles roles disabled';
UserSchema.options.toJSON.hide = '_id password';
UserSchema.options.toJSON.virtuals = true;
UserSchema.options.toJSON.transform = (doc, ret, options) => {
if (options.hide) {
options.hide.split(' ').forEach((prop) => {
@@ -117,20 +119,16 @@ UserSchema.options.toJSON.transform = (doc, ret, options) => {
};
/**
* toObject overrides to remove the password field from the toObject
* output.
* Filters the object for the given user only allowing those with the allowed
* roles/permissions to access particular parameters.
*/
UserSchema.options.toObject = {};
UserSchema.options.toObject.hide = 'password';
UserSchema.options.toObject.transform = (doc, ret, options) => {
if (options.hide) {
options.hide.split(' ').forEach((prop) => {
delete ret[prop];
});
UserSchema.method('filterForUser', function(user = false) {
if (!user || !user.roles.includes('admin')) {
return _.pick(this.toJSON(), ['id', 'displayName', 'settings', 'created_at', 'updated_at']);
}
return ret;
};
return this.toJSON();
});
// Create the User model.
const UserModel = mongoose.model('User', UserSchema);