Skip to content

Commit

Permalink
Add block list import function.
Browse files Browse the repository at this point in the history
  • Loading branch information
mayaeh authored and mei23 committed Jul 19, 2020
1 parent b912566 commit 0c57d2a
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/client/app/common/views/components/settings/profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
</ui-select>
<ui-horizon-group class="fit-bottom">
<ui-button @click="doExport()"><fa :icon="faDownload"/> {{ $t('export') }}</ui-button>
<ui-button @click="doImport()" :disabled="!['following', 'user-lists'].includes(exportTarget)"><fa :icon="faUpload"/> {{ $t('import') }}</ui-button>
<ui-button @click="doImport()" :disabled="!['following', 'blocking', 'user-lists'].includes(exportTarget)"><fa :icon="faUpload"/> {{ $t('import') }}</ui-button>
</ui-horizon-group>
</div>
</section>
Expand Down Expand Up @@ -375,6 +375,7 @@ export default Vue.extend({
this.$chooseDriveFile().then(file => {
this.$root.api(
this.exportTarget == 'following' ? 'i/import-following' :
this.exportTarget == 'blocking' ? 'i/import-blocking' :
this.exportTarget == 'user-lists' ? 'i/import-user-lists' :
null, {
fileId: file.id
Expand Down
10 changes: 10 additions & 0 deletions src/queue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,16 @@ export function createImportFollowingJob(user: ILocalUser, fileId: DriveFile['id
});
}

export function createImportBlockingJob(user: ILocalUser, fileId: DriveFile['id']) {
return dbQueue.add('importBlocking', {
user: user,
fileId: fileId
}, {
removeOnComplete: true,
removeOnFail: true
});
}

export function createImportUserListsJob(user: ILocalUser, fileId: DriveFile['id']) {
return dbQueue.add('importUserLists', {
user: user,
Expand Down
72 changes: 72 additions & 0 deletions src/queue/processors/db/import-blocking.ts
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();
}
2 changes: 2 additions & 0 deletions src/queue/processors/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { exportMute } from './export-mute';
import { exportBlocking } from './export-blocking';
import { exportUserLists } from './export-user-lists';
import { importFollowing } from './import-following';
import { importBlocking } from './import-blocking';
import { importUserLists } from './import-user-lists';

const jobs = {
Expand All @@ -16,6 +17,7 @@ const jobs = {
exportBlocking,
exportUserLists,
importFollowing,
importBlocking,
importUserLists
} as any;

Expand Down
59 changes: 59 additions & 0 deletions src/server/api/endpoints/i/import-blocking.ts
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);
});

0 comments on commit 0c57d2a

Please # to comment.