2018-04-08 02:30:37 +09:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2018-04-24 18:13:06 +09:00
|
|
|
import $ from 'cafy'; import ID from '../../../../cafy-id';
|
2018-04-08 02:30:37 +09:00
|
|
|
import Note, { INote, isValidText, isValidCw, pack } from '../../../../models/note';
|
|
|
|
import { ILocalUser } from '../../../../models/user';
|
|
|
|
import Channel, { IChannel } from '../../../../models/channel';
|
|
|
|
import DriveFile from '../../../../models/drive-file';
|
|
|
|
import create from '../../../../services/note/create';
|
|
|
|
import { IApp } from '../../../../models/app';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a note
|
|
|
|
*/
|
|
|
|
module.exports = (params, user: ILocalUser, app: IApp) => new Promise(async (res, rej) => {
|
|
|
|
// Get 'visibility' parameter
|
2018-04-27 19:12:15 +09:00
|
|
|
const [visibility = 'public', visibilityErr] = $(params.visibility).optional.string().or(['public', 'unlisted', 'private', 'direct']).get();
|
2018-04-08 02:30:37 +09:00
|
|
|
if (visibilityErr) return rej('invalid visibility');
|
|
|
|
|
|
|
|
// Get 'text' parameter
|
2018-04-27 19:12:15 +09:00
|
|
|
const [text = null, textErr] = $(params.text).optional.nullable.string().pipe(isValidText).get();
|
2018-04-08 02:30:37 +09:00
|
|
|
if (textErr) return rej('invalid text');
|
|
|
|
|
|
|
|
// Get 'cw' parameter
|
2018-04-27 19:12:15 +09:00
|
|
|
const [cw, cwErr] = $(params.cw).optional.nullable.string().pipe(isValidCw).get();
|
2018-04-08 02:30:37 +09:00
|
|
|
if (cwErr) return rej('invalid cw');
|
|
|
|
|
|
|
|
// Get 'viaMobile' parameter
|
2018-04-27 19:12:15 +09:00
|
|
|
const [viaMobile = false, viaMobileErr] = $(params.viaMobile).optional.boolean().get();
|
2018-04-08 02:30:37 +09:00
|
|
|
if (viaMobileErr) return rej('invalid viaMobile');
|
|
|
|
|
|
|
|
// Get 'tags' parameter
|
2018-04-27 19:12:15 +09:00
|
|
|
const [tags = [], tagsErr] = $(params.tags).optional.array($().string().range(1, 32)).unique().get();
|
2018-04-08 02:30:37 +09:00
|
|
|
if (tagsErr) return rej('invalid tags');
|
|
|
|
|
|
|
|
// Get 'geo' parameter
|
2018-04-24 18:13:06 +09:00
|
|
|
const [geo, geoErr] = $(params.geo).optional.nullable.object(true)
|
2018-04-08 02:30:37 +09:00
|
|
|
.have('coordinates', $().array().length(2)
|
|
|
|
.item(0, $().number().range(-180, 180))
|
|
|
|
.item(1, $().number().range(-90, 90)))
|
|
|
|
.have('altitude', $().nullable.number())
|
|
|
|
.have('accuracy', $().nullable.number())
|
|
|
|
.have('altitudeAccuracy', $().nullable.number())
|
|
|
|
.have('heading', $().nullable.number().range(0, 360))
|
|
|
|
.have('speed', $().nullable.number())
|
2018-04-27 19:12:15 +09:00
|
|
|
.get();
|
2018-04-08 02:30:37 +09:00
|
|
|
if (geoErr) return rej('invalid geo');
|
|
|
|
|
|
|
|
// Get 'mediaIds' parameter
|
2018-04-27 19:12:15 +09:00
|
|
|
const [mediaIds, mediaIdsErr] = $(params.mediaIds).optional.array($().type(ID)).unique().range(1, 4).get();
|
2018-04-08 02:30:37 +09:00
|
|
|
if (mediaIdsErr) return rej('invalid mediaIds');
|
|
|
|
|
|
|
|
let files = [];
|
|
|
|
if (mediaIds !== undefined) {
|
|
|
|
// Fetch files
|
|
|
|
// forEach だと途中でエラーなどがあっても return できないので
|
|
|
|
// 敢えて for を使っています。
|
|
|
|
for (const mediaId of mediaIds) {
|
|
|
|
// Fetch file
|
|
|
|
// SELECT _id
|
|
|
|
const entity = await DriveFile.findOne({
|
|
|
|
_id: mediaId,
|
|
|
|
'metadata.userId': user._id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (entity === null) {
|
|
|
|
return rej('file not found');
|
|
|
|
} else {
|
|
|
|
files.push(entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
files = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 'renoteId' parameter
|
2018-04-27 19:12:15 +09:00
|
|
|
const [renoteId, renoteIdErr] = $(params.renoteId).optional.type(ID).get();
|
2018-04-08 02:30:37 +09:00
|
|
|
if (renoteIdErr) return rej('invalid renoteId');
|
|
|
|
|
|
|
|
let renote: INote = null;
|
|
|
|
let isQuote = false;
|
|
|
|
if (renoteId !== undefined) {
|
|
|
|
// Fetch renote to note
|
|
|
|
renote = await Note.findOne({
|
|
|
|
_id: renoteId
|
|
|
|
});
|
|
|
|
|
|
|
|
if (renote == null) {
|
|
|
|
return rej('renoteee is not found');
|
|
|
|
} else if (renote.renoteId && !renote.text && !renote.mediaIds) {
|
|
|
|
return rej('cannot renote to renote');
|
|
|
|
}
|
|
|
|
|
|
|
|
isQuote = text != null || files != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 'replyId' parameter
|
2018-04-27 19:12:15 +09:00
|
|
|
const [replyId, replyIdErr] = $(params.replyId).optional.type(ID).get();
|
2018-04-08 02:30:37 +09:00
|
|
|
if (replyIdErr) return rej('invalid replyId');
|
|
|
|
|
|
|
|
let reply: INote = null;
|
|
|
|
if (replyId !== undefined) {
|
|
|
|
// Fetch reply
|
|
|
|
reply = await Note.findOne({
|
|
|
|
_id: replyId
|
|
|
|
});
|
|
|
|
|
|
|
|
if (reply === null) {
|
|
|
|
return rej('in reply to note is not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返信対象が引用でないRenoteだったらエラー
|
|
|
|
if (reply.renoteId && !reply.text && !reply.mediaIds) {
|
|
|
|
return rej('cannot reply to renote');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 'channelId' parameter
|
2018-04-27 19:12:15 +09:00
|
|
|
const [channelId, channelIdErr] = $(params.channelId).optional.type(ID).get();
|
2018-04-08 02:30:37 +09:00
|
|
|
if (channelIdErr) return rej('invalid channelId');
|
|
|
|
|
|
|
|
let channel: IChannel = null;
|
|
|
|
if (channelId !== undefined) {
|
|
|
|
// Fetch channel
|
|
|
|
channel = await Channel.findOne({
|
|
|
|
_id: channelId
|
|
|
|
});
|
|
|
|
|
|
|
|
if (channel === null) {
|
|
|
|
return rej('channel not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返信対象の投稿がこのチャンネルじゃなかったらダメ
|
|
|
|
if (reply && !channelId.equals(reply.channelId)) {
|
|
|
|
return rej('チャンネル内部からチャンネル外部の投稿に返信することはできません');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renote対象の投稿がこのチャンネルじゃなかったらダメ
|
|
|
|
if (renote && !channelId.equals(renote.channelId)) {
|
|
|
|
return rej('チャンネル内部からチャンネル外部の投稿をRenoteすることはできません');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 引用ではないRenoteはダメ
|
|
|
|
if (renote && !isQuote) {
|
|
|
|
return rej('チャンネル内部では引用ではないRenoteをすることはできません');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 返信対象の投稿がチャンネルへの投稿だったらダメ
|
|
|
|
if (reply && reply.channelId != null) {
|
|
|
|
return rej('チャンネル外部からチャンネル内部の投稿に返信することはできません');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renote対象の投稿がチャンネルへの投稿だったらダメ
|
|
|
|
if (renote && renote.channelId != null) {
|
|
|
|
return rej('チャンネル外部からチャンネル内部の投稿をRenoteすることはできません');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 'poll' parameter
|
2018-04-24 18:13:06 +09:00
|
|
|
const [poll, pollErr] = $(params.poll).optional.object(true)
|
|
|
|
.have('choices', $().array($().string())
|
2018-04-08 02:30:37 +09:00
|
|
|
.unique()
|
|
|
|
.range(2, 10)
|
|
|
|
.each(c => c.length > 0 && c.length < 50))
|
2018-04-27 19:12:15 +09:00
|
|
|
.get();
|
2018-04-08 02:30:37 +09:00
|
|
|
if (pollErr) return rej('invalid poll');
|
|
|
|
|
|
|
|
if (poll) {
|
|
|
|
(poll as any).choices = (poll as any).choices.map((choice, i) => ({
|
|
|
|
id: i, // IDを付与
|
|
|
|
text: choice.trim(),
|
|
|
|
votes: 0
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// テキストが無いかつ添付ファイルが無いかつRenoteも無いかつ投票も無かったらエラー
|
|
|
|
if (text === undefined && files === null && renote === null && poll === undefined) {
|
|
|
|
return rej('text, mediaIds, renoteId or poll is required');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 投稿を作成
|
|
|
|
const note = await create(user, {
|
|
|
|
createdAt: new Date(),
|
|
|
|
media: files,
|
2018-04-22 17:04:52 +09:00
|
|
|
poll,
|
|
|
|
text,
|
2018-04-08 02:30:37 +09:00
|
|
|
reply,
|
|
|
|
renote,
|
2018-04-22 17:04:52 +09:00
|
|
|
cw,
|
|
|
|
tags,
|
|
|
|
app,
|
|
|
|
viaMobile,
|
2018-04-08 02:30:37 +09:00
|
|
|
visibility,
|
|
|
|
geo
|
|
|
|
});
|
|
|
|
|
|
|
|
const noteObj = await pack(note, user);
|
|
|
|
|
|
|
|
// Reponse
|
|
|
|
res({
|
|
|
|
createdNote: noteObj
|
|
|
|
});
|
|
|
|
});
|