diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-12-24 08:02:12 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-12-24 11:54:18 +0000 |
| commit | 70d0197e85ae4ef85202781f6d2dc9e76bd508b3 (patch) | |
| tree | 45efb6565e81ba755acc5955e68d5b7119d1e122 /src/nostr/policy/state.rs | |
| parent | f8c3e3920ed2a1bdaab30be912276993449a5476 (diff) | |
feat(purgatory): add broken purgatory implementation
Diffstat (limited to 'src/nostr/policy/state.rs')
| -rw-r--r-- | src/nostr/policy/state.rs | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/src/nostr/policy/state.rs b/src/nostr/policy/state.rs index 43349e2..5e749ed 100644 --- a/src/nostr/policy/state.rs +++ b/src/nostr/policy/state.rs | |||
| @@ -66,6 +66,24 @@ impl StatePolicy { | |||
| 66 | let state = RepositoryState::from_event(event.clone()) | 66 | let state = RepositoryState::from_event(event.clone()) |
| 67 | .map_err(|e| format!("Failed to parse state: {}", e))?; | 67 | .map_err(|e| format!("Failed to parse state: {}", e))?; |
| 68 | 68 | ||
| 69 | // Check if ANY git repositories exist for this identifier (regardless of authorization) | ||
| 70 | // This helps us distinguish "no git data yet" from "not authorized" or "not latest" | ||
| 71 | let has_any_git_data = self.has_git_data_for_identifier(&state.identifier); | ||
| 72 | |||
| 73 | if !has_any_git_data { | ||
| 74 | // No git data exists yet - add to purgatory | ||
| 75 | tracing::debug!( | ||
| 76 | "No git data found for identifier {}, adding state event {} to purgatory", | ||
| 77 | state.identifier, | ||
| 78 | event.id.to_hex() | ||
| 79 | ); | ||
| 80 | self.ctx | ||
| 81 | .purgatory | ||
| 82 | .add_state(event.clone(), state.identifier.clone(), event.pubkey); | ||
| 83 | // Return 0 repos aligned, but this is not an error | ||
| 84 | return Ok(0); | ||
| 85 | } | ||
| 86 | |||
| 69 | // Identify owner repositories for which this is the latest authorized state | 87 | // Identify owner repositories for which this is the latest authorized state |
| 70 | let owner_repos = self.identify_owner_repositories(&state).await?; | 88 | let owner_repos = self.identify_owner_repositories(&state).await?; |
| 71 | let repo_count = owner_repos.len(); | 89 | let repo_count = owner_repos.len(); |
| @@ -97,13 +115,48 @@ impl StatePolicy { | |||
| 97 | ); | 115 | ); |
| 98 | } else { | 116 | } else { |
| 99 | tracing::debug!( | 117 | tracing::debug!( |
| 100 | "No owner repos to align for state - git data not available yet or not latest" | 118 | "No owner repos to align for state - git data exists but author not authorized or not latest" |
| 101 | ); | 119 | ); |
| 102 | } | 120 | } |
| 103 | 121 | ||
| 104 | Ok(total_aligned) | 122 | Ok(total_aligned) |
| 105 | } | 123 | } |
| 106 | 124 | ||
| 125 | /// Check if any git repositories exist for the given identifier | ||
| 126 | /// | ||
| 127 | /// Scans the git_data_path for any directories matching the pattern: | ||
| 128 | /// `<any-npub>/<identifier>.git` | ||
| 129 | /// | ||
| 130 | /// This is used to distinguish "no git data yet" from "not authorized". | ||
| 131 | fn has_git_data_for_identifier(&self, identifier: &str) -> bool { | ||
| 132 | let git_data_path = &self.ctx.git_data_path; | ||
| 133 | |||
| 134 | // Check if git_data_path exists | ||
| 135 | if !git_data_path.exists() { | ||
| 136 | return false; | ||
| 137 | } | ||
| 138 | |||
| 139 | // Scan for any npub directories | ||
| 140 | let read_dir = match std::fs::read_dir(git_data_path) { | ||
| 141 | Ok(dir) => dir, | ||
| 142 | Err(_) => return false, | ||
| 143 | }; | ||
| 144 | |||
| 145 | for entry in read_dir.flatten() { | ||
| 146 | if let Ok(file_type) = entry.file_type() { | ||
| 147 | if file_type.is_dir() { | ||
| 148 | // Check if <npub>/<identifier>.git exists | ||
| 149 | let repo_path = entry.path().join(format!("{}.git", identifier)); | ||
| 150 | if repo_path.exists() { | ||
| 151 | return true; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | false | ||
| 158 | } | ||
| 159 | |||
| 107 | /// Check if this state event is the latest for its identifier among authorized authors | 160 | /// Check if this state event is the latest for its identifier among authorized authors |
| 108 | /// | 161 | /// |
| 109 | /// A state is considered "latest" if no other state event in the database | 162 | /// A state is considered "latest" if no other state event in the database |