monkeeShark/packages/backend/src/queue/processors/ImportBlockingProcessorService.ts

102 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-09-17 18:27:08 +00:00
import { Inject, Injectable } from '@nestjs/common';
import { IsNull } from 'typeorm';
2022-09-17 18:27:08 +00:00
import { DI } from '@/di-symbols.js';
import type { UsersRepository, DriveFilesRepository } from '@/models/index.js';
2022-09-17 18:27:08 +00:00
import type Logger from '@/logger.js';
import * as Acct from '@/misc/acct.js';
2022-12-04 01:16:03 +00:00
import { RemoteUserResolveService } from '@/core/RemoteUserResolveService.js';
2022-09-17 18:27:08 +00:00
import { DownloadService } from '@/core/DownloadService.js';
import { UtilityService } from '@/core/UtilityService.js';
import { QueueLoggerService } from '../QueueLoggerService.js';
import type Bull from 'bull';
import type { DbUserImportJobData, DbUserImportToDbJobData } from '../types.js';
import { bindThis } from '@/decorators.js';
import { QueueService } from '@/core/QueueService.js';
2022-09-17 18:27:08 +00:00
@Injectable()
export class ImportBlockingProcessorService {
2022-09-18 18:11:50 +00:00
private logger: Logger;
2022-09-17 18:27:08 +00:00
constructor(
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
@Inject(DI.driveFilesRepository)
private driveFilesRepository: DriveFilesRepository,
private queueService: QueueService,
2022-09-17 18:27:08 +00:00
private utilityService: UtilityService,
2022-12-04 01:16:03 +00:00
private remoteUserResolveService: RemoteUserResolveService,
2022-09-17 18:27:08 +00:00
private downloadService: DownloadService,
private queueLoggerService: QueueLoggerService,
) {
2022-09-18 18:11:50 +00:00
this.logger = this.queueLoggerService.logger.createSubLogger('import-blocking');
2022-09-17 18:27:08 +00:00
}
@bindThis
2022-09-17 18:27:08 +00:00
public async process(job: Bull.Job<DbUserImportJobData>, done: () => void): Promise<void> {
2022-09-18 18:11:50 +00:00
this.logger.info(`Importing blocking of ${job.data.user.id} ...`);
2022-09-17 18:27:08 +00:00
const user = await this.usersRepository.findOneBy({ id: job.data.user.id });
if (user == null) {
done();
return;
}
const file = await this.driveFilesRepository.findOneBy({
id: job.data.fileId,
});
if (file == null) {
done();
return;
}
const csv = await this.downloadService.downloadTextFile(file.url);
const targets = csv.trim().split('\n');
this.queueService.createImportBlockingToDbJob({ id: user.id }, targets);
2022-09-17 18:27:08 +00:00
this.logger.succ('Import jobs created');
done();
}
2022-09-17 18:27:08 +00:00
@bindThis
public async processDb(job: Bull.Job<DbUserImportToDbJobData>): Promise<void> {
const line = job.data.target;
const user = job.data.user;
2022-09-17 18:27:08 +00:00
try {
const acct = line.split(',')[0].trim();
const { username, host } = Acct.parse(acct);
2022-09-17 18:27:08 +00:00
if (!host) return;
2022-09-17 18:27:08 +00:00
let target = this.utilityService.isSelfHost(host) ? await this.usersRepository.findOneBy({
host: IsNull(),
usernameLower: username.toLowerCase(),
}) : await this.usersRepository.findOneBy({
host: this.utilityService.toPuny(host),
usernameLower: username.toLowerCase(),
});
2022-09-17 18:27:08 +00:00
if (host == null && target == null) return;
2022-09-17 18:27:08 +00:00
if (target == null) {
target = await this.remoteUserResolveService.resolveUser(username, host);
}
2022-09-17 18:27:08 +00:00
if (target == null) {
throw `Unable to resolve user: @${username}@${host}`;
}
2022-09-17 18:27:08 +00:00
// skip myself
if (target.id === job.data.user.id) return;
2022-09-17 18:27:08 +00:00
this.logger.info(`Block ${target.id} ...`);
2022-09-17 18:27:08 +00:00
this.queueService.createBlockJob([{ from: { id: user.id }, to: { id: target.id }, silent: true }]);
} catch (e) {
this.logger.warn(`Error: ${e}`);
}
2022-09-17 18:27:08 +00:00
}
}