2019-07-17 13:28:16 +03:00
|
|
|
const _ = require('lodash');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../shared/config');
|
2020-04-30 22:26:12 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-10-06 11:42:44 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
2021-06-15 17:36:27 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
2019-07-17 13:28:16 +03:00
|
|
|
const models = require('../../models');
|
|
|
|
const mail = require('../mail');
|
|
|
|
|
2021-10-06 11:42:44 +03:00
|
|
|
const messages = {
|
|
|
|
setupAlreadyCompleted: 'Setup has already been completed.',
|
|
|
|
setupMustBeCompleted: 'Setup must be completed before making this request.',
|
|
|
|
setupUnableToRun: 'Database missing fixture data. Please reset database and try again.',
|
|
|
|
sampleBlogDescription: 'Thoughts, stories and ideas.',
|
|
|
|
yourNewGhostBlog: 'Your New Ghost Site',
|
|
|
|
unableToSendWelcomeEmail: 'Unable to send welcome email, your site will continue to function.'
|
|
|
|
};
|
|
|
|
|
2019-07-17 13:28:16 +03:00
|
|
|
/**
|
|
|
|
* Returns setup status
|
|
|
|
*
|
|
|
|
* @return {Promise<Boolean>}
|
|
|
|
*/
|
|
|
|
async function checkIsSetup() {
|
|
|
|
return models.User.isSetup();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows an assertion to be made about setup status.
|
|
|
|
*
|
|
|
|
* @param {Boolean} status True: setup must be complete. False: setup must not be complete.
|
|
|
|
* @return {Function} returns a "task ready" function
|
|
|
|
*/
|
|
|
|
function assertSetupCompleted(status) {
|
|
|
|
return async function checkPermission(__) {
|
|
|
|
const isSetup = await checkIsSetup();
|
|
|
|
|
|
|
|
if (isSetup === status) {
|
|
|
|
return __;
|
|
|
|
}
|
|
|
|
|
2021-10-06 11:42:44 +03:00
|
|
|
const completed = tpl(messages.setupAlreadyCompleted);
|
|
|
|
const notCompleted = tpl(messages.setupMustBeCompleted);
|
2019-07-17 13:28:16 +03:00
|
|
|
|
|
|
|
function throwReason(reason) {
|
2020-04-30 22:26:12 +03:00
|
|
|
throw new errors.NoPermissionError({message: reason});
|
2019-07-17 13:28:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isSetup) {
|
|
|
|
throwReason(completed);
|
|
|
|
} else {
|
|
|
|
throwReason(notCompleted);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setupUser(userData) {
|
|
|
|
const context = {context: {internal: true}};
|
|
|
|
|
|
|
|
const owner = await models.User.findOne({role: 'Owner', status: 'all'});
|
|
|
|
|
|
|
|
if (!owner) {
|
2021-12-01 13:22:01 +03:00
|
|
|
throw new errors.InternalServerError({
|
2021-10-06 11:42:44 +03:00
|
|
|
message: tpl(messages.setupUnableToRun)
|
2019-07-17 13:28:16 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await models.User.setup(userData, _.extend({id: owner.id}, context));
|
|
|
|
|
|
|
|
return {
|
|
|
|
user: user,
|
|
|
|
userData: userData
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function doSettings(data, settingsAPI) {
|
|
|
|
const context = {context: {user: data.user.id}};
|
|
|
|
const user = data.user;
|
|
|
|
const blogTitle = data.userData.blogTitle;
|
|
|
|
|
|
|
|
let userSettings;
|
|
|
|
|
|
|
|
if (!blogTitle || typeof blogTitle !== 'string') {
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
|
|
|
userSettings = [
|
|
|
|
{key: 'title', value: blogTitle.trim()},
|
2021-10-06 11:42:44 +03:00
|
|
|
{key: 'description', value: tpl(messages.sampleBlogDescription)}
|
2019-07-17 13:28:16 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
await settingsAPI.edit({settings: userSettings}, context);
|
|
|
|
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2021-05-09 13:12:50 +03:00
|
|
|
async function doProduct(data, productsAPI) {
|
|
|
|
const context = {context: {user: data.user.id}};
|
|
|
|
const user = data.user;
|
|
|
|
const blogTitle = data.userData.blogTitle;
|
|
|
|
|
|
|
|
if (!blogTitle || typeof blogTitle !== 'string') {
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const page = await productsAPI.browse({limit: 1});
|
|
|
|
|
|
|
|
const [product] = page.products;
|
|
|
|
if (!product) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
productsAPI.edit({products: [{name: blogTitle.trim()}]}, {context: context.context, id: product.id});
|
|
|
|
} catch (e) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-07-30 17:15:53 +03:00
|
|
|
function sendWelcomeEmail(email, mailAPI) {
|
2019-07-17 13:28:16 +03:00
|
|
|
if (config.get('sendWelcomeEmail')) {
|
2019-07-30 17:13:53 +03:00
|
|
|
const data = {
|
|
|
|
ownerEmail: email
|
|
|
|
};
|
|
|
|
|
2019-07-17 13:28:16 +03:00
|
|
|
return mail.utils.generateContent({data: data, template: 'welcome'})
|
|
|
|
.then((content) => {
|
|
|
|
const message = {
|
2020-04-29 18:44:27 +03:00
|
|
|
to: email,
|
2021-10-06 11:42:44 +03:00
|
|
|
subject: tpl(messages.yourNewGhostBlog),
|
2020-04-29 18:44:27 +03:00
|
|
|
html: content.html,
|
|
|
|
text: content.text
|
|
|
|
};
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
mail: [{
|
|
|
|
message: message,
|
|
|
|
options: {}
|
|
|
|
}]
|
|
|
|
};
|
2019-07-17 13:28:16 +03:00
|
|
|
|
|
|
|
mailAPI.send(payload, {context: {internal: true}})
|
|
|
|
.catch((err) => {
|
2021-10-06 11:42:44 +03:00
|
|
|
err.context = tpl(messages.unableToSendWelcomeEmail);
|
2020-04-30 22:26:12 +03:00
|
|
|
logging.error(err);
|
2019-07-17 13:28:16 +03:00
|
|
|
});
|
2019-07-30 17:13:53 +03:00
|
|
|
});
|
2019-07-17 13:28:16 +03:00
|
|
|
}
|
2019-08-20 13:13:43 +03:00
|
|
|
return Promise.resolve();
|
2019-07-17 13:28:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
checkIsSetup: checkIsSetup,
|
|
|
|
assertSetupCompleted: assertSetupCompleted,
|
|
|
|
setupUser: setupUser,
|
|
|
|
doSettings: doSettings,
|
2021-05-09 13:12:50 +03:00
|
|
|
doProduct: doProduct,
|
2019-07-30 17:15:53 +03:00
|
|
|
sendWelcomeEmail: sendWelcomeEmail
|
2019-07-17 13:28:16 +03:00
|
|
|
};
|