* fix(backend): use insertOne insteadof insert/findOneOrFail combination * fix: typo * fix(backend): inherit mainAlias? * refactor(backend): use extend * fix(backend): invalid entityTarget * fix(backend): fake where * chore: debug * chore: debug * test: log * fix(backend): column names * fix(backend): remove dummy from * revert: log * fix(backend): position * fix(backend): automatic aliasing * chore(backend): alias * chore(backend): remove from * fix(backend): type * fix(backend): avoid pure name * test(backend): fix type * chore(backend): use cte * fix(backend): avoid useless alias * fix(backend): fix typo * fix(backend): __disambiguation__ * fix(backend): quote * chore(backend): t * chore(backend): accessible * chore(backend): concrete returning * fix(backend): quote * chore: log more * chore: log metadata * chore(backend): use raw * fix(backend): returning column name * fix(backend): transform * build(backend): wanna logging * build(backend): transform empty * build(backend): build alias * build(backend): restore name * chore: return entity * fix: test case * test(backend): 204 * chore(backend): log sql * chore(backend): assert user joined * fix(backend): typo * chore(backend): log long sql * chore(backend): log join * chore(backend): log join depth null * chore(backend): joinAttributes * chore(backend): override createJoinExpression * chore: join log * fix(backend): escape * test(backend): log log * chore(backend): join gonna success? * chore(backend): relations * chore(backend): undefined * chore(backend): target * chore(backend): remove log * chore(backend): log chart update * chore(backend): log columns * chore(backend): check hasMetadata * chore(backend): unshift id when not included * chore(backend): missing select * chore(backend): remove debug code
102 lines
3.1 KiB
TypeScript
102 lines
3.1 KiB
TypeScript
/*
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
import { Injectable, Inject } from '@nestjs/common';
|
|
import _Ajv from 'ajv';
|
|
import { IdService } from '@/core/IdService.js';
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
|
import Logger from '@/logger.js';
|
|
import type { AntennasRepository } from '@/models/_.js';
|
|
import { DI } from '@/di-symbols.js';
|
|
import { bindThis } from '@/decorators.js';
|
|
import { QueueLoggerService } from '../QueueLoggerService.js';
|
|
import { DBAntennaImportJobData } from '../types.js';
|
|
import type * as Bull from 'bullmq';
|
|
|
|
const Ajv = _Ajv.default;
|
|
|
|
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' },
|
|
localOnly: { type: 'boolean' },
|
|
excludeBots: { type: 'boolean' },
|
|
withReplies: { type: 'boolean' },
|
|
withFile: { type: 'boolean' },
|
|
},
|
|
required: ['name', 'src', 'keywords', 'excludeKeywords', 'users', 'caseSensitive', 'withReplies', 'withFile'],
|
|
});
|
|
|
|
@Injectable()
|
|
export class ImportAntennasProcessorService {
|
|
private logger: Logger;
|
|
|
|
constructor (
|
|
@Inject(DI.antennasRepository)
|
|
private antennasRepository: AntennasRepository,
|
|
|
|
private queueLoggerService: QueueLoggerService,
|
|
private idService: IdService,
|
|
private globalEventService: GlobalEventService,
|
|
) {
|
|
this.logger = this.queueLoggerService.logger.createSubLogger('import-antennas');
|
|
}
|
|
|
|
@bindThis
|
|
public async process(job: Bull.Job<DBAntennaImportJobData>): Promise<void> {
|
|
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.insertOne({
|
|
id: this.idService.gen(now.getTime()),
|
|
lastUsedAt: now,
|
|
userId: job.data.user.id,
|
|
name: antenna.name,
|
|
src: antenna.src === 'list' && antenna.userListAccts ? 'users' : antenna.src,
|
|
userListId: null,
|
|
keywords: antenna.keywords,
|
|
excludeKeywords: antenna.excludeKeywords,
|
|
users: (antenna.src === 'list' && antenna.userListAccts !== null ? antenna.userListAccts : antenna.users).filter(Boolean),
|
|
caseSensitive: antenna.caseSensitive,
|
|
localOnly: antenna.localOnly,
|
|
excludeBots: antenna.excludeBots,
|
|
withReplies: antenna.withReplies,
|
|
withFile: antenna.withFile,
|
|
});
|
|
this.logger.succ('Antenna created: ' + result.id);
|
|
this.globalEventService.publishInternalEvent('antennaCreated', result);
|
|
}
|
|
} catch (err: any) {
|
|
this.logger.error(err);
|
|
}
|
|
}
|
|
}
|