diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2026-02-18 17:12:17 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2026-02-18 17:12:17 +0000 |
| commit | d76003b629a4a03dba23a8a1c41da6e4ac4c30cf (patch) | |
| tree | 38412fdeed3e7996923603fe1964db4e5ce94bdc /src/nostr | |
| parent | 806936e7d1aab5dfd0c2ad6b98a115122dc1785c (diff) | |
feat: upgrade repo to Full sync and trigger PR event subscription after announcement promotion
When git data arrives for a purgatory announcement and promotes it to the
database, the relay now:
1. Upgrades the announcement's sync level in RepoSyncIndex from StateOnly
to Full (git/sync.rs: process_purgatory_announcements)
2. Sends AddFilters actions to SyncManager for all connected relays, using
Full sync filters (Layer 2 #a/#A/#q) to subscribe to PR events
(purgatory/sync/context.rs: RealSyncContext.process_newly_available_git_data)
3. For user-submitted purgatory announcements, registers the repo in
RepoSyncIndex with StateOnly level and sends AddFilters to SyncManager
so it discovers and connects to relays listed in the announcement tags
(nostr/builder.rs: handle_announcement AcceptPurgatory path)
The RealSyncContext now accepts optional repo_sync_index and sync_action_tx
parameters. main.rs wires these up from SyncManager. PolicyContext gains
repo_sync_index and sync_action_tx fields for the write policy path.
Diffstat (limited to 'src/nostr')
| -rw-r--r-- | src/nostr/builder.rs | 118 | ||||
| -rw-r--r-- | src/nostr/policy/mod.rs | 37 |
2 files changed, 155 insertions, 0 deletions
diff --git a/src/nostr/builder.rs b/src/nostr/builder.rs index aff12a6..4c66f6d 100644 --- a/src/nostr/builder.rs +++ b/src/nostr/builder.rs | |||
| @@ -98,6 +98,24 @@ impl Nip34WritePolicy { | |||
| 98 | self.ctx.set_local_relay(relay); | 98 | self.ctx.set_local_relay(relay); |
| 99 | } | 99 | } |
| 100 | 100 | ||
| 101 | /// Set the repo sync index for relay discovery from user-submitted purgatory announcements. | ||
| 102 | /// | ||
| 103 | /// When a user submits an announcement that goes to purgatory (no git data yet), | ||
| 104 | /// the relay needs to discover and connect to relays listed in the announcement's | ||
| 105 | /// `relays` and `clone` tags. This index is updated when the announcement is accepted | ||
| 106 | /// into purgatory, triggering the sync system to connect and sync state events. | ||
| 107 | pub fn set_repo_sync_index(&self, index: crate::sync::RepoSyncIndex) { | ||
| 108 | self.ctx.set_repo_sync_index(index); | ||
| 109 | } | ||
| 110 | |||
| 111 | /// Set the sync action sender for sending AddFilters actions to SyncManager. | ||
| 112 | /// | ||
| 113 | /// This allows the write policy to notify the SyncManager when user-submitted | ||
| 114 | /// purgatory announcements need relay discovery (triggering new connections). | ||
| 115 | pub fn set_sync_action_tx(&self, tx: tokio::sync::mpsc::Sender<crate::sync::AddFilters>) { | ||
| 116 | self.ctx.set_sync_action_tx(tx); | ||
| 117 | } | ||
| 118 | |||
| 101 | /// Handle repository announcement event | 119 | /// Handle repository announcement event |
| 102 | async fn handle_announcement(&self, event: &Event) -> WritePolicyResult { | 120 | async fn handle_announcement(&self, event: &Event) -> WritePolicyResult { |
| 103 | let event_id_str = event.id.to_bech32().unwrap_or_else(|_| event.id.to_hex()); | 121 | let event_id_str = event.id.to_bech32().unwrap_or_else(|_| event.id.to_hex()); |
| @@ -146,6 +164,106 @@ impl Nip34WritePolicy { | |||
| 146 | "Accepted announcement to purgatory: {} (waiting for git data)", | 164 | "Accepted announcement to purgatory: {} (waiting for git data)", |
| 147 | event_id_str | 165 | event_id_str |
| 148 | ); | 166 | ); |
| 167 | |||
| 168 | // Register in repo_sync_index with StateOnly level so the sync | ||
| 169 | // system discovers and connects to relays listed in this announcement. | ||
| 170 | // This is needed for user-submitted announcements (not via sync path) | ||
| 171 | // to trigger relay discovery and state event sync. | ||
| 172 | if let Some(repo_sync_index) = self.ctx.get_repo_sync_index() { | ||
| 173 | if let Some(identifier) = event.tags.iter().find_map(|tag| { | ||
| 174 | let tag_vec = tag.as_slice(); | ||
| 175 | if tag_vec.len() >= 2 && tag_vec[0] == "d" { | ||
| 176 | Some(tag_vec[1].to_string()) | ||
| 177 | } else { | ||
| 178 | None | ||
| 179 | } | ||
| 180 | }) { | ||
| 181 | let repo_id = | ||
| 182 | format!("30617:{}:{}", event.pubkey, identifier); | ||
| 183 | |||
| 184 | // Get relay URLs stored in purgatory for this announcement | ||
| 185 | let relays = self | ||
| 186 | .ctx | ||
| 187 | .purgatory | ||
| 188 | .find_announcement(&event.pubkey, &identifier) | ||
| 189 | .map(|entry| entry.relays) | ||
| 190 | .unwrap_or_default(); | ||
| 191 | |||
| 192 | if !relays.is_empty() { | ||
| 193 | use crate::sync::{ | ||
| 194 | AddFilters, PendingItems, RepoSyncNeeds, SyncLevel, | ||
| 195 | }; | ||
| 196 | |||
| 197 | // Update repo_sync_index with StateOnly for this repo | ||
| 198 | let new_repos = { | ||
| 199 | let mut index = repo_sync_index.write().await; | ||
| 200 | let entry = | ||
| 201 | index.entry(repo_id.clone()).or_insert_with(|| { | ||
| 202 | RepoSyncNeeds { | ||
| 203 | relays: std::collections::HashSet::new(), | ||
| 204 | root_events: std::collections::HashSet::new(), | ||
| 205 | sync_level: SyncLevel::StateOnly, | ||
| 206 | } | ||
| 207 | }); | ||
| 208 | entry.relays.extend(relays.iter().cloned()); | ||
| 209 | // Don't upgrade if already Full | ||
| 210 | tracing::info!( | ||
| 211 | repo_id = %repo_id, | ||
| 212 | relay_count = entry.relays.len(), | ||
| 213 | "Registered user-submitted purgatory announcement in \ | ||
| 214 | RepoSyncIndex with StateOnly level for relay discovery" | ||
| 215 | ); | ||
| 216 | // Return cloned relays for AddFilters | ||
| 217 | relays.clone() | ||
| 218 | }; | ||
| 219 | |||
| 220 | // Send AddFilters to SyncManager so it connects to these relays | ||
| 221 | if let Some(tx) = self.ctx.get_sync_action_tx() { | ||
| 222 | // Build state-only filters for this repo | ||
| 223 | let state_only_repos: std::collections::HashSet<String> = | ||
| 224 | std::iter::once(repo_id.clone()).collect(); | ||
| 225 | let filters = | ||
| 226 | crate::sync::filters::build_sync_level_aware_filters( | ||
| 227 | &std::collections::HashSet::new(), | ||
| 228 | &state_only_repos, | ||
| 229 | &std::collections::HashSet::new(), | ||
| 230 | None, | ||
| 231 | ); | ||
| 232 | |||
| 233 | for relay_url in new_repos { | ||
| 234 | // Skip our own domain | ||
| 235 | if relay_url.contains(&self.ctx.domain) { | ||
| 236 | continue; | ||
| 237 | } | ||
| 238 | let action = AddFilters { | ||
| 239 | relay_url: relay_url.clone(), | ||
| 240 | items: PendingItems { | ||
| 241 | repos: state_only_repos.clone(), | ||
| 242 | root_events: std::collections::HashSet::new(), | ||
| 243 | }, | ||
| 244 | filters: filters.clone(), | ||
| 245 | }; | ||
| 246 | if let Err(e) = tx.send(action).await { | ||
| 247 | tracing::warn!( | ||
| 248 | relay = %relay_url, | ||
| 249 | error = %e, | ||
| 250 | "Failed to send AddFilters action for \ | ||
| 251 | user-submitted purgatory announcement" | ||
| 252 | ); | ||
| 253 | } else { | ||
| 254 | tracing::info!( | ||
| 255 | relay = %relay_url, | ||
| 256 | repo_id = %repo_id, | ||
| 257 | "Sent AddFilters to SyncManager for \ | ||
| 258 | user-submitted purgatory announcement relay" | ||
| 259 | ); | ||
| 260 | } | ||
| 261 | } | ||
| 262 | } | ||
| 263 | } | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 149 | WritePolicyResult::Reject { | 267 | WritePolicyResult::Reject { |
| 150 | status: true, // Client sees OK | 268 | status: true, // Client sees OK |
| 151 | message: "purgatory: won't be served until git data arrives".into(), | 269 | message: "purgatory: won't be served until git data arrives".into(), |
diff --git a/src/nostr/policy/mod.rs b/src/nostr/policy/mod.rs index 1566b6c..78a09fc 100644 --- a/src/nostr/policy/mod.rs +++ b/src/nostr/policy/mod.rs | |||
| @@ -20,6 +20,7 @@ pub use crate::git::sync::AlignmentResult; | |||
| 20 | 20 | ||
| 21 | use super::SharedDatabase; | 21 | use super::SharedDatabase; |
| 22 | use crate::purgatory::Purgatory; | 22 | use crate::purgatory::Purgatory; |
| 23 | use crate::sync::{AddFilters, RepoSyncIndex}; | ||
| 23 | use nostr_relay_builder::LocalRelay; | 24 | use nostr_relay_builder::LocalRelay; |
| 24 | use std::sync::Arc; | 25 | use std::sync::Arc; |
| 25 | 26 | ||
| @@ -34,6 +35,16 @@ pub struct PolicyContext { | |||
| 34 | pub local_relay: Arc<std::sync::RwLock<Option<LocalRelay>>>, | 35 | pub local_relay: Arc<std::sync::RwLock<Option<LocalRelay>>>, |
| 35 | /// Configuration reference for policy settings (includes blacklists) | 36 | /// Configuration reference for policy settings (includes blacklists) |
| 36 | pub config: crate::config::Config, | 37 | pub config: crate::config::Config, |
| 38 | /// Optional repo sync index for triggering relay discovery when announcements | ||
| 39 | /// go to purgatory via user submission (not via the sync path). | ||
| 40 | /// Wrapped in Arc<RwLock> for interior mutability (PolicyContext is Clone). | ||
| 41 | pub repo_sync_index: Arc<std::sync::RwLock<Option<RepoSyncIndex>>>, | ||
| 42 | /// Optional sender for AddFilters actions to SyncManager. | ||
| 43 | /// Used to trigger relay discovery when user-submitted purgatory announcements | ||
| 44 | /// are registered with StateOnly sync level. | ||
| 45 | /// Wrapped in Arc<RwLock> for interior mutability (PolicyContext is Clone). | ||
| 46 | pub sync_action_tx: | ||
| 47 | Arc<std::sync::RwLock<Option<tokio::sync::mpsc::Sender<AddFilters>>>>, | ||
| 37 | } | 48 | } |
| 38 | 49 | ||
| 39 | impl PolicyContext { | 50 | impl PolicyContext { |
| @@ -51,6 +62,8 @@ impl PolicyContext { | |||
| 51 | purgatory, | 62 | purgatory, |
| 52 | local_relay: Arc::new(std::sync::RwLock::new(None)), | 63 | local_relay: Arc::new(std::sync::RwLock::new(None)), |
| 53 | config, | 64 | config, |
| 65 | repo_sync_index: Arc::new(std::sync::RwLock::new(None)), | ||
| 66 | sync_action_tx: Arc::new(std::sync::RwLock::new(None)), | ||
| 54 | } | 67 | } |
| 55 | } | 68 | } |
| 56 | 69 | ||
| @@ -68,4 +81,28 @@ impl PolicyContext { | |||
| 68 | let guard = self.local_relay.read().unwrap(); | 81 | let guard = self.local_relay.read().unwrap(); |
| 69 | guard.clone() | 82 | guard.clone() |
| 70 | } | 83 | } |
| 84 | |||
| 85 | /// Set the repo sync index for relay discovery from user-submitted purgatory announcements. | ||
| 86 | pub fn set_repo_sync_index(&self, index: RepoSyncIndex) { | ||
| 87 | let mut guard = self.repo_sync_index.write().unwrap(); | ||
| 88 | *guard = Some(index); | ||
| 89 | } | ||
| 90 | |||
| 91 | /// Get a clone of the repo sync index if it's been set. | ||
| 92 | pub fn get_repo_sync_index(&self) -> Option<RepoSyncIndex> { | ||
| 93 | let guard = self.repo_sync_index.read().unwrap(); | ||
| 94 | guard.clone() | ||
| 95 | } | ||
| 96 | |||
| 97 | /// Set the sync action sender for sending AddFilters actions to SyncManager. | ||
| 98 | pub fn set_sync_action_tx(&self, tx: tokio::sync::mpsc::Sender<AddFilters>) { | ||
| 99 | let mut guard = self.sync_action_tx.write().unwrap(); | ||
| 100 | *guard = Some(tx); | ||
| 101 | } | ||
| 102 | |||
| 103 | /// Get a clone of the sync action sender if it's been set. | ||
| 104 | pub fn get_sync_action_tx(&self) -> Option<tokio::sync::mpsc::Sender<AddFilters>> { | ||
| 105 | let guard = self.sync_action_tx.read().unwrap(); | ||
| 106 | guard.clone() | ||
| 107 | } | ||
| 71 | } | 108 | } |