2021-04-27 15:28:04 +03:00
|
|
|
const themeService = require('../../services/themes');
|
2021-03-03 15:47:09 +03:00
|
|
|
const limitService = require('../../services/limits');
|
2019-08-09 17:11:24 +03:00
|
|
|
const models = require('../../models');
|
|
|
|
|
2021-07-07 17:49:45 +03:00
|
|
|
// Used to emit theme.uploaded which is used in core/server/analytics-events
|
|
|
|
const events = require('../../lib/common/events');
|
|
|
|
|
2019-08-09 17:11:24 +03:00
|
|
|
module.exports = {
|
|
|
|
docName: 'themes',
|
|
|
|
|
|
|
|
browse: {
|
|
|
|
permissions: true,
|
|
|
|
query() {
|
2021-07-07 16:14:20 +03:00
|
|
|
return themeService.api.getJSON();
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
activate: {
|
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
'name'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
name: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
2021-04-09 19:30:17 +03:00
|
|
|
async query(frame) {
|
2019-08-09 17:11:24 +03:00
|
|
|
let themeName = frame.options.name;
|
2021-04-09 19:30:17 +03:00
|
|
|
|
|
|
|
if (limitService.isLimited('customThemes')) {
|
|
|
|
await limitService.errorIfWouldGoOverLimit('customThemes', {value: themeName});
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:11:24 +03:00
|
|
|
const newSettings = [{
|
|
|
|
key: 'active_theme',
|
|
|
|
value: themeName
|
|
|
|
}];
|
|
|
|
|
2021-07-07 16:14:20 +03:00
|
|
|
return themeService.api.activate(themeName)
|
2019-08-09 17:11:24 +03:00
|
|
|
.then((checkedTheme) => {
|
|
|
|
// @NOTE: we use the model, not the API here, as we don't want to trigger permissions
|
|
|
|
return models.Settings.edit(newSettings, frame.options)
|
|
|
|
.then(() => checkedTheme);
|
|
|
|
})
|
|
|
|
.then((checkedTheme) => {
|
2021-07-07 16:14:20 +03:00
|
|
|
return themeService.api.getJSON(themeName, checkedTheme);
|
2019-08-09 17:11:24 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-02-12 12:19:17 +03:00
|
|
|
install: {
|
|
|
|
headers: {},
|
|
|
|
options: [
|
|
|
|
'source',
|
|
|
|
'ref'
|
|
|
|
],
|
|
|
|
validation: {
|
2021-06-28 14:16:45 +03:00
|
|
|
options: {
|
|
|
|
source: {
|
|
|
|
required: true,
|
|
|
|
values: ['github']
|
|
|
|
},
|
|
|
|
ref: {
|
|
|
|
required: true
|
|
|
|
}
|
2021-02-12 12:19:17 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
method: 'add'
|
|
|
|
},
|
|
|
|
async query(frame) {
|
|
|
|
if (frame.options.source === 'github') {
|
2021-09-03 19:29:37 +03:00
|
|
|
const {theme, themeOverridden} = await themeService.api.installFromGithub(frame.options.ref);
|
2021-02-12 12:19:17 +03:00
|
|
|
|
2021-09-03 19:29:37 +03:00
|
|
|
if (themeOverridden) {
|
|
|
|
this.headers.cacheInvalidate = true;
|
2021-03-03 15:47:09 +03:00
|
|
|
}
|
|
|
|
|
2021-09-03 19:29:37 +03:00
|
|
|
events.emit('theme.uploaded', {name: theme.name});
|
2021-02-12 12:19:17 +03:00
|
|
|
|
2021-09-03 19:29:37 +03:00
|
|
|
return theme;
|
2021-02-12 12:19:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-08-09 17:11:24 +03:00
|
|
|
upload: {
|
|
|
|
headers: {},
|
|
|
|
permissions: {
|
|
|
|
method: 'add'
|
|
|
|
},
|
2021-03-03 15:47:09 +03:00
|
|
|
async query(frame) {
|
2021-03-04 14:34:11 +03:00
|
|
|
if (limitService.isLimited('customThemes')) {
|
2021-04-09 19:30:17 +03:00
|
|
|
// Sending a bad string to make sure it fails (empty string isn't valid)
|
|
|
|
await limitService.errorIfWouldGoOverLimit('customThemes', {value: '.'});
|
2021-03-03 15:47:09 +03:00
|
|
|
}
|
|
|
|
|
2019-08-09 17:11:24 +03:00
|
|
|
// @NOTE: consistent filename uploads
|
|
|
|
frame.options.originalname = frame.file.originalname.toLowerCase();
|
|
|
|
|
|
|
|
let zip = {
|
|
|
|
path: frame.file.path,
|
|
|
|
name: frame.file.originalname
|
|
|
|
};
|
|
|
|
|
2021-07-07 16:14:20 +03:00
|
|
|
return themeService.api.setFromZip(zip)
|
2019-08-09 17:11:24 +03:00
|
|
|
.then(({theme, themeOverridden}) => {
|
|
|
|
if (themeOverridden) {
|
|
|
|
// CASE: clear cache
|
|
|
|
this.headers.cacheInvalidate = true;
|
|
|
|
}
|
2021-02-12 12:19:17 +03:00
|
|
|
events.emit('theme.uploaded', {name: theme.name});
|
2019-08-09 17:11:24 +03:00
|
|
|
return theme;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
download: {
|
|
|
|
options: [
|
|
|
|
'name'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
name: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
method: 'read'
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
let themeName = frame.options.name;
|
|
|
|
|
2021-07-07 16:14:20 +03:00
|
|
|
return themeService.api.getZip(themeName);
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy: {
|
|
|
|
statusCode: 204,
|
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
'name'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
name: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
|
|
|
let themeName = frame.options.name;
|
|
|
|
|
2021-07-07 16:14:20 +03:00
|
|
|
return themeService.api.destroy(themeName);
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|