2019-02-05 04:48:08 +02:00
|
|
|
import $ from 'cafy';
|
2019-02-01 17:16:27 +02:00
|
|
|
import * as ms from 'ms';
|
2018-11-05 04:09:05 +02:00
|
|
|
import { length } from 'stringz';
|
2021-08-19 15:55:45 +03:00
|
|
|
import create from '@/services/note/create';
|
|
|
|
import define from '../../define';
|
|
|
|
import { fetchMeta } from '@/misc/fetch-meta';
|
|
|
|
import { ApiError } from '../../error';
|
|
|
|
import { ID } from '@/misc/cafy-id';
|
|
|
|
import { User } from '@/models/entities/user';
|
|
|
|
import { Users, DriveFiles, Notes, Channels, Blockings } from '@/models/index';
|
|
|
|
import { DriveFile } from '@/models/entities/drive-file';
|
|
|
|
import { Note } from '@/models/entities/note';
|
|
|
|
import { DB_MAX_NOTE_TEXT_LENGTH } from '@/misc/hard-limits';
|
|
|
|
import { noteVisibilities } from '../../../../types';
|
|
|
|
import { Channel } from '@/models/entities/channel';
|
2018-11-05 04:09:05 +02:00
|
|
|
|
2019-10-25 18:16:47 +03:00
|
|
|
let maxNoteTextLength = 500;
|
2018-11-05 04:09:05 +02:00
|
|
|
|
|
|
|
setInterval(() => {
|
2018-11-06 00:14:43 +02:00
|
|
|
fetchMeta().then(m => {
|
|
|
|
maxNoteTextLength = m.maxNoteTextLength;
|
2018-11-05 04:09:05 +02:00
|
|
|
});
|
|
|
|
}, 3000);
|
2018-07-05 16:35:35 +03:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 04:20:58 +02:00
|
|
|
tags: ['notes'],
|
|
|
|
|
2020-02-15 14:33:32 +02:00
|
|
|
requireCredential: true as const,
|
2018-07-15 21:25:35 +03:00
|
|
|
|
|
|
|
limit: {
|
|
|
|
duration: ms('1hour'),
|
2018-08-17 22:21:49 +03:00
|
|
|
max: 300
|
2018-07-15 21:25:35 +03:00
|
|
|
},
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
kind: 'write:notes',
|
2018-07-15 21:25:35 +03:00
|
|
|
|
2018-07-05 16:35:35 +03:00
|
|
|
params: {
|
2018-11-01 20:32:24 +02:00
|
|
|
visibility: {
|
2020-05-26 08:33:55 +03:00
|
|
|
validator: $.optional.str.or(noteVisibilities as unknown as string[]),
|
2018-07-05 16:35:35 +03:00
|
|
|
default: 'public',
|
2018-11-01 20:32:24 +02:00
|
|
|
},
|
2018-07-05 17:36:07 +03:00
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
visibleUserIds: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.arr($.type(ID)).unique().min(0),
|
2018-11-01 20:32:24 +02:00
|
|
|
},
|
2018-07-05 17:36:07 +03:00
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
text: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.nullable.str.pipe(text =>
|
2019-09-30 19:46:31 +03:00
|
|
|
text.trim() != ''
|
|
|
|
&& length(text.trim()) <= maxNoteTextLength
|
|
|
|
&& Array.from(text.trim()).length <= DB_MAX_NOTE_TEXT_LENGTH // DB limit
|
2018-11-05 04:09:05 +02:00
|
|
|
),
|
2021-06-08 08:14:58 +03:00
|
|
|
default: null,
|
2018-11-01 20:32:24 +02:00
|
|
|
},
|
2018-07-05 17:36:07 +03:00
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
cw: {
|
2019-04-07 15:50:36 +03:00
|
|
|
validator: $.optional.nullable.str.pipe(Notes.validateCw),
|
2018-11-01 20:32:24 +02:00
|
|
|
},
|
2018-07-05 17:36:07 +03:00
|
|
|
|
2018-11-15 22:47:29 +02:00
|
|
|
localOnly: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.bool,
|
2018-11-15 22:47:29 +02:00
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
|
2018-12-22 20:25:33 +02:00
|
|
|
noExtractMentions: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.bool,
|
2018-12-22 20:25:33 +02:00
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
noExtractHashtags: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.bool,
|
2018-12-22 20:25:33 +02:00
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
noExtractEmojis: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.bool,
|
2018-12-22 20:25:33 +02:00
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
fileIds: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.arr($.type(ID)).unique().range(1, 4),
|
2018-11-01 20:32:24 +02:00
|
|
|
},
|
2018-09-05 13:32:46 +03:00
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
mediaIds: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.arr($.type(ID)).unique().range(1, 4),
|
2019-02-24 21:18:09 +02:00
|
|
|
deprecated: true,
|
2018-11-01 20:32:24 +02:00
|
|
|
},
|
2018-07-05 17:36:07 +03:00
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
replyId: {
|
2020-08-18 16:44:21 +03:00
|
|
|
validator: $.optional.nullable.type(ID),
|
2018-11-01 20:32:24 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
renoteId: {
|
2020-08-18 16:44:21 +03:00
|
|
|
validator: $.optional.nullable.type(ID),
|
2018-11-01 20:32:24 +02:00
|
|
|
},
|
|
|
|
|
2020-08-18 16:44:21 +03:00
|
|
|
channelId: {
|
|
|
|
validator: $.optional.nullable.type(ID),
|
|
|
|
},
|
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
poll: {
|
2020-10-17 14:12:00 +03:00
|
|
|
validator: $.optional.nullable.obj({
|
2018-11-01 20:32:24 +02:00
|
|
|
choices: $.arr($.str)
|
|
|
|
.unique()
|
|
|
|
.range(2, 10)
|
2019-03-06 15:55:47 +02: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 09:33:07 +02:00
|
|
|
}).strict(),
|
2018-07-05 20:58:29 +03:00
|
|
|
ref: 'poll'
|
2018-11-01 20:32:24 +02:00
|
|
|
}
|
2018-07-05 17:47:36 +03:00
|
|
|
},
|
2018-07-05 18:04:40 +03:00
|
|
|
|
2018-07-05 17:47:36 +03:00
|
|
|
res: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-02-24 02:45:27 +02:00
|
|
|
properties: {
|
2018-07-05 17:47:36 +03:00
|
|
|
createdNote: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
ref: 'Note',
|
2018-07-05 17:47:36 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-22 04:46:58 +02: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 15:55:47 +02:00
|
|
|
|
|
|
|
cannotCreateAlreadyExpiredPoll: {
|
|
|
|
message: 'Poll is already expired.',
|
|
|
|
code: 'CANNOT_CREATE_ALREADY_EXPIRED_POLL',
|
|
|
|
id: '04da457d-b083-4055-9082-955525eda5a5'
|
2020-08-18 16:44:21 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
noSuchChannel: {
|
|
|
|
message: 'No such channel.',
|
|
|
|
code: 'NO_SUCH_CHANNEL',
|
|
|
|
id: 'b1653923-5453-4edc-b786-7c4f39bb0bbb'
|
|
|
|
},
|
2021-08-17 15:48:59 +03:00
|
|
|
|
|
|
|
youHaveBeenBlocked: {
|
|
|
|
message: 'You have been blocked by this user.',
|
|
|
|
code: 'YOU_HAVE_BEEN_BLOCKED',
|
|
|
|
id: 'b390d7e1-8a5e-46ed-b625-06271cafd3d3'
|
|
|
|
},
|
2018-07-05 16:35:35 +03:00
|
|
|
}
|
|
|
|
};
|
2018-04-07 20:30:37 +03:00
|
|
|
|
2020-03-28 04:24:37 +02:00
|
|
|
export default define(meta, async (ps, user) => {
|
2019-04-07 15:50:36 +03:00
|
|
|
let visibleUsers: User[] = [];
|
2018-11-01 20:32:24 +02:00
|
|
|
if (ps.visibleUserIds) {
|
2019-04-12 19:43:22 +03:00
|
|
|
visibleUsers = (await Promise.all(ps.visibleUserIds.map(id => Users.findOne(id))))
|
|
|
|
.filter(x => x != null) as User[];
|
2018-04-28 22:30:51 +03:00
|
|
|
}
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
let files: DriveFile[] = [];
|
2018-09-05 13:32:46 +03:00
|
|
|
const fileIds = ps.fileIds != null ? ps.fileIds : ps.mediaIds != null ? ps.mediaIds : null;
|
|
|
|
if (fileIds != null) {
|
2019-04-12 19:43:22 +03:00
|
|
|
files = (await Promise.all(fileIds.map(fileId =>
|
|
|
|
DriveFiles.findOne({
|
2019-04-07 15:50:36 +03:00
|
|
|
id: fileId,
|
|
|
|
userId: user.id
|
2019-04-12 19:43:22 +03:00
|
|
|
})
|
|
|
|
))).filter(file => file != null) as DriveFile[];
|
2018-04-07 20:30:37 +03:00
|
|
|
}
|
|
|
|
|
2019-04-12 19:43:22 +03:00
|
|
|
let renote: Note | undefined;
|
2018-11-01 20:32:24 +02:00
|
|
|
if (ps.renoteId != null) {
|
2018-04-07 20:30:37 +03:00
|
|
|
// Fetch renote to note
|
2019-04-07 15:50:36 +03:00
|
|
|
renote = await Notes.findOne(ps.renoteId);
|
2018-04-07 20:30:37 +03:00
|
|
|
|
|
|
|
if (renote == null) {
|
2019-02-22 04:46:58 +02:00
|
|
|
throw new ApiError(meta.errors.noSuchRenoteTarget);
|
2018-09-05 13:32:46 +03:00
|
|
|
} else if (renote.renoteId && !renote.text && !renote.fileIds) {
|
2019-02-22 04:46:58 +02:00
|
|
|
throw new ApiError(meta.errors.cannotReRenote);
|
2018-04-07 20:30:37 +03:00
|
|
|
}
|
2021-08-17 15:48:59 +03:00
|
|
|
|
|
|
|
// Check blocking
|
|
|
|
if (renote.userId !== user.id) {
|
|
|
|
const block = await Blockings.findOne({
|
|
|
|
blockerId: renote.userId,
|
|
|
|
blockeeId: user.id,
|
|
|
|
});
|
|
|
|
if (block) {
|
|
|
|
throw new ApiError(meta.errors.youHaveBeenBlocked);
|
|
|
|
}
|
|
|
|
}
|
2018-04-07 20:30:37 +03:00
|
|
|
}
|
|
|
|
|
2019-04-12 19:43:22 +03:00
|
|
|
let reply: Note | undefined;
|
2018-11-01 20:32:24 +02:00
|
|
|
if (ps.replyId != null) {
|
2018-04-07 20:30:37 +03:00
|
|
|
// Fetch reply
|
2019-04-07 15:50:36 +03:00
|
|
|
reply = await Notes.findOne(ps.replyId);
|
2018-04-07 20:30:37 +03:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
if (reply == null) {
|
2019-02-22 04:46:58 +02:00
|
|
|
throw new ApiError(meta.errors.noSuchReplyTarget);
|
2018-04-07 20:30:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 返信対象が引用でないRenoteだったらエラー
|
2018-09-05 13:32:46 +03:00
|
|
|
if (reply.renoteId && !reply.text && !reply.fileIds) {
|
2019-02-22 04:46:58 +02:00
|
|
|
throw new ApiError(meta.errors.cannotReplyToPureRenote);
|
2018-04-07 20:30:37 +03:00
|
|
|
}
|
2021-08-17 15:48:59 +03:00
|
|
|
|
|
|
|
// Check blocking
|
|
|
|
if (reply.userId !== user.id) {
|
|
|
|
const block = await Blockings.findOne({
|
|
|
|
blockerId: reply.userId,
|
|
|
|
blockeeId: user.id,
|
|
|
|
});
|
|
|
|
if (block) {
|
|
|
|
throw new ApiError(meta.errors.youHaveBeenBlocked);
|
|
|
|
}
|
|
|
|
}
|
2018-04-07 20:30:37 +03:00
|
|
|
}
|
|
|
|
|
2018-07-05 17:36:07 +03:00
|
|
|
if (ps.poll) {
|
2019-03-06 15:55:47 +02: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-07 20:30:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// テキストが無いかつ添付ファイルが無いかつRenoteも無いかつ投票も無かったらエラー
|
2018-11-21 22:06:51 +02:00
|
|
|
if (!(ps.text || files.length || renote || ps.poll)) {
|
2019-02-22 04:46:58 +02:00
|
|
|
throw new ApiError(meta.errors.contentRequired);
|
2018-04-07 20:30:37 +03:00
|
|
|
}
|
|
|
|
|
2020-08-18 16:44:21 +03:00
|
|
|
let channel: Channel | undefined;
|
|
|
|
if (ps.channelId != null) {
|
|
|
|
channel = await Channels.findOne(ps.channelId);
|
|
|
|
|
|
|
|
if (channel == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchChannel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 20:30:37 +03:00
|
|
|
// 投稿を作成
|
2019-02-22 04:46:58 +02:00
|
|
|
const note = await create(user, {
|
2018-04-07 20:30:37 +03:00
|
|
|
createdAt: new Date(),
|
2018-09-05 13:32:46 +03:00
|
|
|
files: files,
|
2019-03-06 15:55:47 +02: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-12 19:43:22 +03:00
|
|
|
} : undefined,
|
|
|
|
text: ps.text || undefined,
|
2018-04-07 20:30:37 +03:00
|
|
|
reply,
|
|
|
|
renote,
|
2018-07-05 17:36:07 +03:00
|
|
|
cw: ps.cw,
|
2018-11-15 22:47:29 +02:00
|
|
|
localOnly: ps.localOnly,
|
2018-07-05 17:36:07 +03:00
|
|
|
visibility: ps.visibility,
|
2018-04-28 22:30:51 +03:00
|
|
|
visibleUsers,
|
2020-08-18 16:44:21 +03:00
|
|
|
channel,
|
2019-04-12 19:43:22 +03:00
|
|
|
apMentions: ps.noExtractMentions ? [] : undefined,
|
|
|
|
apHashtags: ps.noExtractHashtags ? [] : undefined,
|
|
|
|
apEmojis: ps.noExtractEmojis ? [] : undefined,
|
2018-04-07 20:30:37 +03:00
|
|
|
});
|
2019-02-22 04:46:58 +02:00
|
|
|
|
|
|
|
return {
|
2019-04-07 15:50:36 +03:00
|
|
|
createdNote: await Notes.pack(note, user)
|
2019-02-22 04:46:58 +02:00
|
|
|
};
|
|
|
|
});
|