diff --git a/src/controllers/user.js b/src/controllers/user.js index 3405c1ea1d..edb7202b39 100644 --- a/src/controllers/user.js +++ b/src/controllers/user.js @@ -1,9 +1,12 @@ 'use strict'; var async = require('async'); +var converter = require('json-2-csv'); var user = require('../user'); var meta = require('../meta'); +var posts = require('../posts'); +var batch = require('../batch'); var accountHelpers = require('./accounts/helpers'); var userController = module.exports; @@ -97,3 +100,36 @@ userController.getUserDataByUID = function (callerUid, uid, callback) { callback(null, results.userData); }); }; + +userController.exportPosts = function (req, res, next) { + async.waterfall([ + function (next) { + var payload = []; + batch.processSortedSet('uid:' + req.params.uid + ':posts', function (pids, next) { + async.map(pids, posts.getPostData, function (err, posts) { + if (err) { + return next(err); + } + + // Convert newlines in content + posts = posts.map(function (post) { + post.content = '"' + post.content.replace(/\n/g, '\\n').replace(/"/g, '\\"') + '"'; + return post; + }); + + payload = payload.concat(posts); + next(); + }); + }, function (err) { + next(err, payload); + }); + }, + async.apply(converter.json2csv), + ], function (err, csv) { + if (err) { + return next(err); + } + + res.set('Content-Type', 'text/csv').set('Content-Disposition', 'attachment; filename="' + req.params.uid + '_posts.csv"').send(csv); + }); +}; diff --git a/src/routes/api.js b/src/routes/api.js index 34f14f5a8c..0e531b7236 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -15,6 +15,8 @@ module.exports = function (app, middleware, controllers) { router.get('/user/username/:username', middleware.checkGlobalPrivacySettings, controllers.user.getUserByUsername); router.get('/user/email/:email', middleware.checkGlobalPrivacySettings, controllers.user.getUserByEmail); + router.get('/user/uid/:uid/export/posts', middleware.checkAccountPermissions, controllers.user.exportPosts); + router.get('/:type/pid/:id', controllers.api.getObject); router.get('/:type/tid/:id', controllers.api.getObject); router.get('/:type/cid/:id', controllers.api.getObject);