diff --git a/Cargo.lock b/Cargo.lock index ce85337..4414499 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2183,6 +2183,7 @@ version = "0.0.1" dependencies = [ "fixt", "futures", + "hc_zome_notifications_provider_fcm_types", "hc_zome_notifications_types", "hdk", "holochain", @@ -6696,6 +6697,7 @@ version = "0.0.1" dependencies = [ "fixt", "futures", + "hc_zome_notifications_provider_fcm_types", "hdk", "holochain", "serde", diff --git a/notifications/zomes/coordinator/notifications/src/lib.rs b/notifications/zomes/coordinator/notifications/src/lib.rs index 66ce465..0aa4f94 100644 --- a/notifications/zomes/coordinator/notifications/src/lib.rs +++ b/notifications/zomes/coordinator/notifications/src/lib.rs @@ -52,7 +52,8 @@ fn get_all_notifications_provider() -> ExternResult> { Ok(pubkeys) } -pub fn get_available_notification_provider() -> ExternResult> { +#[hdk_extern] +pub fn get_available_notification_provider(_: ()) -> ExternResult> { let all_providers = get_all_notifications_provider()?; for provider in all_providers { @@ -74,7 +75,7 @@ pub fn get_available_notification_provider() -> ExternResult #[hdk_extern] pub fn request_notify_agent(input: NotifyAgentInput) -> ExternResult<()> { - let Some(provider) = get_available_notification_provider()? else { + let Some(provider) = get_available_notification_provider(())? else { return Err(wasm_error!(WasmErrorInner::Guest(String::from("Can't find any notifications provider"))))?; }; diff --git a/providers/fcm/apps/notifications_provider_fcm/dnas/notifications_provider_fcm/workdir/dna.yaml b/providers/fcm/apps/notifications_provider_fcm/dnas/notifications_provider_fcm/workdir/dna.yaml index a3d782c..71ca0e5 100644 --- a/providers/fcm/apps/notifications_provider_fcm/dnas/notifications_provider_fcm/workdir/dna.yaml +++ b/providers/fcm/apps/notifications_provider_fcm/dnas/notifications_provider_fcm/workdir/dna.yaml @@ -15,7 +15,7 @@ coordinator: zomes: - name: notifications_provider_fcm hash: ~ - bundled: "../../../../../../../target/wasm32-unknown-unknown/release/hc_zome_notifications_provider_coordinator.wasm" + bundled: "../../../../../../../target/wasm32-unknown-unknown/release/hc_zome_notifications_provider_fcm_coordinator.wasm" dependencies: - name: notifications_provider_fcm_integrity dylib: ~ diff --git a/providers/fcm/zomes/coordinator/notifications_provider_fcm_bridge/Cargo.toml b/providers/fcm/zomes/coordinator/notifications_provider_fcm_bridge/Cargo.toml index f853000..d0bb9ad 100644 --- a/providers/fcm/zomes/coordinator/notifications_provider_fcm_bridge/Cargo.toml +++ b/providers/fcm/zomes/coordinator/notifications_provider_fcm_bridge/Cargo.toml @@ -13,6 +13,7 @@ hdk = { workspace = true } serde = { workspace = true } hc_zome_notifications_types = { workspace = true } +hc_zome_notifications_provider_fcm_types = { path = "../../../crates/notifications_provider_fcm_types" } [dev-dependencies] fixt = "*" diff --git a/providers/fcm/zomes/coordinator/notifications_provider_fcm_bridge/src/lib.rs b/providers/fcm/zomes/coordinator/notifications_provider_fcm_bridge/src/lib.rs index 4d20e90..d1df38c 100644 --- a/providers/fcm/zomes/coordinator/notifications_provider_fcm_bridge/src/lib.rs +++ b/providers/fcm/zomes/coordinator/notifications_provider_fcm_bridge/src/lib.rs @@ -1,3 +1,4 @@ +use hc_zome_notifications_provider_fcm_types::RegisterFCMTokenInput; use hdk::prelude::*; use hc_zome_notifications_types::NotifyAgentInput; @@ -21,3 +22,25 @@ pub fn notify_agent(input: NotifyAgentInput) -> ExternResult<()> { )))), } } + +#[hdk_extern] +pub fn register_fcm_token(token: String) -> ExternResult<()> { + let call_info = call_info()?; + let response = call( + CallTargetCell::OtherRole(String::from("notification_provider_fcm")), + ZomeName::from("notifications_provider_fcm"), + FunctionName::from("register_fcm_token_for_agent"), + None, + RegisterFCMTokenInput { + token, + agent: call_info.provenance, + }, + )?; + + match response { + ZomeCallResponse::Ok(_) => Ok(()), + _ => Err(wasm_error!(WasmErrorInner::Guest(format!( + "Error registering fcm token: {response:?}" + )))), + } +} diff --git a/providers/fcm/zomes/coordinator/notifications_provider_fcm_recipient/src/lib.rs b/providers/fcm/zomes/coordinator/notifications_provider_fcm_recipient/src/lib.rs index 51b3578..a8c8cf1 100644 --- a/providers/fcm/zomes/coordinator/notifications_provider_fcm_recipient/src/lib.rs +++ b/providers/fcm/zomes/coordinator/notifications_provider_fcm_recipient/src/lib.rs @@ -2,5 +2,36 @@ use hdk::prelude::*; #[hdk_extern] pub fn register_new_fcm_token(token: String) -> ExternResult<()> { - Ok(()) + let response = call( + CallTargetCell::Local, + "notifications", + "get_available_provider".into(), + None, + (), + )?; + + let ZomeCallResponse::Ok(result ) = response else { + return Err(wasm_error!(WasmErrorInner::Guest(format!("Failed to get available provider {response:?}")))); + }; + + let maybe_provider: Option = result.decode().map_err(|err| wasm_error!(err))?; + + let Some(provider) = maybe_provider else { + return Err(wasm_error!(WasmErrorInner::Guest(format!("There is no provider available")))); + }; + + let response = call_remote( + provider, + "notifications_provider_fcm_bridge", + "register_fcm_token".into(), + None, + token, + )?; + + match response { + ZomeCallResponse::Ok(_) => Ok(()), + _ => Err(wasm_error!(WasmErrorInner::Guest(format!( + "Error registering fcm token: {response:?}" + )))), + } } diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 47c619a..0757541 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" hdk = { workspace = true } serde = { workspace = true } +hc_zome_notifications_provider_fcm_types = { path = "../providers/fcm/crates/notifications_provider_fcm_types" } [dev-dependencies] fixt = "*" diff --git a/tests/tests/complex_flow.rs b/tests/tests/complex_flow.rs index c688880..70fbe6f 100644 --- a/tests/tests/complex_flow.rs +++ b/tests/tests/complex_flow.rs @@ -1,5 +1,6 @@ use std::collections::BTreeMap; +use hc_zome_notifications_provider_fcm_types::ServiceAccountKey; use hdk::prelude::*; use holochain::test_utils::consistency_10s; use holochain::{conductor::config::ConductorConfig, sweettest::*}; @@ -65,7 +66,18 @@ async fn complex_flow() { /* Setup provider */ // Publish Service Account Key + let _r: () = provider + .call( + &provider_fcm_zome, + "publish_new_service_account_key", + sample_service_account_key(), + ) + .await; + // assert_eq!(record_1, None); // Announce as provider + let _r: () = provider + .call(&provider_notifications_zome, "announce_as_provider", ()) + .await; /* Setup recipient */ // Register FCM token @@ -76,10 +88,30 @@ async fn complex_flow() { // FCM provider zome sends signal // Turn on recipient again - // let record_1: Option = conductors[0] - // .call(&alice_zome, "get_agent_profile", alice_pub_key) - // .await; - // assert_eq!(record_1, None); - // consistency_10s([&alice, &bobbo]).await; } + +fn sample_service_account_key() -> ServiceAccountKey { + ServiceAccountKey { + /// key_type + key_type: None, + /// project_id + project_id: None, + /// private_key_id + private_key_id: None, + /// private_key + private_key: String::from("pk"), + /// client_email + client_email: String::from("pk"), + /// client_id + client_id: None, + /// auth_uri + auth_uri: None, + /// token_uri + token_uri: String::from("tu"), + /// auth_provider_x509_cert_url + auth_provider_x509_cert_url: None, + /// client_x509_cert_url + client_x509_cert_url: None, + } +}