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

252 lines
7.4 KiB
TypeScript
Raw Normal View History

2016-12-29 07:49:51 +09:00
/**
* Module dependencies
*/
2017-03-09 03:50:09 +09:00
import $ from 'cafy';
2017-03-25 15:56:26 +09:00
import deepEqual = require('deep-equal');
2018-04-08 02:30:37 +09:00
import Note, { INote, isValidText, isValidCw, pack } from '../../../../models/note';
2018-04-02 17:11:14 +09:00
import { ILocalUser } from '../../../../models/user';
2018-04-02 04:01:34 +09:00
import Channel, { IChannel } from '../../../../models/channel';
2018-03-29 20:32:18 +09:00
import DriveFile from '../../../../models/drive-file';
2018-04-08 02:30:37 +09:00
import create from '../../../../services/note/create';
2018-04-05 00:50:57 +09:00
import { IApp } from '../../../../models/app';
2016-12-29 07:49:51 +09:00
/**
2018-04-08 02:30:37 +09:00
* Create a note
2016-12-29 07:49:51 +09:00
*
2017-03-01 17:37:01 +09:00
* @param {any} params
* @param {any} user
* @param {any} app
* @return {Promise<any>}
2016-12-29 07:49:51 +09:00
*/
2018-04-05 00:50:57 +09:00
module.exports = (params, user: ILocalUser, app: IApp) => new Promise(async (res, rej) => {
2018-04-03 17:46:09 +09: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-29 07:49:51 +09:00
// Get 'text' parameter
2017-03-09 03:50:09 +09:00
const [text, textErr] = $(params.text).optional.string().pipe(isValidText).$;
2017-03-02 03:16:39 +09:00
if (textErr) return rej('invalid text');
2016-12-29 07:49:51 +09:00
2018-03-30 11:24:07 +09:00
// Get 'cw' parameter
const [cw, cwErr] = $(params.cw).optional.string().pipe(isValidCw).$;
if (cwErr) return rej('invalid cw');
2018-03-29 14:48:47 +09:00
// Get 'viaMobile' parameter
const [viaMobile = false, viaMobileErr] = $(params.viaMobile).optional.boolean().$;
if (viaMobileErr) return rej('invalid viaMobile');
2018-03-04 09:39:25 +09:00
2018-02-26 00:39:05 +09: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 08:44:37 +09:00
// Get 'geo' parameter
const [geo, geoErr] = $(params.geo).optional.nullable.strict.object()
2018-03-29 15:23:15 +09:00
.have('coordinates', $().array().length(2)
.item(0, $().number().range(-180, 180))
.item(1, $().number().range(-90, 90)))
2018-03-05 08:44:37 +09: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 14:48:47 +09: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 23:39:58 +09:00
2017-03-02 03:16:39 +09:00
let files = [];
2017-03-03 20:28:42 +09:00
if (mediaIds !== undefined) {
2016-12-29 07:49:51 +09:00
// Fetch files
// forEach だと途中でエラーなどがあっても return できないので
// 敢えて for を使っています。
2017-05-24 20:50:17 +09:00
for (const mediaId of mediaIds) {
2016-12-29 07:49:51 +09:00
// Fetch file
// SELECT _id
const entity = await DriveFile.findOne({
2017-03-02 05:11:37 +09:00
_id: mediaId,
2018-03-29 14:48:47 +09:00
'metadata.userId': user._id
2017-05-05 16:46:50 +09:00
});
2016-12-29 07:49:51 +09:00
if (entity === null) {
return rej('file not found');
} else {
files.push(entity);
}
}
} else {
files = null;
}
2018-04-08 02:30:37 +09:00
// Get 'renoteId' parameter
const [renoteId, renoteIdErr] = $(params.renoteId).optional.id().$;
if (renoteIdErr) return rej('invalid renoteId');
2017-01-18 05:39:50 +09:00
2018-04-08 02:30:37 +09:00
let renote: INote = null;
2017-10-31 22:09:09 +09:00
let isQuote = false;
2018-04-08 02:30:37 +09:00
if (renoteId !== undefined) {
// Fetch renote to note
renote = await Note.findOne({
_id: renoteId
2016-12-29 07:49:51 +09:00
});
2018-04-08 02:30:37 +09:00
if (renote == null) {
return rej('renoteee is not found');
} else if (renote.renoteId && !renote.text && !renote.mediaIds) {
return rej('cannot renote to renote');
2016-12-29 07:49:51 +09:00
}
2018-04-08 02:30:37 +09:00
// Fetch recently note
const latestNote = await Note.findOne({
2018-03-29 14:48:47 +09:00
userId: user._id
2017-01-17 11:11:22 +09:00
}, {
2017-05-05 16:46:50 +09:00
sort: {
_id: -1
}
});
2016-12-29 07:49:51 +09:00
2017-10-31 22:09:09 +09:00
isQuote = text != null || files != null;
2018-04-08 02:30:37 +09:00
// 直近と同じRenote対象かつ引用じゃなかったらエラー
if (latestNote &&
latestNote.renoteId &&
latestNote.renoteId.equals(renote._id) &&
2017-10-31 22:09:09 +09:00
!isQuote) {
2018-04-08 02:30:37 +09:00
return rej('cannot renote same note that already reposted in your latest note');
2016-12-29 07:49:51 +09:00
}
2018-04-08 02:30:37 +09:00
// 直近がRenote対象かつ引用じゃなかったらエラー
if (latestNote &&
latestNote._id.equals(renote._id) &&
2017-10-31 22:09:09 +09:00
!isQuote) {
2018-04-08 02:30:37 +09:00
return rej('cannot renote your latest note');
2016-12-29 07:49:51 +09:00
}
}
2018-03-29 14:48:47 +09:00
// Get 'replyId' parameter
const [replyId, replyIdErr] = $(params.replyId).optional.id().$;
if (replyIdErr) return rej('invalid replyId');
2017-01-18 05:39:50 +09:00
2018-04-08 02:30:37 +09:00
let reply: INote = null;
2017-11-01 10:45:01 +09:00
if (replyId !== undefined) {
2017-01-18 05:39:50 +09:00
// Fetch reply
2018-04-08 02:30:37 +09:00
reply = await Note.findOne({
2017-11-01 10:45:01 +09:00
_id: replyId
2016-12-29 07:49:51 +09:00
});
2017-11-01 10:45:01 +09:00
if (reply === null) {
2018-04-08 02:30:37 +09:00
return rej('in reply to note is not found');
2016-12-29 07:49:51 +09:00
}
2018-04-08 02:30:37 +09:00
// 返信対象が引用でないRenoteだったらエラー
if (reply.renoteId && !reply.text && !reply.mediaIds) {
return rej('cannot reply to renote');
2016-12-29 07:49:51 +09:00
}
}
2018-03-29 14:48:47 +09:00
// Get 'channelId' parameter
const [channelId, channelIdErr] = $(params.channelId).optional.id().$;
if (channelIdErr) return rej('invalid channelId');
2017-10-31 22:09:09 +09: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 14:48:47 +09:00
if (reply && !channelId.equals(reply.channelId)) {
2017-10-31 22:09:09 +09:00
return rej('チャンネル内部からチャンネル外部の投稿に返信することはできません');
}
2018-04-08 02:30:37 +09:00
// Renote対象の投稿がこのチャンネルじゃなかったらダメ
if (renote && !channelId.equals(renote.channelId)) {
return rej('チャンネル内部からチャンネル外部の投稿をRenoteすることはできません');
2017-10-31 22:09:09 +09:00
}
2018-04-08 02:30:37 +09:00
// 引用ではないRenoteはダメ
if (renote && !isQuote) {
return rej('チャンネル内部では引用ではないRenoteをすることはできません');
2017-10-31 22:09:09 +09:00
}
2017-11-01 01:38:19 +09:00
} else {
// 返信対象の投稿がチャンネルへの投稿だったらダメ
2018-03-29 14:48:47 +09:00
if (reply && reply.channelId != null) {
2017-11-01 01:38:19 +09:00
return rej('チャンネル外部からチャンネル内部の投稿に返信することはできません');
}
2018-04-08 02:30:37 +09:00
// Renote対象の投稿がチャンネルへの投稿だったらダメ
if (renote && renote.channelId != null) {
return rej('チャンネル外部からチャンネル内部の投稿をRenoteすることはできません');
2017-11-01 01:38:19 +09:00
}
2017-10-31 22:09:09 +09:00
}
2017-02-14 13:59:26 +09:00
// Get 'poll' parameter
2017-03-09 03:59:12 +09:00
const [poll, pollErr] = $(params.poll).optional.strict.object()
2017-03-09 03:50:09 +09:00
.have('choices', $().array('string')
.unique()
.range(2, 10)
.each(c => c.length > 0 && c.length < 50))
.$;
2017-03-02 05:11:37 +09:00
if (pollErr) return rej('invalid poll');
2017-03-09 03:50:09 +09:00
if (poll) {
(poll as any).choices = (poll as any).choices.map((choice, i) => ({
2017-02-14 13:59:26 +09:00
id: i, // IDを付与
2017-03-02 05:11:37 +09:00
text: choice.trim(),
2017-02-14 13:59:26 +09:00
votes: 0
}));
}
2018-04-08 02:30:37 +09:00
// テキストが無いかつ添付ファイルが無いかつRenoteも無いかつ投票も無かったらエラー
if (text === undefined && files === null && renote === null && poll === undefined) {
return rej('text, mediaIds, renoteId or poll is required');
2016-12-29 07:49:51 +09:00
}
2017-03-25 15:56:26 +09:00
// 直近の投稿と重複してたらエラー
// TODO: 直近の投稿が一日前くらいなら重複とは見なさない
2018-04-08 02:30:37 +09:00
if (user.latestNote) {
2017-03-25 15:56:26 +09:00
if (deepEqual({
2018-04-08 02:30:37 +09:00
text: user.latestNote.text,
reply: user.latestNote.replyId ? user.latestNote.replyId.toString() : null,
renote: user.latestNote.renoteId ? user.latestNote.renoteId.toString() : null,
mediaIds: (user.latestNote.mediaIds || []).map(id => id.toString())
2017-03-25 15:56:26 +09:00
}, {
2017-10-31 22:14:12 +09:00
text: text,
2017-11-01 10:45:01 +09:00
reply: reply ? reply._id.toString() : null,
2018-04-08 02:30:37 +09:00
renote: renote ? renote._id.toString() : null,
2018-03-29 14:48:47 +09:00
mediaIds: (files || []).map(file => file._id.toString())
2017-10-31 22:14:12 +09:00
})) {
2017-03-25 15:56:26 +09:00
return rej('duplicate');
}
}
2018-04-02 17:11:14 +09:00
// 投稿を作成
2018-04-08 02:30:37 +09:00
const note = await create(user, {
2018-04-02 17:11:14 +09:00
createdAt: new Date(),
2018-04-05 00:50:57 +09:00
media: files,
2018-04-02 17:11:14 +09:00
poll: poll,
text: text,
2018-04-05 00:50:57 +09:00
reply,
2018-04-08 02:30:37 +09:00
renote,
2018-04-02 17:11:14 +09:00
cw: cw,
tags: tags,
2018-04-05 00:50:57 +09:00
app: app,
2018-04-02 17:11:14 +09:00
viaMobile: viaMobile,
2018-04-03 17:46:09 +09:00
visibility,
2018-04-02 17:11:14 +09:00
geo
2018-04-05 00:50:57 +09:00
});
2016-12-29 07:49:51 +09:00
2018-04-08 02:30:37 +09:00
const noteObj = await pack(note, user);
2016-12-29 07:49:51 +09:00
2018-04-02 17:11:14 +09:00
// Reponse
res({
2018-04-08 02:30:37 +09:00
createdNote: noteObj
2018-04-02 17:11:14 +09:00
});
2016-12-29 07:49:51 +09:00
});