From cf33e483f7e6f40e8cbbbc0118a7df70bdaf651f Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 29 Mar 2018 20:32:18 +0900 Subject: 整理した MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/models/post-reaction.ts | 53 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/models/post-reaction.ts (limited to 'src/models/post-reaction.ts') diff --git a/src/models/post-reaction.ts b/src/models/post-reaction.ts new file mode 100644 index 0000000000..3fc33411fb --- /dev/null +++ b/src/models/post-reaction.ts @@ -0,0 +1,53 @@ +import * as mongo from 'mongodb'; +import deepcopy = require('deepcopy'); +import db from '../db/mongodb'; +import Reaction from './post-reaction'; +import { pack as packUser } from './user'; + +const PostReaction = db.get('postReactions'); +export default PostReaction; + +export interface IPostReaction { + _id: mongo.ObjectID; + createdAt: Date; + deletedAt: Date; + postId: mongo.ObjectID; + userId: mongo.ObjectID; + reaction: string; +} + +/** + * Pack a reaction for API response + * + * @param {any} reaction + * @param {any} me? + * @return {Promise} + */ +export const pack = ( + reaction: any, + me?: any +) => new Promise(async (resolve, reject) => { + let _reaction: any; + + // Populate the reaction if 'reaction' is ID + if (mongo.ObjectID.prototype.isPrototypeOf(reaction)) { + _reaction = await Reaction.findOne({ + _id: reaction + }); + } else if (typeof reaction === 'string') { + _reaction = await Reaction.findOne({ + _id: new mongo.ObjectID(reaction) + }); + } else { + _reaction = deepcopy(reaction); + } + + // Rename _id to id + _reaction.id = _reaction._id; + delete _reaction._id; + + // Populate user + _reaction.user = await packUser(_reaction.userId, me); + + resolve(_reaction); +}); -- cgit v1.2.3-freya