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