-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55f662f
commit 76cc9ac
Showing
25 changed files
with
8,518 additions
and
59 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
manifest_version: "1" | ||
name: notifications_provider | ||
description: ~ | ||
roles: | ||
- name: notifications_provider_test | ||
provisioning: | ||
strategy: create | ||
deferred: false | ||
dna: | ||
bundled: "./notifications_provider_test.dna" | ||
modifiers: | ||
network_seed: ~ | ||
properties: ~ | ||
origin_time: ~ | ||
quantum_time: ~ | ||
installed_hash: ~ | ||
clone_limit: 0 | ||
- name: notifications_provider | ||
provisioning: | ||
strategy: create | ||
deferred: false | ||
dna: | ||
bundled: "../dnas/notifications_provider/workdir/notifications_provider.dna" | ||
modifiers: | ||
network_seed: ~ | ||
properties: ~ | ||
origin_time: ~ | ||
quantum_time: ~ | ||
installed_hash: ~ | ||
clone_limit: 0 | ||
- name: fcm_push_notifications_provider | ||
provisioning: | ||
strategy: create | ||
deferred: false | ||
dna: | ||
bundled: "../dnas/fcm_push_notifications_provider/workdir/fcm_push_notifications_provider.dna" | ||
modifiers: | ||
network_seed: ~ | ||
properties: ~ | ||
origin_time: ~ | ||
quantum_time: ~ | ||
installed_hash: ~ | ||
clone_limit: 0 |
10 changes: 10 additions & 0 deletions
10
crates/hc_zome_fcm_push_notifications_provider_types/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "hc_zome_fcm_push_notifications_provider_types" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
hdi = { workspace = true } | ||
serde = { workspace = true } |
52 changes: 52 additions & 0 deletions
52
crates/hc_zome_fcm_push_notifications_provider_types/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use hdi::prelude::*; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct NotifyAgentInput { | ||
pub notification: SerializedBytes, | ||
pub agent: AgentPubKey, | ||
} | ||
|
||
/// JSON schema of secret service account key. | ||
/// | ||
/// You can obtain the key from the [Cloud Console](https://console.cloud.google.com/). | ||
/// | ||
/// You can use `helpers::read_service_account_key()` as a quick way to read a JSON client | ||
/// secret into a ServiceAccountKey. | ||
#[hdk_entry_helper] | ||
#[derive(Clone)] | ||
pub struct ServiceAccountKey { | ||
#[serde(rename = "type")] | ||
/// key_type | ||
pub key_type: Option<String>, | ||
/// project_id | ||
pub project_id: Option<String>, | ||
/// private_key_id | ||
pub private_key_id: Option<String>, | ||
/// private_key | ||
pub private_key: String, | ||
/// client_email | ||
pub client_email: String, | ||
/// client_id | ||
pub client_id: Option<String>, | ||
/// auth_uri | ||
pub auth_uri: Option<String>, | ||
/// token_uri | ||
pub token_uri: String, | ||
/// auth_provider_x509_cert_url | ||
pub auth_provider_x509_cert_url: Option<String>, | ||
/// client_x509_cert_url | ||
pub client_x509_cert_url: Option<String>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct NotifyAgentSignal { | ||
pub notification: SerializedBytes, | ||
pub token: String, | ||
pub service_account_key: ServiceAccountKey, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct RegisterFCMTokenInput { | ||
pub token: String, | ||
pub agent: AgentPubKey, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "hc_zome_trait_pending_notifications" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
hdk = { workspace = true } | ||
serde = "1" | ||
hc_zome_traits = { git = "https://github.com/holochain-open-dev/zome-traits", branch = "main" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use hc_zome_traits::*; | ||
use hdk::prelude::{holo_hash::DnaHash, *}; | ||
|
||
pub type Hrl = (DnaHash, AnyDhtHash); | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct HrlWithContext { | ||
pub hrl: Hrl, | ||
pub context: SerializedBytes, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct PendingNotification { | ||
pub id: AnyDhtHash, | ||
pub title: String, | ||
pub body: String, | ||
// pub hrl_to_navigate_to_on_click: HrlWithContext, | ||
} | ||
|
||
#[zome_trait] | ||
pub trait PendingNotifications { | ||
fn get_pending_notifications(_: ()) -> ExternResult<Vec<PendingNotification>>; | ||
|
||
fn emit_new_pending_notification( | ||
pending_notification: PendingNotification, | ||
) -> ExternResult<()> { | ||
emit_signal(pending_notification)?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
manifest_version: "1" | ||
name: fcm_push_notifications_provider | ||
integrity: | ||
network_seed: ~ | ||
properties: ~ | ||
origin_time: 1703170182915758 | ||
zomes: | ||
- name: fcm_push_notifications_provider_integrity | ||
hash: ~ | ||
bundled: "../../../target/wasm32-unknown-unknown/release/fcm_push_notifications_provider_integrity.wasm" | ||
dependencies: ~ | ||
dylib: ~ | ||
coordinator: | ||
zomes: | ||
- name: fcm_push_notifications_provider | ||
hash: ~ | ||
bundled: "../../../target/wasm32-unknown-unknown/release/fcm_push_notifications_provider.wasm" | ||
dependencies: | ||
- name: fcm_push_notifications_provider_integrity | ||
dylib: ~ |
23 changes: 23 additions & 0 deletions
23
..._push_notifications_provider/zomes/coordinator/fcm_push_notifications_provider/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "hc_zome_fcm_push_notifications_provider_coordinator" | ||
version = "0.0.1" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
name = "hc_zome_fcm_push_notifications_provider_coordinator" | ||
|
||
[dependencies] | ||
hdk = { workspace = true } | ||
|
||
serde = { workspace = true } | ||
|
||
hc_zome_fcm_push_notifications_provider_integrity = { workspace = true } | ||
|
||
|
||
[dev-dependencies] | ||
fixt = "*" | ||
futures = { version = "0.3.1", default-features = false } | ||
hdk = { workspace = true, features = ["encoding", "test_utils"] } | ||
holochain = { workspace = true } | ||
tokio = { version = "1.3", features = ["full"] } |
37 changes: 37 additions & 0 deletions
37
...otifications_provider/zomes/coordinator/fcm_push_notifications_provider/src/fcm_tokens.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use fcm_notifications_provider_integrity::LinkTypes; | ||
use hc_zome_notifications_provider_types::RegisterFCMTokenInput; | ||
use hdk::prelude::*; | ||
|
||
#[hdk_extern] | ||
pub fn register_fcm_token_for_agent(input: RegisterFCMTokenInput) -> ExternResult<()> { | ||
let links = get_links(input.agent.clone(), LinkTypes::FCMToken, None)?; | ||
|
||
for link in links { | ||
delete_link(link.create_link_hash)?; | ||
} | ||
|
||
create_link( | ||
input.agent.clone(), | ||
input.agent, | ||
LinkTypes::FCMToken, | ||
input.token.as_bytes().to_vec(), | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn get_fcm_token_for_agent(agent: AgentPubKey) -> ExternResult<Option<String>> { | ||
let links = get_links(agent.clone(), LinkTypes::FCMToken, None)?; | ||
|
||
let Some(link) = links.first().cloned() else { | ||
return Ok(None); | ||
}; | ||
|
||
let token = String::from_utf8(link.tag.into_inner()).map_err(|err| { | ||
wasm_error!(WasmErrorInner::Guest(format!( | ||
"Malformed token tag {err:?}" | ||
))) | ||
})?; | ||
|
||
Ok(Some(token)) | ||
} |
26 changes: 26 additions & 0 deletions
26
..._push_notifications_provider/zomes/coordinator/fcm_push_notifications_provider/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use hc_zome_notifications_provider_types::{NotifyAgentInput, NotifyAgentSignal}; | ||
use hdk::prelude::*; | ||
|
||
mod fcm_tokens; | ||
mod service_account_keys; | ||
|
||
#[hdk_extern] | ||
pub fn notify_agent(input: NotifyAgentInput) -> ExternResult<()> { | ||
let Some(token ) = fcm_tokens::get_fcm_token_for_agent(input.agent)? else { | ||
return Err(wasm_error!(WasmErrorInner::Guest(String::from("Agent hasn't registered their FCM token yet")))); | ||
}; | ||
|
||
let Some(service_account_key)= service_account_keys::get_current_service_account_key()? else { | ||
return Err(wasm_error!(WasmErrorInner::Guest(String::from("FCM authority hasn't registered a service account key yet")))); | ||
}; | ||
|
||
let signal = NotifyAgentSignal { | ||
notification: input.notification, | ||
token, | ||
service_account_key, | ||
}; | ||
|
||
emit_signal(signal)?; | ||
|
||
Ok(()) | ||
} |
57 changes: 57 additions & 0 deletions
57
...ns_provider/zomes/coordinator/fcm_push_notifications_provider/src/service_account_keys.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use fcm_notifications_provider_integrity::*; | ||
use hc_zome_notifications_provider_types::ServiceAccountKey; | ||
use hdk::prelude::*; | ||
|
||
fn service_account_key_path() -> Path { | ||
Path::from("service_account_keys") | ||
} | ||
|
||
#[hdk_extern] | ||
pub fn publish_new_service_account_key(key: ServiceAccountKey) -> ExternResult<()> { | ||
let links = get_links( | ||
service_account_key_path().path_entry_hash()?, | ||
LinkTypes::ServiceAccountKeys, | ||
None, | ||
)?; | ||
|
||
for link in links { | ||
delete_link(link.create_link_hash)?; | ||
} | ||
|
||
let action_hash = create_entry(EntryTypes::ServiceAccountKey(key))?; | ||
|
||
create_link( | ||
service_account_key_path().path_entry_hash()?, | ||
action_hash, | ||
LinkTypes::ServiceAccountKeys, | ||
(), | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn get_current_service_account_key() -> ExternResult<Option<ServiceAccountKey>> { | ||
let links = get_links( | ||
service_account_key_path().path_entry_hash()?, | ||
LinkTypes::ServiceAccountKeys, | ||
None, | ||
)?; | ||
|
||
let Some(link) = links.first().cloned() else { | ||
return Ok(None); | ||
}; | ||
|
||
let Some(record) = get(link.target.into_any_dht_hash().ok_or(wasm_error!(WasmErrorInner::Guest(String::from("Malformed link"))))?, GetOptions ::default())? else { | ||
return Ok(None); | ||
}; | ||
|
||
let key: ServiceAccountKey = record | ||
.entry() | ||
.as_option() | ||
.ok_or(wasm_error!(WasmErrorInner::Guest(String::from( | ||
"Malformed key" | ||
))))? | ||
.try_into()?; | ||
|
||
Ok(Some(key)) | ||
} |
5 changes: 5 additions & 0 deletions
5
...ifications_provider/zomes/coordinator/fcm_push_notifications_provider/tests/common/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
use hdk::prelude::*; | ||
use holochain::sweettest::*; | ||
|
||
use fcm_push_notifications_provider_integrity::*; | ||
|
13 changes: 13 additions & 0 deletions
13
...cm_push_notifications_provider/zomes/integrity/fcm_push_notifications_provider/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "hc_zome_fcm_push_notifications_provider_integrity" | ||
version = "0.0.1" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
name = "hc_zome_fcm_push_notifications_provider_integrity" | ||
|
||
[dependencies] | ||
hdi = { workspace = true } | ||
|
||
serde = { workspace = true } |
17 changes: 17 additions & 0 deletions
17
...cm_push_notifications_provider/zomes/integrity/fcm_push_notifications_provider/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use hc_zome_fcm_push_notifications_provider_types::ServiceAccountKey; | ||
use hdi::prelude::*; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(tag = "type")] | ||
#[hdk_entry_defs] | ||
#[unit_enum(UnitEntryTypes)] | ||
pub enum EntryTypes { | ||
ServiceAccountKey(ServiceAccountKey), | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[hdk_link_types] | ||
pub enum LinkTypes { | ||
ServiceAccountKeys, | ||
FCMToken, | ||
} |
Oops, something went wrong.