2022-04-17 13:51:59 +02:00
|
|
|
import { In } from 'typeorm';
|
2022-02-27 03:07:39 +01:00
|
|
|
import create from '@/services/note/create.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
import { Users, DriveFiles, Notes, Channels, Blockings } from '@/models/index.js';
|
|
|
|
import { DriveFile } from '@/models/entities/drive-file.js';
|
|
|
|
import { Note } from '@/models/entities/note.js';
|
|
|
|
import { Channel } from '@/models/entities/channel.js';
|
|
|
|
import { MAX_NOTE_TEXT_LENGTH } from '@/const.js';
|
2022-04-17 13:51:59 +02:00
|
|
|
import { noteVisibilities } from '../../../../types.js';
|
|
|
|
import { ApiError } from '../../error.js';
|
|
|
|
import define from '../../define.js';
|
2022-07-14 00:01:23 +02:00
|
|
|
import { HOUR } from '@/const.js';
|
2022-03-08 21:17:58 +01:00
|
|
|
import { getNote } from '../../common/getters.js';
|
2018-07-05 15:35:35 +02:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['notes'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: true,
|
2018-07-15 20:25:35 +02:00
|
|
|
|
|
|
|
limit: {
|
2022-07-14 00:01:23 +02:00
|
|
|
duration: HOUR,
|
2021-12-09 15:58:30 +01:00
|
|
|
max: 300,
|
2018-07-15 20:25:35 +02:00
|
|
|
},
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
kind: 'write:notes',
|
2018-07-15 20:25:35 +02:00
|
|
|
|
2018-07-05 16:47:36 +02:00
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-02-24 01:45:27 +01:00
|
|
|
properties: {
|
2018-07-05 16:47:36 +02:00
|
|
|
createdNote: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 15:35:26 +02:00
|
|
|
ref: 'Note',
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
|
|
|
},
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchRenoteTarget: {
|
|
|
|
message: 'No such renote target.',
|
|
|
|
code: 'NO_SUCH_RENOTE_TARGET',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: 'b5c90186-4ab0-49c8-9bba-a1f76c282ba4',
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
cannotReRenote: {
|
|
|
|
message: 'You can not Renote a pure Renote.',
|
|
|
|
code: 'CANNOT_RENOTE_TO_A_PURE_RENOTE',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: 'fd4cc33e-2a37-48dd-99cc-9b806eb2031a',
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
noSuchReplyTarget: {
|
|
|
|
message: 'No such reply target.',
|
|
|
|
code: 'NO_SUCH_REPLY_TARGET',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: '749ee0f6-d3da-459a-bf02-282e2da4292c',
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
cannotReplyToPureRenote: {
|
|
|
|
message: 'You can not reply to a pure Renote.',
|
|
|
|
code: 'CANNOT_REPLY_TO_A_PURE_RENOTE',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: '3ac74a84-8fd5-4bb0-870f-01804f82ce15',
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
2019-03-06 14:55:47 +01:00
|
|
|
cannotCreateAlreadyExpiredPoll: {
|
|
|
|
message: 'Poll is already expired.',
|
|
|
|
code: 'CANNOT_CREATE_ALREADY_EXPIRED_POLL',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: '04da457d-b083-4055-9082-955525eda5a5',
|
2020-08-18 15:44:21 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
noSuchChannel: {
|
|
|
|
message: 'No such channel.',
|
|
|
|
code: 'NO_SUCH_CHANNEL',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: 'b1653923-5453-4edc-b786-7c4f39bb0bbb',
|
2020-08-18 15:44:21 +02:00
|
|
|
},
|
2021-08-17 14:48:59 +02:00
|
|
|
|
|
|
|
youHaveBeenBlocked: {
|
|
|
|
message: 'You have been blocked by this user.',
|
|
|
|
code: 'YOU_HAVE_BEEN_BLOCKED',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: 'b390d7e1-8a5e-46ed-b625-06271cafd3d3',
|
2021-08-17 14:48:59 +02:00
|
|
|
},
|
2022-12-12 02:17:07 +01:00
|
|
|
|
|
|
|
accountLocked: {
|
|
|
|
message: 'You migrated. Your account is now locked.',
|
|
|
|
code: 'ACCOUNT_LOCKED',
|
|
|
|
id: 'd390d7e1-8a5e-46ed-b625-06271cafd3d3',
|
|
|
|
},
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2018-04-07 19:30:37 +02:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
2022-07-25 23:17:58 +02:00
|
|
|
visibility: { type: 'string', enum: noteVisibilities, default: 'public' },
|
2022-02-19 06:05:32 +01:00
|
|
|
visibleUserIds: { type: 'array', uniqueItems: true, items: {
|
|
|
|
type: 'string', format: 'misskey:id',
|
|
|
|
} },
|
2022-04-03 06:57:26 +02:00
|
|
|
text: { type: 'string', maxLength: MAX_NOTE_TEXT_LENGTH, nullable: true },
|
2022-02-19 06:05:32 +01:00
|
|
|
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 },
|
2022-04-03 06:57:26 +02:00
|
|
|
fileIds: {
|
|
|
|
type: 'array',
|
|
|
|
uniqueItems: true,
|
|
|
|
minItems: 1,
|
|
|
|
maxItems: 16,
|
|
|
|
items: { type: 'string', format: 'misskey:id' },
|
|
|
|
},
|
|
|
|
mediaIds: {
|
|
|
|
deprecated: true,
|
|
|
|
description: 'Use `fileIds` instead. If both are specified, this property is discarded.',
|
|
|
|
type: 'array',
|
|
|
|
uniqueItems: true,
|
|
|
|
minItems: 1,
|
|
|
|
maxItems: 16,
|
|
|
|
items: { type: 'string', format: 'misskey:id' },
|
|
|
|
},
|
2022-02-19 06:05:32 +01:00
|
|
|
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: {
|
2022-04-03 06:57:26 +02:00
|
|
|
type: 'object',
|
|
|
|
nullable: true,
|
2022-02-19 06:05:32 +01:00
|
|
|
properties: {
|
|
|
|
choices: {
|
2022-04-03 06:57:26 +02:00
|
|
|
type: 'array',
|
|
|
|
uniqueItems: true,
|
|
|
|
minItems: 2,
|
|
|
|
maxItems: 10,
|
|
|
|
items: { type: 'string', minLength: 1, maxLength: 50 },
|
2022-02-19 06:05:32 +01:00
|
|
|
},
|
|
|
|
multiple: { type: 'boolean', default: false },
|
|
|
|
expiresAt: { type: 'integer', nullable: true },
|
|
|
|
expiredAfter: { type: 'integer', nullable: true, minimum: 1 },
|
|
|
|
},
|
|
|
|
required: ['choices'],
|
|
|
|
},
|
|
|
|
},
|
2022-04-03 06:57:26 +02:00
|
|
|
anyOf: [
|
|
|
|
{
|
|
|
|
// (re)note with text, files and poll are optional
|
|
|
|
properties: {
|
2022-05-28 17:26:17 +02:00
|
|
|
text: { type: 'string', minLength: 1, maxLength: MAX_NOTE_TEXT_LENGTH, nullable: false },
|
2022-04-03 06:57:26 +02:00
|
|
|
},
|
|
|
|
required: ['text'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// (re)note with files, text and poll are optional
|
|
|
|
required: ['fileIds'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// (re)note with files, text and poll are optional
|
|
|
|
required: ['mediaIds'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// (re)note with poll, text and files are optional
|
|
|
|
properties: {
|
2022-04-17 13:51:59 +02:00
|
|
|
poll: { type: 'object', nullable: false },
|
2022-04-03 06:57:26 +02:00
|
|
|
},
|
|
|
|
required: ['poll'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// pure renote
|
|
|
|
required: ['renoteId'],
|
|
|
|
},
|
|
|
|
],
|
2022-02-19 06:05:32 +01:00
|
|
|
} as const;
|
|
|
|
|
2022-01-02 18:12:50 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 06:05:32 +01:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2022-12-16 01:06:12 +01:00
|
|
|
if(user.movedToUri != null) throw new ApiError(meta.errors.accountLocked);
|
2019-04-07 14:50:36 +02:00
|
|
|
let visibleUsers: User[] = [];
|
2018-11-01 19:32:24 +01:00
|
|
|
if (ps.visibleUserIds) {
|
2022-04-08 12:01:38 +02:00
|
|
|
visibleUsers = await Users.findBy({
|
|
|
|
id: In(ps.visibleUserIds),
|
|
|
|
});
|
2018-04-28 21:30:51 +02:00
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
let files: DriveFile[] = [];
|
2018-09-05 12:32:46 +02:00
|
|
|
const fileIds = ps.fileIds != null ? ps.fileIds : ps.mediaIds != null ? ps.mediaIds : null;
|
|
|
|
if (fileIds != null) {
|
2022-05-14 08:09:10 +02:00
|
|
|
files = await DriveFiles.createQueryBuilder('file')
|
|
|
|
.where('file.userId = :userId AND file.id IN (:...fileIds)', {
|
|
|
|
userId: user.id,
|
|
|
|
fileIds,
|
|
|
|
})
|
2022-05-15 11:32:00 +02:00
|
|
|
.orderBy('array_position(ARRAY[:...fileIds], "id"::text)')
|
2022-05-14 08:09:10 +02:00
|
|
|
.setParameters({ fileIds })
|
|
|
|
.getMany();
|
2018-04-07 19:30:37 +02:00
|
|
|
}
|
|
|
|
|
2022-04-17 13:51:59 +02:00
|
|
|
let renote: Note | null = null;
|
2018-11-01 19:32:24 +01:00
|
|
|
if (ps.renoteId != null) {
|
2018-04-07 19:30:37 +02:00
|
|
|
// Fetch renote to note
|
2022-07-25 18:15:21 +02:00
|
|
|
renote = await getNote(ps.renoteId, user).catch(e => {
|
|
|
|
if (e.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchRenoteTarget);
|
|
|
|
throw e;
|
|
|
|
});
|
2018-04-07 19:30:37 +02:00
|
|
|
|
2022-07-25 18:15:21 +02:00
|
|
|
if (renote.renoteId && !renote.text && !renote.fileIds && !renote.hasPoll) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.cannotReRenote);
|
2018-04-07 19:30:37 +02:00
|
|
|
}
|
2021-08-17 14:48:59 +02:00
|
|
|
|
|
|
|
// Check blocking
|
|
|
|
if (renote.userId !== user.id) {
|
2022-03-26 07:34:00 +01:00
|
|
|
const block = await Blockings.findOneBy({
|
2021-08-17 14:48:59 +02:00
|
|
|
blockerId: renote.userId,
|
|
|
|
blockeeId: user.id,
|
|
|
|
});
|
|
|
|
if (block) {
|
|
|
|
throw new ApiError(meta.errors.youHaveBeenBlocked);
|
|
|
|
}
|
|
|
|
}
|
2018-04-07 19:30:37 +02:00
|
|
|
}
|
|
|
|
|
2022-04-17 13:51:59 +02:00
|
|
|
let reply: Note | null = null;
|
2018-11-01 19:32:24 +01:00
|
|
|
if (ps.replyId != null) {
|
2018-04-07 19:30:37 +02:00
|
|
|
// Fetch reply
|
2022-07-25 18:15:21 +02:00
|
|
|
reply = await getNote(ps.replyId, user).catch(e => {
|
|
|
|
if (e.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchReplyTarget);
|
|
|
|
throw e;
|
|
|
|
});
|
2018-04-07 19:30:37 +02:00
|
|
|
|
2022-07-25 18:15:21 +02:00
|
|
|
if (reply.renoteId && !reply.text && !reply.fileIds && !reply.hasPoll) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.cannotReplyToPureRenote);
|
2018-04-07 19:30:37 +02:00
|
|
|
}
|
2021-08-17 14:48:59 +02:00
|
|
|
|
|
|
|
// Check blocking
|
|
|
|
if (reply.userId !== user.id) {
|
2022-03-26 07:34:00 +01:00
|
|
|
const block = await Blockings.findOneBy({
|
2021-08-17 14:48:59 +02:00
|
|
|
blockerId: reply.userId,
|
|
|
|
blockeeId: user.id,
|
|
|
|
});
|
|
|
|
if (block) {
|
|
|
|
throw new ApiError(meta.errors.youHaveBeenBlocked);
|
|
|
|
}
|
|
|
|
}
|
2018-04-07 19:30:37 +02:00
|
|
|
}
|
|
|
|
|
2018-07-05 16:36:07 +02:00
|
|
|
if (ps.poll) {
|
2019-03-06 14:55:47 +01:00
|
|
|
if (typeof ps.poll.expiresAt === 'number') {
|
2021-11-13 11:10:14 +01:00
|
|
|
if (ps.poll.expiresAt < Date.now()) {
|
2019-03-06 14:55:47 +01:00
|
|
|
throw new ApiError(meta.errors.cannotCreateAlreadyExpiredPoll);
|
2021-11-13 11:10:14 +01:00
|
|
|
}
|
2019-03-06 14:55:47 +01:00
|
|
|
} else if (typeof ps.poll.expiredAfter === 'number') {
|
|
|
|
ps.poll.expiresAt = Date.now() + ps.poll.expiredAfter;
|
|
|
|
}
|
2018-04-07 19:30:37 +02:00
|
|
|
}
|
|
|
|
|
2022-04-17 13:51:59 +02:00
|
|
|
let channel: Channel | null = null;
|
2020-08-18 15:44:21 +02:00
|
|
|
if (ps.channelId != null) {
|
2022-03-26 07:34:00 +01:00
|
|
|
channel = await Channels.findOneBy({ id: ps.channelId });
|
2020-08-18 15:44:21 +02:00
|
|
|
|
|
|
|
if (channel == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchChannel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-08 01:05:07 +01:00
|
|
|
// Create a post
|
2019-02-22 03:46:58 +01:00
|
|
|
const note = await create(user, {
|
2018-04-07 19:30:37 +02:00
|
|
|
createdAt: new Date(),
|
2018-09-05 12:32:46 +02:00
|
|
|
files: files,
|
2019-03-06 14:55:47 +01:00
|
|
|
poll: ps.poll ? {
|
|
|
|
choices: ps.poll.choices,
|
|
|
|
multiple: ps.poll.multiple || false,
|
2021-12-09 15:58:30 +01:00
|
|
|
expiresAt: ps.poll.expiresAt ? new Date(ps.poll.expiresAt) : null,
|
2019-04-12 18:43:22 +02:00
|
|
|
} : undefined,
|
|
|
|
text: ps.text || undefined,
|
2018-04-07 19:30:37 +02:00
|
|
|
reply,
|
|
|
|
renote,
|
2018-07-05 16:36:07 +02:00
|
|
|
cw: ps.cw,
|
2018-11-15 21:47:29 +01:00
|
|
|
localOnly: ps.localOnly,
|
2018-07-05 16:36:07 +02:00
|
|
|
visibility: ps.visibility,
|
2018-04-28 21:30:51 +02:00
|
|
|
visibleUsers,
|
2020-08-18 15:44:21 +02:00
|
|
|
channel,
|
2019-04-12 18:43:22 +02:00
|
|
|
apMentions: ps.noExtractMentions ? [] : undefined,
|
|
|
|
apHashtags: ps.noExtractHashtags ? [] : undefined,
|
|
|
|
apEmojis: ps.noExtractEmojis ? [] : undefined,
|
2018-04-07 19:30:37 +02:00
|
|
|
});
|
2019-02-22 03:46:58 +01:00
|
|
|
|
|
|
|
return {
|
2021-12-09 15:58:30 +01:00
|
|
|
createdNote: await Notes.pack(note, user),
|
2019-02-22 03:46:58 +01:00
|
|
|
};
|
|
|
|
});
|