Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion scripts/core-boundaries/rules/feature-rules.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export const optionalDependencyFeatureOwnerRules = [
{ depName: 'anyhow', ownerFeatures: ['browser-control', 'debug-log', 'feedback', 'mcp', 'remote-connect', 'remote-ssh', 'remote-ssh-concrete'] },
{
depName: 'async-trait',
ownerFeatures: ['feedback','git', 'mcp', 'remote-connect', 'remote-ssh', 'remote-ssh-concrete', 'review-platform', 'script-tool-runtime', 'speech', 'workspace-search'],
ownerFeatures: ['feedback', 'git', 'mcp', 'miniapp-market', 'remote-connect', 'remote-ssh', 'remote-ssh-concrete', 'review-platform', 'script-tool-runtime', 'speech', 'workspace-search'],
},
{
depName: 'base64',
Expand Down
69 changes: 31 additions & 38 deletions src/apps/desktop/src/api/miniapp_market_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use bitfun_services_integrations::miniapp_market::{
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
#[cfg(target_env = "ohos")]
use std::sync::Arc;
use std::sync::OnceLock;
use std::time::{SystemTime, UNIX_EPOCH};
use tauri::{AppHandle, Emitter, Manager, State, WebviewWindow};
Expand All @@ -32,6 +34,20 @@ use tokio::sync::Mutex;
const MARKET_UPLOAD_PROGRESS_EVENT: &str = "miniapp-market-upload-progress";
const MARKET_ACCOUNT_CHANGED_EVENT: &str = "miniapp-market-account-changed";

async fn market_client() -> Result<MarketClient, String> {
#[cfg(target_env = "ohos")]
{
let store =
Arc::new(crate::api::ohos::market_credentials::OhosMarketCredentialStore::new());
return MarketClient::from_environment_with_credential_store(store)
.await
.map_err(market_error);
}

#[cfg(not(target_env = "ohos"))]
MarketClient::from_environment().await.map_err(market_error)
}

#[derive(Debug, Clone)]
struct PendingDesktopAuth {
request: DesktopAuthPollRequest,
Expand Down Expand Up @@ -170,27 +186,21 @@ pub struct MarketPackageInspection {
pub async fn miniapp_market_browse(
request: MarketBrowseRequest,
) -> Result<CursorPage<MarketListingSummary>, String> {
let client = MarketClient::from_environment()
.await
.map_err(market_error)?;
let client = market_client().await?;
client.browse(&request).await.map_err(market_error)
}

#[tauri::command]
pub async fn miniapp_market_get_listing(
request: MarketSlugRequest,
) -> Result<MarketListingDetail, String> {
let mut client = MarketClient::from_environment()
.await
.map_err(market_error)?;
let mut client = market_client().await?;
client.listing(&request.slug).await.map_err(market_error)
}

#[tauri::command]
pub async fn miniapp_market_auth_start() -> Result<DesktopAuthStartView, String> {
let client = MarketClient::from_environment()
.await
.map_err(market_error)?;
let client = market_client().await?;
let started = client.start_desktop_auth().await.map_err(market_error)?;
let mut pending = pending_desktop_auth().lock().await;
let now = unix_now();
Expand Down Expand Up @@ -231,9 +241,7 @@ pub async fn miniapp_market_auth_poll(
}
pending
};
let mut client = MarketClient::from_environment()
.await
.map_err(market_error)?;
let mut client = market_client().await?;
let response: DesktopAuthPollResponse = client
.poll_desktop_auth(&pending.request)
.await
Expand All @@ -254,17 +262,13 @@ pub async fn miniapp_market_auth_poll(

#[tauri::command]
pub async fn miniapp_market_me() -> Result<Option<MarketMe>, String> {
let mut client = MarketClient::from_environment()
.await
.map_err(market_error)?;
let mut client = market_client().await?;
client.me().await.map_err(market_error)
}

#[tauri::command]
pub async fn miniapp_market_logout(app: AppHandle) -> Result<(), String> {
let mut client = MarketClient::from_environment()
.await
.map_err(market_error)?;
let mut client = market_client().await?;
client.logout().await.map_err(market_error)?;
emit_market_account_changed(&app, "signed-out");
Ok(())
Expand All @@ -277,9 +281,7 @@ pub async fn miniapp_market_set_rating(
if request.value.is_some_and(|value| !(1..=5).contains(&value)) {
return Err("Rating must be between 1 and 5.".to_string());
}
let mut client = MarketClient::from_environment()
.await
.map_err(market_error)?;
let mut client = market_client().await?;
client
.set_rating(&request.slug, request.value)
.await
Expand All @@ -290,9 +292,7 @@ pub async fn miniapp_market_set_rating(
pub async fn miniapp_market_set_favorite(
request: MarketSetFavoriteRequest,
) -> Result<FavoriteAggregate, String> {
let mut client = MarketClient::from_environment()
.await
.map_err(market_error)?;
let mut client = market_client().await?;
client
.set_favorite(&request.slug, request.enabled)
.await
Expand All @@ -301,19 +301,15 @@ pub async fn miniapp_market_set_favorite(

#[tauri::command]
pub async fn miniapp_market_list_submissions() -> Result<Vec<MarketSubmission>, String> {
let mut client = MarketClient::from_environment()
.await
.map_err(market_error)?;
let mut client = market_client().await?;
client.list_submissions().await.map_err(market_error)
}

#[tauri::command]
pub async fn miniapp_market_withdraw_submission(
request: MarketSubmissionIdRequest,
) -> Result<MarketSubmission, String> {
let mut client = MarketClient::from_environment()
.await
.map_err(market_error)?;
let mut client = market_client().await?;
client
.withdraw_submission(&request.submission_id)
.await
Expand Down Expand Up @@ -397,9 +393,7 @@ pub async fn miniapp_market_install(
state: State<'_, AppState>,
request: MarketInstallRequest,
) -> Result<MarketInstallResult, String> {
let mut client = MarketClient::from_environment()
.await
.map_err(market_error)?;
let mut client = market_client().await?;
let detail = client.listing(&request.slug).await.map_err(market_error)?;
let release = find_release(&detail, request.release_number)?;
validate_minimum_bitfun_version(&release.min_bitfun_version)?;
Expand Down Expand Up @@ -588,7 +582,9 @@ pub async fn miniapp_market_capture_window(
.outer_size()
.map_err(|error| format!("Could not read the BitFun window size: {error}"))?;
if size.width < 320 || size.height < 240 {
return Err("The BitFun window is too small to capture a review screenshot.".to_string());
return Err(
"The BitFun window is too small to capture a review screenshot.".to_string(),
);
}

let capture_dir = app
Expand Down Expand Up @@ -649,7 +645,6 @@ pub async fn miniapp_market_capture_window(
{
Err("Unable to support the ohos".to_string())
}

}

#[tauri::command]
Expand All @@ -663,9 +658,7 @@ pub async fn miniapp_market_submit_installed(
.get(&request.app_id)
.await
.map_err(|error| error.to_string())?;
let mut client = MarketClient::from_environment()
.await
.map_err(market_error)?;
let mut client = market_client().await?;
let mut progress = |submission_id: Option<&str>, phase: &'static str, completed, total| {
emit_upload_progress(&app, submission_id, phase, completed, total);
};
Expand Down
113 changes: 113 additions & 0 deletions src/apps/desktop/src/api/ohos/market_credentials.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#![cfg(target_env = "ohos")]

use async_trait::async_trait;
use bitfun_services_integrations::miniapp_market::{
MarketCredentialStore, StoredMarketCredentials,
};
use serde::{Deserialize, Serialize};

const ARKTS_FUNCTION: &str = "market_secure_credentials";

#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case")]
enum CredentialAction {
Load,
Store,
Clear,
}

#[derive(Debug, Serialize)]
struct CredentialRequest<'a> {
action: CredentialAction,
#[serde(skip_serializing_if = "Option::is_none")]
value: Option<&'a str>,
}

#[derive(Debug, Deserialize)]
struct CredentialResponse {
status: String,
value: Option<String>,
code: Option<String>,
}

#[derive(Debug)]
pub struct OhosMarketCredentialStore;

impl OhosMarketCredentialStore {
pub fn new() -> Self {
Self
}

async fn call(&self, request: CredentialRequest<'_>) -> Result<CredentialResponse, String> {
let input = serde_json::to_string(&request)
.map_err(|error| format!("encode market credential request: {error}"))?;
let output = bitfun_core::util::call_arkts_string_function(ARKTS_FUNCTION, input)
.await
.map_err(|error| format!("call OpenHarmony market credential store: {error}"))?;
serde_json::from_str(&output)
.map_err(|error| format!("decode market credential response: {error}"))
}
}

#[async_trait]
impl MarketCredentialStore for OhosMarketCredentialStore {
async fn load(&self) -> Result<Option<StoredMarketCredentials>, String> {
let response = self
.call(CredentialRequest {
action: CredentialAction::Load,
value: None,
})
.await?;
match response.status.as_str() {
"ok" => response
.value
.ok_or_else(|| "market credential response omitted its value".to_string())
.and_then(|value| {
serde_json::from_str(&value)
.map_err(|error| format!("parse market credentials: {error}"))
})
.map(Some),
"not_found" => Ok(None),
_ => Err(format!(
"OpenHarmony market credential load failed: code={}",
response.code.as_deref().unwrap_or("UNKNOWN")
)),
}
}

async fn save(&self, credentials: &StoredMarketCredentials) -> Result<(), String> {
let value = serde_json::to_string(credentials)
.map_err(|error| format!("serialize market credentials: {error}"))?;
let response = self
.call(CredentialRequest {
action: CredentialAction::Store,
value: Some(&value),
})
.await?;
if response.status == "ok" {
Ok(())
} else {
Err(format!(
"OpenHarmony market credential store failed: code={}",
response.code.as_deref().unwrap_or("UNKNOWN")
))
}
}

async fn clear(&self) -> Result<(), String> {
let response = self
.call(CredentialRequest {
action: CredentialAction::Clear,
value: None,
})
.await?;
if matches!(response.status.as_str(), "ok" | "not_found") {
Ok(())
} else {
Err(format!(
"OpenHarmony market credential delete failed: code={}",
response.code.as_deref().unwrap_or("UNKNOWN")
))
}
}
}
6 changes: 4 additions & 2 deletions src/apps/desktop/src/api/ohos/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub mod ohos_file_system;
pub mod window;
pub mod browser;
#[cfg(target_env = "ohos")]
pub mod feedback_credentials;
#[cfg(target_env = "ohos")]
pub mod market_credentials;
pub mod ohos_file_system;
pub mod update;
pub mod window;
Loading
Loading