mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 01:41:46 +03:00
c5f8853ad9
refs https://github.com/TryGhost/Team/issues/1362 - Casper is already installed, so the installation from github always fail. - There is no need to display an error message in that case. Added regression tests for blog setup with the default theme: - Check whether there are no notifications after completing the setup - Also test the setup with the default theme
210 lines
6.0 KiB
JavaScript
210 lines
6.0 KiB
JavaScript
const _ = require('lodash');
|
|
const config = require('../../../shared/config');
|
|
const errors = require('@tryghost/errors');
|
|
const tpl = require('@tryghost/tpl');
|
|
const logging = require('@tryghost/logging');
|
|
const models = require('../../models');
|
|
const mail = require('../mail');
|
|
|
|
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.',
|
|
failedThemeInstall: 'Theme {themeName} didn\'t install because of the error: {error}'
|
|
};
|
|
|
|
/**
|
|
* 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 __;
|
|
}
|
|
|
|
const completed = tpl(messages.setupAlreadyCompleted);
|
|
const notCompleted = tpl(messages.setupMustBeCompleted);
|
|
|
|
function throwReason(reason) {
|
|
throw new errors.NoPermissionError({message: reason});
|
|
}
|
|
|
|
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) {
|
|
throw new errors.InternalServerError({
|
|
message: tpl(messages.setupUnableToRun)
|
|
});
|
|
}
|
|
|
|
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;
|
|
const description = data.userData.description ? data.userData.description.trim() : null;
|
|
|
|
let userSettings;
|
|
|
|
if (!blogTitle || typeof blogTitle !== 'string') {
|
|
return user;
|
|
}
|
|
|
|
userSettings = [
|
|
{key: 'title', value: blogTitle.trim()},
|
|
{key: 'description', value: description || tpl(messages.sampleBlogDescription)}
|
|
];
|
|
|
|
if (data.userData.accentColor) {
|
|
userSettings.push({
|
|
key: 'accent_color', value: data.userData.accentColor
|
|
});
|
|
}
|
|
|
|
await settingsAPI.edit({settings: userSettings}, context);
|
|
|
|
return user;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
function sendWelcomeEmail(email, mailAPI) {
|
|
if (config.get('sendWelcomeEmail')) {
|
|
const data = {
|
|
ownerEmail: email
|
|
};
|
|
|
|
return mail.utils.generateContent({data: data, template: 'welcome'})
|
|
.then((content) => {
|
|
const message = {
|
|
to: email,
|
|
subject: tpl(messages.yourNewGhostBlog),
|
|
html: content.html,
|
|
text: content.text
|
|
};
|
|
|
|
const payload = {
|
|
mail: [{
|
|
message: message,
|
|
options: {}
|
|
}]
|
|
};
|
|
|
|
mailAPI.send(payload, {context: {internal: true}})
|
|
.catch((err) => {
|
|
err.context = tpl(messages.unableToSendWelcomeEmail);
|
|
logging.error(err);
|
|
});
|
|
});
|
|
}
|
|
return Promise.resolve();
|
|
}
|
|
|
|
async function installTheme(data, api) {
|
|
const {theme: themeName} = data.userData;
|
|
|
|
if (!themeName) {
|
|
return data;
|
|
}
|
|
|
|
if (themeName.toLowerCase() === 'tryghost/casper') {
|
|
logging.warn('Skipping theme install as Casper is the default theme.');
|
|
return data;
|
|
}
|
|
|
|
// Use the api instead of the services as the api performs extra logic
|
|
try {
|
|
const installResults = await api.themes.install({
|
|
source: 'github',
|
|
ref: themeName,
|
|
context: {internal: true}
|
|
});
|
|
const theme = installResults.themes[0];
|
|
|
|
await api.themes.activate({
|
|
name: theme.name,
|
|
context: {internal: true}
|
|
});
|
|
} catch (error) {
|
|
//Fallback to Casper by doing nothing as the theme setting update is the last step
|
|
logging.warn(tpl(messages.failedThemeInstall, {themeName, error: error.message}));
|
|
|
|
await api.notifications.add({
|
|
notifications: [{
|
|
custom: true, //avoids update-check from deleting the notification
|
|
type: 'warn',
|
|
message: 'The installation of the theme you have selected wasn\'t successful.'
|
|
}]
|
|
}, {context: {internal: true}});
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
module.exports = {
|
|
checkIsSetup: checkIsSetup,
|
|
assertSetupCompleted: assertSetupCompleted,
|
|
setupUser: setupUser,
|
|
doSettings: doSettings,
|
|
doProduct: doProduct,
|
|
sendWelcomeEmail: sendWelcomeEmail,
|
|
installTheme: installTheme
|
|
};
|