リストのエクスポートをできるように

#4259
This commit is contained in:
syuilo 2019-03-11 09:59:07 +09:00
parent 307a1ece7a
commit 3791fee78d
6 changed files with 105 additions and 0 deletions

View File

@ -706,6 +706,7 @@ common/views/components/profile-editor.vue:
following-list: "フォロー"
mute-list: "ミュート"
blocking-list: "ブロック"
user-lists: "リスト"
export-requested: "エクスポートをリクエストしました。これには時間がかかる場合があります。エクスポートが終わると、ドライブにファイルが追加されます。"
enter-password: "パスワードを入力してください"
danger-zone: "危険な設定"

View File

@ -97,6 +97,7 @@
<option value="following">{{ $t('export-targets.following-list') }}</option>
<option value="mute">{{ $t('export-targets.mute-list') }}</option>
<option value="blocking">{{ $t('export-targets.blocking-list') }}</option>
<option value="user-lists">{{ $t('export-targets.user-lists') }}</option>
</ui-select>
<ui-button @click="doExport()"><fa :icon="faDownload"/> {{ $t('export') }}</ui-button>
</div>
@ -284,6 +285,7 @@ export default Vue.extend({
this.exportTarget == 'following' ? 'i/export-following' :
this.exportTarget == 'mute' ? 'i/export-mute' :
this.exportTarget == 'blocking' ? 'i/export-blocking' :
this.exportTarget == 'user-lists' ? 'i/export-user-lists' :
null, {});
this.$root.dialog({

View File

@ -136,6 +136,15 @@ export function createExportBlockingJob(user: ILocalUser) {
});
}
export function createExportUserListsJob(user: ILocalUser) {
return dbQueue.add('exportUserLists', {
user: user
}, {
removeOnComplete: true,
removeOnFail: true
});
}
export default function() {
if (!program.onlyServer) {
deliverQueue.process(128, processDeliver);

View File

@ -0,0 +1,73 @@
import * as Bull from 'bull';
import * as tmp from 'tmp';
import * as fs from 'fs';
import * as mongo from 'mongodb';
import { queueLogger } from '../../logger';
import addFile from '../../../services/drive/add-file';
import User from '../../../models/user';
import dateFormat = require('dateformat');
import config from '../../../config';
import UserList from '../../../models/user-list';
const logger = queueLogger.createSubLogger('export-user-lists');
export async function exportUserLists(job: Bull.Job, done: any): Promise<void> {
logger.info(`Exporting user lists of ${job.data.user._id} ...`);
const user = await User.findOne({
_id: new mongo.ObjectID(job.data.user._id.toString())
});
const lists = await UserList.find({
userId: user._id
});
// Create temp file
const [path, cleanup] = await new Promise<[string, any]>((res, rej) => {
tmp.file((e, path, fd, cleanup) => {
if (e) return rej(e);
res([path, cleanup]);
});
});
logger.info(`Temp file is ${path}`);
const stream = fs.createWriteStream(path, { flags: 'a' });
for (const list of lists) {
const users = await User.find({
_id: { $in: list.userIds }
}, {
fields: {
username: true,
host: true
}
});
for (const u of users) {
const acct = u.host ? `${u.username}@${u.host}` : `${u.username}@${config.host}`;
const content = `${list.title},${acct}`;
await new Promise((res, rej) => {
stream.write(content + '\n', err => {
if (err) {
logger.error(err);
rej(err);
} else {
res();
}
});
});
}
}
stream.end();
logger.succ(`Exported to: ${path}`);
const fileName = 'user-lists-' + dateFormat(new Date(), 'yyyy-mm-dd-HH-MM-ss') + '.csv';
const driveFile = await addFile(user, path, fileName);
logger.succ(`Exported to: ${driveFile._id}`);
cleanup();
done();
}

View File

@ -5,6 +5,7 @@ import { exportNotes } from './export-notes';
import { exportFollowing } from './export-following';
import { exportMute } from './export-mute';
import { exportBlocking } from './export-blocking';
import { exportUserLists } from './export-user-lists';
const jobs = {
deleteNotes,
@ -13,6 +14,7 @@ const jobs = {
exportFollowing,
exportMute,
exportBlocking,
exportUserLists
} as any;
export default function(dbQueue: Bull.Queue) {

View File

@ -0,0 +1,18 @@
import define from '../../define';
import { createExportUserListsJob } from '../../../../queue';
import ms = require('ms');
export const meta = {
secure: true,
requireCredential: true,
limit: {
duration: ms('1min'),
max: 1,
},
};
export default define(meta, async (ps, user) => {
createExportUserListsJob(user);
return;
});