2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-02-15 05:37:18 +01:00
|
|
|
import type { UserListsRepository, AntennasRepository } from '@/models/index.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
|
|
|
import { AntennaEntityService } from '@/core/entities/AntennaEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-01-12 13:02:26 +01:00
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { ApiError } from '../../error.js';
|
2020-01-29 20:37:25 +01:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['antennas'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: true,
|
2020-01-29 20:37:25 +01:00
|
|
|
|
|
|
|
kind: 'write:account',
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchUserList: {
|
|
|
|
message: 'No such user list.',
|
|
|
|
code: 'NO_SUCH_USER_LIST',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: '95063e93-a283-4b8b-9aa5-bcdb8df69a7f',
|
2020-02-14 17:03:59 +01:00
|
|
|
},
|
|
|
|
|
2022-12-26 00:50:17 +01:00
|
|
|
tooManyAntennas: {
|
|
|
|
message: 'You cannot create antenna any more.',
|
|
|
|
code: 'TOO_MANY_ANTENNAS',
|
|
|
|
id: 'faf47050-e8b5-438c-913c-db2b1576fde4',
|
|
|
|
},
|
2021-03-06 14:34:11 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2021-12-09 15:58:30 +01:00
|
|
|
ref: 'Antenna',
|
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2020-01-29 20:37:25 +01: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: 100 },
|
2023-02-15 05:37:18 +01:00
|
|
|
src: { type: 'string', enum: ['home', 'all', 'users', 'list'] },
|
2022-02-19 06:05:32 +01:00
|
|
|
userListId: { type: 'string', format: 'misskey:id', nullable: true },
|
|
|
|
keywords: { type: 'array', items: {
|
|
|
|
type: 'array', items: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
} },
|
|
|
|
excludeKeywords: { type: 'array', items: {
|
|
|
|
type: 'array', items: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
} },
|
|
|
|
users: { type: 'array', items: {
|
|
|
|
type: 'string',
|
|
|
|
} },
|
|
|
|
caseSensitive: { type: 'boolean' },
|
|
|
|
withReplies: { type: 'boolean' },
|
|
|
|
withFile: { type: 'boolean' },
|
|
|
|
notify: { type: 'boolean' },
|
|
|
|
},
|
|
|
|
required: ['name', 'src', 'keywords', 'excludeKeywords', 'users', 'caseSensitive', 'withReplies', 'withFile', 'notify'],
|
|
|
|
} 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.antennasRepository)
|
|
|
|
private antennasRepository: AntennasRepository,
|
2020-03-04 03:45:33 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Inject(DI.userListsRepository)
|
|
|
|
private userListsRepository: UserListsRepository,
|
2020-03-04 03:45:33 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
private antennaEntityService: AntennaEntityService,
|
2023-01-12 13:02:26 +01:00
|
|
|
private roleService: RoleService,
|
2022-09-17 20:27:08 +02:00
|
|
|
private idService: IdService,
|
|
|
|
private globalEventService: GlobalEventService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2022-12-26 00:50:17 +01:00
|
|
|
const currentAntennasCount = await this.antennasRepository.countBy({
|
|
|
|
userId: me.id,
|
|
|
|
});
|
2023-01-15 12:52:53 +01:00
|
|
|
if (currentAntennasCount > (await this.roleService.getUserPolicies(me.id)).antennaLimit) {
|
2022-12-26 00:50:17 +01:00
|
|
|
throw new ApiError(meta.errors.tooManyAntennas);
|
|
|
|
}
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
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(meta.errors.noSuchUserList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const antenna = await this.antennasRepository.insert({
|
|
|
|
id: this.idService.genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|