2019-03-11 11:43:58 +01:00
|
|
|
import * as Bull from 'bull';
|
|
|
|
|
|
|
|
import { queueLogger } from '../../logger';
|
|
|
|
import parseAcct from '../../../misc/acct/parse';
|
2019-04-07 16:06:07 +02:00
|
|
|
import { resolveUser } from '../../../remote/resolve-user';
|
2019-03-11 11:57:50 +01:00
|
|
|
import { pushUserToUserList } from '../../../services/user-list/push';
|
2019-03-11 12:23:29 +01:00
|
|
|
import { downloadTextFile } from '../../../misc/download-text-file';
|
2019-04-09 16:59:32 +02:00
|
|
|
import { isSelfHost, toPuny } from '../../../misc/convert-host';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { DriveFiles, Users, UserLists, UserListJoinings } from '../../../models';
|
|
|
|
import { genId } from '../../../misc/gen-id';
|
2019-03-11 11:43:58 +01:00
|
|
|
|
|
|
|
const logger = queueLogger.createSubLogger('import-user-lists');
|
|
|
|
|
|
|
|
export async function importUserLists(job: Bull.Job, done: any): Promise<void> {
|
2019-04-07 14:50:36 +02:00
|
|
|
logger.info(`Importing user lists of ${job.data.user.id} ...`);
|
2019-03-11 11:43:58 +01:00
|
|
|
|
2019-04-12 18:43:22 +02:00
|
|
|
const user = await Users.findOne(job.data.user.id);
|
|
|
|
if (user == null) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
2019-03-11 11:43:58 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
const file = await DriveFiles.findOne({
|
|
|
|
id: job.data.fileId
|
2019-03-11 11:43:58 +01:00
|
|
|
});
|
2019-04-12 18:43:22 +02:00
|
|
|
if (file == null) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
2019-03-11 11:43:58 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
const csv = await downloadTextFile(file.url);
|
2019-03-11 11:43:58 +01:00
|
|
|
|
|
|
|
for (const line of csv.trim().split('\n')) {
|
|
|
|
const listName = line.split(',')[0].trim();
|
|
|
|
const { username, host } = parseAcct(line.split(',')[1].trim());
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
let list = await UserLists.findOne({
|
|
|
|
userId: user.id,
|
|
|
|
name: listName
|
2019-03-11 11:43:58 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (list == null) {
|
2019-04-07 14:50:36 +02:00
|
|
|
list = await UserLists.save({
|
|
|
|
id: genId(),
|
2019-03-11 11:43:58 +01:00
|
|
|
createdAt: new Date(),
|
2019-04-07 14:50:36 +02:00
|
|
|
userId: user.id,
|
|
|
|
name: listName,
|
2019-03-11 11:43:58 +01:00
|
|
|
userIds: []
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-12 18:43:22 +02:00
|
|
|
let target = isSelfHost(host!) ? await Users.findOne({
|
2019-03-11 11:43:58 +01:00
|
|
|
host: null,
|
|
|
|
usernameLower: username.toLowerCase()
|
2019-04-07 14:50:36 +02:00
|
|
|
}) : await Users.findOne({
|
2019-04-12 18:43:22 +02:00
|
|
|
host: toPuny(host!),
|
2019-03-11 11:43:58 +01:00
|
|
|
usernameLower: username.toLowerCase()
|
|
|
|
});
|
|
|
|
|
|
|
|
if (target == null) {
|
|
|
|
target = await resolveUser(username, host);
|
|
|
|
}
|
|
|
|
|
2019-04-12 18:43:22 +02:00
|
|
|
if (await UserListJoinings.findOne({ userListId: list.id, userId: target.id }) != null) continue;
|
|
|
|
|
2019-03-11 11:57:50 +01:00
|
|
|
pushUserToUserList(target, list);
|
2019-03-11 11:43:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.succ('Imported');
|
|
|
|
done();
|
|
|
|
}
|