Skip to content

Commit

Permalink
Fix #341
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Nov 23, 2019
1 parent 2870d17 commit 8d59770
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions src/queue/processors/db/import-user-lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,48 @@ export async function importUserLists(job: Bull.Job, done: any): Promise<void> {

const csv = await downloadTextFile(url);

let linenum = 0;

for (const line of csv.trim().split('\n')) {
const listName = line.split(',')[0].trim();
const { username, host } = parseAcct(line.split(',')[1].trim());
linenum++;

let list = await UserList.findOne({
userId: user._id,
title: listName
});
try {
const listName = line.split(',')[0].trim();
const { username, host } = parseAcct(line.split(',')[1].trim());

if (list == null) {
list = await UserList.insert({
createdAt: new Date(),
let list = await UserList.findOne({
userId: user._id,
title: listName,
userIds: []
title: listName
});
}

let target = isSelfHost(host) ? await User.findOne({
host: null,
usernameLower: username.toLowerCase()
}) : await User.findOne({
host: toDbHost(host),
usernameLower: username.toLowerCase()
});
if (list == null) {
list = await UserList.insert({
createdAt: new Date(),
userId: user._id,
title: listName,
userIds: []
});
}

let target = isSelfHost(host) ? await User.findOne({
host: null,
usernameLower: username.toLowerCase()
}) : await User.findOne({
host: toDbHost(host),
usernameLower: username.toLowerCase()
});

if (host == null && target == null) continue;
if (list.userIds.some(id => id.equals(target._id))) continue;
if (host == null && target == null) continue;
if (list.userIds.some(id => id.equals(target._id))) continue;

if (target == null) {
target = await resolveUser(username, host);
}
if (target == null) {
target = await resolveUser(username, host);
}

pushUserToUserList(target, list);
pushUserToUserList(target, list);
} catch (e) {
logger.warn(`Error in line:${linenum} ${e}`);
}
}

logger.succ('Imported');
Expand Down

0 comments on commit 8d59770

Please sign in to comment.