2021-11-12 10:47:04 +00:00
|
|
|
import ms from 'ms';
|
2018-11-05 02:09:05 +00:00
|
|
|
import { length } from 'stringz';
|
2021-08-19 12:55:45 +00:00
|
|
|
import create from '@/services/note/create';
|
|
|
|
import define from '../../define';
|
|
|
|
import { fetchMeta } from '@/misc/fetch-meta';
|
|
|
|
import { ApiError } from '../../error';
|
|
|
|
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 02:09:05 +00:00
|
|
|
|
2019-10-25 15:16:47 +00:00
|
|
|
let maxNoteTextLength = 500;
|
2018-11-05 02:09:05 +00:00
|
|
|
|
|
|
|
setInterval(() => {
|
2018-11-05 22:14:43 +00:00
|
|
|
fetchMeta().then(m => {
|
|
|
|
maxNoteTextLength = m.maxNoteTextLength;
|
2018-11-05 02:09:05 +00:00
|
|
|
});
|
|
|
|
}, 3000);
|
2018-07-05 13:35:35 +00:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['notes'],
|
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: true,
|
2018-07-15 18:25:35 +00:00
|
|
|
|
|
|
|
limit: {
|
|
|
|
duration: ms('1hour'),
|
2021-12-09 14:58:30 +00:00
|
|
|
max: 300,
|
2018-07-15 18:25:35 +00:00
|
|
|
},
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
kind: 'write:notes',
|
2018-07-15 18:25:35 +00:00
|
|
|
|
2018-07-05 14:47:36 +00:00
|
|
|
res: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-02-24 00:45:27 +00:00
|
|
|
properties: {
|
2018-07-05 14:47:36 +00:00
|
|
|
createdNote: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 13:35:26 +00:00
|
|
|
ref: 'Note',
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
|
|
|
},
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchRenoteTarget: {
|
|
|
|
message: 'No such renote target.',
|
|
|
|
code: 'NO_SUCH_RENOTE_TARGET',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: 'b5c90186-4ab0-49c8-9bba-a1f76c282ba4',
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
cannotReRenote: {
|
|
|
|
message: 'You can not Renote a pure Renote.',
|
|
|
|
code: 'CANNOT_RENOTE_TO_A_PURE_RENOTE',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: 'fd4cc33e-2a37-48dd-99cc-9b806eb2031a',
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
noSuchReplyTarget: {
|
|
|
|
message: 'No such reply target.',
|
|
|
|
code: 'NO_SUCH_REPLY_TARGET',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: '749ee0f6-d3da-459a-bf02-282e2da4292c',
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
cannotReplyToPureRenote: {
|
|
|
|
message: 'You can not reply to a pure Renote.',
|
|
|
|
code: 'CANNOT_REPLY_TO_A_PURE_RENOTE',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: '3ac74a84-8fd5-4bb0-870f-01804f82ce15',
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
contentRequired: {
|
|
|
|
message: 'Content required. You need to set text, fileIds, renoteId or poll.',
|
|
|
|
code: 'CONTENT_REQUIRED',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: '6f57e42b-c348-439b-bc45-993995cc515a',
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
2019-03-06 13:55:47 +00:00
|
|
|
|
|
|
|
cannotCreateAlreadyExpiredPoll: {
|
|
|
|
message: 'Poll is already expired.',
|
|
|
|
code: 'CANNOT_CREATE_ALREADY_EXPIRED_POLL',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: '04da457d-b083-4055-9082-955525eda5a5',
|
2020-08-18 13:44:21 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
noSuchChannel: {
|
|
|
|
message: 'No such channel.',
|
|
|
|
code: 'NO_SUCH_CHANNEL',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: 'b1653923-5453-4edc-b786-7c4f39bb0bbb',
|
2020-08-18 13:44:21 +00:00
|
|
|
},
|
2021-08-17 12:48:59 +00:00
|
|
|
|
|
|
|
youHaveBeenBlocked: {
|
|
|
|
message: 'You have been blocked by this user.',
|
|
|
|
code: 'YOU_HAVE_BEEN_BLOCKED',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: 'b390d7e1-8a5e-46ed-b625-06271cafd3d3',
|
2021-08-17 12:48:59 +00:00
|
|
|
},
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2018-04-07 17:30:37 +00:00
|
|
|
|
2022-02-19 05:05:32 +00:00
|
|
|
const paramDef = {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
visibility: { type: 'string', enum: ['public', 'home', 'followers', 'specified'], default: "public" },
|
|
|
|
visibleUserIds: { type: 'array', uniqueItems: true, items: {
|
|
|
|
type: 'string', format: 'misskey:id',
|
|
|
|
} },
|
|
|
|
text: { type: 'string', nullable: true, maxLength: 3000, default: null },
|
|
|
|
cw: { type: 'string', nullable: true, maxLength: 100 },
|
|
|
|
localOnly: { type: 'boolean', default: false },
|
|
|
|
noExtractMentions: { type: 'boolean', default: false },
|
|
|
|
noExtractHashtags: { type: 'boolean', default: false },
|
|
|
|
noExtractEmojis: { type: 'boolean', default: false },
|
|
|
|
fileIds: { type: 'array', uniqueItems: true, minItems: 1, maxItems: 16, items: {
|
|
|
|
type: 'string', format: 'misskey:id',
|
|
|
|
} },
|
|
|
|
mediaIds: { type: 'array', uniqueItems: true, minItems: 1, maxItems: 16, items: {
|
|
|
|
type: 'string', format: 'misskey:id',
|
|
|
|
} },
|
|
|
|
replyId: { type: 'string', format: 'misskey:id', nullable: true },
|
|
|
|
renoteId: { type: 'string', format: 'misskey:id', nullable: true },
|
|
|
|
channelId: { type: 'string', format: 'misskey:id', nullable: true },
|
|
|
|
poll: {
|
|
|
|
type: 'object', nullable: true,
|
|
|
|
properties: {
|
|
|
|
choices: {
|
|
|
|
type: 'array', uniqueItems: true, minItems: 2, maxItems: 10,
|
|
|
|
items: {
|
|
|
|
type: 'string', minLength: 1, maxLength: 50,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
multiple: { type: 'boolean', default: false },
|
|
|
|
expiresAt: { type: 'integer', nullable: true },
|
|
|
|
expiredAfter: { type: 'integer', nullable: true, minimum: 1 },
|
|
|
|
},
|
|
|
|
required: ['choices'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
required: [],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 05:05:32 +00:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2019-04-07 12:50:36 +00:00
|
|
|
let visibleUsers: User[] = [];
|
2018-11-01 18:32:24 +00:00
|
|
|
if (ps.visibleUserIds) {
|
2019-04-12 16:43:22 +00:00
|
|
|
visibleUsers = (await Promise.all(ps.visibleUserIds.map(id => Users.findOne(id))))
|
|
|
|
.filter(x => x != null) as User[];
|
2018-04-28 19:30:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
let files: DriveFile[] = [];
|
2018-09-05 10:32:46 +00:00
|
|
|
const fileIds = ps.fileIds != null ? ps.fileIds : ps.mediaIds != null ? ps.mediaIds : null;
|
|
|
|
if (fileIds != null) {
|
2019-04-12 16:43:22 +00:00
|
|
|
files = (await Promise.all(fileIds.map(fileId =>
|
|
|
|
DriveFiles.findOne({
|
2019-04-07 12:50:36 +00:00
|
|
|
id: fileId,
|
2021-12-09 14:58:30 +00:00
|
|
|
userId: user.id,
|
2019-04-12 16:43:22 +00:00
|
|
|
})
|
|
|
|
))).filter(file => file != null) as DriveFile[];
|
2018-04-07 17:30:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
let renote: Note | undefined;
|
2018-11-01 18:32:24 +00:00
|
|
|
if (ps.renoteId != null) {
|
2018-04-07 17:30:37 +00:00
|
|
|
// Fetch renote to note
|
2019-04-07 12:50:36 +00:00
|
|
|
renote = await Notes.findOne(ps.renoteId);
|
2018-04-07 17:30:37 +00:00
|
|
|
|
|
|
|
if (renote == null) {
|
2019-02-22 02:46:58 +00:00
|
|
|
throw new ApiError(meta.errors.noSuchRenoteTarget);
|
2018-09-05 10:32:46 +00:00
|
|
|
} else if (renote.renoteId && !renote.text && !renote.fileIds) {
|
2019-02-22 02:46:58 +00:00
|
|
|
throw new ApiError(meta.errors.cannotReRenote);
|
2018-04-07 17:30:37 +00:00
|
|
|
}
|
2021-08-17 12:48:59 +00: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 17:30:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
let reply: Note | undefined;
|
2018-11-01 18:32:24 +00:00
|
|
|
if (ps.replyId != null) {
|
2018-04-07 17:30:37 +00:00
|
|
|
// Fetch reply
|
2019-04-07 12:50:36 +00:00
|
|
|
reply = await Notes.findOne(ps.replyId);
|
2018-04-07 17:30:37 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (reply == null) {
|
2019-02-22 02:46:58 +00:00
|
|
|
throw new ApiError(meta.errors.noSuchReplyTarget);
|
2018-04-07 17:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 返信対象が引用でないRenoteだったらエラー
|
2018-09-05 10:32:46 +00:00
|
|
|
if (reply.renoteId && !reply.text && !reply.fileIds) {
|
2019-02-22 02:46:58 +00:00
|
|
|
throw new ApiError(meta.errors.cannotReplyToPureRenote);
|
2018-04-07 17:30:37 +00:00
|
|
|
}
|
2021-08-17 12:48:59 +00: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 17:30:37 +00:00
|
|
|
}
|
|
|
|
|
2018-07-05 14:36:07 +00:00
|
|
|
if (ps.poll) {
|
2019-03-06 13:55:47 +00:00
|
|
|
if (typeof ps.poll.expiresAt === 'number') {
|
2021-11-13 10:10:14 +00:00
|
|
|
if (ps.poll.expiresAt < Date.now()) {
|
2019-03-06 13:55:47 +00:00
|
|
|
throw new ApiError(meta.errors.cannotCreateAlreadyExpiredPoll);
|
2021-11-13 10:10:14 +00:00
|
|
|
}
|
2019-03-06 13:55:47 +00:00
|
|
|
} else if (typeof ps.poll.expiredAfter === 'number') {
|
|
|
|
ps.poll.expiresAt = Date.now() + ps.poll.expiredAfter;
|
|
|
|
}
|
2018-04-07 17:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// テキストが無いかつ添付ファイルが無いかつRenoteも無いかつ投票も無かったらエラー
|
2018-11-21 20:06:51 +00:00
|
|
|
if (!(ps.text || files.length || renote || ps.poll)) {
|
2019-02-22 02:46:58 +00:00
|
|
|
throw new ApiError(meta.errors.contentRequired);
|
2018-04-07 17:30:37 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 13:44:21 +00: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 17:30:37 +00:00
|
|
|
// 投稿を作成
|
2019-02-22 02:46:58 +00:00
|
|
|
const note = await create(user, {
|
2018-04-07 17:30:37 +00:00
|
|
|
createdAt: new Date(),
|
2018-09-05 10:32:46 +00:00
|
|
|
files: files,
|
2019-03-06 13:55:47 +00:00
|
|
|
poll: ps.poll ? {
|
|
|
|
choices: ps.poll.choices,
|
|
|
|
multiple: ps.poll.multiple || false,
|
2021-12-09 14:58:30 +00:00
|
|
|
expiresAt: ps.poll.expiresAt ? new Date(ps.poll.expiresAt) : null,
|
2019-04-12 16:43:22 +00:00
|
|
|
} : undefined,
|
|
|
|
text: ps.text || undefined,
|
2018-04-07 17:30:37 +00:00
|
|
|
reply,
|
|
|
|
renote,
|
2018-07-05 14:36:07 +00:00
|
|
|
cw: ps.cw,
|
2018-11-15 20:47:29 +00:00
|
|
|
localOnly: ps.localOnly,
|
2018-07-05 14:36:07 +00:00
|
|
|
visibility: ps.visibility,
|
2018-04-28 19:30:51 +00:00
|
|
|
visibleUsers,
|
2020-08-18 13:44:21 +00:00
|
|
|
channel,
|
2019-04-12 16:43:22 +00:00
|
|
|
apMentions: ps.noExtractMentions ? [] : undefined,
|
|
|
|
apHashtags: ps.noExtractHashtags ? [] : undefined,
|
|
|
|
apEmojis: ps.noExtractEmojis ? [] : undefined,
|
2018-04-07 17:30:37 +00:00
|
|
|
});
|
2019-02-22 02:46:58 +00:00
|
|
|
|
|
|
|
return {
|
2021-12-09 14:58:30 +00:00
|
|
|
createdNote: await Notes.pack(note, user),
|
2019-02-22 02:46:58 +00:00
|
|
|
};
|
|
|
|
});
|