From 57e0e60ee44667ca76c5e4df764bfff0175e067e Mon Sep 17 00:00:00 2001 From: Alexander Danilov Date: Thu, 1 Feb 2024 16:25:45 +0600 Subject: [PATCH] Change _update_check_interval values from hours to seconds according to documentation in setUpdateCheckInterval() --- src/backup.js | 2 +- src/migrations.js | 25 ++++++++++++++++++++++--- src/worker.js | 6 +++--- test/manager.0.base.spec.js | 4 ++++ test/migrations.spec.js | 5 +++++ 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/backup.js b/src/backup.js index 5db5b05..e4c21b1 100644 --- a/src/backup.js +++ b/src/backup.js @@ -47,7 +47,7 @@ export const exportIitcSettings = (all_storage) => { // Loop through all_storage and check if the keys are present in storage_keys // If present, add them to the iitc_settings object for (const key in all_storage) { - if (storage_keys.includes(key)) { + if (storage_keys.includes(key) && isSet(all_storage[key])) { iitc_settings[key] = all_storage[key]; } } diff --git a/src/migrations.js b/src/migrations.js index 767869c..cf1c6a4 100644 --- a/src/migrations.js +++ b/src/migrations.js @@ -6,7 +6,7 @@ export function number_of_migrations() { return migrates.length; } -const migrates = [migration_0001, migration_0002, migration_0003, migration_0004]; +const migrates = [migration_0001, migration_0002, migration_0003, migration_0004, migration_0005]; export async function migrate(storage) { const storage_iitc_code = await storage.get(['release_iitc_code', 'beta_iitc_code', 'custom_iitc_code']); @@ -25,6 +25,12 @@ export async function migrate(storage) { 'local_plugins_user', ]); const storage_misc = await storage.get(['channel', 'network_host', 'lastversion', 'storage_version']); + const update_check_interval = await storage.get([ + 'release_update_check_interval', + 'beta_update_check_interval', + 'custom_update_check_interval', + 'external_update_check_interval', + ]); if (!isSet(storage_misc['storage_version']) && isSet(storage_misc['lastversion'])) { storage_misc['storage_version'] = 0; @@ -34,13 +40,13 @@ export async function migrate(storage) { for (const migrate of migrates) { const index = migrates.indexOf(migrate); if (parseInt(storage_misc['storage_version']) < index + 1) { - await migrate(storage_iitc_code, storage_plugins_flat, storage_plugins_user, storage_misc); + await migrate(storage_iitc_code, storage_plugins_flat, storage_plugins_user, storage_misc, update_check_interval); is_migrated = true; } } storage_misc['storage_version'] = migrates.length; - await storage.set({ ...storage_iitc_code, ...storage_plugins_flat, ...storage_plugins_user, ...storage_misc }); + await storage.set({ ...storage_iitc_code, ...storage_plugins_flat, ...storage_plugins_user, ...storage_misc, ...update_check_interval }); return is_migrated; } @@ -96,3 +102,16 @@ async function migration_0004(storage_iitc_code) { } } } + +async function migration_0005(storage_iitc_code, storage_plugins_flat, storage_plugins_user, storage_misc, update_check_interval) { + for (let channel of Object.keys(update_check_interval)) { + const interval = update_check_interval[channel]; + if (!isSet(interval)) { + delete update_check_interval[channel]; + continue; + } + if (interval !== 24 * 60 * 60) { + update_check_interval[channel] = interval * 60 * 60; + } + } +} diff --git a/src/worker.js b/src/worker.js index bf86b39..dd6763e 100644 --- a/src/worker.js +++ b/src/worker.js @@ -263,7 +263,7 @@ export class Worker { async _getUrl(url, variant, retry) { if (retry > 1) { let seconds = retry * retry; - if (seconds > 60 * 60 * 24) seconds = 60 * 60 * 24; + if (seconds > 30 * 60) seconds = 30 * 60; // maximum is 30 minutes try { this.message('serverNotAvailableRetry', String(seconds)); } catch { @@ -311,7 +311,7 @@ export class Worker { this.channel + '_plugins_user', ]); - let update_check_interval = storage[this.channel + '_update_check_interval'] * 60 * 60; + let update_check_interval = storage[this.channel + '_update_check_interval']; if (!update_check_interval) update_check_interval = 24 * 60 * 60; if (!isSet(storage[this.channel + '_last_modified']) || !isSet(storage.last_check_update)) { @@ -455,7 +455,7 @@ export class Worker { async _checkExternalUpdates(force) { const local = await this.storage.get(['channel', 'last_check_external_update', 'external_update_check_interval', this.channel + '_plugins_user']); - let update_check_interval = local['external_update_check_interval'] * 60 * 60; + let update_check_interval = local['external_update_check_interval']; if (!update_check_interval) { update_check_interval = 24 * 60 * 60; } diff --git a/test/manager.0.base.spec.js b/test/manager.0.base.spec.js index 9375063..a2a891b 100644 --- a/test/manager.0.base.spec.js +++ b/test/manager.0.base.spec.js @@ -57,6 +57,10 @@ describe('manage.js base integration tests', function () { const fn = await manager.setUpdateCheckInterval(24 * 60 * 60, 'release'); expect(fn).to.be.undefined; }); + it('Should set correct interval in seconds', async function () { + const interval = await storage.get(['release_update_check_interval']).then((data) => data.release_update_check_interval); + expect(interval).to.equal(24 * 60 * 60); + }); }); describe('inject', function () { diff --git a/test/migrations.spec.js b/test/migrations.spec.js index c5b4b62..8ae87f7 100644 --- a/test/migrations.spec.js +++ b/test/migrations.spec.js @@ -34,6 +34,7 @@ describe('test migrations', function () { category: 'Controls', }, }, + release_update_check_interval: 6, lastversion: '1.7.0', storage_version: 0, }); @@ -62,5 +63,9 @@ describe('test migrations', function () { expect(db_data['release_iitc_core']['author']).to.equal('jonatkins'); expect(db_data['release_iitc_core']['code']).to.to.include('jonatkins'); }); + it('Should change _update_check_interval from hours to seconds', async function () { + const db_data = await storage.get(['release_update_check_interval']); + expect(db_data['release_update_check_interval']).to.be.equal(6 * 60 * 60); + }); }); });