mizzkey/src/server/api/endpoints/posts/create.ts

312 lines
8.8 KiB
TypeScript
Raw Normal View History

2016-12-28 23:49:51 +01:00
/**
* Module dependencies
*/
2017-03-08 19:50:09 +01:00
import $ from 'cafy';
2017-03-25 07:56:26 +01:00
import deepEqual = require('deep-equal');
2018-04-02 10:11:14 +02:00
import renderAcct from '../../../../acct/render';
import config from '../../../../config';
2018-04-02 05:58:53 +02:00
import html from '../../../../text/html';
import parse from '../../../../text/parse';
2018-04-02 13:16:13 +02:00
import Post, { IPost, isValidText, isValidCw } from '../../../../models/post';
2018-04-02 10:11:14 +02:00
import { ILocalUser } from '../../../../models/user';
2018-04-01 21:01:34 +02:00
import Channel, { IChannel } from '../../../../models/channel';
2018-03-29 13:32:18 +02:00
import DriveFile from '../../../../models/drive-file';
2018-04-02 10:11:14 +02:00
import create from '../../../../post/create';
import distribute from '../../../../post/distribute';
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
*/
2018-04-01 21:01:34 +02:00
module.exports = (params, user: ILocalUser, app) => new Promise(async (res, rej) => {
2018-04-03 10:46:09 +02:00
// Get 'visibility' parameter
const [visibility = 'public', visibilityErr] = $(params.visibility).optional.string().or(['public', 'unlisted', 'private', 'direct']).$;
if (visibilityErr) return rej('invalid visibility');
2016-12-28 23:49:51 +01:00
// Get 'text' parameter
2017-03-08 19:50:09 +01:00
const [text, textErr] = $(params.text).optional.string().pipe(isValidText).$;
2017-03-01 19:16:39 +01:00
if (textErr) return rej('invalid text');
2016-12-28 23:49:51 +01:00
2018-03-30 04:24:07 +02:00
// Get 'cw' parameter
const [cw, cwErr] = $(params.cw).optional.string().pipe(isValidCw).$;
if (cwErr) return rej('invalid cw');
2018-03-29 07:48:47 +02:00
// Get 'viaMobile' parameter
const [viaMobile = false, viaMobileErr] = $(params.viaMobile).optional.boolean().$;
if (viaMobileErr) return rej('invalid viaMobile');
2018-03-04 01:39:25 +01:00
2018-02-25 16:39:05 +01:00
// Get 'tags' parameter
const [tags = [], tagsErr] = $(params.tags).optional.array('string').unique().eachQ(t => t.range(1, 32)).$;
if (tagsErr) return rej('invalid tags');
2018-03-05 00:44:37 +01:00
// Get 'geo' parameter
const [geo, geoErr] = $(params.geo).optional.nullable.strict.object()
2018-03-29 08:23:15 +02:00
.have('coordinates', $().array().length(2)
.item(0, $().number().range(-180, 180))
.item(1, $().number().range(-90, 90)))
2018-03-05 00:44:37 +01:00
.have('altitude', $().nullable.number())
.have('accuracy', $().nullable.number())
.have('altitudeAccuracy', $().nullable.number())
.have('heading', $().nullable.number().range(0, 360))
.have('speed', $().nullable.number())
.$;
if (geoErr) return rej('invalid geo');
2018-03-29 07:48:47 +02:00
// Get 'mediaIds' parameter
const [mediaIds, mediaIdsErr] = $(params.mediaIds).optional.array('id').unique().range(1, 4).$;
if (mediaIdsErr) return rej('invalid mediaIds');
2017-02-23 15:39:58 +01:00
2017-03-01 19:16:39 +01:00
let files = [];
2017-03-03 12:28:42 +01:00
if (mediaIds !== undefined) {
2016-12-28 23:49:51 +01:00
// Fetch files
// forEach だと途中でエラーなどがあっても return できないので
// 敢えて for を使っています。
2017-05-24 13:50:17 +02:00
for (const mediaId of mediaIds) {
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,
2018-03-29 07:48:47 +02:00
'metadata.userId': user._id
2017-05-05 09:46:50 +02:00
});
2016-12-28 23:49:51 +01:00
if (entity === null) {
return rej('file not found');
} else {
files.push(entity);
}
}
} else {
files = null;
}
2018-03-29 07:48:47 +02:00
// Get 'repostId' parameter
const [repostId, repostIdErr] = $(params.repostId).optional.id().$;
if (repostIdErr) return rej('invalid repostId');
2017-01-17 21:39:50 +01:00
2017-10-31 14:09:09 +01:00
let repost: IPost = null;
let isQuote = false;
2017-03-03 12:28:42 +01:00
if (repostId !== undefined) {
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');
2018-03-29 07:48:47 +02:00
} else if (repost.repostId && !repost.text && !repost.mediaIds) {
2016-12-28 23:49:51 +01:00
return rej('cannot repost to repost');
}
// Fetch recently post
const latestPost = await Post.findOne({
2018-03-29 07:48:47 +02:00
userId: user._id
2017-01-17 03:11:22 +01:00
}, {
2017-05-05 09:46:50 +02:00
sort: {
_id: -1
}
});
2016-12-28 23:49:51 +01:00
2017-10-31 14:09:09 +01:00
isQuote = text != null || files != null;
2016-12-28 23:49:51 +01:00
// 直近と同じRepost対象かつ引用じゃなかったらエラー
if (latestPost &&
2018-03-29 07:48:47 +02:00
latestPost.repostId &&
latestPost.repostId.equals(repost._id) &&
2017-10-31 14:09:09 +01:00
!isQuote) {
return rej('cannot repost same post that already reposted in your latest post');
2016-12-28 23:49:51 +01:00
}
// 直近がRepost対象かつ引用じゃなかったらエラー
if (latestPost &&
2017-04-14 13:45:37 +02:00
latestPost._id.equals(repost._id) &&
2017-10-31 14:09:09 +01:00
!isQuote) {
return rej('cannot repost your latest post');
2016-12-28 23:49:51 +01:00
}
}
2018-03-29 07:48:47 +02:00
// Get 'replyId' parameter
const [replyId, replyIdErr] = $(params.replyId).optional.id().$;
if (replyIdErr) return rej('invalid replyId');
2017-01-17 21:39:50 +01:00
2017-11-01 02:45:01 +01:00
let reply: IPost = null;
if (replyId !== undefined) {
2017-01-17 21:39:50 +01:00
// Fetch reply
2017-11-01 02:45:01 +01:00
reply = await Post.findOne({
_id: replyId
2016-12-28 23:49:51 +01:00
});
2017-11-01 02:45:01 +01:00
if (reply === null) {
2017-03-01 21:11:37 +01:00
return rej('in reply to post is not found');
2016-12-28 23:49:51 +01:00
}
// 返信対象が引用でないRepostだったらエラー
2018-03-29 07:48:47 +02:00
if (reply.repostId && !reply.text && !reply.mediaIds) {
2016-12-28 23:49:51 +01:00
return rej('cannot reply to repost');
}
}
2018-03-29 07:48:47 +02:00
// Get 'channelId' parameter
const [channelId, channelIdErr] = $(params.channelId).optional.id().$;
if (channelIdErr) return rej('invalid channelId');
2017-10-31 14:09:09 +01:00
let channel: IChannel = null;
if (channelId !== undefined) {
// Fetch channel
channel = await Channel.findOne({
_id: channelId
});
if (channel === null) {
return rej('channel not found');
}
// 返信対象の投稿がこのチャンネルじゃなかったらダメ
2018-03-29 07:48:47 +02:00
if (reply && !channelId.equals(reply.channelId)) {
2017-10-31 14:09:09 +01:00
return rej('チャンネル内部からチャンネル外部の投稿に返信することはできません');
}
// Repost対象の投稿がこのチャンネルじゃなかったらダメ
2018-03-29 07:48:47 +02:00
if (repost && !channelId.equals(repost.channelId)) {
2017-10-31 14:09:09 +01:00
return rej('チャンネル内部からチャンネル外部の投稿をRepostすることはできません');
}
// 引用ではないRepostはダメ
if (repost && !isQuote) {
return rej('チャンネル内部では引用ではないRepostをすることはできません');
}
2017-10-31 17:38:19 +01:00
} else {
// 返信対象の投稿がチャンネルへの投稿だったらダメ
2018-03-29 07:48:47 +02:00
if (reply && reply.channelId != null) {
2017-10-31 17:38:19 +01:00
return rej('チャンネル外部からチャンネル内部の投稿に返信することはできません');
}
// Repost対象の投稿がチャンネルへの投稿だったらダメ
2018-03-29 07:48:47 +02:00
if (repost && repost.channelId != null) {
2017-10-31 17:38:19 +01:00
return rej('チャンネル外部からチャンネル内部の投稿をRepostすることはできません');
}
2017-10-31 14:09:09 +01:00
}
2017-02-14 05:59:26 +01:00
// Get 'poll' parameter
2017-03-08 19:59:12 +01:00
const [poll, pollErr] = $(params.poll).optional.strict.object()
2017-03-08 19:50:09 +01:00
.have('choices', $().array('string')
.unique()
.range(2, 10)
.each(c => c.length > 0 && c.length < 50))
.$;
2017-03-01 21:11:37 +01:00
if (pollErr) return rej('invalid poll');
2017-03-08 19:50:09 +01:00
if (poll) {
(poll as any).choices = (poll as any).choices.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
}));
}
// テキストが無いかつ添付ファイルが無いかつRepostも無いかつ投票も無かったらエラー
2017-03-08 19:50:09 +01:00
if (text === undefined && files === null && repost === null && poll === undefined) {
2018-03-29 07:48:47 +02:00
return rej('text, mediaIds, repostId or poll is required');
2016-12-28 23:49:51 +01:00
}
2017-03-25 07:56:26 +01:00
// 直近の投稿と重複してたらエラー
// TODO: 直近の投稿が一日前くらいなら重複とは見なさない
2018-03-29 07:48:47 +02:00
if (user.latestPost) {
2017-03-25 07:56:26 +01:00
if (deepEqual({
2018-03-29 07:48:47 +02:00
text: user.latestPost.text,
reply: user.latestPost.replyId ? user.latestPost.replyId.toString() : null,
repost: user.latestPost.repostId ? user.latestPost.repostId.toString() : null,
mediaIds: (user.latestPost.mediaIds || []).map(id => id.toString())
2017-03-25 07:56:26 +01:00
}, {
2017-10-31 14:14:12 +01:00
text: text,
2017-11-01 02:45:01 +01:00
reply: reply ? reply._id.toString() : null,
2017-10-31 14:14:12 +01:00
repost: repost ? repost._id.toString() : null,
2018-03-29 07:48:47 +02:00
mediaIds: (files || []).map(file => file._id.toString())
2017-10-31 14:14:12 +01:00
})) {
2017-03-25 07:56:26 +01:00
return rej('duplicate');
}
}
2018-02-25 16:39:05 +01:00
let tokens = null;
if (text) {
// Analyze
tokens = parse(text);
// Extract hashtags
const hashtags = tokens
.filter(t => t.type == 'hashtag')
.map(t => t.hashtag);
hashtags.forEach(tag => {
if (tags.indexOf(tag) == -1) {
tags.push(tag);
}
});
}
2018-04-02 10:11:14 +02:00
let atMentions = [];
2016-12-28 23:49:51 +01:00
// If has text content
if (text) {
2017-04-14 13:45:37 +02: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);
// ハッシュタグをデータベースに登録
registerHashtags(user, hashtags);
*/
2016-12-28 23:49:51 +01:00
// Extract an '@' mentions
2018-04-02 10:11:14 +02:00
atMentions = tokens
2016-12-28 23:49:51 +01:00
.filter(t => t.type == 'mention')
2018-04-02 10:11:14 +02:00
.map(renderAcct)
2016-12-28 23:49:51 +01:00
// Drop dupulicates
.filter((v, i, s) => s.indexOf(v) == i);
2018-04-02 10:11:14 +02:00
}
2016-12-28 23:49:51 +01:00
2018-04-02 10:11:14 +02:00
// 投稿を作成
const post = await create({
createdAt: new Date(),
channelId: channel ? channel._id : undefined,
index: channel ? channel.index + 1 : undefined,
mediaIds: files ? files.map(file => file._id) : [],
poll: poll,
text: text,
textHtml: tokens === null ? null : html(tokens),
cw: cw,
tags: tags,
userId: user._id,
appId: app ? app._id : null,
viaMobile: viaMobile,
2018-04-03 10:46:09 +02:00
visibility,
2018-04-02 10:11:14 +02:00
geo
}, reply, repost, atMentions);
2016-12-28 23:49:51 +01:00
2018-04-02 13:16:13 +02:00
const postObj = await distribute(user, post.mentions, post);
2016-12-28 23:49:51 +01:00
2018-04-02 10:11:14 +02:00
// Reponse
res({
createdPost: postObj
});
2016-12-28 23:49:51 +01:00
// Register to search database
2018-04-02 10:11:14 +02:00
if (post.text && config.elasticsearch.enable) {
2016-12-28 23:49:51 +01:00
const es = require('../../../db/elasticsearch');
es.index({
index: 'misskey',
type: 'post',
id: post._id.toString(),
body: {
text: post.text
}
});
}
});