2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-05-06 01:17:55 +02:00
|
|
|
import { Injectable, Inject } from '@nestjs/common';
|
2023-06-25 14:13:15 +02:00
|
|
|
import _Ajv from 'ajv';
|
2023-05-06 01:17:55 +02:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
|
|
|
import Logger from '@/logger.js';
|
2023-09-15 07:28:29 +02:00
|
|
|
import type { AntennasRepository } from '@/models/_.js';
|
2023-05-06 01:17:55 +02:00
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { bindThis } from '@/decorators.js';
|
|
|
|
import { QueueLoggerService } from '../QueueLoggerService.js';
|
|
|
|
import { DBAntennaImportJobData } from '../types.js';
|
2023-05-29 04:54:49 +02:00
|
|
|
import type * as Bull from 'bullmq';
|
2023-05-06 01:17:55 +02:00
|
|
|
|
2023-06-25 14:13:15 +02:00
|
|
|
const Ajv = _Ajv.default;
|
|
|
|
|
2023-05-06 02:34:21 +02:00
|
|
|
const validate = new Ajv().compile({
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
name: { type: 'string', minLength: 1, maxLength: 100 },
|
|
|
|
src: { type: 'string', enum: ['home', 'all', 'users', 'list'] },
|
2023-07-08 00:08:16 +02:00
|
|
|
userListAccts: {
|
|
|
|
type: 'array',
|
2023-05-06 02:34:21 +02:00
|
|
|
items: {
|
|
|
|
type: 'string',
|
2023-07-08 00:08:16 +02:00
|
|
|
},
|
2023-05-06 02:34:21 +02:00
|
|
|
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' },
|
2023-10-16 08:06:00 +02:00
|
|
|
localOnly: { type: 'boolean' },
|
2023-05-06 02:34:21 +02:00
|
|
|
withReplies: { type: 'boolean' },
|
|
|
|
withFile: { type: 'boolean' },
|
|
|
|
notify: { type: 'boolean' },
|
|
|
|
},
|
|
|
|
required: ['name', 'src', 'keywords', 'excludeKeywords', 'users', 'caseSensitive', 'withReplies', 'withFile', 'notify'],
|
|
|
|
});
|
|
|
|
|
2023-05-06 01:17:55 +02:00
|
|
|
@Injectable()
|
|
|
|
export class ImportAntennasProcessorService {
|
|
|
|
private logger: Logger;
|
2023-05-06 02:34:21 +02:00
|
|
|
|
2023-05-06 01:17:55 +02:00
|
|
|
constructor (
|
|
|
|
@Inject(DI.antennasRepository)
|
|
|
|
private antennasRepository: AntennasRepository,
|
2023-05-06 02:34:21 +02:00
|
|
|
|
2023-05-06 01:17:55 +02:00
|
|
|
private queueLoggerService: QueueLoggerService,
|
|
|
|
private idService: IdService,
|
|
|
|
private globalEventService: GlobalEventService,
|
|
|
|
) {
|
|
|
|
this.logger = this.queueLoggerService.logger.createSubLogger('import-antennas');
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2023-05-29 04:54:49 +02:00
|
|
|
public async process(job: Bull.Job<DBAntennaImportJobData>): Promise<void> {
|
2023-05-06 01:17:55 +02:00
|
|
|
const now = new Date();
|
|
|
|
try {
|
|
|
|
for (const antenna of job.data.antenna) {
|
|
|
|
if (antenna.keywords.length === 0 || antenna.keywords[0].every(x => x === '')) continue;
|
|
|
|
if (!validate(antenna)) {
|
|
|
|
this.logger.warn('Validation Failed');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const result = await this.antennasRepository.insert({
|
2023-10-16 03:45:22 +02:00
|
|
|
id: this.idService.gen(now.getTime()),
|
2023-05-06 01:17:55 +02:00
|
|
|
lastUsedAt: now,
|
|
|
|
userId: job.data.user.id,
|
|
|
|
name: antenna.name,
|
2023-05-06 02:34:21 +02:00
|
|
|
src: antenna.src === 'list' && antenna.userListAccts ? 'users' : antenna.src,
|
2023-05-06 01:17:55 +02:00
|
|
|
userListId: null,
|
|
|
|
keywords: antenna.keywords,
|
|
|
|
excludeKeywords: antenna.excludeKeywords,
|
2023-05-06 02:34:21 +02:00
|
|
|
users: (antenna.src === 'list' && antenna.userListAccts !== null ? antenna.userListAccts : antenna.users).filter(Boolean),
|
2023-05-06 01:17:55 +02:00
|
|
|
caseSensitive: antenna.caseSensitive,
|
2023-10-16 08:06:00 +02:00
|
|
|
localOnly: antenna.localOnly,
|
2023-05-06 01:17:55 +02:00
|
|
|
withReplies: antenna.withReplies,
|
|
|
|
withFile: antenna.withFile,
|
|
|
|
notify: antenna.notify,
|
|
|
|
}).then(x => this.antennasRepository.findOneByOrFail(x.identifiers[0]));
|
|
|
|
this.logger.succ('Antenna created: ' + result.id);
|
|
|
|
this.globalEventService.publishInternalEvent('antennaCreated', result);
|
|
|
|
}
|
|
|
|
} catch (err: any) {
|
|
|
|
this.logger.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|