-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import * as Bull from 'bull'; | ||
|
||
import { queueLogger } from '../../logger'; | ||
import block from '../../../services/blocking/create'; | ||
import parseAcct from '../../../misc/acct/parse'; | ||
import { resolveUser } from '../../../remote/resolve-user'; | ||
import { downloadTextFile } from '../../../misc/download-text-file'; | ||
import { isSelfHost, toPuny } from '../../../misc/convert-host'; | ||
import { Users, DriveFiles } from '../../../models'; | ||
|
||
const logger = queueLogger.createSubLogger('import-blocking'); | ||
|
||
export async function importBlocking(job: Bull.Job, done: any): Promise<void> { | ||
logger.info(`Importing blocking of ${job.data.user.id} ...`); | ||
|
||
const user = await Users.findOne(job.data.user.id); | ||
if (user == null) { | ||
done(); | ||
return; | ||
} | ||
|
||
const file = await DriveFiles.findOne({ | ||
id: job.data.fileId | ||
}); | ||
if (file == null) { | ||
done(); | ||
return; | ||
} | ||
|
||
const csv = await downloadTextFile(file.url); | ||
|
||
let linenum = 0; | ||
|
||
for (const line of csv.trim().split('\n')) { | ||
linenum++; | ||
|
||
try { | ||
const acct = line.split(',')[0].trim(); | ||
const { username, host } = parseAcct(acct); | ||
|
||
let target = isSelfHost(host!) ? await Users.findOne({ | ||
host: null, | ||
usernameLower: username.toLowerCase() | ||
}) : await Users.findOne({ | ||
host: toPuny(host!), | ||
usernameLower: username.toLowerCase() | ||
}); | ||
|
||
if (host == null && target == null) continue; | ||
|
||
if (target == null) { | ||
target = await resolveUser(username, host); | ||
} | ||
|
||
if (target == null) { | ||
throw `cannot resolve user: @${username}@${host}`; | ||
} | ||
|
||
// skip myself | ||
if (target.id === job.data.user.id) continue; | ||
|
||
logger.info(`Block[${linenum}] ${target.id} ...`); | ||
|
||
block(user, target); | ||
} catch (e) { | ||
logger.warn(`Error in line:${linenum} ${e}`); | ||
} | ||
} | ||
|
||
logger.succ('Imported'); | ||
done(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import $ from 'cafy'; | ||
import { ID } from '../../../../misc/cafy-id'; | ||
import define from '../../define'; | ||
import { createImportBlockingJob } from '../../../../queue'; | ||
import ms = require('ms'); | ||
import { ApiError } from '../../error'; | ||
import { DriveFiles } from '../../../../models'; | ||
|
||
export const meta = { | ||
secure: true, | ||
requireCredential: true, | ||
limit: { | ||
duration: ms('1hour'), | ||
max: 1, | ||
}, | ||
|
||
params: { | ||
fileId: { | ||
validator: $.type(ID), | ||
} | ||
}, | ||
|
||
errors: { | ||
noSuchFile: { | ||
message: 'No such file.', | ||
code: 'NO_SUCH_FILE', | ||
id: '91235249-f185-43a4-bbdd-3d77d21cc989' | ||
}, | ||
|
||
unexpectedFileType: { | ||
message: 'We need csv file.', | ||
code: 'UNEXPECTED_FILE_TYPE', | ||
id: '2ae6ec7e-14ba-4f94-aaab-53f5593796b5' | ||
}, | ||
|
||
tooBigFile: { | ||
message: 'That file is too big.', | ||
code: 'TOO_BIG_FILE', | ||
id: 'a8ac0052-c404-4561-ab05-86b0b5e52c65' | ||
}, | ||
|
||
emptyFile: { | ||
message: 'That file is empty.', | ||
code: 'EMPTY_FILE', | ||
id: 'd650cf08-e9b9-4e3e-a337-7e0598c7b7d3' | ||
}, | ||
} | ||
}; | ||
|
||
export default define(meta, async (ps, user) => { | ||
const file = await DriveFiles.findOne(ps.fileId); | ||
|
||
if (file == null) throw new ApiError(meta.errors.noSuchFile); | ||
//if (!file.type.endsWith('/csv')) throw new ApiError(meta.errors.unexpectedFileType); | ||
if (file.size > 50000) throw new ApiError(meta.errors.tooBigFile); | ||
if (file.size === 0) throw new ApiError(meta.errors.emptyFile); | ||
|
||
createImportBlockingJob(user, file.id); | ||
}); |