2016-12-29 07:49:51 +09:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import * as mongo from 'mongodb';
|
2017-03-18 20:05:11 +09:00
|
|
|
import deepcopy = require('deepcopy');
|
2017-09-08 17:24:11 +09:00
|
|
|
import { default as Post, IPost } from '../models/post';
|
2017-03-20 04:24:19 +09:00
|
|
|
import Reaction from '../models/post-reaction';
|
2017-09-08 17:24:11 +09:00
|
|
|
import { IUser } from '../models/user';
|
2017-02-14 13:59:26 +09:00
|
|
|
import Vote from '../models/poll-vote';
|
2017-02-08 23:37:37 +09:00
|
|
|
import serializeApp from './app';
|
2017-11-01 01:38:19 +09:00
|
|
|
import serializeChannel from './channel';
|
2016-12-29 07:49:51 +09:00
|
|
|
import serializeUser from './user';
|
|
|
|
import serializeDriveFile from './drive-file';
|
2017-03-18 20:05:11 +09:00
|
|
|
import parse from '../common/text';
|
2017-11-05 22:13:28 +09:00
|
|
|
import rap from '@prezzemolo/rap'
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize a post
|
|
|
|
*
|
2017-09-08 17:24:11 +09:00
|
|
|
* @param post target
|
|
|
|
* @param me? serializee
|
|
|
|
* @param options? serialize options
|
|
|
|
* @return response
|
2016-12-29 07:49:51 +09:00
|
|
|
*/
|
|
|
|
const self = (
|
2017-09-08 17:24:11 +09:00
|
|
|
post: string | mongo.ObjectID | IPost,
|
|
|
|
me?: string | mongo.ObjectID | IUser,
|
2016-12-29 07:49:51 +09:00
|
|
|
options?: {
|
2017-02-14 13:59:26 +09:00
|
|
|
detail: boolean
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
2017-03-01 17:37:01 +09:00
|
|
|
) => new Promise<any>(async (resolve, reject) => {
|
2016-12-29 07:49:51 +09:00
|
|
|
const opts = options || {
|
2017-02-14 13:59:26 +09:00
|
|
|
detail: true,
|
2016-12-29 07:49:51 +09:00
|
|
|
};
|
|
|
|
|
2017-09-08 17:24:11 +09:00
|
|
|
// Me
|
|
|
|
const meId: mongo.ObjectID = me
|
2017-09-08 20:49:53 +09:00
|
|
|
? mongo.ObjectID.prototype.isPrototypeOf(me)
|
|
|
|
? me as mongo.ObjectID
|
|
|
|
: typeof me === 'string'
|
|
|
|
? new mongo.ObjectID(me)
|
|
|
|
: (me as IUser)._id
|
|
|
|
: null;
|
2017-09-08 17:24:11 +09:00
|
|
|
|
2016-12-29 07:49:51 +09: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 20:05:11 +09:00
|
|
|
// Parse text
|
|
|
|
if (_post.text) {
|
|
|
|
_post.ast = parse(_post.text);
|
|
|
|
}
|
|
|
|
|
2016-12-29 07:49:51 +09:00
|
|
|
// Populate user
|
2017-11-05 22:13:28 +09:00
|
|
|
_post.user = serializeUser(_post.user_id, meId);
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2017-02-08 23:37:37 +09:00
|
|
|
// Populate app
|
|
|
|
if (_post.app_id) {
|
2017-11-05 22:13:28 +09:00
|
|
|
_post.app = serializeApp(_post.app_id);
|
2017-02-08 23:37:37 +09:00
|
|
|
}
|
|
|
|
|
2017-11-01 01:38:19 +09:00
|
|
|
// Populate channel
|
|
|
|
if (_post.channel_id) {
|
2017-11-05 22:13:28 +09:00
|
|
|
_post.channel = serializeChannel(_post.channel_id);
|
2017-11-01 01:38:19 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate media
|
2016-12-29 07:49:51 +09:00
|
|
|
if (_post.media_ids) {
|
2017-11-05 22:13:28 +09:00
|
|
|
_post.media = Promise.all(_post.media_ids.map(fileId =>
|
2017-11-05 21:11:16 +09:00
|
|
|
serializeDriveFile(fileId)
|
2016-12-29 07:49:51 +09:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2017-08-28 00:03:57 +09:00
|
|
|
// When requested a detailed post data
|
|
|
|
if (opts.detail) {
|
|
|
|
// Get previous post info
|
2017-11-05 22:13:28 +09:00
|
|
|
_post.prev = (async () => {
|
|
|
|
const prev = Post.findOne({
|
|
|
|
user_id: _post.user_id,
|
|
|
|
_id: {
|
|
|
|
$lt: id
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
_id: true
|
|
|
|
},
|
|
|
|
sort: {
|
|
|
|
_id: -1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return prev ? prev._id : null;
|
|
|
|
})()
|
2017-08-28 00:03:57 +09:00
|
|
|
|
|
|
|
// Get next post info
|
2017-11-05 22:13:28 +09:00
|
|
|
_post.next = (async () => {
|
|
|
|
const next = await Post.findOne({
|
|
|
|
user_id: _post.user_id,
|
|
|
|
_id: {
|
|
|
|
$gt: id
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
_id: true
|
|
|
|
},
|
|
|
|
sort: {
|
|
|
|
_id: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return next ? next._id : null;
|
|
|
|
})()
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2017-11-01 10:22:40 +09:00
|
|
|
if (_post.reply_id) {
|
2017-08-28 00:03:57 +09:00
|
|
|
// Populate reply to post
|
2017-11-05 22:13:28 +09:00
|
|
|
_post.reply = self(_post.reply_id, meId, {
|
2017-08-28 00:03:57 +09:00
|
|
|
detail: false
|
2017-02-14 13:59:26 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-28 00:03:57 +09:00
|
|
|
if (_post.repost_id) {
|
|
|
|
// Populate repost
|
2017-11-05 22:13:28 +09:00
|
|
|
_post.repost = self(_post.repost_id, meId, {
|
2017-08-28 00:03:57 +09:00
|
|
|
detail: _post.text == null
|
2016-12-29 07:49:51 +09:00
|
|
|
});
|
2017-08-28 00:03:57 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// Poll
|
2017-09-08 17:24:11 +09:00
|
|
|
if (meId && _post.poll) {
|
2017-11-05 22:13:28 +09:00
|
|
|
_post.poll = (async (poll) => {
|
|
|
|
const vote = await Vote
|
|
|
|
.findOne({
|
|
|
|
user_id: meId,
|
|
|
|
post_id: id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (vote != null) {
|
|
|
|
const myChoice = poll.choices
|
|
|
|
.filter(c => c.id == vote.choice)[0];
|
|
|
|
|
|
|
|
myChoice.is_voted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return poll
|
|
|
|
})(_post.poll)
|
2017-08-28 00:03:57 +09:00
|
|
|
}
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2017-08-28 00:03:57 +09:00
|
|
|
// Fetch my reaction
|
2017-09-08 17:24:11 +09:00
|
|
|
if (meId) {
|
2017-11-05 22:13:28 +09:00
|
|
|
_post.my_reaction = (async () => {
|
|
|
|
const reaction = await Reaction
|
|
|
|
.findOne({
|
|
|
|
user_id: meId,
|
|
|
|
post_id: id,
|
|
|
|
deleted_at: { $exists: false }
|
|
|
|
});
|
|
|
|
|
|
|
|
if (reaction) {
|
|
|
|
return reaction.reaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
})();
|
2017-03-20 04:24:19 +09:00
|
|
|
}
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
|
|
|
|
2017-11-05 22:13:28 +09:00
|
|
|
// resolve promises in _post object
|
|
|
|
_post = await rap(_post)
|
|
|
|
|
2016-12-29 07:49:51 +09:00
|
|
|
resolve(_post);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default self;
|