mizzkey/packages/backend/src/queue/processors/ImportFollowingProcessorService.ts

104 lines
3.1 KiB
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
2022-09-17 20:27:08 +02:00
import { Inject, Injectable } from '@nestjs/common';
import { IsNull } from 'typeorm';
2022-09-17 20:27:08 +02:00
import { DI } from '@/di-symbols.js';
import type { UsersRepository, DriveFilesRepository } from '@/models/_.js';
2022-09-17 20:27:08 +02:00
import type Logger from '@/logger.js';
import * as Acct from '@/misc/acct.js';
2022-12-04 02:16:03 +01:00
import { RemoteUserResolveService } from '@/core/RemoteUserResolveService.js';
2022-09-17 20:27:08 +02:00
import { DownloadService } from '@/core/DownloadService.js';
import { UtilityService } from '@/core/UtilityService.js';
import { bindThis } from '@/decorators.js';
import { QueueService } from '@/core/QueueService.js';
import { QueueLoggerService } from '../QueueLoggerService.js';
import type * as Bull from 'bullmq';
import type { DbUserImportJobData, DbUserImportToDbJobData } from '../types.js';
2022-09-17 20:27:08 +02:00
@Injectable()
export class ImportFollowingProcessorService {
2022-09-18 20:11:50 +02:00
private logger: Logger;
2022-09-17 20:27:08 +02:00
constructor(
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
@Inject(DI.driveFilesRepository)
private driveFilesRepository: DriveFilesRepository,
private queueService: QueueService,
2022-09-17 20:27:08 +02:00
private utilityService: UtilityService,
2022-12-04 02:16:03 +01:00
private remoteUserResolveService: RemoteUserResolveService,
2022-09-17 20:27:08 +02:00
private downloadService: DownloadService,
private queueLoggerService: QueueLoggerService,
) {
2022-09-18 20:11:50 +02:00
this.logger = this.queueLoggerService.logger.createSubLogger('import-following');
2022-09-17 20:27:08 +02:00
}
@bindThis
public async process(job: Bull.Job<DbUserImportJobData>): Promise<void> {
2022-09-18 20:11:50 +02:00
this.logger.info(`Importing following of ${job.data.user.id} ...`);
2022-09-17 20:27:08 +02:00
const user = await this.usersRepository.findOneBy({ id: job.data.user.id });
if (user == null) {
return;
}
const file = await this.driveFilesRepository.findOneBy({
id: job.data.fileId,
});
if (file == null) {
return;
}
const csv = await this.downloadService.downloadTextFile(file.url);
const targets = csv.trim().split('\n');
this.queueService.createImportFollowingToDbJob({ id: user.id }, targets);
2022-09-17 20:27:08 +02:00
this.logger.succ('Import jobs created');
}
@bindThis
public async processDb(job: Bull.Job<DbUserImportToDbJobData>): Promise<void> {
const line = job.data.target;
const user = job.data.user;
2022-09-17 20:27:08 +02:00
try {
const acct = line.split(',')[0].trim();
const { username, host } = Acct.parse(acct);
2022-09-17 20:27:08 +02:00
if (!host) return;
2022-09-17 20:27:08 +02: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 20:27:08 +02:00
if (host == null && target == null) return;
2022-09-17 20:27:08 +02:00
if (target == null) {
target = await this.remoteUserResolveService.resolveUser(username, host);
}
2022-09-17 20:27:08 +02:00
if (target == null) {
throw new Error(`Unable to resolve user: @${username}@${host}`);
}
2022-09-17 20:27:08 +02:00
// skip myself
if (target.id === job.data.user.id) return;
2022-09-17 20:27:08 +02:00
this.logger.info(`Follow ${target.id} ...`);
2022-09-17 20:27:08 +02:00
this.queueService.createFollowJob([{ from: user, to: { id: target.id }, silent: true }]);
} catch (e) {
this.logger.warn(`Error: ${e}`);
2022-09-17 20:27:08 +02:00
}
}
}