From b05ee0a65448579e675adf9da1393792aaf9484a Mon Sep 17 00:00:00 2001 From: vkamn Date: Fri, 1 May 2026 20:50:24 +0800 Subject: [PATCH] chore: return missing code after merge with mmvm (#2553) --- .../api/subscriptionController.cpp | 46 +++++++++---------- .../controllers/api/subscriptionController.h | 2 +- client/core/models/api/apiConfig.h | 2 +- .../api/servicesCatalogUiController.cpp | 3 ++ .../api/subscriptionUiController.cpp | 23 ++++++---- .../api/subscriptionUiController.h | 2 +- .../Components/SubscriptionExpiredDrawer.qml | 10 ++-- .../qml/Pages2/PageSettingsApiServerInfo.qml | 2 +- 8 files changed, 48 insertions(+), 42 deletions(-) diff --git a/client/core/controllers/api/subscriptionController.cpp b/client/core/controllers/api/subscriptionController.cpp index e71abbcaf..ce34d4690 100644 --- a/client/core/controllers/api/subscriptionController.cpp +++ b/client/core/controllers/api/subscriptionController.cpp @@ -332,13 +332,8 @@ ErrorCode SubscriptionController::importServiceFromAppStore(const QString &userC appendProtocolDataToApiPayload(serviceProtocol, protocolData, apiPayload); apiPayload[apiDefs::key::transactionId] = transactionId; - GatewayController gatewayController(m_appSettingsRepository->getGatewayEndpoint(), - m_appSettingsRepository->isDevGatewayEnv(), - apiDefs::requestTimeoutMsecs, - m_appSettingsRepository->isStrictKillSwitchEnabled()); - QByteArray responseBody; - ErrorCode errorCode = gatewayController.post(QString("%1v1/subscriptions"), apiPayload, responseBody); + ErrorCode errorCode = executeRequest(QString("%1v1/subscriptions"), apiPayload, responseBody, isTestPurchase); if (errorCode != ErrorCode::NoError) { return errorCode; } @@ -351,6 +346,9 @@ ErrorCode SubscriptionController::importServiceFromAppStore(const QString &userC return ErrorCode::ApiPurchaseError; } + QString normalizedKey = key; + normalizedKey.replace(QStringLiteral("vpn://"), QString()); + // Check if server with this VPN key already exists for (int i = 0; i < m_serversRepository->serversCount(); ++i) { ServerConfig existingServerConfig = m_serversRepository->server(i); @@ -362,7 +360,8 @@ ErrorCode SubscriptionController::importServiceFromAppStore(const QString &userC const ApiV2ServerConfig* apiV2 = existingServerConfig.as(); existingVpnKey = apiV2 ? apiV2->vpnKey() : QString(); } - if (existingVpnKey == key) { + existingVpnKey.replace(QStringLiteral("vpn://"), QString()); + if (!existingVpnKey.isEmpty() && existingVpnKey == normalizedKey) { if (duplicateServerIndex) { *duplicateServerIndex = i; } @@ -371,9 +370,6 @@ ErrorCode SubscriptionController::importServiceFromAppStore(const QString &userC } } - QString normalizedKey = key; - normalizedKey.replace(QStringLiteral("vpn://"), QString()); - QByteArray configString = QByteArray::fromBase64(normalizedKey.toUtf8(), QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals); QByteArray configUncompressed = qUncompress(configString); if (!configUncompressed.isEmpty()) { @@ -423,6 +419,7 @@ ErrorCode SubscriptionController::updateServiceFromGateway(int serverIndex, cons if (!apiV2) { return ErrorCode::InternalError; } + const bool isTestPurchase = apiV2->apiConfig.isTestPurchase; QString serviceProtocol = apiV2->serviceProtocol(); ProtocolData protocolData = generateProtocolData(serviceProtocol); @@ -445,7 +442,7 @@ ErrorCode SubscriptionController::updateServiceFromGateway(int serverIndex, cons } QByteArray responseBody; - ErrorCode errorCode = executeRequest(QString("%1v1/config"), apiPayload, responseBody); + ErrorCode errorCode = executeRequest(QString("%1v1/config"), apiPayload, responseBody, isTestPurchase); if (errorCode != ErrorCode::NoError) { if (errorCode == ErrorCode::ApiSubscriptionExpiredError && !apiV2->apiConfig.isInAppPurchase) { ServerConfig expiredServerConfig = serverConfigModel; @@ -494,7 +491,7 @@ ErrorCode SubscriptionController::updateServiceFromGateway(int serverIndex, cons return ErrorCode::NoError; } -ErrorCode SubscriptionController::deactivateDevice(int serverIndex, bool isRemoveEvent) +ErrorCode SubscriptionController::deactivateDevice(int serverIndex) { ServerConfig serverConfigModel = m_serversRepository->server(serverIndex); @@ -524,8 +521,9 @@ ErrorCode SubscriptionController::deactivateDevice(int serverIndex, bool isRemov QJsonObject apiPayload = gatewayRequestData.toJsonObject(); + const bool isTestPurchase = apiV2->apiConfig.isTestPurchase; QByteArray responseBody; - ErrorCode errorCode = executeRequest(QString("%1v1/revoke_config"), apiPayload, responseBody); + ErrorCode errorCode = executeRequest(QString("%1v1/revoke_config"), apiPayload, responseBody, isTestPurchase); if (errorCode != ErrorCode::NoError && errorCode != ErrorCode::ApiNotFoundError) { return errorCode; } @@ -567,8 +565,9 @@ ErrorCode SubscriptionController::deactivateExternalDevice(int serverIndex, cons QJsonObject apiPayload = gatewayRequestData.toJsonObject(); + const bool isTestPurchase = apiV2->apiConfig.isTestPurchase; QByteArray responseBody; - ErrorCode errorCode = executeRequest(QString("%1v1/revoke_config"), apiPayload, responseBody); + ErrorCode errorCode = executeRequest(QString("%1v1/revoke_config"), apiPayload, responseBody, isTestPurchase); if (errorCode != ErrorCode::NoError && errorCode != ErrorCode::ApiNotFoundError) { return errorCode; } @@ -595,6 +594,7 @@ ErrorCode SubscriptionController::exportNativeConfig(int serverIndex, const QStr if (!apiV2) { return ErrorCode::InternalError; } + const bool isTestPurchase = apiV2->apiConfig.isTestPurchase; QString protocol = configKey::awg; ProtocolData protocolData = generateProtocolData(protocol); @@ -613,7 +613,7 @@ ErrorCode SubscriptionController::exportNativeConfig(int serverIndex, const QStr appendProtocolDataToApiPayload(protocol, protocolData, apiPayload); QByteArray responseBody; - ErrorCode errorCode = executeRequest(QString("%1v1/native_config"), apiPayload, responseBody); + ErrorCode errorCode = executeRequest(QString("%1v1/native_config"), apiPayload, responseBody, isTestPurchase); if (errorCode != ErrorCode::NoError) { return errorCode; } @@ -636,6 +636,7 @@ ErrorCode SubscriptionController::revokeNativeConfig(int serverIndex, const QStr if (!apiV2) { return ErrorCode::InternalError; } + const bool isTestPurchase = apiV2->apiConfig.isTestPurchase; QString protocol = configKey::awg; QJsonObject authDataJson = apiV2->authData.toJson(); @@ -652,7 +653,7 @@ ErrorCode SubscriptionController::revokeNativeConfig(int serverIndex, const QStr QJsonObject apiPayload = gatewayRequestData.toJsonObject(); QByteArray responseBody; - ErrorCode errorCode = executeRequest(QString("%1v1/revoke_native_config"), apiPayload, responseBody); + ErrorCode errorCode = executeRequest(QString("%1v1/revoke_native_config"), apiPayload, responseBody, isTestPurchase); if (errorCode != ErrorCode::NoError && errorCode != ErrorCode::ApiNotFoundError) { return errorCode; } @@ -746,7 +747,7 @@ ErrorCode SubscriptionController::prepareVpnKeyExport(int serverIndex, QString & ErrorCode SubscriptionController::validateAndUpdateConfig(int serverIndex, bool hasInstalledContainers) { ServerConfig serverConfigModel = m_serversRepository->server(serverIndex); - + apiDefs::ConfigSource configSource; if (serverConfigModel.isApiV1()) { configSource = apiDefs::ConfigSource::Telegram; @@ -760,11 +761,11 @@ ErrorCode SubscriptionController::validateAndUpdateConfig(int serverIndex, bool removeApiConfig(serverIndex); return updateServiceFromTelegram(serverIndex); } else if (configSource == apiDefs::ConfigSource::AmneziaGateway && !hasInstalledContainers) { - return updateServiceFromGateway(serverIndex, "", false); + return updateServiceFromGateway(serverIndex, "", true); } else if (configSource && isApiKeyExpired(serverIndex)) { qDebug() << "attempt to update api config by expires_at event"; if (configSource == apiDefs::ConfigSource::AmneziaGateway) { - return updateServiceFromGateway(serverIndex, "", false); + return updateServiceFromGateway(serverIndex, "", true); } else { removeApiConfig(serverIndex); return updateServiceFromTelegram(serverIndex); @@ -1062,6 +1063,7 @@ QFuture> SubscriptionController::getRenewalLink(int se QJsonObject apiPayload = gatewayRequestData.toJsonObject(); apiPayload[apiDefs::key::cliVersion] = QString(APP_VERSION); + apiPayload[apiDefs::key::subscriptionStatus] = getSubscriptionStatusForRenewal(apiV2->apiConfig); auto gatewayController = QSharedPointer::create(m_appSettingsRepository->getGatewayEndpoint(isTestPurchase), m_appSettingsRepository->isDevGatewayEnv(isTestPurchase), @@ -1081,11 +1083,7 @@ QFuture> SubscriptionController::getRenewalLink(int se QJsonObject responseJson = QJsonDocument::fromJson(responseBody).object(); const QString url = responseJson.value("renewal_url").toString(); - if (url.isEmpty()) { - promise->addResult(qMakePair(ErrorCode::InternalError, QString())); - } else { - promise->addResult(qMakePair(ErrorCode::NoError, url)); - } + promise->addResult(qMakePair(ErrorCode::NoError, url)); promise->finish(); }); watcher->setFuture(postFuture); diff --git a/client/core/controllers/api/subscriptionController.h b/client/core/controllers/api/subscriptionController.h index 05ec80a5b..2780ab1da 100644 --- a/client/core/controllers/api/subscriptionController.h +++ b/client/core/controllers/api/subscriptionController.h @@ -65,7 +65,7 @@ public: ErrorCode updateServiceFromGateway(int serverIndex, const QString &newCountryCode, bool isConnectEvent); - ErrorCode deactivateDevice(int serverIndex, bool isRemoveEvent); + ErrorCode deactivateDevice(int serverIndex); ErrorCode deactivateExternalDevice(int serverIndex, const QString &uuid, const QString &serverCountryCode); diff --git a/client/core/models/api/apiConfig.h b/client/core/models/api/apiConfig.h index a2cff58d0..05ecda478 100644 --- a/client/core/models/api/apiConfig.h +++ b/client/core/models/api/apiConfig.h @@ -58,7 +58,7 @@ struct ApiConfig QString stackType; QString cliVersion; - bool isTestPurchase; + bool isTestPurchase = false; bool isInAppPurchase = false; bool subscriptionExpiredByServer = false; diff --git a/client/ui/controllers/api/servicesCatalogUiController.cpp b/client/ui/controllers/api/servicesCatalogUiController.cpp index 734bdd9ac..bf5fed650 100644 --- a/client/ui/controllers/api/servicesCatalogUiController.cpp +++ b/client/ui/controllers/api/servicesCatalogUiController.cpp @@ -23,6 +23,9 @@ bool ServicesCatalogUiController::fillAvailableServices() } m_apiServicesModel->updateModel(servicesData); + if (m_apiServicesModel->rowCount() > 0) { + m_apiServicesModel->setServiceIndex(0); + } return true; } diff --git a/client/ui/controllers/api/subscriptionUiController.cpp b/client/ui/controllers/api/subscriptionUiController.cpp index 370599a24..75d96553e 100644 --- a/client/ui/controllers/api/subscriptionUiController.cpp +++ b/client/ui/controllers/api/subscriptionUiController.cpp @@ -307,7 +307,8 @@ bool SubscriptionUiController::updateServiceFromGateway(const int serverIndex, c if (oldServerConfig.isApiV2()) { const ApiV2ServerConfig *oldApiV2 = oldServerConfig.as(); if (oldApiV2) { - wasSubscriptionExpired = oldApiV2->apiConfig.isSubscriptionExpired(); + wasSubscriptionExpired = oldApiV2->apiConfig.subscriptionExpiredByServer + || oldApiV2->apiConfig.isSubscriptionExpired(); } } @@ -328,8 +329,9 @@ bool SubscriptionUiController::updateServiceFromGateway(const int serverIndex, c } else { if (errorCode == ErrorCode::ApiSubscriptionExpiredError) { emit subscriptionExpiredOnServer(); + } else { + emit errorOccurred(errorCode); } - emit errorOccurred(errorCode); return false; } } @@ -352,14 +354,10 @@ bool SubscriptionUiController::updateServiceFromTelegram(const int serverIndex) } } -bool SubscriptionUiController::deactivateDevice(int serverIndex, const bool isRemoveEvent) +bool SubscriptionUiController::deactivateDevice(int serverIndex) { - - ErrorCode errorCode = m_subscriptionController->deactivateDevice(serverIndex, isRemoveEvent); + ErrorCode errorCode = m_subscriptionController->deactivateDevice(serverIndex); if (errorCode != ErrorCode::NoError) { - if (errorCode == ErrorCode::ApiSubscriptionExpiredError && isRemoveEvent) { - return true; - } emit errorOccurred(errorCode); return false; } @@ -386,7 +384,11 @@ void SubscriptionUiController::validateConfig() ErrorCode errorCode = m_subscriptionController->validateAndUpdateConfig(serverIndex, hasInstalledContainers); if (errorCode != ErrorCode::NoError) { - emit errorOccurred(errorCode); + if (errorCode == ErrorCode::ApiSubscriptionExpiredError) { + emit subscriptionExpiredOnServer(); + } else { + emit errorOccurred(errorCode); + } emit configValidated(false); return; } @@ -476,6 +478,9 @@ void SubscriptionUiController::getRenewalLink(int serverIndex) emit errorOccurred(errorCode); return; } + if (url.isEmpty()) { + return; + } emit renewalLinkReceived(url); }); watcher->setFuture(m_subscriptionController->getRenewalLink(serverIndex)); diff --git a/client/ui/controllers/api/subscriptionUiController.h b/client/ui/controllers/api/subscriptionUiController.h index 73e37593f..d64a2eb2d 100644 --- a/client/ui/controllers/api/subscriptionUiController.h +++ b/client/ui/controllers/api/subscriptionUiController.h @@ -48,7 +48,7 @@ public slots: bool updateServiceFromGateway(const int serverIndex, const QString &newCountryCode, const QString &newCountryName, bool reloadServiceConfig = false); bool updateServiceFromTelegram(const int serverIndex); - bool deactivateDevice(int serverIndex, const bool isRemoveEvent); + bool deactivateDevice(int serverIndex); bool deactivateExternalDevice(int serverIndex, const QString &uuid, const QString &serverCountryCode); void validateConfig(); diff --git a/client/ui/qml/Components/SubscriptionExpiredDrawer.qml b/client/ui/qml/Components/SubscriptionExpiredDrawer.qml index d9b6b29c8..24fe66087 100644 --- a/client/ui/qml/Components/SubscriptionExpiredDrawer.qml +++ b/client/ui/qml/Components/SubscriptionExpiredDrawer.qml @@ -15,7 +15,7 @@ DrawerType2 { property bool isRenewalAvailable: false onOpened: { - isRenewalAvailable = ServersModel.getProcessedServerData("isRenewalAvailable") && !ApiAccountInfoModel.data("isInAppPurchase") + isRenewalAvailable = ServersModel.getDefaultServerData("isRenewalAvailable") && !ApiAccountInfoModel.data("isInAppPurchase") } expandedStateContent: ColumnLayout { @@ -28,7 +28,7 @@ DrawerType2 { spacing: 0 onImplicitHeightChanged: { - root.expandedHeight = content.implicitHeight + 32 + SettingsController.safeAreaBottomMargin + root.expandedHeight = content.implicitHeight + 32 + PageController.safeAreaBottomMargin } Item { @@ -43,7 +43,7 @@ DrawerType2 { anchors.left: parent.left anchors.right: parent.right - text: ServersModel.getProcessedServerData("name") + qsTr(" subscription has expired") + text: ServersModel.getDefaultServerData("name") + qsTr(" subscription has expired") horizontalAlignment: Text.AlignLeft } } @@ -76,7 +76,7 @@ DrawerType2 { textColor: AmneziaStyle.color.midnightBlack clickedFunc: function() { - SubscriptionUiController.getRenewalLink(ServersUiController.getProcessedServerIndex()) + SubscriptionUiController.getRenewalLink(ServersUiController.defaultIndex) } } @@ -96,7 +96,7 @@ DrawerType2 { clickedFunc: function() { PageController.showBusyIndicator(true) - let result = ApiSettingsController.getAccountInfo(false) + let result = SubscriptionUiController.getAccountInfo(ServersUiController.defaultIndex, false) PageController.showBusyIndicator(false) if (result) { root.closeTriggered() diff --git a/client/ui/qml/Pages2/PageSettingsApiServerInfo.qml b/client/ui/qml/Pages2/PageSettingsApiServerInfo.qml index 143352fb3..7eb119244 100644 --- a/client/ui/qml/Pages2/PageSettingsApiServerInfo.qml +++ b/client/ui/qml/Pages2/PageSettingsApiServerInfo.qml @@ -469,7 +469,7 @@ PageType { PageController.showNotificationMessage(qsTr("Cannot unlink device during active connection")) } else { PageController.showBusyIndicator(true) - if (SubscriptionUiController.deactivateDevice(ServersUiController.getProcessedServerIndex(), false)) { + if (SubscriptionUiController.deactivateDevice(ServersUiController.getProcessedServerIndex())) { SubscriptionUiController.getAccountInfo(ServersUiController.getProcessedServerIndex(), true) } PageController.showBusyIndicator(false)