2021-08-19 14:55:45 +02:00
|
|
|
import define from '../../define';
|
|
|
|
import { ApiError } from '../../error';
|
|
|
|
import { Channels, DriveFiles } from '@/models/index';
|
|
|
|
import { Channel } from '@/models/entities/channel';
|
|
|
|
import { genId } from '@/misc/gen-id';
|
2020-08-18 15:44:21 +02:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['channels'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: true,
|
2020-08-18 15:44:21 +02:00
|
|
|
|
|
|
|
kind: 'write:channels',
|
|
|
|
|
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2020-08-18 15:44:21 +02:00
|
|
|
ref: 'Channel',
|
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchFile: {
|
|
|
|
message: 'No such file.',
|
|
|
|
code: 'NO_SUCH_FILE',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: 'cd1e9f3e-5a12-4ab4-96f6-5d0a2cc32050',
|
2020-08-18 15:44:21 +02:00
|
|
|
},
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2020-08-18 15:44:21 +02:00
|
|
|
|
2022-02-19 06:05:32 +01:00
|
|
|
const paramDef = {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
name: { type: 'string', minLength: 1, maxLength: 128 },
|
|
|
|
description: { type: 'string', nullable: true, minLength: 1, maxLength: 2048 },
|
|
|
|
bannerId: { type: 'string', format: 'misskey:id', nullable: true },
|
|
|
|
},
|
|
|
|
required: ['name'],
|
|
|
|
} 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) => {
|
2020-08-18 15:44:21 +02:00
|
|
|
let banner = null;
|
|
|
|
if (ps.bannerId != null) {
|
|
|
|
banner = await DriveFiles.findOne({
|
|
|
|
id: ps.bannerId,
|
2021-12-09 15:58:30 +01:00
|
|
|
userId: user.id,
|
2020-08-18 15:44:21 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (banner == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 16:51:26 +01:00
|
|
|
const channel = await Channels.insert({
|
2020-08-18 15:44:21 +02:00
|
|
|
id: genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
userId: user.id,
|
|
|
|
name: ps.name,
|
|
|
|
description: ps.description || null,
|
|
|
|
bannerId: banner ? banner.id : null,
|
2022-01-25 16:51:26 +01:00
|
|
|
} as Channel).then(x => Channels.findOneOrFail(x.identifiers[0]));
|
2020-08-18 15:44:21 +02:00
|
|
|
|
|
|
|
return await Channels.pack(channel, user);
|
|
|
|
});
|