2021-04-27 16:18:04 +03:00
|
|
|
const events = require('../../lib/common/events');
|
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');
|
2021-01-21 11:57:52 +03:00
|
|
|
const models = require('../../models');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
docName: 'themes',
|
|
|
|
|
|
|
|
browse: {
|
|
|
|
permissions: true,
|
|
|
|
query() {
|
2021-07-07 16:14:20 +03:00
|
|
|
return themeService.api.getJSON();
|
2021-01-21 11:57:52 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
activate: {
|
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
'name'
|
|
|
|
],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
name: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: true,
|
2021-05-21 10:55:52 +03:00
|
|
|
async query(frame) {
|
2021-01-21 11:57:52 +03:00
|
|
|
let themeName = frame.options.name;
|
2021-05-21 10:55:52 +03:00
|
|
|
|
|
|
|
if (limitService.isLimited('customThemes')) {
|
|
|
|
await limitService.errorIfWouldGoOverLimit('customThemes', {value: themeName});
|
|
|
|
}
|
|
|
|
|
2021-01-21 11:57:52 +03:00
|
|
|
const newSettings = [{
|
|
|
|
key: 'active_theme',
|
|
|
|
value: themeName
|
|
|
|
}];
|
|
|
|
|
2021-07-07 16:14:20 +03:00
|
|
|
return themeService.api.activate(themeName)
|
2021-01-21 11:57:52 +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);
|
2021-01-21 11:57:52 +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-05-21 10:55:52 +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
|
|
|
}
|
|
|
|
|
2021-01-21 11:57:52 +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)
|
2021-01-21 11:57:52 +03:00
|
|
|
.then(({theme, themeOverridden}) => {
|
|
|
|
if (themeOverridden) {
|
|
|
|
// CASE: clear cache
|
|
|
|
this.headers.cacheInvalidate = true;
|
|
|
|
}
|
|
|
|
events.emit('theme.uploaded');
|
|
|
|
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);
|
2021-01-21 11:57:52 +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);
|
2021-01-21 11:57:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|