paricafe/src/api/serializers/post.ts

156 lines
2.8 KiB
TypeScript
Raw Normal View History

2016-12-28 16:49:51 -06:00
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
2017-03-18 06:05:11 -05:00
import deepcopy = require('deepcopy');
2016-12-28 16:49:51 -06:00
import Post from '../models/post';
2017-03-19 14:24:19 -05:00
import Reaction from '../models/post-reaction';
2017-02-13 22:59:26 -06:00
import Vote from '../models/poll-vote';
2017-02-08 08:37:37 -06:00
import serializeApp from './app';
2016-12-28 16:49:51 -06:00
import serializeUser from './user';
import serializeDriveFile from './drive-file';
2017-03-18 06:05:11 -05:00
import parse from '../common/text';
2016-12-28 16:49:51 -06:00
/**
* Serialize a post
*
2017-03-01 02:37:01 -06:00
* @param {any} post
* @param {any} me?
* @param {any} options?
* @return {Promise<any>}
2016-12-28 16:49:51 -06:00
*/
const self = (
post: any,
me?: any,
options?: {
2017-02-13 22:59:26 -06:00
detail: boolean
2016-12-28 16:49:51 -06:00
}
2017-03-01 02:37:01 -06:00
) => new Promise<any>(async (resolve, reject) => {
2016-12-28 16:49:51 -06:00
const opts = options || {
2017-02-13 22:59:26 -06:00
detail: true,
2016-12-28 16:49:51 -06:00
};
let _post: any;
// Populate the post if 'post' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(post)) {
_post = await Post.findOne({
_id: post
});
} else if (typeof post === 'string') {
_post = await Post.findOne({
_id: new mongo.ObjectID(post)
});
} else {
_post = deepcopy(post);
}
const id = _post._id;
// Rename _id to id
_post.id = _post._id;
delete _post._id;
delete _post.mentions;
2017-03-18 06:05:11 -05:00
// Parse text
if (_post.text) {
_post.ast = parse(_post.text);
}
2016-12-28 16:49:51 -06:00
// Populate user
_post.user = await serializeUser(_post.user_id, me);
2017-02-08 08:37:37 -06:00
// Populate app
if (_post.app_id) {
_post.app = await serializeApp(_post.app_id);
}
2016-12-28 16:49:51 -06:00
if (_post.media_ids) {
// Populate media
_post.media = await Promise.all(_post.media_ids.map(async fileId =>
await serializeDriveFile(fileId)
));
}
2017-08-27 10:03:57 -05:00
// When requested a detailed post data
if (opts.detail) {
// Get previous post info
const prev = await Post.findOne({
user_id: _post.user_id,
_id: {
$lt: id
}
}, {
fields: {
_id: true
},
sort: {
_id: -1
}
2016-12-28 16:49:51 -06:00
});
2017-08-27 10:03:57 -05:00
_post.prev = prev ? prev._id : null;
// Get next post info
const next = await Post.findOne({
user_id: _post.user_id,
_id: {
$gt: id
}
}, {
fields: {
_id: true
},
sort: {
_id: 1
}
2016-12-28 16:49:51 -06:00
});
2017-08-27 10:03:57 -05:00
_post.next = next ? next._id : null;
2016-12-28 16:49:51 -06:00
2017-08-27 10:03:57 -05:00
if (_post.reply_to_id) {
// Populate reply to post
_post.reply_to = await self(_post.reply_to_id, me, {
detail: false
2017-02-13 22:59:26 -06:00
});
}
2017-08-27 10:03:57 -05:00
if (_post.repost_id) {
// Populate repost
_post.repost = await self(_post.repost_id, me, {
detail: _post.text == null
2016-12-28 16:49:51 -06:00
});
2017-08-27 10:03:57 -05:00
}
// Poll
if (me && _post.poll) {
const vote = await Vote
.findOne({
user_id: me._id,
post_id: id
});
if (vote != null) {
_post.poll.choices.filter(c => c.id == vote.choice)[0].is_voted = true;
}
}
2016-12-28 16:49:51 -06:00
2017-08-27 10:03:57 -05:00
// Fetch my reaction
if (me) {
const reaction = await Reaction
.findOne({
user_id: me._id,
post_id: id,
deleted_at: { $exists: false }
});
if (reaction) {
_post.my_reaction = reaction.reaction;
}
2017-03-19 14:24:19 -05:00
}
2016-12-28 16:49:51 -06:00
}
resolve(_post);
});
export default self;