summaryrefslogtreecommitdiff
path: root/src/queue
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2019-03-11 09:59:07 +0900
committersyuilo <syuilotan@yahoo.co.jp>2019-03-11 09:59:07 +0900
commitce0d4f77fae0452369ed430dc1866a8ec5f4b2ca (patch)
tree14892ed2d8f49a2ece4405653d837d87df4d6916 /src/queue
parentRemove debug code (diff)
downloadsharkey-ce0d4f77fae0452369ed430dc1866a8ec5f4b2ca.tar.gz
sharkey-ce0d4f77fae0452369ed430dc1866a8ec5f4b2ca.tar.bz2
sharkey-ce0d4f77fae0452369ed430dc1866a8ec5f4b2ca.zip
リストのエクスポートをできるように
#4259
Diffstat (limited to 'src/queue')
-rw-r--r--src/queue/index.ts9
-rw-r--r--src/queue/processors/db/export-user-lists.ts73
-rw-r--r--src/queue/processors/db/index.ts2
3 files changed, 84 insertions, 0 deletions
diff --git a/src/queue/index.ts b/src/queue/index.ts
index 83cebe247b..00a4a48f14 100644
--- a/src/queue/index.ts
+++ b/src/queue/index.ts
@@ -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);
diff --git a/src/queue/processors/db/export-user-lists.ts b/src/queue/processors/db/export-user-lists.ts
new file mode 100644
index 0000000000..6b5f72c15e
--- /dev/null
+++ b/src/queue/processors/db/export-user-lists.ts
@@ -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();
+}
diff --git a/src/queue/processors/db/index.ts b/src/queue/processors/db/index.ts
index 31a176cdae..8ac9c1a3d6 100644
--- a/src/queue/processors/db/index.ts
+++ b/src/queue/processors/db/index.ts
@@ -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) {