From e5b2de3461ebaadd51e77cff99d6cc2a972acfa9 Mon Sep 17 00:00:00 2001 From: "Stephane Lacoin (aka nxmatic)" Date: Mon, 8 Apr 2024 15:34:46 +0200 Subject: [PATCH] release 4.0.0-HF01 fixing up nuxeo connect subscription handling --- package.json | 2 +- src/main/connect-locator.js | 14 ++- ...dio-project.js => connect-registration.js} | 22 +++- src/main/groovy/index.js | 4 +- src/main/server-connector.js | 13 ++- src/main/studio-hot-reloader.js | 10 +- src/popup/index.js | 102 +++++++++--------- 7 files changed, 97 insertions(+), 70 deletions(-) rename src/main/groovy/{registered-studio-project.js => connect-registration.js} (67%) diff --git a/package.json b/package.json index 2ec6d313..0dd4d0c6 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "4.0.0", + "version": "4.0.0-HF01", "dependencies": { "bootstrap": "^5.3.3", "crypto-js": "^4.2.0", diff --git a/src/main/connect-locator.js b/src/main/connect-locator.js index 8f2ee2b6..77e4eefd 100644 --- a/src/main/connect-locator.js +++ b/src/main/connect-locator.js @@ -36,7 +36,14 @@ class ConnectLocator extends ServiceWorkerComponent { asRegistration(url) { return this.worker.serverConnector .asRuntimeInfo() - .then(({ serverUrl: nuxeoUrl, connectUrl }) => { + .then((info) => ({ connectRegistration: { connectUrl: undefined }, ...info })) + .then(({ serverUrl: nuxeoUrl, connectRegistration: { connectUrl } }) => { + if (!connectUrl) { + return { + location: 'about:blank', + credentials: undefined + }; + } const nuxeoKey = this.nuxeoKeyOf(new URL(nuxeoUrl)); if (url) { const { location, credentials } = this.extractCredentialsAndCleanUrl(url); @@ -51,7 +58,10 @@ class ConnectLocator extends ServiceWorkerComponent { .then((store) => new URL(store[nuxeoKey])) .then((location) => ({ location, credentialsKey: this.credentialsKeyOf(location) })) .then(({ location, credentialsKey }) => this.worker.browserStore.get({ [credentialsKey]: undefined }) - .then((credentialsStore) => ({ location, credentials: credentialsStore[credentialsKey] }))); + .then((credentialsStore) => ({ + location, + credentials: credentialsStore[credentialsKey] + }))); }); } diff --git a/src/main/groovy/registered-studio-project.js b/src/main/groovy/connect-registration.js similarity index 67% rename from src/main/groovy/registered-studio-project.js rename to src/main/groovy/connect-registration.js index 63aa40ea..8b8a735e 100644 --- a/src/main/groovy/registered-studio-project.js +++ b/src/main/groovy/connect-registration.js @@ -1,10 +1,13 @@ -export default function registedStudioProject() { +export default function connectRegistration() { return ` import org.apache.commons.logging.Log import org.apache.commons.logging.LogFactory import groovy.json.JsonOutput import org.nuxeo.connect.client.we.StudioSnapshotHelper + import org.nuxeo.connect.connector.CanNotReachConnectServer import org.nuxeo.connect.connector.http.ConnectUrlConfig + import org.nuxeo.connect.data.SubscriptionStatus + import org.nuxeo.connect.data.SubscriptionStatusType import org.nuxeo.connect.identity.LogicalInstanceIdentifier import org.nuxeo.connect.packages.PackageManager import org.nuxeo.connect.packages.dependencies.TargetPlatformFilterHelper @@ -35,9 +38,21 @@ export default function registedStudioProject() { return snapshotPackage.targetPlatforms.any { it == nxInstance.asString() } } - ConnectRegistrationService registrations = Framework.getService(ConnectRegistrationService.class) + def connectSubscriptionOf(gateway) { + try { + return gateway.getConnector().getConnectStatus() + } catch (Exception cause) { + SubscriptionStatus errorStatus = new SubscriptionStatus() + errorStatus.setErrorMessage(JsonOutput.toJson([message: cause.message, type: cause.getClass().getName()])) + errorStatus.setContractStatus(SubscriptionStatusType.UNKNOWN.getValue()) + return errorStatus; + } + } + + ConnectRegistrationService gateway = Framework.getService(ConnectRegistrationService.class) String connectUrl = ConnectUrlConfig.getBaseUrl() - LogicalInstanceIdentifier clid = registrations.getCLID() + LogicalInstanceIdentifier clid = gateway.getCLID() + SubscriptionStatus connectSubscription = connectSubscriptionOf(gateway) PackageManager packages = Framework.getService(PackageManager.class) Package snapshotPackage = StudioSnapshotHelper.getSnapshot(packages.listRemoteAssociatedStudioPackages()) @@ -47,6 +62,7 @@ export default function registedStudioProject() { nx: nxInstance.asString(), clid: clid, connectUrl: connectUrl, + connectSubscription: connectSubscription, developmentMode: Framework.isDevModeSet(), match: isMatching(snapshotPackage, nxInstance), package: studioPackage, diff --git a/src/main/groovy/index.js b/src/main/groovy/index.js index 9d9ef97e..8304b439 100644 --- a/src/main/groovy/index.js +++ b/src/main/groovy/index.js @@ -1,14 +1,14 @@ import connectLocation from './connect-location'; +import connectRegistration from './connect-registration'; import developedStudioProjects from './developed-studio-projects'; import installedAddons from './installed-addons'; -import registedStudioProject from './registered-studio-project'; import registerDevelopedStudioProject from './register-developed-studio-project'; export default { 'connect-location': connectLocation, + 'connect-registration': connectRegistration, 'developed-studio-projects': developedStudioProjects, 'installed-addons': installedAddons, - 'registered-studio-project': registedStudioProject, 'register-developed-studio-project': registerDevelopedStudioProject, // Add more scripts here as needed }; diff --git a/src/main/server-connector.js b/src/main/server-connector.js index dc2f9438..9148dad7 100644 --- a/src/main/server-connector.js +++ b/src/main/server-connector.js @@ -106,22 +106,21 @@ class ServerConnector extends ServiceWorkerComponent { throw cause; }); } + isConnected() { return Promise.resolve(this.disconnect != null); } asRuntimeInfo() { return Promise.all([ - this.asConnectLocation(), this.asInstalledAddons(), - this.asRegisteredStudioProject() + this.asConnectRegistration(), ]) - .then(([connectLocation, installedAddons, registredStudioProject]) => ({ + .then(([installedAddons, connectRegistration]) => ({ nuxeo: this.nuxeo, serverUrl: this.serverUrl, - connectUrl: connectLocation, installedAddons, - registredStudioProject, + connectRegistration, })); } @@ -129,9 +128,9 @@ class ServerConnector extends ServiceWorkerComponent { return this.executeScript('connect-location'); } - asRegisteredStudioProject() { + asConnectRegistration() { return this - .executeScript('registered-studio-project') + .executeScript('connect-registration') .then((result) => ({ ...result, serverUrl: this.serverUrl })); } diff --git a/src/main/studio-hot-reloader.js b/src/main/studio-hot-reloader.js index 91d00f19..68972ffc 100644 --- a/src/main/studio-hot-reloader.js +++ b/src/main/studio-hot-reloader.js @@ -111,8 +111,8 @@ class StudioHotReloader extends ServiceWorkerComponent { handleLegacyErrors(error) { return this.worker.serverConnector .asRuntimeInfo() - .then(({ nuxeo, registeredStudioProject }) => { - if (!registeredStudioProject) { + .then(({ nuxeo, connectRegistration }) => { + if (!connectRegistration) { const notification = { id: 'studio-not-registered', options: { @@ -123,14 +123,14 @@ class StudioHotReloader extends ServiceWorkerComponent { }; return this.worker.serverConnector.handleErrors(error, notification); } - const { package: registeredStudioPackage } = registeredStudioProject || { package: 'unknown' }; + const { package: registeredStudioPackage } = connectRegistration || { package: 'unknown' }; const nuxeoServerVersion = NuxeoServerVersion.create(nuxeo.version); const nuxeoLegacyVersion = NuxeoServerVersion.create('9.2'); if (!nuxeoServerVersion.lte(nuxeoLegacyVersion)) throw error; // Error handling for Nuxeo 9.2 and older let dependencyErrorNotification = null; - if (registeredStudioProject.nx !== registeredStudioPackage.studioDistrib[0]) { + if (connectRegistration.nx !== registeredStudioPackage.studioDistrib[0]) { const message = `${registeredStudioPackage.name} - ${registeredStudioPackage.studioDistrib[0]} cannot be installed on Nuxeo ${nuxeo.version}.`; dependencyErrorNotification = { id: 'dependencies-ismatch', @@ -142,7 +142,7 @@ class StudioHotReloader extends ServiceWorkerComponent { } }; } - if (!registeredStudioProject.match && registeredStudioPackage.deps.length > 0) { + if (!connectRegistration.match && registeredStudioPackage.deps.length > 0) { const deps = registeredStudioPackage.deps; const items = []; deps.forEach((dep) => { diff --git a/src/popup/index.js b/src/popup/index.js index 14198c27..f9e68900 100644 --- a/src/popup/index.js +++ b/src/popup/index.js @@ -163,10 +163,10 @@ function loadPage(worker) { }; const noStudioPackageFound = () => { - $('#no-studio-buttons').css('display', 'block'); - $('div.nuxeoctl-command').append('nuxeoctl register'); - $('div.shade').show(); - $('#no-studio-package-registered').show(); + $('#no-studio-buttons').hide(); + // $('div.nuxeoctl-command').append('nuxeoctl register'); + // $('div.shade').show(); + // $('#no-studio-package-registered').show(); worker.desktopNotifier.notify( 'no_studio_project', { @@ -412,7 +412,7 @@ function loadPage(worker) { .then((selectBox) => { if (!selectBox) { return worker.serverConnector - .asRegisteredStudioProject() + .asConnectRegistration() .then((registration) => { const { package: studioPackage } = registration; if (!studioPackage) return registration; @@ -452,14 +452,35 @@ function loadPage(worker) { }); }) // eslint-disable-next-line no-shadow - .then(({ serverUrl, developmentMode, package: registeredPackage }) => { + .then((registration) => { + const { + // eslint-disable-next-line no-shadow + serverUrl: serverLocation, + connectUrl: connectLocation, + connectSubscription, + developmentMode, + package: registeredPackage + } = registration; if (!developmentMode) { $('div.shade').show(); $('#development-mode-disabled').show(); - $('#development-mode-disabled #serverUrl').text(serverUrl); + $('#development-mode-disabled #serverUrl').text(serverLocation); + } + if (connectSubscription.errorMessage) { + const error = JSON.parse(connectSubscription.errorMessage); + const alertText = ` + Cannot retrieve your server connect registration from \`${connectUrl}\`... + ${JSON.stringify(error, null, 2)} + `.replace(/\n/g, '
'); + + Swal.fire({ + title: 'Warning', + html: alertText, + icon: 'warning', + }); } if (registeredPackage) { - studioPackageFound(connectUrl, registeredPackage); + studioPackageFound(new URL(connectLocation), registeredPackage); } else { noStudioPackageFound(); } @@ -473,54 +494,35 @@ function loadPage(worker) { $('#traces-button').toggleClass('enabled', isEnabled); })); - pendingPromises.push(worker.serverConnector + pendingPromises.push(worker + .serverConnector .asRuntimeInfo() - .then((info) => { - $('#platform-version').text(` ${info.nuxeo.serverVersion.version}`); - return info; - }) .then((info) => { const nuxeoServerVersion = NuxeoServerVersion.create(info.nuxeo.serverVersion.version); const lts2019 = NuxeoServerVersion.create('10.10'); - if (nuxeoServerVersion.lt(lts2019)) { - worker.browserStore - .set({ highlight: true }) - .then(() => adjustStorageButtons()); - } - return info; - }) - .then((info) => { serverUrl = info.serverUrl.replace(/\/$/, ''); - return serverUrl; - }) - // eslint-disable-next-line no-shadow - .then((serverUrl) => { const serverString = DOMPurify.sanitize(serverUrl); - $('div.server-name-url').text(serverString); - return serverUrl; - }) - // eslint-disable-next-line no-shadow - .then((serverUrl) => { - registerLink( - '#automation-doc', - serverUrl.concat('/site/automation/doc/') - ); - return serverUrl; - }) - // eslint-disable-next-line no-shadow - .then((serverUrl) => { - worker.serverConnector - .asInstalledAddons() - .then((addons) => { - if (!addons.includes('nuxeo-web-ui')) { - $('#designer-livepreview').hide(); - } - const playgroundUrl = addons.includes('nuxeo-api-playground') - ? `${serverUrl}/playground/#/${serverUrl}` - : `http://nuxeo.github.io/api-playground/#/${serverUrl}`; - return registerLink('#api-playground', playgroundUrl); - }); - return serverUrl; + + const promises = [ + Promise.resolve($('#platform-version').text(` ${info.nuxeo.serverVersion.version}`)), + nuxeoServerVersion.lt(lts2019) + ? worker.browserStore.set({ highlight: true }).then(() => adjustStorageButtons()) + : Promise.resolve(), + Promise.resolve($('div.server-name-url').text(serverString)), + Promise.resolve(registerLink('#automation-doc', serverUrl.concat('/site/automation/doc/'))), + Promise.resolve(info.installedAddons) + .then((addons) => { + if (!addons.includes('nuxeo-web-ui')) { + $('#designer-livepreview').hide(); + } + const playgroundUrl = addons.includes('nuxeo-api-playground') + ? `${serverUrl}/playground/#/${serverUrl}` + : `http://nuxeo.github.io/api-playground/#/${serverUrl}`; + return registerLink('#api-playground', playgroundUrl); + }) + ]; + + return Promise.all(promises); })); pendingPromises.push(worker.tabNavigationHandler