2019-02-05 11:48:08 +09:00
|
|
|
import $ from 'cafy';
|
2019-02-02 00:16:27 +09:00
|
|
|
import * as ms from 'ms';
|
2018-11-05 11:09:05 +09:00
|
|
|
import { length } from 'stringz';
|
2018-04-08 02:30:37 +09:00
|
|
|
import create from '../../../../services/note/create';
|
2018-11-02 13:47:44 +09:00
|
|
|
import define from '../../define';
|
2019-04-24 08:11:19 +09:00
|
|
|
import { fetchMeta } from '../../../../misc/fetch-meta';
|
2019-02-22 11:46:58 +09:00
|
|
|
import { ApiError } from '../../error';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { ID } from '../../../../misc/cafy-id';
|
|
|
|
import { User } from '../../../../models/entities/user';
|
|
|
|
import { Users, DriveFiles, Notes } from '../../../../models';
|
|
|
|
import { DriveFile } from '../../../../models/entities/drive-file';
|
|
|
|
import { Note } from '../../../../models/entities/note';
|
2019-10-01 01:46:31 +09:00
|
|
|
import { DB_MAX_NOTE_TEXT_LENGTH } from '../../../../misc/hard-limits';
|
2018-11-05 11:09:05 +09:00
|
|
|
|
|
|
|
let maxNoteTextLength = 1000;
|
|
|
|
|
|
|
|
setInterval(() => {
|
2018-11-06 07:14:43 +09:00
|
|
|
fetchMeta().then(m => {
|
|
|
|
maxNoteTextLength = m.maxNoteTextLength;
|
2018-11-05 11:09:05 +09:00
|
|
|
});
|
|
|
|
}, 3000);
|
2018-07-05 22:35:35 +09:00
|
|
|
|
|
|
|
export const meta = {
|
2018-10-22 05:16:27 +09:00
|
|
|
stability: 'stable',
|
|
|
|
|
2018-07-05 23:47:36 +09:00
|
|
|
desc: {
|
2018-08-29 06:59:43 +09:00
|
|
|
'ja-JP': '投稿します。'
|
2018-07-05 23:47:36 +09:00
|
|
|
},
|
2018-07-06 00:04:40 +09:00
|
|
|
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['notes'],
|
|
|
|
|
2018-07-16 03:25:35 +09:00
|
|
|
requireCredential: true,
|
|
|
|
|
|
|
|
limit: {
|
|
|
|
duration: ms('1hour'),
|
2018-08-18 04:21:49 +09:00
|
|
|
max: 300
|
2018-07-16 03:25:35 +09:00
|
|
|
},
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
kind: 'write:notes',
|
2018-07-16 03:25:35 +09:00
|
|
|
|
2018-07-05 22:35:35 +09:00
|
|
|
params: {
|
2018-11-02 03:32:24 +09:00
|
|
|
visibility: {
|
2019-06-06 19:45:25 +09:00
|
|
|
validator: $.optional.str.or(['public', 'home', 'followers', 'specified']),
|
2018-07-05 22:35:35 +09:00
|
|
|
default: 'public',
|
|
|
|
desc: {
|
2018-08-29 06:59:43 +09:00
|
|
|
'ja-JP': '投稿の公開範囲'
|
2018-07-05 22:35:35 +09:00
|
|
|
}
|
2018-11-02 03:32:24 +09:00
|
|
|
},
|
2018-07-05 23:36:07 +09:00
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
visibleUserIds: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.arr($.type(ID)).unique().min(0),
|
2018-07-05 22:35:35 +09:00
|
|
|
desc: {
|
2018-08-29 06:59:43 +09:00
|
|
|
'ja-JP': '(投稿の公開範囲が specified の場合)投稿を閲覧できるユーザー'
|
2018-07-05 22:35:35 +09:00
|
|
|
}
|
2018-11-02 03:32:24 +09:00
|
|
|
},
|
2018-07-05 23:36:07 +09:00
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
text: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.nullable.str.pipe(text =>
|
2019-10-01 01:46:31 +09:00
|
|
|
text.trim() != ''
|
|
|
|
&& length(text.trim()) <= maxNoteTextLength
|
|
|
|
&& Array.from(text.trim()).length <= DB_MAX_NOTE_TEXT_LENGTH // DB limit
|
2018-11-05 11:09:05 +09:00
|
|
|
),
|
2018-11-02 03:32:24 +09:00
|
|
|
default: null as any,
|
2018-07-05 22:35:35 +09:00
|
|
|
desc: {
|
2018-08-29 06:59:43 +09:00
|
|
|
'ja-JP': '投稿内容'
|
2018-07-05 22:35:35 +09:00
|
|
|
}
|
2018-11-02 03:32:24 +09:00
|
|
|
},
|
2018-07-05 23:36:07 +09:00
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
cw: {
|
2019-04-07 21:50:36 +09:00
|
|
|
validator: $.optional.nullable.str.pipe(Notes.validateCw),
|
2018-07-05 23:36:07 +09:00
|
|
|
desc: {
|
2018-08-29 06:59:43 +09:00
|
|
|
'ja-JP': 'コンテンツの警告。このパラメータを指定すると設定したテキストで投稿のコンテンツを隠す事が出来ます。'
|
2018-07-05 23:36:07 +09:00
|
|
|
}
|
2018-11-02 03:32:24 +09:00
|
|
|
},
|
2018-07-05 23:36:07 +09:00
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
viaMobile: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.bool,
|
2018-07-05 23:36:07 +09:00
|
|
|
default: false,
|
|
|
|
desc: {
|
2018-08-29 06:59:43 +09:00
|
|
|
'ja-JP': 'モバイルデバイスからの投稿か否か。'
|
2018-07-05 23:36:07 +09:00
|
|
|
}
|
2018-11-02 03:32:24 +09:00
|
|
|
},
|
|
|
|
|
2018-11-16 05:47:29 +09:00
|
|
|
localOnly: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.bool,
|
2018-11-16 05:47:29 +09:00
|
|
|
default: false,
|
|
|
|
desc: {
|
|
|
|
'ja-JP': 'ローカルのみに投稿か否か。'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-12-23 03:25:33 +09:00
|
|
|
noExtractMentions: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.bool,
|
2018-12-23 03:25:33 +09:00
|
|
|
default: false,
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '本文からメンションを展開しないか否か。'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
noExtractHashtags: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.bool,
|
2018-12-23 03:25:33 +09:00
|
|
|
default: false,
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '本文からハッシュタグを展開しないか否か。'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
noExtractEmojis: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.bool,
|
2018-12-23 03:25:33 +09:00
|
|
|
default: false,
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '本文からカスタム絵文字を展開しないか否か。'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
geo: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.nullable.obj({
|
2018-11-02 03:32:24 +09:00
|
|
|
coordinates: $.arr().length(2)
|
|
|
|
.item(0, $.num.range(-180, 180))
|
|
|
|
.item(1, $.num.range(-90, 90)),
|
2019-02-13 16:33:07 +09:00
|
|
|
altitude: $.nullable.num,
|
|
|
|
accuracy: $.nullable.num,
|
|
|
|
altitudeAccuracy: $.nullable.num,
|
|
|
|
heading: $.nullable.num.range(0, 360),
|
|
|
|
speed: $.nullable.num
|
|
|
|
}).strict(),
|
2018-07-05 23:36:07 +09:00
|
|
|
desc: {
|
2018-08-29 06:59:43 +09:00
|
|
|
'ja-JP': '位置情報'
|
2018-07-06 02:58:29 +09:00
|
|
|
},
|
|
|
|
ref: 'geo'
|
2018-11-02 03:32:24 +09:00
|
|
|
},
|
2018-07-05 23:36:07 +09:00
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
fileIds: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.arr($.type(ID)).unique().range(1, 4),
|
2018-09-05 19:32:46 +09:00
|
|
|
desc: {
|
|
|
|
'ja-JP': '添付するファイル'
|
|
|
|
}
|
2018-11-02 03:32:24 +09:00
|
|
|
},
|
2018-09-05 19:32:46 +09:00
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
mediaIds: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.arr($.type(ID)).unique().range(1, 4),
|
2019-02-25 04:18:09 +09:00
|
|
|
deprecated: true,
|
2018-07-05 23:36:07 +09:00
|
|
|
desc: {
|
2018-09-05 19:32:46 +09:00
|
|
|
'ja-JP': '添付するファイル (このパラメータは廃止予定です。代わりに fileIds を使ってください。)'
|
2018-07-05 23:36:07 +09:00
|
|
|
}
|
2018-11-02 03:32:24 +09:00
|
|
|
},
|
2018-07-05 23:36:07 +09:00
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
replyId: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.type(ID),
|
2018-11-02 03:32:24 +09:00
|
|
|
desc: {
|
|
|
|
'ja-JP': '返信対象'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
renoteId: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.type(ID),
|
2018-07-05 23:36:07 +09:00
|
|
|
desc: {
|
2018-08-29 06:59:43 +09:00
|
|
|
'ja-JP': 'Renote対象'
|
2018-07-05 23:36:07 +09:00
|
|
|
}
|
2018-11-02 03:32:24 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
poll: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.obj({
|
2018-11-02 03:32:24 +09:00
|
|
|
choices: $.arr($.str)
|
|
|
|
.unique()
|
|
|
|
.range(2, 10)
|
2019-03-06 22:55:47 +09:00
|
|
|
.each(c => c.length > 0 && c.length < 50),
|
|
|
|
multiple: $.optional.bool,
|
|
|
|
expiresAt: $.optional.nullable.num.int(),
|
|
|
|
expiredAfter: $.optional.nullable.num.int().min(1)
|
2019-02-13 16:33:07 +09:00
|
|
|
}).strict(),
|
2018-07-05 23:36:07 +09:00
|
|
|
desc: {
|
2018-08-29 06:59:43 +09:00
|
|
|
'ja-JP': 'アンケート'
|
2018-07-06 02:58:29 +09:00
|
|
|
},
|
|
|
|
ref: 'poll'
|
2018-11-02 03:32:24 +09:00
|
|
|
}
|
2018-07-05 23:47:36 +09:00
|
|
|
},
|
2018-07-06 00:04:40 +09:00
|
|
|
|
2018-07-05 23:47:36 +09:00
|
|
|
res: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-02-24 09:45:27 +09:00
|
|
|
properties: {
|
2018-07-05 23:47:36 +09:00
|
|
|
createdNote: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
ref: 'Note',
|
2019-02-24 09:45:27 +09:00
|
|
|
description: '作成した投稿'
|
2018-07-05 23:47:36 +09:00
|
|
|
}
|
|
|
|
}
|
2019-02-22 11:46:58 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchRenoteTarget: {
|
|
|
|
message: 'No such renote target.',
|
|
|
|
code: 'NO_SUCH_RENOTE_TARGET',
|
|
|
|
id: 'b5c90186-4ab0-49c8-9bba-a1f76c282ba4'
|
|
|
|
},
|
|
|
|
|
|
|
|
cannotReRenote: {
|
|
|
|
message: 'You can not Renote a pure Renote.',
|
|
|
|
code: 'CANNOT_RENOTE_TO_A_PURE_RENOTE',
|
|
|
|
id: 'fd4cc33e-2a37-48dd-99cc-9b806eb2031a'
|
|
|
|
},
|
|
|
|
|
|
|
|
noSuchReplyTarget: {
|
|
|
|
message: 'No such reply target.',
|
|
|
|
code: 'NO_SUCH_REPLY_TARGET',
|
|
|
|
id: '749ee0f6-d3da-459a-bf02-282e2da4292c'
|
|
|
|
},
|
|
|
|
|
|
|
|
cannotReplyToPureRenote: {
|
|
|
|
message: 'You can not reply to a pure Renote.',
|
|
|
|
code: 'CANNOT_REPLY_TO_A_PURE_RENOTE',
|
|
|
|
id: '3ac74a84-8fd5-4bb0-870f-01804f82ce15'
|
|
|
|
},
|
|
|
|
|
|
|
|
contentRequired: {
|
|
|
|
message: 'Content required. You need to set text, fileIds, renoteId or poll.',
|
|
|
|
code: 'CONTENT_REQUIRED',
|
|
|
|
id: '6f57e42b-c348-439b-bc45-993995cc515a'
|
|
|
|
},
|
2019-03-06 22:55:47 +09:00
|
|
|
|
|
|
|
cannotCreateAlreadyExpiredPoll: {
|
|
|
|
message: 'Poll is already expired.',
|
|
|
|
code: 'CANNOT_CREATE_ALREADY_EXPIRED_POLL',
|
|
|
|
id: '04da457d-b083-4055-9082-955525eda5a5'
|
|
|
|
}
|
2018-07-05 22:35:35 +09:00
|
|
|
}
|
|
|
|
};
|
2018-04-08 02:30:37 +09:00
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
export default define(meta, async (ps, user, app) => {
|
2019-04-07 21:50:36 +09:00
|
|
|
let visibleUsers: User[] = [];
|
2018-11-02 03:32:24 +09:00
|
|
|
if (ps.visibleUserIds) {
|
2019-04-13 01:43:22 +09:00
|
|
|
visibleUsers = (await Promise.all(ps.visibleUserIds.map(id => Users.findOne(id))))
|
|
|
|
.filter(x => x != null) as User[];
|
2018-04-29 04:30:51 +09:00
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
let files: DriveFile[] = [];
|
2018-09-05 19:32:46 +09:00
|
|
|
const fileIds = ps.fileIds != null ? ps.fileIds : ps.mediaIds != null ? ps.mediaIds : null;
|
|
|
|
if (fileIds != null) {
|
2019-04-13 01:43:22 +09:00
|
|
|
files = (await Promise.all(fileIds.map(fileId =>
|
|
|
|
DriveFiles.findOne({
|
2019-04-07 21:50:36 +09:00
|
|
|
id: fileId,
|
|
|
|
userId: user.id
|
2019-04-13 01:43:22 +09:00
|
|
|
})
|
|
|
|
))).filter(file => file != null) as DriveFile[];
|
2018-04-08 02:30:37 +09:00
|
|
|
}
|
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
let renote: Note | undefined;
|
2018-11-02 03:32:24 +09:00
|
|
|
if (ps.renoteId != null) {
|
2018-04-08 02:30:37 +09:00
|
|
|
// Fetch renote to note
|
2019-04-07 21:50:36 +09:00
|
|
|
renote = await Notes.findOne(ps.renoteId);
|
2018-04-08 02:30:37 +09:00
|
|
|
|
|
|
|
if (renote == null) {
|
2019-02-22 11:46:58 +09:00
|
|
|
throw new ApiError(meta.errors.noSuchRenoteTarget);
|
2018-09-05 19:32:46 +09:00
|
|
|
} else if (renote.renoteId && !renote.text && !renote.fileIds) {
|
2019-02-22 11:46:58 +09:00
|
|
|
throw new ApiError(meta.errors.cannotReRenote);
|
2018-04-08 02:30:37 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
let reply: Note | undefined;
|
2018-11-02 03:32:24 +09:00
|
|
|
if (ps.replyId != null) {
|
2018-04-08 02:30:37 +09:00
|
|
|
// Fetch reply
|
2019-04-07 21:50:36 +09:00
|
|
|
reply = await Notes.findOne(ps.replyId);
|
2018-04-08 02:30:37 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
if (reply == null) {
|
2019-02-22 11:46:58 +09:00
|
|
|
throw new ApiError(meta.errors.noSuchReplyTarget);
|
2018-04-08 02:30:37 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// 返信対象が引用でないRenoteだったらエラー
|
2018-09-05 19:32:46 +09:00
|
|
|
if (reply.renoteId && !reply.text && !reply.fileIds) {
|
2019-02-22 11:46:58 +09:00
|
|
|
throw new ApiError(meta.errors.cannotReplyToPureRenote);
|
2018-04-08 02:30:37 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-05 23:36:07 +09:00
|
|
|
if (ps.poll) {
|
2019-03-06 22:55:47 +09:00
|
|
|
if (typeof ps.poll.expiresAt === 'number') {
|
|
|
|
if (ps.poll.expiresAt < Date.now())
|
|
|
|
throw new ApiError(meta.errors.cannotCreateAlreadyExpiredPoll);
|
|
|
|
} else if (typeof ps.poll.expiredAfter === 'number') {
|
|
|
|
ps.poll.expiresAt = Date.now() + ps.poll.expiredAfter;
|
|
|
|
}
|
2018-04-08 02:30:37 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// テキストが無いかつ添付ファイルが無いかつRenoteも無いかつ投票も無かったらエラー
|
2018-11-22 05:06:51 +09:00
|
|
|
if (!(ps.text || files.length || renote || ps.poll)) {
|
2019-02-22 11:46:58 +09:00
|
|
|
throw new ApiError(meta.errors.contentRequired);
|
2018-04-08 02:30:37 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// 投稿を作成
|
2019-02-22 11:46:58 +09:00
|
|
|
const note = await create(user, {
|
2018-04-08 02:30:37 +09:00
|
|
|
createdAt: new Date(),
|
2018-09-05 19:32:46 +09:00
|
|
|
files: files,
|
2019-03-06 22:55:47 +09:00
|
|
|
poll: ps.poll ? {
|
|
|
|
choices: ps.poll.choices,
|
|
|
|
multiple: ps.poll.multiple || false,
|
|
|
|
expiresAt: ps.poll.expiresAt ? new Date(ps.poll.expiresAt) : null
|
2019-04-13 01:43:22 +09:00
|
|
|
} : undefined,
|
|
|
|
text: ps.text || undefined,
|
2018-04-08 02:30:37 +09:00
|
|
|
reply,
|
|
|
|
renote,
|
2018-07-05 23:36:07 +09:00
|
|
|
cw: ps.cw,
|
2018-04-22 17:04:52 +09:00
|
|
|
app,
|
2018-07-05 23:36:07 +09:00
|
|
|
viaMobile: ps.viaMobile,
|
2018-11-16 05:47:29 +09:00
|
|
|
localOnly: ps.localOnly,
|
2018-07-05 23:36:07 +09:00
|
|
|
visibility: ps.visibility,
|
2018-04-29 04:30:51 +09:00
|
|
|
visibleUsers,
|
2019-04-13 01:43:22 +09:00
|
|
|
apMentions: ps.noExtractMentions ? [] : undefined,
|
|
|
|
apHashtags: ps.noExtractHashtags ? [] : undefined,
|
|
|
|
apEmojis: ps.noExtractEmojis ? [] : undefined,
|
2018-07-05 23:36:07 +09:00
|
|
|
geo: ps.geo
|
2018-04-08 02:30:37 +09:00
|
|
|
});
|
2019-02-22 11:46:58 +09:00
|
|
|
|
|
|
|
return {
|
2019-04-07 21:50:36 +09:00
|
|
|
createdNote: await Notes.pack(note, user)
|
2019-02-22 11:46:58 +09:00
|
|
|
};
|
|
|
|
});
|