2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-01-14 07:59:15 +01:00
|
|
|
import ms from 'ms';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { ChannelsRepository, DriveFilesRepository } from '@/models/index.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import type { Channel } from '@/models/entities/Channel.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { ChannelEntityService } from '@/core/entities/ChannelEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { ApiError } from '../../error.js';
|
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',
|
|
|
|
|
2023-01-14 07:59:15 +01:00
|
|
|
limit: {
|
|
|
|
duration: ms('1hour'),
|
|
|
|
max: 10,
|
|
|
|
},
|
|
|
|
|
2020-08-18 15:44:21 +02:00
|
|
|
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-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01:00
|
|
|
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-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
2020-08-18 15:44:21 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Inject(DI.channelsRepository)
|
|
|
|
private channelsRepository: ChannelsRepository,
|
|
|
|
|
|
|
|
private idService: IdService,
|
|
|
|
private channelEntityService: ChannelEntityService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
let banner = null;
|
|
|
|
if (ps.bannerId != null) {
|
|
|
|
banner = await this.driveFilesRepository.findOneBy({
|
|
|
|
id: ps.bannerId,
|
|
|
|
userId: me.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (banner == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchFile);
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 15:44:21 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
const channel = await this.channelsRepository.insert({
|
|
|
|
id: this.idService.genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
userId: me.id,
|
|
|
|
name: ps.name,
|
|
|
|
description: ps.description ?? null,
|
|
|
|
bannerId: banner ? banner.id : null,
|
|
|
|
} as Channel).then(x => this.channelsRepository.findOneByOrFail(x.identifiers[0]));
|
|
|
|
|
|
|
|
return await this.channelEntityService.pack(channel, me);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|