0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-08 02:52:39 -05:00

🐛 import subscribers (#7834)

closes #7748

- logic for import of subscribers was just missing
This commit is contained in:
Katharina Irrgang 2017-01-10 20:38:20 +01:00 committed by Kevin Ansfield
parent 8031102596
commit 5210271474
4 changed files with 60 additions and 11 deletions

View file

@ -129,6 +129,12 @@ DataImporter.prototype.doImport = function (data) {
}
return utils.importSettings(tableData.settings, t);
}).then(function (results) {
if (results) {
importResults = importResults.concat(results);
}
return utils.importSubscribers(tableData.subscribers, t);
}).then(function (results) {
if (results) {
importResults = importResults.concat(results);

View file

@ -5,7 +5,7 @@ var Promise = require('bluebird'),
globalUtils = require('../../utils'),
i18n = require('../../i18n'),
internal = {context: {internal: true}},
internalContext = {context: {internal: true}},
utils,
areEmpty,
updatedSettingKeys,
@ -36,7 +36,7 @@ stripProperties = function stripProperties(properties, data) {
};
utils = {
internal: internal,
internal: internalContext,
processUsers: function preProcessUsers(tableData, owner, existingUsers, objs) {
// We need to:
@ -202,7 +202,7 @@ utils = {
ops.push(models.Tag.findOne({name: tag.name}, {transacting: transaction}).then(function (_tag) {
if (!_tag) {
return models.Tag.add(tag, _.extend({}, internal, {transacting: transaction}))
return models.Tag.add(tag, _.extend({}, internalContext, {transacting: transaction}))
.catch(function (error) {
return Promise.reject({raw: error, model: 'tag', data: tag});
});
@ -234,7 +234,7 @@ utils = {
post.created_at = Date.now();
}
ops.push(models.Post.add(post, _.extend({}, internal, {transacting: transaction, importing: true}))
ops.push(models.Post.add(post, _.extend({}, internalContext, {transacting: transaction, importing: true}))
.catch(function (error) {
return Promise.reject({raw: error, model: 'post', data: post});
}).reflect()
@ -262,7 +262,7 @@ utils = {
user.password = globalUtils.uid(50);
user.status = 'locked';
ops.push(models.User.add(user, _.extend({}, internal, {transacting: transaction}))
ops.push(models.User.add(user, _.extend({}, internalContext, {transacting: transaction}))
.catch(function (error) {
return Promise.reject({raw: error, model: 'user', data: user});
}));
@ -292,7 +292,7 @@ utils = {
datum.key = updatedSettingKeys[datum.key] || datum.key;
});
ops.push(models.Settings.edit(tableData, _.extend({}, internal, {transacting: transaction})).catch(function (error) {
ops.push(models.Settings.edit(tableData, _.extend({}, internalContext, {transacting: transaction})).catch(function (error) {
// Ignore NotFound errors
if (!(error instanceof errors.NotFoundError)) {
return Promise.reject({raw: error, model: 'setting', data: tableData});
@ -302,6 +302,31 @@ utils = {
return Promise.all(ops);
},
importSubscribers: function importSubscribers(tableData, transaction) {
if (!tableData) {
return Promise.resolve();
}
var ops = [];
tableData = stripProperties(['id'], tableData);
_.each(tableData, function (subscriber) {
ops.push(models.Subscriber.add(subscriber, _.extend({}, internalContext, {transacting: transaction}))
.catch(function (error) {
// ignore duplicates
if (error.code && error.message.toLowerCase().indexOf('unique') === -1) {
return Promise.reject({
raw: error,
model: 'subscriber',
data: subscriber
});
}
}).reflect());
});
return Promise.all(ops);
},
/** For later **/
importApps: function importApps(tableData, transaction) {
if (!tableData) {
@ -315,7 +340,7 @@ utils = {
// Avoid duplicates
ops.push(models.App.findOne({name: app.name}, {transacting: transaction}).then(function (_app) {
if (!_app) {
return models.App.add(app, _.extend({}, internal, {transacting: transaction}))
return models.App.add(app, _.extend({}, internalContext, {transacting: transaction}))
.catch(function (error) {
return Promise.reject({raw: error, model: 'app', data: app});
});

View file

@ -127,17 +127,21 @@ describe('Import', function () {
knex('users').select(),
knex('posts').select(),
knex('settings').select(),
knex('tags').select()
knex('tags').select(),
knex('subscribers').select()
]);
}).then(function (importedData) {
should.exist(importedData);
importedData.length.should.equal(4, 'Did not get data successfully');
importedData.length.should.equal(5, 'Did not get data successfully');
var users = importedData[0],
posts = importedData[1],
settings = importedData[2],
tags = importedData[3];
tags = importedData[3],
subscribers = importedData[4];
subscribers.length.should.equal(2, 'There should be two subscribers');
// we always have 1 user, the owner user we added
users.length.should.equal(1, 'There should only be one user');

View file

@ -164,6 +164,20 @@
"post_id": 1,
"tag_id": 1
}
],
"subscribers": [
{
"id": 1,
"email": "subscriber1@test.com"
},
{
"id": 2,
"email": "subscriber2@test.com"
},
{
"id": 3,
"email": "subscriber2@test.com"
}
]
}
}
}