77 lines
2.4 KiB
TypeScript
77 lines
2.4 KiB
TypeScript
import { Inject, Injectable } from '@nestjs/common';
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
import { IdService } from '@/core/IdService.js';
|
|
import type { UserListsRepository, AntennasRepository } from '@/models/index.js';
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
|
import { AntennaEntityService } from '@/core/entities/AntennaEntityService.js';
|
|
import { DI } from '@/di-symbols.js';
|
|
import { RoleService } from '@/core/RoleService.js';
|
|
import { ApiError } from '../../error.js';
|
|
|
|
// eslint-disable-next-line import/no-default-export
|
|
@Injectable()
|
|
export default class extends Endpoint<'antenna/create'> {
|
|
name = 'antenna/create' as const;
|
|
constructor(
|
|
@Inject(DI.antennasRepository)
|
|
private antennasRepository: AntennasRepository,
|
|
|
|
@Inject(DI.userListsRepository)
|
|
private userListsRepository: UserListsRepository,
|
|
|
|
private antennaEntityService: AntennaEntityService,
|
|
private roleService: RoleService,
|
|
private idService: IdService,
|
|
private globalEventService: GlobalEventService,
|
|
) {
|
|
super(async (ps, me) => {
|
|
if ((ps.keywords.length === 0) || ps.keywords[0].every(x => x === '')) {
|
|
throw new Error('invalid param');
|
|
}
|
|
|
|
const currentAntennasCount = await this.antennasRepository.countBy({
|
|
userId: me.id,
|
|
});
|
|
if (currentAntennasCount > (await this.roleService.getUserPolicies(me.id)).antennaLimit) {
|
|
throw new ApiError(this.meta.errors.tooManyAntennas);
|
|
}
|
|
|
|
let userList;
|
|
|
|
if (ps.src === 'list' && ps.userListId) {
|
|
userList = await this.userListsRepository.findOneBy({
|
|
id: ps.userListId,
|
|
userId: me.id,
|
|
});
|
|
|
|
if (userList == null) {
|
|
throw new ApiError(this.meta.errors.noSuchUserList);
|
|
}
|
|
}
|
|
|
|
const now = new Date();
|
|
|
|
const antenna = await this.antennasRepository.insert({
|
|
id: this.idService.genId(),
|
|
createdAt: now,
|
|
lastUsedAt: now,
|
|
userId: me.id,
|
|
name: ps.name,
|
|
src: ps.src,
|
|
userListId: userList ? userList.id : null,
|
|
keywords: ps.keywords,
|
|
excludeKeywords: ps.excludeKeywords,
|
|
users: ps.users,
|
|
caseSensitive: ps.caseSensitive,
|
|
withReplies: ps.withReplies,
|
|
withFile: ps.withFile,
|
|
notify: ps.notify,
|
|
}).then(x => this.antennasRepository.findOneByOrFail(x.identifiers[0]));
|
|
|
|
this.globalEventService.publishInternalEvent('antennaCreated', antenna);
|
|
|
|
return await this.antennaEntityService.pack(antenna);
|
|
});
|
|
}
|
|
}
|