2019-03-11 15:34:19 +00:00
|
|
|
import * as Bull from 'bull';
|
|
|
|
|
2021-08-19 12:55:45 +00:00
|
|
|
import { queueLogger } from '../../logger';
|
|
|
|
import follow from '@/services/following/create';
|
2021-11-11 17:02:25 +00:00
|
|
|
import * as Acct from 'misskey-js/built/acct';
|
2021-08-19 12:55:45 +00:00
|
|
|
import { resolveUser } from '@/remote/resolve-user';
|
|
|
|
import { downloadTextFile } from '@/misc/download-text-file';
|
|
|
|
import { isSelfHost, toPuny } from '@/misc/convert-host';
|
|
|
|
import { Users, DriveFiles } from '@/models/index';
|
|
|
|
import { DbUserImportJobData } from '@/queue/types';
|
2019-03-11 15:34:19 +00:00
|
|
|
|
|
|
|
const logger = queueLogger.createSubLogger('import-following');
|
|
|
|
|
2021-05-08 09:56:21 +00:00
|
|
|
export async function importFollowing(job: Bull.Job<DbUserImportJobData>, done: any): Promise<void> {
|
2019-04-07 12:50:36 +00:00
|
|
|
logger.info(`Importing following of ${job.data.user.id} ...`);
|
2019-03-11 15:34:19 +00:00
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
const user = await Users.findOne(job.data.user.id);
|
|
|
|
if (user == null) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
2019-03-11 15:34:19 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
const file = await DriveFiles.findOne({
|
2021-12-09 14:58:30 +00:00
|
|
|
id: job.data.fileId,
|
2019-03-11 15:34:19 +00:00
|
|
|
});
|
2019-04-12 16:43:22 +00:00
|
|
|
if (file == null) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
2019-03-11 15:34:19 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
const csv = await downloadTextFile(file.url);
|
2019-03-11 15:34:19 +00:00
|
|
|
|
2019-04-05 15:41:08 +00:00
|
|
|
let linenum = 0;
|
|
|
|
|
2019-03-11 15:34:19 +00:00
|
|
|
for (const line of csv.trim().split('\n')) {
|
2019-04-05 15:41:08 +00:00
|
|
|
linenum++;
|
2019-03-11 15:34:19 +00:00
|
|
|
|
2019-04-05 15:41:08 +00:00
|
|
|
try {
|
2019-05-03 09:33:25 +00:00
|
|
|
const acct = line.split(',')[0].trim();
|
2021-11-11 17:02:25 +00:00
|
|
|
const { username, host } = Acct.parse(acct);
|
2019-03-11 15:34:19 +00:00
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
let target = isSelfHost(host!) ? await Users.findOne({
|
2019-04-05 15:41:08 +00:00
|
|
|
host: null,
|
2021-12-09 14:58:30 +00:00
|
|
|
usernameLower: username.toLowerCase(),
|
2019-04-07 12:50:36 +00:00
|
|
|
}) : await Users.findOne({
|
2019-04-12 16:43:22 +00:00
|
|
|
host: toPuny(host!),
|
2021-12-09 14:58:30 +00:00
|
|
|
usernameLower: username.toLowerCase(),
|
2019-04-05 15:41:08 +00:00
|
|
|
});
|
2019-03-11 15:34:19 +00:00
|
|
|
|
2019-04-05 15:41:08 +00:00
|
|
|
if (host == null && target == null) continue;
|
|
|
|
|
|
|
|
if (target == null) {
|
|
|
|
target = await resolveUser(username, host);
|
|
|
|
}
|
2019-03-11 15:34:19 +00:00
|
|
|
|
2019-04-05 15:41:08 +00:00
|
|
|
if (target == null) {
|
|
|
|
throw `cannot resolve user: @${username}@${host}`;
|
|
|
|
}
|
2019-04-05 15:07:23 +00:00
|
|
|
|
2019-04-05 15:41:08 +00:00
|
|
|
// skip myself
|
2019-04-07 12:50:36 +00:00
|
|
|
if (target.id === job.data.user.id) continue;
|
2019-03-12 04:12:49 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
logger.info(`Follow[${linenum}] ${target.id} ...`);
|
2019-04-05 15:41:08 +00:00
|
|
|
|
|
|
|
follow(user, target);
|
|
|
|
} catch (e) {
|
|
|
|
logger.warn(`Error in line:${linenum} ${e}`);
|
|
|
|
}
|
2019-03-11 15:34:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.succ('Imported');
|
|
|
|
done();
|
|
|
|
}
|