1
0
forked from mirror/misskey
misskey/src/models/post.ts

227 lines
4.3 KiB
TypeScript
Raw Normal View History

2017-09-08 04:13:01 +09:00
import * as mongo from 'mongodb';
2018-02-02 08:06:01 +09:00
import deepcopy = require('deepcopy');
import rap from '@prezzemolo/rap';
2018-03-29 20:32:18 +09:00
import db from '../db/mongodb';
2018-02-02 08:06:01 +09: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 09:12:33 +09:00
2018-04-03 23:45:13 +09:00
Post.createIndex('uri', { sparse: true, unique: true });
2018-02-02 08:06:01 +09:00
export default Post;
2017-03-02 03:16:39 +09:00
export function isValidText(text: string): boolean {
return text.length <= 1000 && text.trim() != '';
}
2017-09-08 04:13:01 +09:00
2018-03-30 11:24:07 +09:00
export function isValidCw(text: string): boolean {
return text.length <= 100 && text.trim() != '';
}
2017-09-08 04:13:01 +09:00
export type IPost = {
_id: mongo.ObjectID;
2018-03-29 14:48:47 +09:00
channelId: mongo.ObjectID;
createdAt: Date;
mediaIds: mongo.ObjectID[];
replyId: mongo.ObjectID;
repostId: mongo.ObjectID;
2018-02-04 14:52:33 +09:00
poll: any; // todo
2017-09-08 04:13:01 +09:00
text: string;
2018-04-01 18:07:29 +09:00
tags: string[];
2018-03-31 19:53:30 +09:00
textHtml: string;
2018-03-30 11:24:07 +09:00
cw: string;
2018-03-29 14:48:47 +09:00
userId: mongo.ObjectID;
appId: mongo.ObjectID;
viaMobile: boolean;
repostCount: number;
repliesCount: number;
reactionCounts: any;
mentions: mongo.ObjectID[];
2018-04-02 14:18:01 +09:00
visibility: 'public' | 'unlisted' | 'private' | 'direct';
2018-03-05 08:44:37 +09:00
geo: {
2018-03-29 15:23:15 +09:00
coordinates: number[];
2018-03-05 08:44:37 +09:00
altitude: number;
accuracy: number;
altitudeAccuracy: number;
heading: number;
speed: number;
};
2018-04-03 23:45:13 +09:00
uri: string;
2017-09-08 04:13:01 +09:00
};
2018-02-02 08:06:01 +09: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 15:23:15 +09:00
if (_post.geo) delete _post.geo.type;
2018-02-02 08:06:01 +09:00
// Populate user
2018-03-29 14:48:47 +09:00
_post.user = packUser(_post.userId, meId);
2018-02-02 08:06:01 +09:00
// Populate app
2018-03-29 14:48:47 +09:00
if (_post.appId) {
_post.app = packApp(_post.appId);
2018-02-02 08:06:01 +09:00
}
// Populate channel
2018-03-29 14:48:47 +09:00
if (_post.channelId) {
_post.channel = packChannel(_post.channelId);
2018-02-02 08:06:01 +09:00
}
// Populate media
2018-03-29 14:48:47 +09:00
if (_post.mediaIds) {
_post.media = Promise.all(_post.mediaIds.map(fileId =>
2018-02-02 08:06:01 +09: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 14:48:47 +09:00
userId: _post.userId,
2018-02-02 08:06:01 +09: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 14:48:47 +09:00
userId: _post.userId,
2018-02-02 08:06:01 +09:00
_id: {
$gt: id
}
}, {
fields: {
_id: true
},
sort: {
_id: 1
}
});
return next ? next._id : null;
})();
2018-03-29 14:48:47 +09:00
if (_post.replyId) {
2018-02-02 08:06:01 +09:00
// Populate reply to post
2018-03-29 14:48:47 +09:00
_post.reply = pack(_post.replyId, meId, {
2018-02-02 08:06:01 +09:00
detail: false
});
}
2018-03-29 14:48:47 +09:00
if (_post.repostId) {
2018-02-02 08:06:01 +09:00
// Populate repost
2018-03-29 14:48:47 +09:00
_post.repost = pack(_post.repostId, meId, {
2018-02-02 08:06:01 +09:00
detail: _post.text == null
});
}
// Poll
if (meId && _post.poll) {
_post.poll = (async (poll) => {
const vote = await Vote
.findOne({
2018-03-29 14:48:47 +09:00
userId: meId,
postId: id
2018-02-02 08:06:01 +09:00
});
if (vote != null) {
const myChoice = poll.choices
.filter(c => c.id == vote.choice)[0];
2018-03-29 14:48:47 +09:00
myChoice.isVoted = true;
2018-02-02 08:06:01 +09:00
}
return poll;
})(_post.poll);
}
// Fetch my reaction
if (meId) {
2018-03-29 14:48:47 +09:00
_post.myReaction = (async () => {
2018-02-02 08:06:01 +09:00
const reaction = await Reaction
.findOne({
2018-03-29 14:48:47 +09:00
userId: meId,
postId: id,
deletedAt: { $exists: false }
2018-02-02 08:06:01 +09:00
});
if (reaction) {
return reaction.reaction;
}
return null;
})();
}
}
// resolve promises in _post object
_post = await rap(_post);
return _post;
};