This commit is contained in:
syuilo 2023-05-06 09:34:21 +09:00
parent 30bc59c943
commit c7d4264ecf
3 changed files with 50 additions and 40 deletions

View file

@ -9,10 +9,10 @@ import Logger from '@/logger.js';
import { DriveService } from '@/core/DriveService.js'; import { DriveService } from '@/core/DriveService.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
import { createTemp } from '@/misc/create-temp.js'; import { createTemp } from '@/misc/create-temp.js';
import { UtilityService } from '@/core/UtilityService.js';
import { QueueLoggerService } from '../QueueLoggerService.js'; import { QueueLoggerService } from '../QueueLoggerService.js';
import type { DBExportAntennasData } from '../types.js'; import type { DBExportAntennasData } from '../types.js';
import type Bull from 'bull'; import type Bull from 'bull';
import { UtilityService } from '@/core/UtilityService.js';
@Injectable() @Injectable()
export class ExportAntennasProcessorService { export class ExportAntennasProcessorService {
@ -21,12 +21,16 @@ export class ExportAntennasProcessorService {
constructor ( constructor (
@Inject(DI.config) @Inject(DI.config)
private config: Config, private config: Config,
@Inject(DI.usersRepository) @Inject(DI.usersRepository)
private usersRepository: UsersRepository, private usersRepository: UsersRepository,
@Inject(DI.antennasRepository) @Inject(DI.antennasRepository)
private antennsRepository: AntennasRepository, private antennsRepository: AntennasRepository,
@Inject(DI.userListJoiningsRepository) @Inject(DI.userListJoiningsRepository)
private userListJoiningsRepository: UserListJoiningsRepository, private userListJoiningsRepository: UserListJoiningsRepository,
private driveService: DriveService, private driveService: DriveService,
private utilityService: UtilityService, private utilityService: UtilityService,
private queueLoggerService: QueueLoggerService, private queueLoggerService: QueueLoggerService,
@ -72,7 +76,7 @@ export class ExportAntennasProcessorService {
keywords: antenna.keywords, keywords: antenna.keywords,
excludeKeywords: antenna.excludeKeywords, excludeKeywords: antenna.excludeKeywords,
users: antenna.users, users: antenna.users,
userListAcct: typeof users !== 'undefined' ? users.map((u) => { userListAccts: typeof users !== 'undefined' ? users.map((u) => {
return this.utilityService.getFullApAccount(u.username, u.host); // acct return this.utilityService.getFullApAccount(u.username, u.host); // acct
}) : null, }) : null,
caseSensitive: antenna.caseSensitive, caseSensitive: antenna.caseSensitive,

View file

@ -10,12 +10,47 @@ import { QueueLoggerService } from '../QueueLoggerService.js';
import { DBAntennaImportJobData } from '../types.js'; import { DBAntennaImportJobData } from '../types.js';
import type Bull from 'bull'; import type Bull from 'bull';
const validate = new Ajv().compile({
type: 'object',
properties: {
name: { type: 'string', minLength: 1, maxLength: 100 },
src: { type: 'string', enum: ['home', 'all', 'users', 'list'] },
userListAccts: {
type: 'array',
items: {
type: 'string',
},
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'],
});
@Injectable() @Injectable()
export class ImportAntennasProcessorService { export class ImportAntennasProcessorService {
private logger: Logger; private logger: Logger;
constructor ( constructor (
@Inject(DI.antennasRepository) @Inject(DI.antennasRepository)
private antennasRepository: AntennasRepository, private antennasRepository: AntennasRepository,
private queueLoggerService: QueueLoggerService, private queueLoggerService: QueueLoggerService,
private idService: IdService, private idService: IdService,
private globalEventService: GlobalEventService, private globalEventService: GlobalEventService,
@ -27,38 +62,6 @@ export class ImportAntennasProcessorService {
public async process(job: Bull.Job<DBAntennaImportJobData>, done: () => void): Promise<void> { public async process(job: Bull.Job<DBAntennaImportJobData>, done: () => void): Promise<void> {
const now = new Date(); const now = new Date();
try { try {
const validate = new Ajv().compile({
type: 'object',
properties: {
name: { type: 'string', minLength: 1, maxLength: 100 },
src: { type: 'string', enum: ['home', 'all', 'users', 'list'] },
userListAcct: {
type: 'array',
items: {
type: 'string',
},
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'],
});
for (const antenna of job.data.antenna) { for (const antenna of job.data.antenna) {
if (antenna.keywords.length === 0 || antenna.keywords[0].every(x => x === '')) continue; if (antenna.keywords.length === 0 || antenna.keywords[0].every(x => x === '')) continue;
if (!validate(antenna)) { if (!validate(antenna)) {
@ -71,11 +74,11 @@ export class ImportAntennasProcessorService {
lastUsedAt: now, lastUsedAt: now,
userId: job.data.user.id, userId: job.data.user.id,
name: antenna.name, name: antenna.name,
src: antenna.src === 'list' && antenna.userListAcct ? 'users' : antenna.src, src: antenna.src === 'list' && antenna.userListAccts ? 'users' : antenna.src,
userListId: null, userListId: null,
keywords: antenna.keywords, keywords: antenna.keywords,
excludeKeywords: antenna.excludeKeywords, excludeKeywords: antenna.excludeKeywords,
users: (antenna.src === 'list' && antenna.userListAcct !== null ? antenna.userListAcct : antenna.users).filter(Boolean), users: (antenna.src === 'list' && antenna.userListAccts !== null ? antenna.userListAccts : antenna.users).filter(Boolean),
caseSensitive: antenna.caseSensitive, caseSensitive: antenna.caseSensitive,
withReplies: antenna.withReplies, withReplies: antenna.withReplies,
withFile: antenna.withFile, withFile: antenna.withFile,

View file

@ -4,9 +4,9 @@ import { Endpoint } from '@/server/api/endpoint-base.js';
import { QueueService } from '@/core/QueueService.js'; import { QueueService } from '@/core/QueueService.js';
import type { AntennasRepository, DriveFilesRepository, UsersRepository, Antenna as _Antenna } from '@/models/index.js'; import type { AntennasRepository, DriveFilesRepository, UsersRepository, Antenna as _Antenna } from '@/models/index.js';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import { ApiError } from '../../error.js';
import { RoleService } from '@/core/RoleService.js'; import { RoleService } from '@/core/RoleService.js';
import { DownloadService } from '@/core/DownloadService.js'; import { DownloadService } from '@/core/DownloadService.js';
import { ApiError } from '../../error.js';
export const meta = { export const meta = {
secure: true, secure: true,
@ -54,13 +54,16 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
constructor ( constructor (
@Inject(DI.driveFilesRepository) @Inject(DI.driveFilesRepository)
private driveFilesRepository: DriveFilesRepository, private driveFilesRepository: DriveFilesRepository,
@Inject(DI.antennasRepository) @Inject(DI.antennasRepository)
private antennasRepository: AntennasRepository, private antennasRepository: AntennasRepository,
@Inject(DI.usersRepository) @Inject(DI.usersRepository)
private usersRepository: UsersRepository, private usersRepository: UsersRepository,
private roleService: RoleService, private roleService: RoleService,
private queueService: QueueService, private queueService: QueueService,
private downloadService: DownloadService private downloadService: DownloadService,
) { ) {
super(meta, paramDef, async (ps, me) => { super(meta, paramDef, async (ps, me) => {
const users = await this.usersRepository.findOneBy({ id: me.id }); const users = await this.usersRepository.findOneBy({ id: me.id });
@ -68,7 +71,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
const file = await this.driveFilesRepository.findOneBy({ id: ps.fileId }); const file = await this.driveFilesRepository.findOneBy({ id: ps.fileId });
if (file === null) throw new ApiError(meta.errors.noSuchFile); if (file === null) throw new ApiError(meta.errors.noSuchFile);
if (file.size === 0) throw new ApiError(meta.errors.emptyFile); if (file.size === 0) throw new ApiError(meta.errors.emptyFile);
const antennas: (_Antenna & { userListAcct: string[] | null })[] = JSON.parse(await this.downloadService.downloadTextFile(file.url)); const antennas: (_Antenna & { userListAccts: string[] | null })[] = JSON.parse(await this.downloadService.downloadTextFile(file.url));
const currentAntennasCount = await this.antennasRepository.countBy({ userId: me.id }); const currentAntennasCount = await this.antennasRepository.countBy({ userId: me.id });
if (currentAntennasCount + antennas.length > (await this.roleService.getUserPolicies(me.id)).antennaLimit) { if (currentAntennasCount + antennas.length > (await this.roleService.getUserPolicies(me.id)).antennaLimit) {
throw new ApiError(meta.errors.tooManyAntennas); throw new ApiError(meta.errors.tooManyAntennas);
@ -78,4 +81,4 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
} }
} }
export type Antenna = (_Antenna & { userListAcct: string[] | null })[]; export type Antenna = (_Antenna & { userListAccts: string[] | null })[];