diff --git a/migrations/20160528090040-mailboxes-import-existing.js b/migrations/20160528090040-mailboxes-import-existing.js new file mode 100644 index 000000000..ab64173bd --- /dev/null +++ b/migrations/20160528090040-mailboxes-import-existing.js @@ -0,0 +1,38 @@ +var dbm = global.dbm || require('db-migrate'); +var type = dbm.dataType; + +// imports mailbox entries for existing users and apps +exports.up = function(db, callback) { + async.series([ + db.runSql.bind(db, 'START TRANSACTION;'), + function addUserMailboxes(done) { + db.all('SELECT username FROM users', function (error, results) { + if (error) return done(error); + + console.dir(results); + + async.eachSeries(results, function (r, next) { + db.runSql('INSERT INTO mailboxes (name) VALUES (?)', [ r.username ], next); + }, done); + }); + }, + function addAppMailboxes(done) { + db.all('SELECT location, manifestJson FROM apps', function (error, results) { + if (error) return done(error); + + console.dir(results); + + async.eachSeries(results, function (r, next) { + var app = { location: r.location, manifest: JSON.parse(r.manifestJson) }; + var from = (app.location ? app.location : app.manifest.title.replace(/[^a-zA-Z0-9]/, '')) + '.app'; + db.runSql('INSERT INTO mailboxes (name) VALUES (?)', [ from ], next); + }, done); + }); + }, + db.runSql.bind(db, 'COMMIT') + ], callback); +}; + +exports.down = function(db, callback) { + callback(); +}; diff --git a/src/addons.js b/src/addons.js index 62f462aad..5e502aa35 100644 --- a/src/addons.js +++ b/src/addons.js @@ -436,7 +436,6 @@ function setupSendMail(app, options, callback) { assert.strictEqual(typeof options, 'object'); assert.strictEqual(typeof callback, 'function'); - // FIXME: to can conflict with a real user! var from = (app.location ? app.location : app.manifest.title.replace(/[^a-zA-Z0-9]/, '')) + '.app'; var cmd = [ '/addons/mail/service.sh', 'add-send', from ]; @@ -457,7 +456,6 @@ function teardownSendMail(app, options, callback) { debugApp(app, 'Tearing down sendmail'); - // FIXME: to can conflict with a real user! var from = (app.location ? app.location : app.manifest.title.replace(/[^a-zA-Z0-9]/, '')) + '.app'; var cmd = [ '/addons/mail/service.sh', 'remove-send', from ]; @@ -478,7 +476,6 @@ function setupRecvMail(app, options, callback) { debugApp(app, 'Setting up recvmail'); - // FIXME: to can conflict with a real user! var to = (app.location ? app.location : app.manifest.title.replace(/[^a-zA-Z0-9]/, '')) + '.app'; var cmd = [ '/addons/mail/service.sh', 'add-recv', to ]; @@ -497,7 +494,6 @@ function teardownRecvMail(app, options, callback) { assert.strictEqual(typeof options, 'object'); assert.strictEqual(typeof callback, 'function'); - // FIXME: to can conflict with a real user! var to = (app.location ? app.location : app.manifest.title.replace(/[^a-zA-Z0-9]/, '')) + '.app'; var cmd = [ '/addons/mail/service.sh', 'remove-recv', to ];