Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
da347ce
Skip wp.com login if access token exists
nbradbury Aug 12, 2025
c95fa12
First pass at installing Jetpack
nbradbury Aug 12, 2025
f9dba6f
Second pass at installing Jetpack
nbradbury Aug 12, 2025
03f85e7
First pass at handling error response
nbradbury Aug 12, 2025
74f5f45
Return a pair for installJetpack and restored launch wpcom login from…
nbradbury Aug 12, 2025
a298f39
Added InstallJetpackResult to make it clear the second item in the pa…
nbradbury Aug 12, 2025
d15a2bb
Added InstallJetpackResult to make it clear the second item in the pa…
nbradbury Aug 12, 2025
8d426ca
refresh the site before starting jetpack install
nbradbury Aug 12, 2025
ae6eaa3
Fixed detekt warnings, fetch site directly
nbradbury Aug 12, 2025
f72cc1f
Added/updated comments
nbradbury Aug 12, 2025
b2f520b
Merge branch 'trunk' of https://github.com/wordpress-mobile/WordPress…
nbradbury Aug 12, 2025
fe56a2e
Handle inactive Jetpack install, refresh site in init
nbradbury Aug 12, 2025
1ee7396
Tweaked comments
nbradbury Aug 12, 2025
1cc674f
Restored check for access token
nbradbury Aug 12, 2025
2c1a58a
Check if we need to install or simply activate the jp plugin
nbradbury Aug 12, 2025
1237bc9
Simplified JetpackInstaller to always install the plugin
nbradbury Aug 12, 2025
c171b36
Increased timeout
nbradbury Aug 12, 2025
d1b59a1
Fixed Detekt warnings
nbradbury Aug 12, 2025
974941a
Merge branch 'trunk' into feature/jetpack-connect-install
nbradbury Aug 12, 2025
b5b7cbc
Simplified JetpackInstaller.kt
nbradbury Aug 12, 2025
a5ac0b4
Simplified JetpackInstaller.kt, p2
nbradbury Aug 12, 2025
5e4aa19
Added log message
nbradbury Aug 12, 2025
fe48169
Get plugin status before activating
nbradbury Aug 12, 2025
6af0b6c
Use correct names for slugs
nbradbury Aug 13, 2025
6354253
Removed iOS commented code
nbradbury Aug 13, 2025
8c4dca1
Reduced timeout from 60 to 45 seconds
nbradbury Aug 13, 2025
e19e769
Simplified installer
nbradbury Aug 13, 2025
ca4efc5
Code cleanup
nbradbury Aug 13, 2025
2a163dd
Removed refreshSite
nbradbury Aug 13, 2025
89ac854
Merge branch 'trunk' into feature/jetpack-connect-install
nbradbury Aug 13, 2025
d705d4c
Pass WpApiClient as a parameter
nbradbury Aug 13, 2025
f7f49ff
Added missing space
nbradbury Aug 13, 2025
7d7d38b
Use a Result<PluginStatus> for installJetpack
nbradbury Aug 13, 2025
4baf46d
Add a delay to wp.com login if already logged in
nbradbury Aug 13, 2025
bbccb48
Removed unnecessary launch
nbradbury Aug 13, 2025
b601a0c
Suppress TooGenericExceptionCaught
nbradbury Aug 13, 2025
b59b354
Merge branch 'trunk' into feature/jetpack-connect-install
nbradbury Aug 13, 2025
67d9bd6
Merge branch 'trunk' into feature/jetpack-connect-install
nbradbury Aug 13, 2025
77eaae8
Merge branch 'trunk' into feature/jetpack-connect-install
adalpari Aug 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package org.wordpress.android.ui.jetpackrestconnection

import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.utils.AppLogWrapper
import org.wordpress.android.util.AppLog
import rs.wordpress.api.kotlin.WpApiClient
import rs.wordpress.api.kotlin.WpRequestResult
import uniffi.wp_api.PluginCreateParams
import uniffi.wp_api.PluginListParams
import uniffi.wp_api.PluginSlug
import uniffi.wp_api.PluginStatus
import uniffi.wp_api.PluginUpdateParams
import uniffi.wp_api.PluginWpOrgDirectorySlug
import uniffi.wp_api.WpAuthenticationProvider
import java.net.URL
import javax.inject.Inject

