2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { IsNull, MoreThan } from 'typeorm';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-22 21:21:31 +00:00
|
|
|
import type { UsersRepository, DriveFilesRepository } from '@/models/index.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { Config } from '@/config.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 { UserFollowingService } from '@/core/UserFollowingService.js';
|
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
|
|
|
import { QueueLoggerService } from '../QueueLoggerService.js';
|
|
|
|
import type Bull from 'bull';
|
|
|
|
import type { DbUserImportJobData } from '../types.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ImportFollowingProcessorService {
|
2022-09-18 18:11:50 +00:00
|
|
|
private logger: Logger;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
|
|
|
private utilityService: UtilityService,
|
|
|
|
private userFollowingService: UserFollowingService,
|
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-following');
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +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 following 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);
|
|
|
|
|
|
|
|
let linenum = 0;
|
|
|
|
|
|
|
|
for (const line of csv.trim().split('\n')) {
|
|
|
|
linenum++;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const acct = line.split(',')[0].trim();
|
|
|
|
const { username, host } = Acct.parse(acct);
|
|
|
|
|
|
|
|
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(),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (host == null && target == null) continue;
|
|
|
|
|
|
|
|
if (target == null) {
|
2022-12-04 01:16:03 +00:00
|
|
|
target = await this.remoteUserResolveService.resolveUser(username, host);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (target == null) {
|
|
|
|
throw `cannot resolve user: @${username}@${host}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip myself
|
|
|
|
if (target.id === job.data.user.id) continue;
|
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger.info(`Follow[${linenum}] ${target.id} ...`);
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
this.userFollowingService.follow(user, target);
|
|
|
|
} catch (e) {
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger.warn(`Error in line:${linenum} ${e}`);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger.succ('Imported');
|
2022-09-17 18:27:08 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
}
|