2017-09-07 21:13:01 +02:00
|
|
|
import * as mongo from 'mongodb';
|
2018-02-02 00:06:01 +01:00
|
|
|
import deepcopy = require('deepcopy');
|
|
|
|
import rap from '@prezzemolo/rap';
|
2018-03-29 13:32:18 +02:00
|
|
|
import db from '../db/mongodb';
|
2018-02-02 00:06:01 +01:00
|
|
|
import { IUser, pack as packUser } from './user';
|
|
|
|
import { pack as packApp } from './app';
|
|
|
|
import { pack as packChannel } from './channel';
|
|
|
|
import Vote from './poll-vote';
|
|
|
|
import Reaction from './post-reaction';
|
|
|
|
import { pack as packFile } from './drive-file';
|
|
|
|
|
|
|
|
const Post = db.get<IPost>('posts');
|
2017-01-17 01:12:33 +01:00
|
|
|
|
2018-02-02 00:06:01 +01:00
|
|
|
export default Post;
|
2017-03-01 19:16:39 +01:00
|
|
|
|
|
|
|
export function isValidText(text: string): boolean {
|
|
|
|
return text.length <= 1000 && text.trim() != '';
|
|
|
|
}
|
2017-09-07 21:13:01 +02:00
|
|
|
|
2018-03-30 04:24:07 +02:00
|
|
|
export function isValidCw(text: string): boolean {
|
|
|
|
return text.length <= 100 && text.trim() != '';
|
|
|
|
}
|
|
|
|
|
2017-09-07 21:13:01 +02:00
|
|
|
export type IPost = {
|
|
|
|
_id: mongo.ObjectID;
|
2018-03-29 07:48:47 +02:00
|
|
|
channelId: mongo.ObjectID;
|
|
|
|
createdAt: Date;
|
|
|
|
mediaIds: mongo.ObjectID[];
|
|
|
|
replyId: mongo.ObjectID;
|
|
|
|
repostId: mongo.ObjectID;
|
2018-02-04 06:52:33 +01:00
|
|
|
poll: any; // todo
|
2017-09-07 21:13:01 +02:00
|
|
|
text: string;
|
2018-04-01 11:07:29 +02:00
|
|
|
tags: string[];
|
2018-03-31 12:53:30 +02:00
|
|
|
textHtml: string;
|
2018-03-30 04:24:07 +02:00
|
|
|
cw: string;
|
2018-03-29 07:48:47 +02:00
|
|
|
userId: mongo.ObjectID;
|
|
|
|
appId: mongo.ObjectID;
|
|
|
|
viaMobile: boolean;
|
|
|
|
repostCount: number;
|
|
|
|
repliesCount: number;
|
|
|
|
reactionCounts: any;
|
|
|
|
mentions: mongo.ObjectID[];
|
2018-03-05 00:44:37 +01:00
|
|
|
geo: {
|
2018-03-29 08:23:15 +02:00
|
|
|
coordinates: number[];
|
2018-03-05 00:44:37 +01:00
|
|
|
altitude: number;
|
|
|
|
accuracy: number;
|
|
|
|
altitudeAccuracy: number;
|
|
|
|
heading: number;
|
|
|
|
speed: number;
|
|
|
|
};
|
2018-04-01 11:12:51 +02:00
|
|
|
tags: string[];
|
2017-09-07 21:13:01 +02:00
|
|
|
};
|
2018-02-02 00:06:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pack a post for API response
|
|
|
|
*
|
|
|
|
* @param post target
|
|
|
|
* @param me? serializee
|
|
|
|
* @param options? serialize options
|
|
|
|
* @return response
|
|
|
|
*/
|
|
|
|
export const pack = async (
|
|
|
|
post: string | mongo.ObjectID | IPost,
|
|
|
|
me?: string | mongo.ObjectID | IUser,
|
|
|
|
options?: {
|
|
|
|
detail: boolean
|
|
|
|
}
|
|
|
|
) => {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_post) throw 'invalid post arg.';
|
|
|
|
|
|
|
|
const id = _post._id;
|
|
|
|
|
|
|
|
// Rename _id to id
|
|
|
|
_post.id = _post._id;
|
|
|
|
delete _post._id;
|
|
|
|
|
|
|
|
delete _post.mentions;
|
2018-03-29 08:23:15 +02:00
|
|
|
if (_post.geo) delete _post.geo.type;
|
2018-02-02 00:06:01 +01:00
|
|
|
|
|
|
|
// Populate user
|
2018-03-29 07:48:47 +02:00
|
|
|
_post.user = packUser(_post.userId, meId);
|
2018-02-02 00:06:01 +01:00
|
|
|
|
|
|
|
// Populate app
|
2018-03-29 07:48:47 +02:00
|
|
|
if (_post.appId) {
|
|
|
|
_post.app = packApp(_post.appId);
|
2018-02-02 00:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate channel
|
2018-03-29 07:48:47 +02:00
|
|
|
if (_post.channelId) {
|
|
|
|
_post.channel = packChannel(_post.channelId);
|
2018-02-02 00:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate media
|
2018-03-29 07:48:47 +02:00
|
|
|
if (_post.mediaIds) {
|
|
|
|
_post.media = Promise.all(_post.mediaIds.map(fileId =>
|
2018-02-02 00:06:01 +01:00
|
|
|
packFile(fileId)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// When requested a detailed post data
|
|
|
|
if (opts.detail) {
|
|
|
|
// Get previous post info
|
|
|
|
_post.prev = (async () => {
|
|
|
|
const prev = await Post.findOne({
|
2018-03-29 07:48:47 +02:00
|
|
|
userId: _post.userId,
|
2018-02-02 00:06:01 +01:00
|
|
|
_id: {
|
|
|
|
$lt: id
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
_id: true
|
|
|
|
},
|
|
|
|
sort: {
|
|
|
|
_id: -1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return prev ? prev._id : null;
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Get next post info
|
|
|
|
_post.next = (async () => {
|
|
|
|
const next = await Post.findOne({
|
2018-03-29 07:48:47 +02:00
|
|
|
userId: _post.userId,
|
2018-02-02 00:06:01 +01:00
|
|
|
_id: {
|
|
|
|
$gt: id
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
_id: true
|
|
|
|
},
|
|
|
|
sort: {
|
|
|
|
_id: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return next ? next._id : null;
|
|
|
|
})();
|
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
if (_post.replyId) {
|
2018-02-02 00:06:01 +01:00
|
|
|
// Populate reply to post
|
2018-03-29 07:48:47 +02:00
|
|
|
_post.reply = pack(_post.replyId, meId, {
|
2018-02-02 00:06:01 +01:00
|
|
|
detail: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
if (_post.repostId) {
|
2018-02-02 00:06:01 +01:00
|
|
|
// Populate repost
|
2018-03-29 07:48:47 +02:00
|
|
|
_post.repost = pack(_post.repostId, meId, {
|
2018-02-02 00:06:01 +01:00
|
|
|
detail: _post.text == null
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Poll
|
|
|
|
if (meId && _post.poll) {
|
|
|
|
_post.poll = (async (poll) => {
|
|
|
|
const vote = await Vote
|
|
|
|
.findOne({
|
2018-03-29 07:48:47 +02:00
|
|
|
userId: meId,
|
|
|
|
postId: id
|
2018-02-02 00:06:01 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (vote != null) {
|
|
|
|
const myChoice = poll.choices
|
|
|
|
.filter(c => c.id == vote.choice)[0];
|
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
myChoice.isVoted = true;
|
2018-02-02 00:06:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return poll;
|
|
|
|
})(_post.poll);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch my reaction
|
|
|
|
if (meId) {
|
2018-03-29 07:48:47 +02:00
|
|
|
_post.myReaction = (async () => {
|
2018-02-02 00:06:01 +01:00
|
|
|
const reaction = await Reaction
|
|
|
|
.findOne({
|
2018-03-29 07:48:47 +02:00
|
|
|
userId: meId,
|
|
|
|
postId: id,
|
|
|
|
deletedAt: { $exists: false }
|
2018-02-02 00:06:01 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (reaction) {
|
|
|
|
return reaction.reaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolve promises in _post object
|
|
|
|
_post = await rap(_post);
|
|
|
|
|
|
|
|
return _post;
|
|
|
|
};
|