/**
* Installs the Jetpack plugin on the given site using wordpress-rs
*/
class JetpackInstaller @Inject constructor(
private val appLogWrapper: AppLogWrapper,
) {
@Suppress("TooGenericExceptionCaught")
suspend fun installJetpack(site: SiteModel): Result<PluginStatus> {
if (!validateCredentials(site)) {
return Result.failure(IllegalArgumentException("Missing credentials for Jetpack installation"))
}

val apiClient = initApiClient(site)

return try {
val info = getPluginInfo(apiClient)
when (info?.status) {
PluginStatus.ACTIVE, PluginStatus.NETWORK_ACTIVE -> {
logDebug("Jetpack is already installed and activated")
Result.success(info.status)
}
PluginStatus.INACTIVE -> {
logDebug("Jetpack is installed but inactive")
val targetStatus = if (info.isNetworkOnly) {
PluginStatus.NETWORK_ACTIVE
} else {
PluginStatus.ACTIVE
}
activatePlugin(apiClient, targetStatus)
}
null -> {
logDebug("Jetpack is not installed")
createAndActivatePlugin(apiClient)
}
}
} catch (e: Exception) {
logError("Failed to install Jetpack: ${e.message}")
Result.failure(e)
}
}

private fun validateCredentials(site: SiteModel): Boolean {
return !site.apiRestUsernamePlain.isNullOrBlank() && !site.apiRestPasswordPlain.isNullOrBlank()
}

private fun initApiClient(site: SiteModel): WpApiClient {
return WpApiClient(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⛏️ What about moving this WpApiClient creation to. WpApiClientProvider, so we are sure every instance creation is done there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I can look into this for this subsequent PR which moves the WpApiClient initialization to a helper class.

wpOrgSiteApiRootUrl = URL(site.wpApiRestUrl),
authProvider = WpAuthenticationProvider.staticWithUsernameAndPassword(
site.apiRestUsernamePlain!!,
site.apiRestPasswordPlain!!
)
)
}

private suspend fun getPluginInfo(apiClient: WpApiClient): PluginInfo? {
val response = apiClient.request { requestBuilder ->
requestBuilder.plugins().listWithEditContext(
params = PluginListParams(search = JETPACK_SLUG.slug)
)
}

return when (response) {
is WpRequestResult.Success -> {
response.response.data.firstOrNull {
it.plugin.slug == JETPACK_SLUG.slug
}?.let {
PluginInfo(
status = it.status,
isNetworkOnly = it.networkOnly
)
}
}
else -> {
logError("Failed to get plugin info")
null
}
}
}

private suspend fun activatePlugin(apiClient: WpApiClient, targetStatus: PluginStatus): Result<PluginStatus> {
logDebug("Activating Jetpack plugin with status: $targetStatus")

val response = apiClient.request { requestBuilder ->
requestBuilder.plugins().update(
pluginSlug = JETPACK_SLUG,
params = PluginUpdateParams(status = targetStatus)
)
}

return when (response) {
is WpRequestResult.Success -> Result.success(response.response.data.status)
is WpRequestResult.WpError<*> -> {
val error = Exception("Activation failed - ${response.errorCode}")
logError(error.message ?: "Activation failed")
Result.failure(error)
}
else -> {
val error = Exception("Activation failed")
logError(error.message ?: "Activation failed")
Result.failure(error)
}
}
}

private suspend fun createAndActivatePlugin(apiClient: WpApiClient): Result<PluginStatus> {
logDebug("Installing and activating Jetpack plugin")

val response = apiClient.request { requestBuilder ->
requestBuilder.plugins().create(
PluginCreateParams(
slug = JETPACK_SLUG_WPORG_DIRECTORY,
status = PluginStatus.ACTIVE
)
)
}

return when (response) {
is WpRequestResult.Success -> {
logDebug("Installation successful")
Result.success(response.response.data.status)
}
is WpRequestResult.WpError<*> -> {
val error = Exception("Installation failed - ${response.errorCode}")
logError(error.message ?: "Installation failed")
Result.failure(error)
}
else -> {
val error = Exception("Installation failed")
logError(error.message ?: "Installation failed")
Result.failure(error)
}
}
}

private fun logDebug(message: String) {
appLogWrapper.d(AppLog.T.API, "$TAG: $message")
}

private fun logError(message: String) {
appLogWrapper.e(AppLog.T.API, "$TAG: $message")
}

private data class PluginInfo(
val status: PluginStatus,
val isNetworkOnly: Boolean
)

companion object {
private const val TAG = "JetpackInstaller"
private val JETPACK_SLUG = PluginSlug("jetpack/jetpack")
private val JETPACK_SLUG_WPORG_DIRECTORY = PluginWpOrgDirectorySlug("jetpack")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,10 @@ private fun ConnectionStepContent(

private fun getErrorText(context: Context, errorType: ErrorType): String {
@StringRes val messageRes = when (errorType) {
ErrorType.FailedToLoginWpCom -> R.string.jetpack_rest_connection_error_login_wpcom
ErrorType.FailedToConnectWpCom -> R.string.jetpack_rest_connection_error_connect_wpcom
ErrorType.LoginWpComFailed -> R.string.jetpack_rest_connection_error_login_wpcom
ErrorType.ConnectWpComFailed -> R.string.jetpack_rest_connection_error_connect_wpcom
ErrorType.InstallJetpackInactive -> R.string.jetpack_rest_connection_error_install_jetpack_inactive
is ErrorType.InstallJetpackFailed -> R.string.jetpack_rest_connection_error_install_jetpack
is ErrorType.Timeout -> R.string.jetpack_rest_connection_error_timeout
is ErrorType.Offline -> R.string.jetpack_rest_connection_error_offline
is ErrorType.Unknown -> R.string.jetpack_rest_connection_error_unknown
Expand Down Expand Up @@ -421,7 +423,7 @@ private fun JetpackRestConnectionScreenPreview() {
ConnectionStep.ConnectSite to StepState(ConnectionStatus.InProgress),
ConnectionStep.ConnectWpCom to StepState(
ConnectionStatus.Failed,
ErrorType.FailedToConnectWpCom
ErrorType.ConnectWpComFailed
),
ConnectionStep.Finalize to StepState(ConnectionStatus.NotStarted)
)
Expand Down
Loading