Skip to content

Commit

Permalink
Imported code
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed Dec 21, 2023
1 parent 55f662f commit 76cc9ac
Show file tree
Hide file tree
Showing 25 changed files with 8,518 additions and 59 deletions.
7,962 changes: 7,962 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

31 changes: 24 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,31 @@ opt-level = "z"
opt-level = "z"

[workspace]
members = ["zomes/coordinator/*", "zomes/integrity/*"]
members = ["dnas/*/zomes/coordinator/*", "dnas/*/zomes/integrity/*", "crates/*"]
resolver = "2"

[workspace.dependencies]
holochain = { version = "0.2.2", default-features = false, features = [
"test_utils",
] }
hdk = "0.2"
hdi = "0.3"
serde = "1"
hdi = "=0.3.4"
hdk = "=0.2.4"
serde = "=1.0.166"


[workspace.dependencies.hc_zome_notifications_provider_coordinator]
path = "dnas/notifications_provider/zomes/coordinator/notifications_provider"

[workspace.dependencies.hc_zome_notifications_provider_integrity]
path = "dnas/notifications_provider/zomes/integrity/notifications_provider"

[workspace.dependencies.hc_zome_fcm_push_notifications_provider_types]
path = "crates/hc_zome_fcm_push_notifications_provider_types"

[workspace.dependencies.hc_zome_fcm_push_notifications_provider_coordinator]
path = "dnas/fcm_push_notifications_provider/zomes/coordinator/fcm_push_notifications_provider"

[workspace.dependencies.hc_zome_fcm_push_notifications_provider_integrity]
path = "dnas/fcm_push_notifications_provider/zomes/integrity/fcm_push_notifications_provider"

[workspace.dependencies.holochain]
default-features = false
features = ["test_utils"]
version = "0.2.2"
44 changes: 44 additions & 0 deletions apps/fcm_push_notifications_provider/happ.yaml
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 crates/hc_zome_fcm_push_notifications_provider_types/Cargo.toml
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 crates/hc_zome_fcm_push_notifications_provider_types/src/lib.rs
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,
}
11 changes: 11 additions & 0 deletions crates/hc_zome_trait_pending_notifications/Cargo.toml
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" }
31 changes: 31 additions & 0 deletions crates/hc_zome_trait_pending_notifications/src/lib.rs
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(())
}
}
21 changes: 21 additions & 0 deletions dnas/fcm_push_notifications_provider/workdir/dna.yaml
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: ~
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"] }
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))
}
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(())
}
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))
}
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::*;

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 }
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,
}
Loading

0 comments on commit 76cc9ac

Please sign in to comment.