From 768fe91caa676e4501aa26e14e01ca47f3ea4ca1 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Wed, 31 Dec 2025 09:18:21 +0000 Subject: purgatory: fix pr event recieve code --- src/nostr/policy/state.rs | 187 +--------------------------------------------- 1 file changed, 2 insertions(+), 185 deletions(-) (limited to 'src/nostr/policy/state.rs') diff --git a/src/nostr/policy/state.rs b/src/nostr/policy/state.rs index 13f2549..1203890 100644 --- a/src/nostr/policy/state.rs +++ b/src/nostr/policy/state.rs @@ -6,15 +6,12 @@ use nostr_relay_builder::builder::WritePolicyResult; /// /// Handles validation of NIP-34 repository state events (kind 30618) /// and aligns git refs with authorized state according to GRASP-01. -use nostr_relay_builder::prelude::{Alphabet, Event, Filter, Kind, PublicKey, SingleLetterTag}; +use nostr_relay_builder::prelude::Event; use super::PolicyContext; use crate::git::authorization::{collect_authorized_maintainers, fetch_repository_data}; use crate::git::{self}; -use crate::nostr::events::{ - validate_state, RepositoryAnnouncement, RepositoryState, KIND_REPOSITORY_ANNOUNCEMENT, - KIND_REPOSITORY_STATE, -}; +use crate::nostr::events::{validate_state, RepositoryAnnouncement, RepositoryState}; /// Result of aligning a repository with authorized state #[derive(Debug, Default)] @@ -168,186 +165,6 @@ impl StatePolicy { } } - /// Check if any git repositories exist for the given identifier - /// - /// Scans the git_data_path for any directories matching the pattern: - /// `/.git` - /// - /// This is used to distinguish "no git data yet" from "not authorized". - fn has_git_data_for_identifier(&self, identifier: &str) -> bool { - let git_data_path = &self.ctx.git_data_path; - - // Check if git_data_path exists - if !git_data_path.exists() { - return false; - } - - // Scan for any npub directories - let read_dir = match std::fs::read_dir(git_data_path) { - Ok(dir) => dir, - Err(_) => return false, - }; - - for entry in read_dir.flatten() { - if let Ok(file_type) = entry.file_type() { - if file_type.is_dir() { - // Check if /.git exists - let repo_path = entry.path().join(format!("{}.git", identifier)); - if repo_path.exists() { - return true; - } - } - } - } - - false - } - - /// Check if this state event is the latest for its identifier among authorized authors - /// - /// A state is considered "latest" if no other state event in the database - /// from an authorized author has a newer timestamp. - async fn is_latest_state_for_identifier( - &self, - state: &RepositoryState, - authorized_pubkeys: &[PublicKey], - ) -> Result { - let filter = Filter::new() - .kind(Kind::from(KIND_REPOSITORY_STATE)) - .custom_tag( - SingleLetterTag::lowercase(Alphabet::D), - state.identifier.clone(), - ); - - match self.ctx.database.query(filter).await { - Ok(events) => { - for event in events { - // Skip comparing to self (same event ID) - if event.id == state.event.id { - continue; - } - // Only consider events from authorized authors for this announcement - if !authorized_pubkeys.contains(&event.pubkey) { - continue; - } - // If any existing event from an authorized author is newer, this is not the latest - if event.created_at > state.event.created_at { - tracing::debug!( - "State {} is not latest: found newer state {} from {} (ts {} > {})", - state.event.id.to_hex(), - event.id.to_hex(), - event.pubkey.to_hex(), - event.created_at.as_secs(), - state.event.created_at.as_secs() - ); - return Ok(false); - } - } - Ok(true) - } - Err(e) => Err(format!("Database query failed: {}", e)), - } - } - - /// Find all repository announcements where the given pubkey is authorized - async fn find_authorized_announcements( - &self, - identifier: &str, - state_author: &PublicKey, - ) -> Result, String> { - let filter = Filter::new() - .kind(Kind::from(KIND_REPOSITORY_ANNOUNCEMENT)) - .custom_tag( - SingleLetterTag::lowercase(Alphabet::D), - identifier.to_string(), - ); - - match self.ctx.database.query(filter).await { - Ok(events) => { - let mut authorized = Vec::new(); - let state_author_hex = state_author.to_hex(); - - for event in events { - if let Ok(announcement) = RepositoryAnnouncement::from_event(event.clone()) { - // Check if state author is authorized for this announcement - let is_owner = event.pubkey == *state_author; - let is_maintainer = announcement.maintainers.contains(&state_author_hex); - - if is_owner || is_maintainer { - tracing::debug!( - "Found authorized announcement for {}: owner={}, maintainer={}", - identifier, - if is_owner { - event.pubkey.to_hex() - } else { - "n/a".to_string() - }, - is_maintainer - ); - authorized.push(announcement); - } - } - } - Ok(authorized) - } - Err(e) => Err(format!("Database query failed: {}", e)), - } - } - - /// Identify all owner repositories for which this state event is the latest authorized state - async fn identify_owner_repositories( - &self, - state: &RepositoryState, - ) -> Result, String> { - // Find all announcements where state author is authorized - let announcements = self - .find_authorized_announcements(&state.identifier, &state.event.pubkey) - .await?; - - if announcements.is_empty() { - tracing::debug!( - "No authorized announcements found for state {} by {}", - state.identifier, - state.event.pubkey.to_hex() - ); - return Ok(Vec::new()); - } - - let mut owner_repos = Vec::new(); - - for announcement in announcements { - // Build the list of authorized pubkeys for this specific announcement - let mut authorized_pubkeys = vec![announcement.event.pubkey]; - for maintainer_hex in &announcement.maintainers { - if let Ok(pk) = PublicKey::from_hex(maintainer_hex) { - authorized_pubkeys.push(pk); - } - } - - // Check if this is the latest state event for THIS announcement's context - if !self - .is_latest_state_for_identifier(state, &authorized_pubkeys) - .await? - { - tracing::debug!( - "Skipping {} in {}'s repo - not the latest state event for this context", - state.identifier, - announcement.event.pubkey.to_hex() - ); - continue; - } - - // Build repository path: //.git - let repo_path = self - .ctx - .git_data_path - .join(announcement.repo_path().clone()); - owner_repos.push((announcement, repo_path)); - } - - Ok(owner_repos) - } - /// Align a repository's refs with the authorized state /// /// This function: -- cgit v1.2.3