diff --git a/server/src/controllers/controller.ts b/server/src/controllers/controller.ts index f46aa695..6c90e163 100644 --- a/server/src/controllers/controller.ts +++ b/server/src/controllers/controller.ts @@ -80,14 +80,15 @@ export class Controller * Transforms an array of model instances to its data ready state that can be sent to the client * @param {ModelInstance} instances The instances to transform * @param {boolean} instances If true, sensitive data will not be sanitized - * @returns {Array} + * @returns {Promise>} */ - getSanitizedData(instances: Array>, verbose: boolean = false): Array + getSanitizedData(instances: Array>, verbose: boolean = false): Promise> { - var sanitizedData = []; + var sanitizedData : Array> = []; + for (var i = 0, l = instances.length; i < l; i++) - sanitizedData.push(instances[i].schema.generateCleanData(!verbose, instances[i]._id)); + sanitizedData.push(instances[i].schema.getAsJson(!verbose, instances[i]._id)); - return sanitizedData; + return Promise.all(sanitizedData); } } \ No newline at end of file diff --git a/server/src/controllers/page-renderer.ts b/server/src/controllers/page-renderer.ts index 15d48f53..178937d5 100644 --- a/server/src/controllers/page-renderer.ts +++ b/server/src/controllers/page-renderer.ts @@ -471,14 +471,16 @@ export default class PageRenderer extends Controller }).then(function (instances) { - var sanitizedData = that.getSanitizedData(instances, Boolean(req.query.verbose)); + return that.getSanitizedData(instances, Boolean(req.query.verbose)); + + }).then(function(sanitizedData){ + res.end(JSON.stringify({ error: false, count: count, message: `Found ${count} renders`, data: sanitizedData })); - }).catch(function (error: Error) { winston.error(error.message, { process: process.pid }); diff --git a/server/src/controllers/posts-controller.ts b/server/src/controllers/posts-controller.ts index 801b7146..67276560 100644 --- a/server/src/controllers/posts-controller.ts +++ b/server/src/controllers/posts-controller.ts @@ -140,7 +140,7 @@ export default class PostsController extends Controller } // Sort by the date created - var sort: IPost = { createdOn: sortOrder }; + var sort: mp.IPost = { createdOn: sortOrder }; // Optionally sort by the last updated if (req.query.sort) @@ -166,15 +166,18 @@ export default class PostsController extends Controller }).then(function (instances) { - var sanitizedData = that.getSanitizedData(instances, Boolean(req.query.verbose)); - res.end(JSON.stringify({ + return that.getSanitizedData(instances, Boolean(req.query.verbose)); + + }).then(function(sanitizedData){ + + res.end(JSON.stringify({ error: false, count: count, message: `Found ${count} posts`, data: sanitizedData })); - }).catch(function (error: Error) + }).catch(function (error: Error) { winston.error(error.message, { process: process.pid }); res.end(JSON.stringify({ @@ -207,20 +210,15 @@ export default class PostsController extends Controller // Only admins are allowed to see private posts if (!instances[0].schema.getByName("public").getValue() && ( !user || users.hasPermission(user, 2) == false ) ) - { - res.end(JSON.stringify({ - error: true, - message: "That post is marked private" - })); + return Promise.reject(new Error("That post is marked private")); - return; - } + return that.getSanitizedData(instances, Boolean(req.query.verbose)); - var sanitizedData = that.getSanitizedData(instances, Boolean(req.query.verbose)); + }).then(function(sanitizedData){ res.end(JSON.stringify({ error: false, - message: `Found ${instances.length} posts`, + message: `Found ${sanitizedData.length} posts`, data: sanitizedData[0] })); @@ -248,11 +246,14 @@ export default class PostsController extends Controller categories.findInstances({}, {}, parseInt(req.query.index), parseInt(req.query.limit)).then(function (instances) { - var sanitizedData = that.getSanitizedData(instances, Boolean(req.query.verbose)); - res.end(JSON.stringify({ + return that.getSanitizedData(instances, Boolean(req.query.verbose)); + + }).then(function(sanitizedData){ + + res.end(JSON.stringify({ error: false, count: sanitizedData.length, - message: `Found ${instances.length} categories`, + message: `Found ${sanitizedData.length} categories`, data: sanitizedData })); @@ -384,10 +385,14 @@ export default class PostsController extends Controller posts.createInstance(token).then(function (instance) { + return instance.schema.getAsJson(false, instance._id); + + }).then(function(json){ + res.end(JSON.stringify({ error: false, message: "New post created", - data: instance.schema.generateCleanData(false, instance._id) + data: json })); }).catch(function (error: Error) @@ -414,10 +419,14 @@ export default class PostsController extends Controller categories.createInstance(token).then(function (instance) { + return instance.schema.getAsJson(true, instance._id); + + }).then(function(json){ + res.end(JSON.stringify({ error: false, message: "New category created", - data: instance.schema.generateCleanData(true, instance._id) + data: json })); }).catch(function (error: Error) diff --git a/server/src/models/model.ts b/server/src/models/model.ts index fedb6901..1534e213 100644 --- a/server/src/models/model.ts +++ b/server/src/models/model.ts @@ -271,6 +271,7 @@ export class Model // Complete return resolve(instance); } + }).catch(function(err: Error){ reject(err); }); diff --git a/server/src/models/schema.ts b/server/src/models/schema.ts index 3b476c8e..5a78921a 100644 --- a/server/src/models/schema.ts +++ b/server/src/models/schema.ts @@ -1,5 +1,7 @@ import {SchemaItem} from "./schema-items/schema-item"; -import {ObjectID} from "mongodb" +import {SchemaForeignKey} from "./schema-items/schema-foreign-key"; +import * as mongodb from "mongodb" +import {ModelInstance} from "./model" import {IModelEntry} from "modepress-api"; /** @@ -63,7 +65,8 @@ export class Schema } /** - * De-serializes the schema items from the mongodb data entry + * De-serializes the schema items from the mongodb data entry. + * I.e. the data is the document from the DB and the schema item sets its values from the document * @param {any} data */ public deserialize(data: any): any @@ -88,28 +91,33 @@ export class Schema } /** - * Serializes the schema items into the JSON format for mongodb + * Serializes the schema items into the JSON * @param {boolean} sanitize If true, the item has to sanitize the data before sending it - * @returns {any} + * @param {ObjectID} id The models dont store the _id property directly, and so this has to be passed for serialization + * @returns {Promise} */ - public generateCleanData(sanitize: boolean, id: ObjectID): T + public getAsJson( sanitize: boolean, id: mongodb.ObjectID ): Promise { - var toReturn : T = {}; - var items = this._items; + var that = this; - for (var i = 0, l = items.length; i < l; i++) - { - // If this data is sensitive and the request must be sanitized - // then skip the item - if ( items[i].getSensitive() && sanitize ) - continue; + return new Promise(function( resolve, reject ) { - toReturn[items[i].name] = items[i].getValue(); - } + var toReturn : T = {}; + var items = that._items; + + for (var i = 0, l = items.length; i < l; i++) + { + // If this data is sensitive and the request must be sanitized + // then skip the item + if ( items[i].getSensitive() && sanitize ) + continue; - (toReturn)._id = id; + toReturn[items[i].name] = items[i].getValue(); + } - return toReturn; + (toReturn)._id = id; + resolve(toReturn); + }); } /**