2018-07-07 12:19:00 +02:00
|
|
|
import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
|
2018-07-15 20:25:35 +02:00
|
|
|
const ms = require('ms');
|
2018-04-07 19:30:37 +02:00
|
|
|
import Note, { INote, isValidText, isValidCw, pack } from '../../../../models/note';
|
2018-06-18 02:54:53 +02:00
|
|
|
import User, { ILocalUser, IUser } from '../../../../models/user';
|
2018-07-05 16:36:07 +02:00
|
|
|
import DriveFile, { IDriveFile } from '../../../../models/drive-file';
|
2018-04-07 19:30:37 +02:00
|
|
|
import create from '../../../../services/note/create';
|
|
|
|
import { IApp } from '../../../../models/app';
|
2018-07-05 15:35:35 +02:00
|
|
|
import getParams from '../../get-params';
|
|
|
|
|
|
|
|
export const meta = {
|
2018-07-05 16:47:36 +02:00
|
|
|
desc: {
|
|
|
|
ja: '投稿します。'
|
|
|
|
},
|
2018-07-05 17:04:40 +02:00
|
|
|
|
2018-07-15 20:25:35 +02:00
|
|
|
requireCredential: true,
|
|
|
|
|
|
|
|
limit: {
|
|
|
|
duration: ms('1hour'),
|
2018-08-17 21:21:49 +02:00
|
|
|
max: 300
|
2018-07-15 20:25:35 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
kind: 'note-write',
|
|
|
|
|
2018-07-05 15:35:35 +02:00
|
|
|
params: {
|
2018-07-05 16:36:07 +02:00
|
|
|
visibility: $.str.optional.or(['public', 'home', 'followers', 'specified', 'private']).note({
|
2018-07-05 15:35:35 +02:00
|
|
|
default: 'public',
|
|
|
|
desc: {
|
|
|
|
ja: '投稿の公開範囲'
|
|
|
|
}
|
2018-07-05 16:36:07 +02:00
|
|
|
}),
|
|
|
|
|
|
|
|
visibleUserIds: $.arr($.type(ID)).optional.unique().min(1).note({
|
2018-07-05 15:35:35 +02:00
|
|
|
desc: {
|
|
|
|
ja: '(投稿の公開範囲が specified の場合)投稿を閲覧できるユーザー'
|
|
|
|
}
|
2018-07-05 16:36:07 +02:00
|
|
|
}),
|
|
|
|
|
|
|
|
text: $.str.optional.nullable.pipe(isValidText).note({
|
2018-07-05 15:35:35 +02:00
|
|
|
default: null,
|
|
|
|
desc: {
|
|
|
|
ja: '投稿内容'
|
|
|
|
}
|
2018-07-05 16:36:07 +02:00
|
|
|
}),
|
|
|
|
|
|
|
|
cw: $.str.optional.nullable.pipe(isValidCw).note({
|
|
|
|
desc: {
|
|
|
|
ja: 'コンテンツの警告。このパラメータを指定すると設定したテキストで投稿のコンテンツを隠す事が出来ます。'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
viaMobile: $.bool.optional.note({
|
|
|
|
default: false,
|
|
|
|
desc: {
|
|
|
|
ja: 'モバイルデバイスからの投稿か否か。'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
geo: $.obj({
|
|
|
|
coordinates: $.arr().length(2)
|
|
|
|
.item(0, $.num.range(-180, 180))
|
|
|
|
.item(1, $.num.range(-90, 90)),
|
|
|
|
altitude: $.num.nullable,
|
|
|
|
accuracy: $.num.nullable,
|
|
|
|
altitudeAccuracy: $.num.nullable,
|
|
|
|
heading: $.num.nullable.range(0, 360),
|
|
|
|
speed: $.num.nullable
|
|
|
|
}).optional.nullable.strict().note({
|
|
|
|
desc: {
|
|
|
|
ja: '位置情報'
|
2018-07-05 19:58:29 +02:00
|
|
|
},
|
|
|
|
ref: 'geo'
|
2018-07-05 16:36:07 +02:00
|
|
|
}),
|
|
|
|
|
|
|
|
mediaIds: $.arr($.type(ID)).optional.unique().range(1, 4).note({
|
|
|
|
desc: {
|
|
|
|
ja: '添付するメディア'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
renoteId: $.type(ID).optional.note({
|
|
|
|
desc: {
|
|
|
|
ja: 'Renote対象'
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
poll: $.obj({
|
|
|
|
choices: $.arr($.str)
|
|
|
|
.unique()
|
|
|
|
.range(2, 10)
|
|
|
|
.each(c => c.length > 0 && c.length < 50)
|
|
|
|
}).optional.strict().note({
|
|
|
|
desc: {
|
|
|
|
ja: 'アンケート'
|
2018-07-05 19:58:29 +02:00
|
|
|
},
|
|
|
|
ref: 'poll'
|
2018-07-05 16:36:07 +02:00
|
|
|
})
|
2018-07-05 16:47:36 +02:00
|
|
|
},
|
2018-07-05 17:04:40 +02:00
|
|
|
|
2018-07-05 16:47:36 +02:00
|
|
|
res: {
|
2018-07-05 17:04:40 +02:00
|
|
|
type: 'object',
|
2018-07-07 06:34:42 +02:00
|
|
|
props: {
|
2018-07-05 16:47:36 +02:00
|
|
|
createdNote: {
|
|
|
|
type: 'entity(Note)',
|
|
|
|
desc: {
|
|
|
|
ja: '作成した投稿'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-05 15:35:35 +02:00
|
|
|
}
|
|
|
|
};
|
2018-04-07 19:30:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a note
|
|
|
|
*/
|
2018-07-05 19:58:29 +02:00
|
|
|
export default (params: any, user: ILocalUser, app: IApp) => new Promise(async (res, rej) => {
|
2018-07-05 15:35:35 +02:00
|
|
|
const [ps, psErr] = getParams(meta, params);
|
|
|
|
if (psErr) return rej(psErr);
|
2018-04-28 21:30:51 +02:00
|
|
|
|
2018-06-18 02:54:53 +02:00
|
|
|
let visibleUsers: IUser[] = [];
|
2018-07-05 15:35:35 +02:00
|
|
|
if (ps.visibleUserIds !== undefined) {
|
|
|
|
visibleUsers = await Promise.all(ps.visibleUserIds.map(id => User.findOne({
|
2018-04-28 21:30:51 +02:00
|
|
|
_id: id
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
|
2018-07-05 16:36:07 +02:00
|
|
|
let files: IDriveFile[] = [];
|
|
|
|
if (ps.mediaIds !== undefined) {
|
2018-04-07 19:30:37 +02:00
|
|
|
// Fetch files
|
|
|
|
// forEach だと途中でエラーなどがあっても return できないので
|
|
|
|
// 敢えて for を使っています。
|
2018-07-05 16:36:07 +02:00
|
|
|
for (const mediaId of ps.mediaIds) {
|
2018-04-07 19:30:37 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
let renote: INote = null;
|
2018-07-05 16:36:07 +02:00
|
|
|
if (ps.renoteId !== undefined) {
|
2018-04-07 19:30:37 +02:00
|
|
|
// Fetch renote to note
|
|
|
|
renote = await Note.findOne({
|
2018-07-05 16:36:07 +02:00
|
|
|
_id: ps.renoteId
|
2018-04-07 19:30:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (renote == null) {
|
|
|
|
return rej('renoteee is not found');
|
|
|
|
} else if (renote.renoteId && !renote.text && !renote.mediaIds) {
|
|
|
|
return rej('cannot renote to renote');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 'replyId' parameter
|
2018-07-05 16:36:07 +02:00
|
|
|
const [replyId, replyIdErr] = $.type(ID).optional.get(params.replyId);
|
2018-04-07 19:30:37 +02: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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-05 16:36:07 +02:00
|
|
|
if (ps.poll) {
|
|
|
|
(ps.poll as any).choices = (ps.poll as any).choices.map((choice: string, i: number) => ({
|
2018-04-07 19:30:37 +02:00
|
|
|
id: i, // IDを付与
|
|
|
|
text: choice.trim(),
|
|
|
|
votes: 0
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// テキストが無いかつ添付ファイルが無いかつRenoteも無いかつ投票も無かったらエラー
|
2018-07-05 16:36:07 +02:00
|
|
|
if ((ps.text === undefined || ps.text === null) && files === null && renote === null && ps.poll === undefined) {
|
2018-04-07 19:30:37 +02:00
|
|
|
return rej('text, mediaIds, renoteId or poll is required');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 投稿を作成
|
|
|
|
const note = await create(user, {
|
|
|
|
createdAt: new Date(),
|
|
|
|
media: files,
|
2018-07-05 16:36:07 +02:00
|
|
|
poll: ps.poll,
|
|
|
|
text: ps.text,
|
2018-04-07 19:30:37 +02:00
|
|
|
reply,
|
|
|
|
renote,
|
2018-07-05 16:36:07 +02:00
|
|
|
cw: ps.cw,
|
2018-04-22 10:04:52 +02:00
|
|
|
app,
|
2018-07-05 16:36:07 +02:00
|
|
|
viaMobile: ps.viaMobile,
|
|
|
|
visibility: ps.visibility,
|
2018-04-28 21:30:51 +02:00
|
|
|
visibleUsers,
|
2018-07-05 16:36:07 +02:00
|
|
|
geo: ps.geo
|
2018-04-07 19:30:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const noteObj = await pack(note, user);
|
|
|
|
|
|
|
|
// Reponse
|
|
|
|
res({
|
|
|
|
createdNote: noteObj
|
|
|
|
});
|
|
|
|
});
|