2019-02-05 11:48:08 +09:00
|
|
|
import $ from 'cafy';
|
|
|
|
import ID, { transform, transformMany } from '../../../../misc/cafy-id';
|
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';
|
|
|
|
import Note, { INote, isValidCw, pack } from '../../../../models/note';
|
2018-11-02 13:47:44 +09:00
|
|
|
import User, { IUser } from '../../../../models/user';
|
2018-07-05 23:36:07 +09:00
|
|
|
import DriveFile, { IDriveFile } from '../../../../models/drive-file';
|
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';
|
2018-11-06 07:14:43 +09:00
|
|
|
import fetchMeta from '../../../../misc/fetch-meta';
|
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
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
kind: 'note-write',
|
|
|
|
|
2018-07-05 22:35:35 +09:00
|
|
|
params: {
|
2018-11-02 03:32:24 +09:00
|
|
|
visibility: {
|
|
|
|
validator: $.str.optional.or(['public', 'home', 'followers', 'specified', 'private']),
|
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: {
|
2018-12-23 03:44:18 +09:00
|
|
|
validator: $.arr($.type(ID)).optional.unique().min(0),
|
2018-11-02 03:32:24 +09:00
|
|
|
transform: transformMany,
|
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: {
|
2018-11-05 11:09:05 +09:00
|
|
|
validator: $.str.optional.nullable.pipe(text =>
|
|
|
|
length(text.trim()) <= maxNoteTextLength && text.trim() != ''
|
|
|
|
),
|
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: {
|
|
|
|
validator: $.str.optional.nullable.pipe(isValidCw),
|
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: {
|
|
|
|
validator: $.bool.optional,
|
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: {
|
|
|
|
validator: $.bool.optional,
|
|
|
|
default: false,
|
|
|
|
desc: {
|
|
|
|
'ja-JP': 'ローカルのみに投稿か否か。'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-12-23 03:25:33 +09:00
|
|
|
noExtractMentions: {
|
|
|
|
validator: $.bool.optional,
|
|
|
|
default: false,
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '本文からメンションを展開しないか否か。'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
noExtractHashtags: {
|
|
|
|
validator: $.bool.optional,
|
|
|
|
default: false,
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '本文からハッシュタグを展開しないか否か。'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
noExtractEmojis: {
|
|
|
|
validator: $.bool.optional,
|
|
|
|
default: false,
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '本文からカスタム絵文字を展開しないか否か。'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
geo: {
|
|
|
|
validator: $.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(),
|
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: {
|
|
|
|
validator: $.arr($.type(ID)).optional.unique().range(1, 4),
|
|
|
|
transform: transformMany,
|
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: {
|
|
|
|
validator: $.arr($.type(ID)).optional.unique().range(1, 4),
|
|
|
|
transform: transformMany,
|
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: {
|
|
|
|
validator: $.type(ID).optional,
|
|
|
|
transform: transform,
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '返信対象'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
renoteId: {
|
|
|
|
validator: $.type(ID).optional,
|
|
|
|
transform: transform,
|
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: {
|
|
|
|
validator: $.obj({
|
|
|
|
choices: $.arr($.str)
|
|
|
|
.unique()
|
|
|
|
.range(2, 10)
|
|
|
|
.each(c => c.length > 0 && c.length < 50)
|
|
|
|
}).optional.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: {
|
2018-07-06 00:04:40 +09:00
|
|
|
type: 'object',
|
2018-07-07 13:34:42 +09:00
|
|
|
props: {
|
2018-07-05 23:47:36 +09:00
|
|
|
createdNote: {
|
|
|
|
type: 'entity(Note)',
|
|
|
|
desc: {
|
2018-08-29 06:59:43 +09:00
|
|
|
'ja-JP': '作成した投稿'
|
2018-07-05 23:47:36 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-05 22:35:35 +09:00
|
|
|
}
|
|
|
|
};
|
2018-04-08 02:30:37 +09:00
|
|
|
|
2018-11-02 13:47:44 +09:00
|
|
|
export default define(meta, (ps, user, app) => new Promise(async (res, rej) => {
|
2018-06-18 09:54:53 +09:00
|
|
|
let visibleUsers: IUser[] = [];
|
2018-11-02 03:32:24 +09:00
|
|
|
if (ps.visibleUserIds) {
|
2018-07-05 22:35:35 +09:00
|
|
|
visibleUsers = await Promise.all(ps.visibleUserIds.map(id => User.findOne({
|
2018-04-29 04:30:51 +09:00
|
|
|
_id: id
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
|
2018-07-05 23:36:07 +09:00
|
|
|
let files: IDriveFile[] = [];
|
2018-09-05 19:32:46 +09:00
|
|
|
const fileIds = ps.fileIds != null ? ps.fileIds : ps.mediaIds != null ? ps.mediaIds : null;
|
|
|
|
if (fileIds != null) {
|
2018-09-10 01:51:46 +09:00
|
|
|
files = await Promise.all(fileIds.map(fileId => {
|
|
|
|
return DriveFile.findOne({
|
2018-09-05 19:32:46 +09:00
|
|
|
_id: fileId,
|
2018-04-08 02:30:37 +09:00
|
|
|
'metadata.userId': user._id
|
|
|
|
});
|
2018-09-10 01:51:46 +09:00
|
|
|
}));
|
2018-04-08 02:30:37 +09:00
|
|
|
|
2018-09-10 01:51:46 +09:00
|
|
|
files = files.filter(file => file != null);
|
2018-04-08 02:30:37 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
let renote: INote = null;
|
2018-11-02 03:32:24 +09:00
|
|
|
if (ps.renoteId != null) {
|
2018-04-08 02:30:37 +09:00
|
|
|
// Fetch renote to note
|
|
|
|
renote = await Note.findOne({
|
2018-07-05 23:36:07 +09:00
|
|
|
_id: ps.renoteId
|
2018-04-08 02:30:37 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
if (renote == null) {
|
|
|
|
return rej('renoteee is not found');
|
2018-09-05 19:32:46 +09:00
|
|
|
} else if (renote.renoteId && !renote.text && !renote.fileIds) {
|
2018-04-08 02:30:37 +09:00
|
|
|
return rej('cannot renote to renote');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let reply: INote = null;
|
2018-11-02 03:32:24 +09:00
|
|
|
if (ps.replyId != null) {
|
2018-04-08 02:30:37 +09:00
|
|
|
// Fetch reply
|
|
|
|
reply = await Note.findOne({
|
2018-11-02 03:32:24 +09:00
|
|
|
_id: ps.replyId
|
2018-04-08 02:30:37 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
if (reply === null) {
|
|
|
|
return rej('in reply to note is not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返信対象が引用でないRenoteだったらエラー
|
2018-09-05 19:32:46 +09:00
|
|
|
if (reply.renoteId && !reply.text && !reply.fileIds) {
|
2018-04-08 02:30:37 +09:00
|
|
|
return rej('cannot reply to renote');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-05 23:36:07 +09:00
|
|
|
if (ps.poll) {
|
|
|
|
(ps.poll as any).choices = (ps.poll as any).choices.map((choice: string, i: number) => ({
|
2018-04-08 02:30:37 +09:00
|
|
|
id: i, // IDを付与
|
|
|
|
text: choice.trim(),
|
|
|
|
votes: 0
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// テキストが無いかつ添付ファイルが無いかつRenoteも無いかつ投票も無かったらエラー
|
2018-11-22 05:06:51 +09:00
|
|
|
if (!(ps.text || files.length || renote || ps.poll)) {
|
2018-09-05 19:32:46 +09:00
|
|
|
return rej('text, fileIds, renoteId or poll is required');
|
2018-04-08 02:30:37 +09:00
|
|
|
}
|
|
|
|
|
2018-12-29 02:55:46 +09:00
|
|
|
// 後方互換性のため
|
|
|
|
if (ps.visibility == 'private') {
|
|
|
|
ps.visibility = 'specified';
|
|
|
|
}
|
|
|
|
|
2018-04-08 02:30:37 +09:00
|
|
|
// 投稿を作成
|
2018-11-13 19:34:09 +09:00
|
|
|
create(user, {
|
2018-04-08 02:30:37 +09:00
|
|
|
createdAt: new Date(),
|
2018-09-05 19:32:46 +09:00
|
|
|
files: files,
|
2018-07-05 23:36:07 +09:00
|
|
|
poll: ps.poll,
|
|
|
|
text: ps.text,
|
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,
|
2018-12-23 03:25:33 +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-11-13 19:34:09 +09:00
|
|
|
})
|
|
|
|
.then(note => pack(note, user))
|
|
|
|
.then(noteObj => {
|
|
|
|
res({
|
|
|
|
createdNote: noteObj
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
rej(e);
|
2018-04-08 02:30:37 +09:00
|
|
|
});
|
2018-11-02 13:47:44 +09:00
|
|
|
}));
|