Skip to content

Commit

Permalink
Added register to bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed Dec 28, 2023
1 parent da1c003 commit a38babc
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions notifications/zomes/coordinator/notifications/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ fn get_all_notifications_provider() -> ExternResult<Vec<AgentPubKey>> {
Ok(pubkeys)
}

pub fn get_available_notification_provider() -> ExternResult<Option<AgentPubKey>> {
#[hdk_extern]
pub fn get_available_notification_provider(_: ()) -> ExternResult<Option<AgentPubKey>> {
let all_providers = get_all_notifications_provider()?;

for provider in all_providers {
Expand All @@ -74,7 +75,7 @@ pub fn get_available_notification_provider() -> ExternResult<Option<AgentPubKey>

#[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"))))?;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: ~
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "*"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use hc_zome_notifications_provider_fcm_types::RegisterFCMTokenInput;
use hdk::prelude::*;

use hc_zome_notifications_types::NotifyAgentInput;
Expand All @@ -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:?}"
)))),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<AgentPubKey> = 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:?}"
)))),
}
}
1 change: 1 addition & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "*"
Expand Down
42 changes: 37 additions & 5 deletions tests/tests/complex_flow.rs
Original file line number Diff line number Diff line change
@@ -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::*};
Expand Down Expand Up @@ -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
Expand All @@ -76,10 +88,30 @@ async fn complex_flow() {
// FCM provider zome sends signal
// Turn on recipient again

// let record_1: Option<Record> = 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,
}
}

0 comments on commit a38babc

Please sign in to comment.