2016-12-29 00:49:51 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import * as mongo from 'mongodb';
|
|
|
|
import Post from '../models/post';
|
|
|
|
import Like from '../models/like';
|
2017-02-14 06:59:26 +02:00
|
|
|
import Vote from '../models/poll-vote';
|
2017-02-08 16:37:37 +02:00
|
|
|
import serializeApp from './app';
|
2016-12-29 00:49:51 +02:00
|
|
|
import serializeUser from './user';
|
|
|
|
import serializeDriveFile from './drive-file';
|
2017-01-02 23:03:19 +02:00
|
|
|
import deepcopy = require('deepcopy');
|
2016-12-29 00:49:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize a post
|
|
|
|
*
|
|
|
|
* @param {Object} post
|
|
|
|
* @param {Object} me?
|
|
|
|
* @param {Object} options?
|
|
|
|
* @return {Promise<Object>}
|
|
|
|
*/
|
|
|
|
const self = (
|
|
|
|
post: any,
|
|
|
|
me?: any,
|
|
|
|
options?: {
|
2017-02-14 06:59:26 +02:00
|
|
|
detail: boolean
|
2016-12-29 00:49:51 +02:00
|
|
|
}
|
|
|
|
) => new Promise<Object>(async (resolve, reject) => {
|
|
|
|
const opts = options || {
|
2017-02-14 06:59:26 +02:00
|
|
|
detail: true,
|
2016-12-29 00:49:51 +02: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;
|
|
|
|
|
|
|
|
// Populate user
|
|
|
|
_post.user = await serializeUser(_post.user_id, me);
|
|
|
|
|
2017-02-08 16:37:37 +02:00
|
|
|
// Populate app
|
|
|
|
if (_post.app_id) {
|
|
|
|
_post.app = await serializeApp(_post.app_id);
|
|
|
|
}
|
|
|
|
|
2016-12-29 00:49:51 +02:00
|
|
|
if (_post.media_ids) {
|
|
|
|
// Populate media
|
|
|
|
_post.media = await Promise.all(_post.media_ids.map(async fileId =>
|
|
|
|
await serializeDriveFile(fileId)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2017-02-14 06:59:26 +02:00
|
|
|
if (_post.reply_to_id && opts.detail) {
|
2016-12-29 00:49:51 +02:00
|
|
|
// Populate reply to post
|
|
|
|
_post.reply_to = await self(_post.reply_to_id, me, {
|
2017-02-14 06:59:26 +02:00
|
|
|
detail: false
|
2016-12-29 00:49:51 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-14 06:59:26 +02:00
|
|
|
if (_post.repost_id && opts.detail) {
|
2016-12-29 00:49:51 +02:00
|
|
|
// Populate repost
|
|
|
|
_post.repost = await self(_post.repost_id, me, {
|
2017-02-14 06:59:26 +02:00
|
|
|
detail: _post.text == null
|
2016-12-29 00:49:51 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-14 06:59:26 +02:00
|
|
|
// Poll
|
|
|
|
if (me && _post.poll && opts.detail) {
|
|
|
|
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-29 00:49:51 +02:00
|
|
|
// Check if it is liked
|
2017-02-14 06:59:26 +02:00
|
|
|
if (me && opts.detail) {
|
2016-12-29 00:49:51 +02:00
|
|
|
const liked = await Like
|
|
|
|
.count({
|
|
|
|
user_id: me._id,
|
2017-01-26 13:45:07 +02:00
|
|
|
post_id: id,
|
|
|
|
deleted_at: { $exists: false }
|
2016-12-29 00:49:51 +02:00
|
|
|
}, {
|
|
|
|
limit: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
_post.is_liked = liked === 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(_post);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default self;
|