Skip to content

Commit 6451953

Browse files
authored
Dynamically add new contact subscriptions (#584)
* Dynamically add new contact subscriptions * Review fixes: only subscribe if contact added * Fix comment
1 parent a6908ab commit 6451953

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

crates/bcr-ebill-api/src/service/notification_service/default_service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ impl NotificationServiceApi for DefaultNotificationService {
335335
async fn add_company_transport(&self, company: &Company, keys: &BcrKeys) -> Result<()> {
336336
self.add_company_client(company, keys).await
337337
}
338-
339338
/// Sent when an identity chain is created or updated
340339
async fn send_identity_chain_events(&self, events: IdentityChainEvent) -> Result<()> {
341340
debug!(
@@ -883,6 +882,7 @@ mod tests {
883882
root_event: Option<nostr::event::Event>) -> bcr_ebill_transport::Result<nostr::event::Event>;
884883
async fn resolve_contact(&self, node_id: &NodeId) -> Result<Option<bcr_ebill_transport::transport::NostrContactData>>;
885884
async fn resolve_public_chain(&self, id: &str, chain_type: BlockchainType) -> Result<Vec<nostr::event::Event>>;
885+
async fn add_contact_subscription(&self, contact: &NodeId) -> Result<()>;
886886
}
887887
}
888888

crates/bcr-ebill-api/src/service/notification_service/nostr.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,12 @@ impl NotificationJsonTransportApi for NostrClient {
383383
.fetch_events(chain_filter(id, chain_type), Some(SortOrder::Asc), None)
384384
.await?)
385385
}
386+
387+
async fn add_contact_subscription(&self, node_id: &NodeId) -> Result<()> {
388+
debug!("adding nostr subscription for contact {node_id}");
389+
self.subscribe(Filter::new().author(node_id.npub())).await?;
390+
Ok(())
391+
}
386392
}
387393

388394
#[derive(Clone)]

crates/bcr-ebill-transport/src/handler/bill_chain_event_processor.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,24 +121,30 @@ impl BillChainEventProcessor {
121121
.iter()
122122
.map(|r| r.as_str().to_owned())
123123
.collect();
124-
if let Err(e) = self
125-
.nostr_contact_store
126-
.upsert(&NostrContact {
124+
self.upsert_contact(
125+
node_id,
126+
&NostrContact {
127127
npub: node_id.npub(),
128128
name: contact.metadata.name,
129129
relays,
130130
trust_level: TrustLevel::Participant,
131131
handshake_status: HandshakeStatus::None,
132-
})
133-
.await
134-
{
135-
error!("Failed to save nostr contact information for node_id {node_id}: {e}");
136-
}
132+
},
133+
)
134+
.await;
137135
} else {
138136
info!("Could not resolve nostr contact information for node_id {node_id}");
139137
}
140138
}
141139

140+
async fn upsert_contact(&self, node_id: &NodeId, contact: &NostrContact) {
141+
if let Err(e) = self.nostr_contact_store.upsert(contact).await {
142+
error!("Failed to save nostr contact information for node_id {node_id}: {e}");
143+
} else if let Err(e) = self.transport.add_contact_subscription(node_id).await {
144+
error!("Failed to add nostr contact subscription for contact node_id {node_id}: {e}");
145+
}
146+
}
147+
142148
async fn add_bill_blocks(
143149
&self,
144150
bill_id: &BillId,
@@ -549,7 +555,7 @@ mod tests {
549555
// If we don't have the contact in the store, we will try to resolve it via Nostr
550556
contact_store.expect_by_node_id().returning(|_| Ok(None));
551557

552-
// If we get data it should be store to the store
558+
// If we get data it should be stored to the store
553559
transport.expect_resolve_contact().returning(|_| {
554560
Ok(Some(NostrContactData {
555561
metadata: Metadata {
@@ -560,8 +566,14 @@ mod tests {
560566
}))
561567
});
562568

569+
// Store new contact
563570
contact_store.expect_upsert().returning(|_| Ok(()));
564571

572+
// Subscribe to contact
573+
transport
574+
.expect_add_contact_subscription()
575+
.returning(|_| Ok(()));
576+
565577
let handler = BillChainEventProcessor::new(
566578
Arc::new(bill_chain_store),
567579
Arc::new(bill_store),

crates/bcr-ebill-transport/src/transport.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ pub trait NotificationJsonTransportApi: ServiceTraitBounds {
6666
id: &str,
6767
chain_type: BlockchainType,
6868
) -> Result<Vec<Event>>;
69+
/// Adds a new Nostr subscription on the primary client for an added contact
70+
async fn add_contact_subscription(&self, contact: &NodeId) -> Result<()>;
6971
}
7072

7173
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)