settings: move language and tz into cloudron.js

This commit is contained in:
Girish Ramakrishnan
2023-08-04 10:10:08 +05:30
parent ec23c7d2b8
commit 775246946a
13 changed files with 246 additions and 165 deletions
+37 -1
View File
@@ -23,7 +23,13 @@ exports = module.exports = {
runSystemChecks,
getBlockDevices
getBlockDevices,
getTimeZone,
setTimeZone,
getLanguage,
setLanguage,
};
const apps = require('./apps.js'),
@@ -41,6 +47,7 @@ const apps = require('./apps.js'),
fs = require('fs'),
logs = require('./logs.js'),
mail = require('./mail.js'),
moment = require('moment-timezone'),
network = require('./network.js'),
notifications = require('./notifications.js'),
oidc = require('./oidc.js'),
@@ -54,6 +61,7 @@ const apps = require('./apps.js'),
shell = require('./shell.js'),
tasks = require('./tasks.js'),
timers = require('timers/promises'),
translation = require('./translation.js'),
users = require('./users.js');
const REBOOT_CMD = path.join(__dirname, 'scripts/reboot.sh');
@@ -354,3 +362,31 @@ async function getBlockDevices() {
};
});
}
async function getTimeZone() {
const tz = await settings.get(settings.TIME_ZONE_KEY);
return tz || 'UTC';
}
async function setTimeZone(tz) {
assert.strictEqual(typeof tz, 'string');
if (moment.tz.names().indexOf(tz) === -1) throw new BoxError(BoxError.BAD_FIELD, 'Bad timeZone');
await settings.set(settings.TIME_ZONE_KEY, tz);
await cron.handleTimeZoneChanged(tz);
}
async function getLanguage() {
const value = await settings.get(settings.LANGUAGE_KEY);
return value || 'en';
}
async function setLanguage(language) {
assert.strictEqual(typeof language, 'string');
const languages = await translation.getLanguages();
if (languages.indexOf(language) === -1) throw new BoxError(BoxError.BAD_FIELD, 'Language not found');
await settings.set(settings.LANGUAGE_KEY, language);
}