2016-12-28 23:49:51 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2017-03-02 13:54:46 +01:00
|
|
|
import it from '../../it';
|
2016-12-28 23:49:51 +01:00
|
|
|
import parse from '../../../common/text';
|
2017-03-02 07:25:27 +01:00
|
|
|
import Post from '../../models/post';
|
|
|
|
import { isValidText } from '../../models/post';
|
2016-12-28 23:49:51 +01:00
|
|
|
import User from '../../models/user';
|
|
|
|
import Following from '../../models/following';
|
|
|
|
import DriveFile from '../../models/drive-file';
|
|
|
|
import serialize from '../../serializers/post';
|
|
|
|
import notify from '../../common/notify';
|
|
|
|
import event from '../../event';
|
2017-01-17 03:11:22 +01:00
|
|
|
import config from '../../../conf';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a post
|
|
|
|
*
|
2017-03-01 09:37:01 +01:00
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @param {any} app
|
|
|
|
* @return {Promise<any>}
|
2016-12-28 23:49:51 +01:00
|
|
|
*/
|
|
|
|
module.exports = (params, user, app) =>
|
|
|
|
new Promise(async (res, rej) =>
|
|
|
|
{
|
|
|
|
// Get 'text' parameter
|
2017-03-02 13:54:46 +01:00
|
|
|
const [text, textErr] = it(params.text).must.be.a.string().validate(isValidText).get();
|
2017-03-01 19:16:39 +01:00
|
|
|
if (textErr) return rej('invalid text');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
// Get 'media_ids' parameter
|
2017-03-02 13:54:46 +01:00
|
|
|
const [mediaIds, mediaIdsErr] = it(params.media_ids).must.be.an.array().unique().range(1, 4).get();
|
2017-03-01 19:16:39 +01:00
|
|
|
if (mediaIdsErr) return rej('invalid media_ids');
|
2017-02-23 15:39:58 +01:00
|
|
|
|
2017-03-01 19:16:39 +01:00
|
|
|
let files = [];
|
|
|
|
if (mediaIds !== null) {
|
2016-12-28 23:49:51 +01:00
|
|
|
// Fetch files
|
|
|
|
// forEach だと途中でエラーなどがあっても return できないので
|
|
|
|
// 敢えて for を使っています。
|
2017-03-01 21:11:37 +01:00
|
|
|
for (let i = 0; i < mediaIds.length; i++) {
|
2017-03-02 13:54:46 +01:00
|
|
|
const [mediaId, mediaIdErr] = it(mediaIds[i]).must.be.an.id().required().get();
|
2017-03-01 21:11:37 +01:00
|
|
|
if (mediaIdErr) return rej('invalid media id');
|
2017-01-17 21:39:50 +01:00
|
|
|
|
2016-12-28 23:49:51 +01:00
|
|
|
// Fetch file
|
|
|
|
// SELECT _id
|
|
|
|
const entity = await DriveFile.findOne({
|
2017-03-01 21:11:37 +01:00
|
|
|
_id: mediaId,
|
2016-12-28 23:49:51 +01:00
|
|
|
user_id: user._id
|
|
|
|
}, {
|
|
|
|
_id: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (entity === null) {
|
|
|
|
return rej('file not found');
|
|
|
|
} else {
|
|
|
|
files.push(entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
files = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 'repost_id' parameter
|
2017-03-02 13:54:46 +01:00
|
|
|
const [repostId, repostIdErr] = it(params.repost_id).must.be.an.id().get();
|
2017-03-01 21:11:37 +01:00
|
|
|
if (repostIdErr) return rej('invalid repost_id');
|
2017-01-17 21:39:50 +01:00
|
|
|
|
2017-03-01 21:11:37 +01:00
|
|
|
let repost = null;
|
|
|
|
if (repostId !== null) {
|
2016-12-28 23:49:51 +01:00
|
|
|
// Fetch repost to post
|
|
|
|
repost = await Post.findOne({
|
2017-03-01 21:11:37 +01:00
|
|
|
_id: repostId
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (repost == null) {
|
|
|
|
return rej('repostee is not found');
|
|
|
|
} else if (repost.repost_id && !repost.text && !repost.media_ids) {
|
|
|
|
return rej('cannot repost to repost');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch recently post
|
|
|
|
const latestPost = await Post.findOne({
|
|
|
|
user_id: user._id
|
2017-01-17 03:11:22 +01:00
|
|
|
}, {
|
2016-12-28 23:49:51 +01:00
|
|
|
sort: {
|
|
|
|
_id: -1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 直近と同じRepost対象かつ引用じゃなかったらエラー
|
|
|
|
if (latestPost &&
|
|
|
|
latestPost.repost_id &&
|
|
|
|
latestPost.repost_id.equals(repost._id) &&
|
|
|
|
text === null && files === null) {
|
|
|
|
return rej('二重Repostです(NEED TRANSLATE)');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 直近がRepost対象かつ引用じゃなかったらエラー
|
|
|
|
if (latestPost &&
|
|
|
|
latestPost._id.equals(repost._id) &&
|
|
|
|
text === null && files === null) {
|
|
|
|
return rej('二重Repostです(NEED TRANSLATE)');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 21:11:37 +01:00
|
|
|
// Get 'in_reply_to_post_id' parameter
|
2017-03-02 13:54:46 +01:00
|
|
|
const [inReplyToPostId, inReplyToPostIdErr] = it(params.reply_to_id, 'id');
|
2017-03-01 21:11:37 +01:00
|
|
|
if (inReplyToPostIdErr) return rej('invalid in_reply_to_post_id');
|
2017-01-17 21:39:50 +01:00
|
|
|
|
2017-03-01 21:11:37 +01:00
|
|
|
let inReplyToPost = null;
|
|
|
|
if (inReplyToPostId !== null) {
|
2017-01-17 21:39:50 +01:00
|
|
|
// Fetch reply
|
2017-03-01 21:11:37 +01:00
|
|
|
inReplyToPost = await Post.findOne({
|
|
|
|
_id: inReplyToPostId
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|
|
|
|
|
2017-03-01 21:11:37 +01:00
|
|
|
if (inReplyToPost === null) {
|
|
|
|
return rej('in reply to post is not found');
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 返信対象が引用でないRepostだったらエラー
|
2017-03-01 21:11:37 +01:00
|
|
|
if (inReplyToPost.repost_id && !inReplyToPost.text && !inReplyToPost.media_ids) {
|
2016-12-28 23:49:51 +01:00
|
|
|
return rej('cannot reply to repost');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-14 05:59:26 +01:00
|
|
|
// Get 'poll' parameter
|
2017-03-02 13:54:46 +01:00
|
|
|
const [_poll, pollErr] = it(params.poll, 'object');
|
2017-03-01 21:11:37 +01:00
|
|
|
if (pollErr) return rej('invalid poll');
|
|
|
|
|
|
|
|
let poll = null;
|
|
|
|
if (_poll !== null) {
|
2017-03-02 13:54:46 +01:00
|
|
|
const [pollChoices, pollChoicesErr] = it(params.poll, 'set', false, [
|
2017-03-01 21:11:37 +01:00
|
|
|
choices => {
|
|
|
|
const shouldReject = choices.some(choice => {
|
|
|
|
if (typeof choice != 'string') return true;
|
|
|
|
if (choice.trim().length == 0) return true;
|
|
|
|
if (choice.trim().length > 50) return true;
|
2017-03-02 07:25:27 +01:00
|
|
|
return false;
|
2017-03-01 21:11:37 +01:00
|
|
|
});
|
|
|
|
return shouldReject ? 'invalid poll choices' : true;
|
|
|
|
},
|
|
|
|
// 選択肢がひとつならエラー
|
|
|
|
choices => choices.length == 1 ? 'poll choices must be ひとつ以上' : true,
|
|
|
|
// 選択肢が多すぎてもエラー
|
|
|
|
choices => choices.length > 10 ? 'many poll choices' : true,
|
|
|
|
]);
|
|
|
|
if (pollChoicesErr) return rej('invalid poll choices');
|
|
|
|
|
|
|
|
_poll.choices = pollChoices.map((choice, i) => ({
|
2017-02-14 05:59:26 +01:00
|
|
|
id: i, // IDを付与
|
2017-03-01 21:11:37 +01:00
|
|
|
text: choice.trim(),
|
2017-02-14 05:59:26 +01:00
|
|
|
votes: 0
|
|
|
|
}));
|
2017-03-01 21:11:37 +01:00
|
|
|
|
|
|
|
poll = _poll;
|
2017-02-14 05:59:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// テキストが無いかつ添付ファイルが無いかつRepostも無いかつ投票も無かったらエラー
|
2017-03-01 21:12:59 +01:00
|
|
|
if (text === null && files === null && repost === null && poll === null) {
|
2017-02-14 05:59:26 +01:00
|
|
|
return rej('text, media_ids, repost_id or poll is required');
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 投稿を作成
|
2017-01-17 03:11:22 +01:00
|
|
|
const post = await Post.insert({
|
2016-12-28 23:49:51 +01:00
|
|
|
created_at: new Date(),
|
2017-02-24 05:39:48 +01:00
|
|
|
media_ids: files ? files.map(file => file._id) : undefined,
|
2017-03-02 07:25:27 +01:00
|
|
|
reply_to_id: inReplyToPost ? inReplyToPost._id : undefined,
|
2016-12-28 23:49:51 +01:00
|
|
|
repost_id: repost ? repost._id : undefined,
|
2017-02-14 05:59:26 +01:00
|
|
|
poll: poll ? poll : undefined,
|
2016-12-28 23:49:51 +01:00
|
|
|
text: text,
|
|
|
|
user_id: user._id,
|
|
|
|
app_id: app ? app._id : null
|
|
|
|
});
|
|
|
|
|
|
|
|
// Serialize
|
|
|
|
const postObj = await serialize(post);
|
|
|
|
|
|
|
|
// Reponse
|
|
|
|
res(postObj);
|
|
|
|
|
|
|
|
//--------------------------------
|
|
|
|
// Post processes
|
|
|
|
|
|
|
|
let mentions = [];
|
|
|
|
|
|
|
|
function addMention(mentionee, type) {
|
|
|
|
// Reject if already added
|
|
|
|
if (mentions.some(x => x.equals(mentionee))) return;
|
|
|
|
|
|
|
|
// Add mention
|
|
|
|
mentions.push(mentionee);
|
|
|
|
|
|
|
|
// Publish event
|
|
|
|
if (!user._id.equals(mentionee)) {
|
|
|
|
event(mentionee, type, postObj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish event to myself's stream
|
|
|
|
event(user._id, 'post', postObj);
|
|
|
|
|
|
|
|
// Fetch all followers
|
|
|
|
const followers = await Following
|
|
|
|
.find({
|
|
|
|
followee_id: user._id,
|
|
|
|
// 削除されたドキュメントは除く
|
|
|
|
deleted_at: { $exists: false }
|
|
|
|
}, {
|
|
|
|
follower_id: true,
|
|
|
|
_id: false
|
2017-01-17 03:11:22 +01:00
|
|
|
});
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
// Publish event to followers stream
|
|
|
|
followers.forEach(following =>
|
|
|
|
event(following.follower_id, 'post', postObj));
|
|
|
|
|
|
|
|
// Increment my posts count
|
2017-01-17 03:11:22 +01:00
|
|
|
User.update({ _id: user._id }, {
|
2016-12-28 23:49:51 +01:00
|
|
|
$inc: {
|
|
|
|
posts_count: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// If has in reply to post
|
2017-03-02 07:25:27 +01:00
|
|
|
if (inReplyToPost) {
|
2016-12-28 23:49:51 +01:00
|
|
|
// Increment replies count
|
2017-03-02 07:25:27 +01:00
|
|
|
Post.update({ _id: inReplyToPost._id }, {
|
2016-12-28 23:49:51 +01:00
|
|
|
$inc: {
|
|
|
|
replies_count: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 自分自身へのリプライでない限りは通知を作成
|
2017-03-02 07:25:27 +01:00
|
|
|
notify(inReplyToPost.user_id, user._id, 'reply', {
|
2016-12-28 23:49:51 +01:00
|
|
|
post_id: post._id
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add mention
|
2017-03-02 07:25:27 +01:00
|
|
|
addMention(inReplyToPost.user_id, 'reply');
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it is repost
|
|
|
|
if (repost) {
|
|
|
|
// Notify
|
|
|
|
const type = text ? 'quote' : 'repost';
|
|
|
|
notify(repost.user_id, user._id, type, {
|
|
|
|
post_id: post._id
|
|
|
|
});
|
|
|
|
|
|
|
|
// If it is quote repost
|
|
|
|
if (text) {
|
|
|
|
// Add mention
|
|
|
|
addMention(repost.user_id, 'quote');
|
|
|
|
} else {
|
|
|
|
// Publish event
|
|
|
|
if (!user._id.equals(repost.user_id)) {
|
|
|
|
event(repost.user_id, 'repost', postObj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 今までで同じ投稿をRepostしているか
|
|
|
|
const existRepost = await Post.findOne({
|
|
|
|
user_id: user._id,
|
|
|
|
repost_id: repost._id,
|
|
|
|
_id: {
|
|
|
|
$ne: post._id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!existRepost) {
|
|
|
|
// Update repostee status
|
2017-01-17 03:11:22 +01:00
|
|
|
Post.update({ _id: repost._id }, {
|
2016-12-28 23:49:51 +01:00
|
|
|
$inc: {
|
|
|
|
repost_count: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If has text content
|
|
|
|
if (text) {
|
|
|
|
// Analyze
|
|
|
|
const tokens = parse(text);
|
2017-03-02 07:25:27 +01:00
|
|
|
/*
|
2016-12-28 23:49:51 +01:00
|
|
|
// Extract a hashtags
|
|
|
|
const hashtags = tokens
|
|
|
|
.filter(t => t.type == 'hashtag')
|
|
|
|
.map(t => t.hashtag)
|
|
|
|
// Drop dupulicates
|
|
|
|
.filter((v, i, s) => s.indexOf(v) == i);
|
|
|
|
|
|
|
|
// ハッシュタグをデータベースに登録
|
2017-03-02 07:25:27 +01:00
|
|
|
registerHashtags(user, hashtags);
|
|
|
|
*/
|
2016-12-28 23:49:51 +01:00
|
|
|
// Extract an '@' mentions
|
|
|
|
const atMentions = tokens
|
|
|
|
.filter(t => t.type == 'mention')
|
|
|
|
.map(m => m.username)
|
|
|
|
// Drop dupulicates
|
|
|
|
.filter((v, i, s) => s.indexOf(v) == i);
|
|
|
|
|
|
|
|
// Resolve all mentions
|
|
|
|
await Promise.all(atMentions.map(async (mention) => {
|
|
|
|
// Fetch mentioned user
|
|
|
|
// SELECT _id
|
|
|
|
const mentionee = await User
|
|
|
|
.findOne({
|
|
|
|
username_lower: mention.toLowerCase()
|
|
|
|
}, { _id: true });
|
|
|
|
|
|
|
|
// When mentioned user not found
|
|
|
|
if (mentionee == null) return;
|
|
|
|
|
|
|
|
// 既に言及されたユーザーに対する返信や引用repostの場合も無視
|
2017-03-02 07:25:27 +01:00
|
|
|
if (inReplyToPost && inReplyToPost.user_id.equals(mentionee._id)) return;
|
2016-12-28 23:49:51 +01:00
|
|
|
if (repost && repost.user_id.equals(mentionee._id)) return;
|
|
|
|
|
|
|
|
// Add mention
|
|
|
|
addMention(mentionee._id, 'mention');
|
|
|
|
|
|
|
|
// Create notification
|
|
|
|
notify(mentionee._id, user._id, 'mention', {
|
|
|
|
post_id: post._id
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register to search database
|
|
|
|
if (text && config.elasticsearch.enable) {
|
|
|
|
const es = require('../../../db/elasticsearch');
|
|
|
|
|
|
|
|
es.index({
|
|
|
|
index: 'misskey',
|
|
|
|
type: 'post',
|
|
|
|
id: post._id.toString(),
|
|
|
|
body: {
|
|
|
|
text: post.text
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append mentions data
|
|
|
|
if (mentions.length > 0) {
|
2017-01-17 03:11:22 +01:00
|
|
|
Post.update({ _id: post._id }, {
|
2016-12-28 23:49:51 +01:00
|
|
|
$set: {
|
|
|
|
mentions: mentions
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|