summaryrefslogtreecommitdiff
path: root/src/api/serializers/post.ts
blob: df917a85956807d3106e4d4bd55183c22ba64338 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
 * Module dependencies
 */
import * as mongo from 'mongodb';
import deepcopy = require('deepcopy');
import { default as Post, IPost } from '../models/post';
import Reaction from '../models/post-reaction';
import { IUser } from '../models/user';
import Vote from '../models/poll-vote';
import serializeApp from './app';
import serializeUser from './user';
import serializeDriveFile from './drive-file';
import parse from '../common/text';

/**
 * Serialize a post
 *
 * @param post target
 * @param me? serializee
 * @param options? serialize options
 * @return response
 */
const self = (
	post: string | mongo.ObjectID | IPost,
	me?: string | mongo.ObjectID | IUser,
	options?: {
		detail: boolean
	}
) => new Promise<any>(async (resolve, reject) => {
	const opts = options || {
		detail: true,
	};

	// Me
	const meId: mongo.ObjectID = me
		? mongo.ObjectID.prototype.isPrototypeOf(me)
			? me as mongo.ObjectID
			: typeof me === 'string'
				? new mongo.ObjectID(me)
				: (me as IUser)._id
		: null;

	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;

	// Parse text
	if (_post.text) {
		_post.ast = parse(_post.text);
	}

	// Populate user
	_post.user = await serializeUser(_post.user_id, meId);

	// Populate app
	if (_post.app_id) {
		_post.app = await serializeApp(_post.app_id);
	}

	if (_post.media_ids) {
		// Populate media
		_post.media = await Promise.all(_post.media_ids.map(async fileId =>
			await serializeDriveFile(fileId)
		));
	}

	// 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
			}
		});
		_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
			}
		});
		_post.next = next ? next._id : null;

		if (_post.reply_to_id) {
			// Populate reply to post
			_post.reply_to = await self(_post.reply_to_id, meId, {
				detail: false
			});
		}

		if (_post.repost_id) {
			// Populate repost
			_post.repost = await self(_post.repost_id, meId, {
				detail: _post.text == null
			});
		}

		// Poll
		if (meId && _post.poll) {
			const vote = await Vote
				.findOne({
					user_id: meId,
					post_id: id
				});

			if (vote != null) {
				const myChoice = _post.poll.choices
					.filter(c => c.id == vote.choice)[0];

				myChoice.is_voted = true;
			}
		}

		// Fetch my reaction
		if (meId) {
			const reaction = await Reaction
				.findOne({
					user_id: meId,
					post_id: id,
					deleted_at: { $exists: false }
				});

			if (reaction) {
				_post.my_reaction = reaction.reaction;
			}
		}
	}

	resolve(_post);
});

export default self;