51 lines
1.6 KiB
TypeScript
51 lines
1.6 KiB
TypeScript
import { Inject, Injectable } from '@nestjs/common';
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
import type { ChannelsRepository, DriveFilesRepository } from '@/models/index.js';
|
|
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';
|
|
import { ApiError } from '../../error.js';
|
|
|
|
// eslint-disable-next-line import/no-default-export
|
|
@Injectable()
|
|
export default class extends Endpoint<'channels/create'> {
|
|
name = 'channels/create' as const;
|
|
constructor(
|
|
@Inject(DI.driveFilesRepository)
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
@Inject(DI.channelsRepository)
|
|
private channelsRepository: ChannelsRepository,
|
|
|
|
private idService: IdService,
|
|
private channelEntityService: ChannelEntityService,
|
|
) {
|
|
super(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(this.meta.errors.noSuchFile);
|
|
}
|
|
}
|
|
|
|
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,
|
|
...(ps.color !== undefined ? { color: ps.color } : {}),
|
|
} as Channel).then(x => this.channelsRepository.findOneByOrFail(x.identifiers[0]));
|
|
|
|
return await this.channelEntityService.pack(channel, me);
|
|
});
|
|
}
|
|
}
|