2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:50:11 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-02-16 15:09:41 +01:00
|
|
|
import { IsNull } from 'typeorm';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 07:28:29 +02:00
|
|
|
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';
|
2022-12-04 07:03:09 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-04-12 02:13:58 +02:00
|
|
|
import { QueueService } from '@/core/QueueService.js';
|
2023-05-29 04:54:49 +02:00
|
|
|
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,
|
|
|
|
|
2023-04-12 02:13:58 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-05-29 04:54:49 +02:00
|
|
|
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);
|
2023-04-12 02:13:58 +02:00
|
|
|
const targets = csv.trim().split('\n');
|
2023-10-17 13:56:17 +02:00
|
|
|
this.queueService.createImportFollowingToDbJob({ id: user.id }, targets, job.data.withReplies);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-04-12 02:13:58 +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
|
|
|
|
2023-04-12 02:13:58 +02:00
|
|
|
try {
|
|
|
|
const acct = line.split(',')[0].trim();
|
|
|
|
const { username, host } = Acct.parse(acct);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-04-12 02:13:58 +02:00
|
|
|
if (!host) return;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-04-12 02:13:58 +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
|
|
|
|
2023-04-12 02:13:58 +02:00
|
|
|
if (host == null && target == null) return;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-04-12 02:13:58 +02:00
|
|
|
if (target == null) {
|
|
|
|
target = await this.remoteUserResolveService.resolveUser(username, host);
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-04-12 02:13:58 +02:00
|
|
|
if (target == null) {
|
2023-05-29 04:54:49 +02:00
|
|
|
throw new Error(`Unable to resolve user: @${username}@${host}`);
|
2023-04-12 02:13:58 +02:00
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-04-12 02:13:58 +02:00
|
|
|
// skip myself
|
|
|
|
if (target.id === job.data.user.id) return;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-10-17 13:56:17 +02:00
|
|
|
this.logger.info(`Follow ${target.id} ${job.data.withReplies ? 'with replies' : 'without replies'} ...`);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-10-17 13:56:17 +02:00
|
|
|
this.queueService.createFollowJob([{ from: user, to: { id: target.id }, silent: true, withReplies: job.data.withReplies }]);
|
2023-04-12 02:13:58 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logger.warn(`Error: ${e}`);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|