From 64b2c2c6e4cf0bea166c5caad9255cbc608953f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Wei=C3=9F?= <77456193+aweiss-dev@users.noreply.github.com> Date: Mon, 9 Dec 2024 16:19:20 +0100 Subject: [PATCH] feat: update backend configs regularly --- package.json | 2 +- src/script/auth/main.tsx | 4 +- src/script/lifecycle/updateRemoteConfigs.ts | 60 +++++++++++++++++++++ src/script/main/app.ts | 5 +- src/script/team/TeamRepository.ts | 15 ++++-- yarn.lock | 45 ++++++++++------ 6 files changed, 105 insertions(+), 26 deletions(-) create mode 100644 src/script/lifecycle/updateRemoteConfigs.ts diff --git a/package.json b/package.json index 15af22da9dd..3cce1bc0e9d 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "@wireapp/avs": "10.0.4", "@wireapp/avs-debugger": "0.0.5", "@wireapp/commons": "5.4.0", - "@wireapp/core": "46.11.6", + "@wireapp/core": "46.13.0", "@wireapp/react-ui-kit": "9.28.0", "@wireapp/store-engine-dexie": "2.1.15", "@wireapp/telemetry": "0.1.3", diff --git a/src/script/auth/main.tsx b/src/script/auth/main.tsx index 56874276b13..86c4081ea14 100644 --- a/src/script/auth/main.tsx +++ b/src/script/auth/main.tsx @@ -38,6 +38,7 @@ import {actionRoot} from './module/action'; import {Root} from './page/Root'; import {Config} from '../Config'; +import {updateApiVersion} from '../lifecycle/updateRemoteConfigs'; import {APIClient} from '../service/APIClientSingleton'; import {Core} from '../service/CoreSingleton'; @@ -76,8 +77,7 @@ const render = (Component: FC): void => { const config = Config.getConfig(); async function runApp() { - const [min, max] = config.SUPPORTED_API_RANGE; - const {domain} = await core.useAPIVersion(min, max, config.ENABLE_DEV_BACKEND_API); + const {domain} = await updateApiVersion(); await initializeDataDog(config, {domain: domain}); render(Root); diff --git a/src/script/lifecycle/updateRemoteConfigs.ts b/src/script/lifecycle/updateRemoteConfigs.ts new file mode 100644 index 00000000000..a559ed45099 --- /dev/null +++ b/src/script/lifecycle/updateRemoteConfigs.ts @@ -0,0 +1,60 @@ +/* + * Wire + * Copyright (C) 2024 Wire Swiss GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see http://www.gnu.org/licenses/. + * + */ + +import {TaskParams} from '@wireapp/core/lib/util/RecurringTaskScheduler'; +import {container} from 'tsyringe'; + +import {getLogger} from 'Util/Logger'; +import {TIME_IN_MILLIS} from 'Util/TimeUtil'; + +import {Config} from '../Config'; +import {Core} from '../service/CoreSingleton'; + +let core: Core | undefined = undefined; +export const updateRemoteConfigLogger = getLogger('updateRemoteConfigs'); + +export const scheduleRecurringTask = async (params: TaskParams) => { + if (!core) { + core = container.resolve(Core); + } + return core.recurringTaskScheduler.registerTask(params); +}; + +export const updateApiVersion = async () => { + if (!core) { + core = container.resolve(Core); + } + const { + SUPPORTED_API_RANGE: [min, max], + ENABLE_DEV_BACKEND_API, + } = Config.getConfig(); + + updateRemoteConfigLogger.info('Updating api-version and info'); + return core.useAPIVersion(min, max, ENABLE_DEV_BACKEND_API); +}; + +export const scheduleApiVersionUpdate = async () => { + // Schedule the task to run every day and add it to the window focus event + await scheduleRecurringTask({ + every: TIME_IN_MILLIS.DAY, + task: updateApiVersion, + key: 'try-api-version-backend-sync', + addTaskOnWindowFocusEvent: true, + }); +}; diff --git a/src/script/main/app.ts b/src/script/main/app.ts index 559f48f9aec..2786ae1fed7 100644 --- a/src/script/main/app.ts +++ b/src/script/main/app.ts @@ -85,6 +85,7 @@ import {externalUrl} from '../externalRoute'; import {IntegrationRepository} from '../integration/IntegrationRepository'; import {IntegrationService} from '../integration/IntegrationService'; import {startNewVersionPolling} from '../lifecycle/newVersionHandler'; +import {scheduleApiVersionUpdate, updateApiVersion} from '../lifecycle/updateRemoteConfigs'; import {MediaRepository} from '../media/MediaRepository'; import {initMLSGroupConversations, initialiseSelfAndTeamConversations} from '../mls'; import {joinConversationsAfterMigrationFinalisation} from '../mls/MLSMigration/migrationFinaliser'; @@ -343,8 +344,8 @@ export class App { async initApp(clientType: ClientType, onProgress: (progress: number, message?: string) => void) { // add body information const startTime = Date.now(); - const [apiVersionMin, apiVersionMax] = this.config.SUPPORTED_API_RANGE; - await this.core.useAPIVersion(apiVersionMin, apiVersionMax, this.config.ENABLE_DEV_BACKEND_API); + await updateApiVersion(); + await scheduleApiVersionUpdate(); const osCssClass = Runtime.isMacOS() ? 'os-mac' : 'os-pc'; const platformCssClass = Runtime.isDesktopApp() ? 'platform-electron' : 'platform-web'; diff --git a/src/script/team/TeamRepository.ts b/src/script/team/TeamRepository.ts index 865027b14ba..6df488df779 100644 --- a/src/script/team/TeamRepository.ts +++ b/src/script/team/TeamRepository.ts @@ -55,6 +55,7 @@ import {EventSource} from '../event/EventSource'; import {NOTIFICATION_HANDLING_STATE} from '../event/NotificationHandlingState'; import {IntegrationMapper} from '../integration/IntegrationMapper'; import {ServiceEntity} from '../integration/ServiceEntity'; +import {scheduleRecurringTask, updateRemoteConfigLogger} from '../lifecycle/updateRemoteConfigs'; import {MLSMigrationStatus, getMLSMigrationStatus} from '../mls/MLSMigration/migrationStatus'; import {APIClient} from '../service/APIClientSingleton'; import {ROLE, ROLE as TEAM_ROLE, roleFromTeamPermissions} from '../user/UserPermission'; @@ -144,7 +145,7 @@ export class TeamRepository extends TypedEventEmitter { }); const members = await this.loadInitialTeamMembers(teamId); - this.scheduleTeamRefresh(); + await this.scheduleTeamRefresh(); return {team, features: newFeatureList, members}; } @@ -162,18 +163,24 @@ export class TeamRepository extends TypedEventEmitter { }; } - private readonly scheduleTeamRefresh = (): void => { + private readonly scheduleTeamRefresh = async (): Promise => { const updateTeam = async () => { try { + updateRemoteConfigLogger.info('Updating team-settings'); await this.getTeam(); await this.updateFeatureConfig(); } catch (error) { this.logger.error(error); } }; + // We want to poll the latest team data every time the app is focused and every day - window.addEventListener('focus', updateTeam); - window.setInterval(updateTeam, TIME_IN_MILLIS.DAY); + await scheduleRecurringTask({ + every: TIME_IN_MILLIS.DAY, + task: updateTeam, + key: 'team-refresh', + addTaskOnWindowFocusEvent: true, + }); }; private async getInitialTeamMembers(teamId: string): Promise { diff --git a/yarn.lock b/yarn.lock index 11569e38759..52b3a607d39 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5948,14 +5948,14 @@ __metadata: languageName: node linkType: hard -"@wireapp/api-client@npm:^27.13.1": - version: 27.13.1 - resolution: "@wireapp/api-client@npm:27.13.1" +"@wireapp/api-client@npm:^27.13.2": + version: 27.13.2 + resolution: "@wireapp/api-client@npm:27.13.2" dependencies: "@wireapp/commons": "npm:^5.4.0" "@wireapp/priority-queue": "npm:^2.1.11" "@wireapp/protocol-messaging": "npm:1.51.0" - axios: "npm:1.7.7" + axios: "npm:1.7.9" axios-retry: "npm:4.5.0" http-status-codes: "npm:2.3.0" logdown: "npm:3.3.1" @@ -5965,7 +5965,7 @@ __metadata: tough-cookie: "npm:4.1.4" ws: "npm:8.18.0" zod: "npm:3.23.8" - checksum: 10/8e297f4da3ac3d4a052d5670b0ddcd6302c9a246ef61760c61b3ecf0e1e78b0d01107fa32a10ad70e8203e7e3a6cab0d2373cc52ece4bfd154f8840b2e9db84b + checksum: 10/1fbe961c5648fd349de4f59df29eb52e80d7999c7114066cfdfbf14a7c42e4ebfa6276218f4694aa4a141e7e6221e1b2bd9f65de48cfcd2c3207e330e00edc9d languageName: node linkType: hard @@ -6023,26 +6023,26 @@ __metadata: languageName: node linkType: hard -"@wireapp/core-crypto@npm:1.1.2": - version: 1.1.2 - resolution: "@wireapp/core-crypto@npm:1.1.2" - checksum: 10/7f0a3fc87f1f58ff0a2067053bfd7669ad4073584e31fe450d9a5528964f65600f20521b092704b8dce699c31b8b478eaadb23d26b4f1d2f224134ba63f1d7fa +"@wireapp/core-crypto@npm:2.0.0": + version: 2.0.0 + resolution: "@wireapp/core-crypto@npm:2.0.0" + checksum: 10/f4d9a687ba32bea4026d831f05dcc20e65436f44e6600663348aaf3089ceb1d0a6ad179d24516fc54436be104805b065ab44fe9418b3c8ef872aba8417f71ce3 languageName: node linkType: hard -"@wireapp/core@npm:46.11.6": - version: 46.11.6 - resolution: "@wireapp/core@npm:46.11.6" +"@wireapp/core@npm:46.13.0": + version: 46.13.0 + resolution: "@wireapp/core@npm:46.13.0" dependencies: - "@wireapp/api-client": "npm:^27.13.1" + "@wireapp/api-client": "npm:^27.13.2" "@wireapp/commons": "npm:^5.4.0" - "@wireapp/core-crypto": "npm:1.1.2" + "@wireapp/core-crypto": "npm:2.0.0" "@wireapp/cryptobox": "npm:12.8.0" "@wireapp/priority-queue": "npm:^2.1.11" "@wireapp/promise-queue": "npm:^2.3.10" "@wireapp/protocol-messaging": "npm:1.51.0" "@wireapp/store-engine": "npm:5.1.11" - axios: "npm:1.7.7" + axios: "npm:1.7.9" bazinga64: "npm:^6.3.11" deepmerge-ts: "npm:6.0.0" hash.js: "npm:1.1.7" @@ -6052,7 +6052,7 @@ __metadata: long: "npm:^5.2.0" uuid: "npm:9.0.1" zod: "npm:3.23.8" - checksum: 10/ee0dfc80fa1e4ca2eaf31a550abf8b87e9d81f402f64c3f2d85f02b3117a1edab4762347c7d4d005f5083d091fdabbfc497503e1671b51bfa2ab4daf89c69ef9 + checksum: 10/30b90cdc3fdc0ba8e015bac7182244c410528ba66ed0a48c7aa647e8354e2b4fa6aa0c9e30af18923aa02551b6c10562632604c6d59537e49dc1e113d4858cb6 languageName: node linkType: hard @@ -6892,6 +6892,17 @@ __metadata: languageName: node linkType: hard +"axios@npm:1.7.9": + version: 1.7.9 + resolution: "axios@npm:1.7.9" + dependencies: + follow-redirects: "npm:^1.15.6" + form-data: "npm:^4.0.0" + proxy-from-env: "npm:^1.1.0" + checksum: 10/b7a5f660ea53ba9c2a745bf5ad77ad8bf4f1338e13ccc3f9f09f810267d6c638c03dac88b55dae8dc98b79c57d2d6835be651d58d2af97c174f43d289a9fd007 + languageName: node + linkType: hard + "axobject-query@npm:^3.1.1": version: 3.2.1 resolution: "axobject-query@npm:3.2.1" @@ -18763,7 +18774,7 @@ __metadata: "@wireapp/avs-debugger": "npm:0.0.5" "@wireapp/commons": "npm:5.4.0" "@wireapp/copy-config": "npm:2.2.10" - "@wireapp/core": "npm:46.11.6" + "@wireapp/core": "npm:46.13.0" "@wireapp/eslint-config": "npm:3.0.7" "@wireapp/prettier-config": "npm:0.6.4" "@wireapp/react-ui-kit": "npm:9.28.0"