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