egirlskey/src/queue/processors/db/import-user-lists.ts

71 lines
2 KiB
TypeScript
Raw Normal View History

2019-03-11 10:43:58 +00:00
import * as Bull from 'bull';
import * as mongo from 'mongodb';
import { queueLogger } from '../../logger';
2019-03-11 10:57:50 +00:00
import User from '../../../models/user';
2019-03-11 10:43:58 +00:00
import UserList from '../../../models/user-list';
import DriveFile from '../../../models/drive-file';
import { getOriginalUrl } from '../../../misc/get-drive-file-url';
import parseAcct from '../../../misc/acct/parse';
import resolveUser from '../../../remote/resolve-user';
2019-03-11 10:57:50 +00:00
import { pushUserToUserList } from '../../../services/user-list/push';
import { downloadTextFile } from '../../../misc/download-text-file';
import { isSelfHost, toDbHost } from '../../../misc/convert-host';
2019-03-11 10:43:58 +00:00
const logger = queueLogger.createSubLogger('import-user-lists');
export async function importUserLists(job: Bull.Job, done: any): Promise<void> {
logger.info(`Importing user lists of ${job.data.user._id} ...`);
const user = await User.findOne({
_id: new mongo.ObjectID(job.data.user._id.toString())
});
const file = await DriveFile.findOne({
_id: new mongo.ObjectID(job.data.fileId.toString())
});
const url = getOriginalUrl(file);
const csv = await downloadTextFile(url);
2019-03-11 10:43:58 +00:00
for (const line of csv.trim().split('\n')) {
const listName = line.split(',')[0].trim();
const { username, host } = parseAcct(line.split(',')[1].trim());
let list = await UserList.findOne({
userId: user._id,
title: listName
});
if (list == null) {
list = await UserList.insert({
createdAt: new Date(),
userId: user._id,
title: listName,
userIds: []
});
}
let target = isSelfHost(host) ? await User.findOne({
2019-03-11 10:43:58 +00:00
host: null,
usernameLower: username.toLowerCase()
}) : await User.findOne({
host: toDbHost(host),
2019-03-11 10:43:58 +00:00
usernameLower: username.toLowerCase()
});
if (host == null && target == null) continue;
if (list.userIds.some(id => id.equals(target._id))) continue;
if (target == null) {
target = await resolveUser(username, host);
}
2019-03-11 10:57:50 +00:00
pushUserToUserList(target, list);
2019-03-11 10:43:58 +00:00
}
logger.succ('Imported');
done();
}