2023-07-27 05:31:52 +00:00
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 05:31:52 +00:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-05-05 23:17:55 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import ms from 'ms';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { QueueService } from '@/core/QueueService.js';
|
2023-09-15 05:28:29 +00:00
|
|
|
import type { AntennasRepository, DriveFilesRepository, UsersRepository, MiAntenna as _Antenna } from '@/models/_.js';
|
2023-05-05 23:17:55 +00:00
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
|
|
|
import { DownloadService } from '@/core/DownloadService.js';
|
2023-05-06 00:34:21 +00:00
|
|
|
import { ApiError } from '../../error.js';
|
2023-05-05 23:17:55 +00:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
secure: true,
|
|
|
|
requireCredential: true,
|
|
|
|
prohibitMoved: true,
|
|
|
|
|
|
|
|
limit: {
|
|
|
|
duration: ms('1hour'),
|
|
|
|
max: 1,
|
|
|
|
},
|
|
|
|
errors: {
|
|
|
|
noSuchFile: {
|
|
|
|
message: 'No such file.',
|
|
|
|
code: 'NO_SUCH_FILE',
|
|
|
|
id: '3b71d086-c3fa-431c-b01d-ded65a777172',
|
|
|
|
},
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
|
|
|
id: 'e842c379-8ac7-4cf7-b07a-4d4de7e4671c',
|
|
|
|
},
|
|
|
|
emptyFile: {
|
|
|
|
message: 'That file is empty.',
|
|
|
|
code: 'EMPTY_FILE',
|
|
|
|
id: '7f60115d-8d93-4b0f-bd0e-3815dcbb389f',
|
|
|
|
},
|
|
|
|
tooManyAntennas: {
|
|
|
|
message: 'You cannot create antenna any more.',
|
|
|
|
code: 'TOO_MANY_ANTENNAS',
|
|
|
|
id: '600917d4-a4cb-4cc5-8ba8-7ac8ea3c7779',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
export const paramDef = {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
fileId: { type: 'string', format: 'misskey:id' },
|
|
|
|
},
|
|
|
|
required: ['fileId'],
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
@Injectable() // eslint-disable-next-line import/no-default-export
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor (
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
2023-06-25 12:13:15 +00:00
|
|
|
|
2023-05-05 23:17:55 +00:00
|
|
|
@Inject(DI.antennasRepository)
|
|
|
|
private antennasRepository: AntennasRepository,
|
2023-05-06 00:34:21 +00:00
|
|
|
|
2023-05-05 23:17:55 +00:00
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
2023-05-06 00:34:21 +00:00
|
|
|
|
2023-05-05 23:17:55 +00:00
|
|
|
private roleService: RoleService,
|
|
|
|
private queueService: QueueService,
|
2023-05-06 00:34:21 +00:00
|
|
|
private downloadService: DownloadService,
|
2023-05-05 23:17:55 +00:00
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2024-02-08 07:04:41 +00:00
|
|
|
const userExist = await this.usersRepository.exists({ where: { id: me.id } });
|
2023-07-11 05:58:58 +00:00
|
|
|
if (!userExist) throw new ApiError(meta.errors.noSuchUser);
|
2023-05-05 23:17:55 +00:00
|
|
|
const file = await this.driveFilesRepository.findOneBy({ id: ps.fileId });
|
|
|
|
if (file === null) throw new ApiError(meta.errors.noSuchFile);
|
|
|
|
if (file.size === 0) throw new ApiError(meta.errors.emptyFile);
|
2023-05-06 00:34:21 +00:00
|
|
|
const antennas: (_Antenna & { userListAccts: string[] | null })[] = JSON.parse(await this.downloadService.downloadTextFile(file.url));
|
2023-05-05 23:17:55 +00:00
|
|
|
const currentAntennasCount = await this.antennasRepository.countBy({ userId: me.id });
|
|
|
|
if (currentAntennasCount + antennas.length > (await this.roleService.getUserPolicies(me.id)).antennaLimit) {
|
|
|
|
throw new ApiError(meta.errors.tooManyAntennas);
|
|
|
|
}
|
|
|
|
this.queueService.createImportAntennasJob(me, antennas);
|
|
|
|
});
|
|
|
|
}
|
2023-06-25 12:13:15 +00:00
|
|
|
}
|
2023-05-05 23:17:55 +00:00
|
|
|
|
2023-05-06 00:34:21 +00:00
|
|
|
export type Antenna = (_Antenna & { userListAccts: string[] | null })[];
|