Move config reading to top-level
| @@ -11,6 +11,10 @@ import _ from 'lodash'; | |||
| 11 | import { jsonParser, urlencodedParser } from '../express-common.js'; | 11 | import { jsonParser, urlencodedParser } from '../express-common.js'; |
| 12 | import { getConfigValue, humanizedISO8601DateTime, tryParse, generateTimestamp, removeOldBackups } from '../util.js'; | 12 | import { getConfigValue, humanizedISO8601DateTime, tryParse, generateTimestamp, removeOldBackups } from '../util.js'; |
| 13 | 13 | ||
| 14 | const isBackupDisabled = getConfigValue('disableChatBackup', false); | ||
| 15 | const maxTotalChatBackups = Number(getConfigValue('maxTotalChatBackups', -1)); | ||
| 16 | const throttleInterval = getConfigValue('chatBackupThrottleInterval', 10_000); | ||
| 17 | |||
| 14 | /** | 18 | /** |
| 15 | * Saves a chat to the backups directory. | 19 | * Saves a chat to the backups directory. |
| 16 | * @param {string} directory The user's backups directory. | 20 | * @param {string} directory The user's backups directory. |
| @@ -19,7 +23,6 @@ import { getConfigValue, humanizedISO8601DateTime, tryParse, generateTimestamp, | |||
| 19 | */ | 23 | */ |
| 20 | function backupChat(directory, name, chat) { | 24 | function backupChat(directory, name, chat) { |
| 21 | try { | 25 | try { |
| 22 | const isBackupDisabled = getConfigValue('disableChatBackup', false); | ||
| 23 | 26 | ||
| 24 | if (isBackupDisabled) { | 27 | if (isBackupDisabled) { |
| 25 | return; | 28 | return; |
| @@ -33,7 +36,6 @@ function backupChat(directory, name, chat) { | |||
| 33 | 36 | ||
| 34 | removeOldBackups(directory, `chat_${name}_`); | 37 | removeOldBackups(directory, `chat_${name}_`); |
| 35 | 38 | ||
| 36 | const maxTotalChatBackups = Number(getConfigValue('maxTotalChatBackups', -1)); | ||
| 37 | if (isNaN(maxTotalChatBackups) || maxTotalChatBackups < 0) { | 39 | if (isNaN(maxTotalChatBackups) || maxTotalChatBackups < 0) { |
| 38 | return; | 40 | return; |
| 39 | } | 41 | } |
| @@ -52,7 +54,6 @@ const backupFunctions = new Map(); | |||
| 52 | * @returns {function(string, string, string): void} Backup function | 54 | * @returns {function(string, string, string): void} Backup function |
| 53 | */ | 55 | */ |
| 54 | function getBackupFunction(handle) { | 56 | function getBackupFunction(handle) { |
| 55 | const throttleInterval = getConfigValue('chatBackupThrottleInterval', 10_000); | ||
| 56 | if (!backupFunctions.has(handle)) { | 57 | if (!backupFunctions.has(handle)) { |
| 57 | backupFunctions.set(handle, _.throttle(backupChat, throttleInterval, { leading: true, trailing: true })); | 58 | backupFunctions.set(handle, _.throttle(backupChat, throttleInterval, { leading: true, trailing: true })); |
| 58 | } | 59 | } |