From 77528f022d2e9f76298331b55303cfc42359c7af Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 30 Oct 2017 17:30:32 +0900 Subject: wip --- src/api/serializers/bbs-thread.ts | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/api/serializers/bbs-thread.ts (limited to 'src/api/serializers') diff --git a/src/api/serializers/bbs-thread.ts b/src/api/serializers/bbs-thread.ts new file mode 100644 index 0000000000..d9e41a8468 --- /dev/null +++ b/src/api/serializers/bbs-thread.ts @@ -0,0 +1,44 @@ +/** + * Module dependencies + */ +import * as mongo from 'mongodb'; +import deepcopy = require('deepcopy'); +import { IUser } from '../models/user'; +import { default as Thread, IBbsThread } from '../models/bbs-thread'; + +/** + * Serialize a thread + * + * @param thread target + * @param me? serializee + * @return response + */ +export default ( + thread: string | mongo.ObjectID | IBbsThread, + me?: string | mongo.ObjectID | IUser +) => new Promise(async (resolve, reject) => { + + let _thread: any; + + // Populate the thread if 'thread' is ID + if (mongo.ObjectID.prototype.isPrototypeOf(thread)) { + _thread = await Thread.findOne({ + _id: thread + }); + } else if (typeof thread === 'string') { + _thread = await Thread.findOne({ + _id: new mongo.ObjectID(thread) + }); + } else { + _thread = deepcopy(thread); + } + + // Rename _id to id + _thread.id = _thread._id; + delete _thread._id; + + // Remove needless properties + delete _thread.user_id; + + resolve(_thread); +}); -- cgit v1.2.3-freya