misskey/src/models/post.ts

241 lines
4.5 KiB
TypeScript
Raw Normal View History

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