diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2026-01-08 11:20:35 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2026-01-08 11:23:28 +0000 |
| commit | 5d02ad6b893f9059044914c115d77cf9d8e589c3 (patch) | |
| tree | b727f9c44d2f2d4e203dc2344e4c9bd5144a77dd /src/git/authorization.rs | |
| parent | 075307804bf66bba10f5bc55cb40e2e6a98a65ee (diff) | |
refactor: replace hardcoded Kind constants with rust-nostr variants
- Replace KIND_REPOSITORY_ANNOUNCEMENT with Kind::GitRepoAnnouncement
- Replace KIND_REPOSITORY_STATE with Kind::RepoState
- Replace KIND_PR with Kind::GitPullRequest
- Replace KIND_PR_UPDATE with Kind::GitPullRequestUpdate
- Replace KIND_USER_GRASP_LIST with Kind::GitUserGraspList
- Replace KIND_PATCH with Kind::GitPatch
- Replace KIND_ISSUE with Kind::GitIssue
- Replace KIND_COMMENT with Kind::Comment
- Replace all Kind::Custom(30617|30618|1617|1618|1619|1621|1111|10317) patterns
- Remove all hardcoded KIND_* constants from events.rs
- Update all match statements to use Kind enum directly
- Update all filter builders to use Kind variants
- Update all test helpers and assertions
Benefits:
- Type safety: compiler prevents wrong kind numbers
- Readability: Kind::GitRepoAnnouncement is self-documenting
- Maintainability: single source of truth (rust-nostr)
- IDE support: full autocompletion and refactoring
- Standards: aligns with rust-nostr best practices
Files modified: 21
Constants removed: 9
Patterns replaced: 100+
Tests passing: 222/222
Diffstat (limited to 'src/git/authorization.rs')
| -rw-r--r-- | src/git/authorization.rs | 32 |
1 files changed, 12 insertions, 20 deletions
diff --git a/src/git/authorization.rs b/src/git/authorization.rs index 7502a52..e174b51 100644 --- a/src/git/authorization.rs +++ b/src/git/authorization.rs | |||
| @@ -36,11 +36,9 @@ use std::sync::Arc; | |||
| 36 | use tracing::{debug, info, warn}; | 36 | use tracing::{debug, info, warn}; |
| 37 | 37 | ||
| 38 | use crate::nostr::builder::SharedDatabase; | 38 | use crate::nostr::builder::SharedDatabase; |
| 39 | use crate::nostr::events::{ | 39 | use crate::nostr::events::{RepositoryAnnouncement, RepositoryState}; |
| 40 | RepositoryAnnouncement, RepositoryState, KIND_PR, KIND_PR_UPDATE, KIND_REPOSITORY_ANNOUNCEMENT, | ||
| 41 | KIND_REPOSITORY_STATE, | ||
| 42 | }; | ||
| 43 | use crate::purgatory::Purgatory; | 40 | use crate::purgatory::Purgatory; |
| 41 | use nostr_sdk::Kind; | ||
| 44 | 42 | ||
| 45 | /// Perform GRASP authorization for a push operation | 43 | /// Perform GRASP authorization for a push operation |
| 46 | /// | 44 | /// |
| @@ -241,10 +239,7 @@ pub async fn fetch_repository_data( | |||
| 241 | identifier: &str, | 239 | identifier: &str, |
| 242 | ) -> Result<RepositoryData> { | 240 | ) -> Result<RepositoryData> { |
| 243 | let filter = Filter::new() | 241 | let filter = Filter::new() |
| 244 | .kinds([ | 242 | .kinds([Kind::GitRepoAnnouncement, Kind::RepoState]) |
| 245 | Kind::from(KIND_REPOSITORY_ANNOUNCEMENT), | ||
| 246 | Kind::from(KIND_REPOSITORY_STATE), | ||
| 247 | ]) | ||
| 248 | .custom_tag( | 243 | .custom_tag( |
| 249 | SingleLetterTag::lowercase(Alphabet::D), | 244 | SingleLetterTag::lowercase(Alphabet::D), |
| 250 | identifier.to_string(), | 245 | identifier.to_string(), |
| @@ -268,11 +263,11 @@ pub async fn fetch_repository_data( | |||
| 268 | let mut states = Vec::new(); | 263 | let mut states = Vec::new(); |
| 269 | 264 | ||
| 270 | for event in events { | 265 | for event in events { |
| 271 | if event.kind == Kind::from(KIND_REPOSITORY_ANNOUNCEMENT) { | 266 | if event.kind == Kind::GitRepoAnnouncement { |
| 272 | if let Ok(announcement) = RepositoryAnnouncement::from_event(event) { | 267 | if let Ok(announcement) = RepositoryAnnouncement::from_event(event) { |
| 273 | announcements.push(announcement); | 268 | announcements.push(announcement); |
| 274 | } | 269 | } |
| 275 | } else if event.kind == Kind::from(KIND_REPOSITORY_STATE) { | 270 | } else if event.kind == Kind::RepoState { |
| 276 | if let Ok(state) = RepositoryState::from_event(event) { | 271 | if let Ok(state) = RepositoryState::from_event(event) { |
| 277 | states.push(state); | 272 | states.push(state); |
| 278 | } | 273 | } |
| @@ -714,10 +709,7 @@ impl AuthorizationContext { | |||
| 714 | /// This matches the reference implementation's filter logic | 709 | /// This matches the reference implementation's filter logic |
| 715 | pub fn create_filter(identifier: &str) -> Filter { | 710 | pub fn create_filter(identifier: &str) -> Filter { |
| 716 | Filter::new() | 711 | Filter::new() |
| 717 | .kinds([ | 712 | .kinds([Kind::GitRepoAnnouncement, Kind::RepoState]) |
| 718 | Kind::from(KIND_REPOSITORY_ANNOUNCEMENT), | ||
| 719 | Kind::from(KIND_REPOSITORY_STATE), | ||
| 720 | ]) | ||
| 721 | .custom_tag( | 713 | .custom_tag( |
| 722 | SingleLetterTag::lowercase(Alphabet::D), | 714 | SingleLetterTag::lowercase(Alphabet::D), |
| 723 | identifier.to_string(), | 715 | identifier.to_string(), |
| @@ -754,7 +746,7 @@ impl AuthorizationContext { | |||
| 754 | 746 | ||
| 755 | for event in &self.events { | 747 | for event in &self.events { |
| 756 | // Check if it's a repository state event | 748 | // Check if it's a repository state event |
| 757 | if event.kind != Kind::from(KIND_REPOSITORY_STATE) { | 749 | if event.kind != Kind::RepoState { |
| 758 | continue; | 750 | continue; |
| 759 | } | 751 | } |
| 760 | 752 | ||
| @@ -806,7 +798,7 @@ impl AuthorizationContext { | |||
| 806 | 798 | ||
| 807 | for event in &self.events { | 799 | for event in &self.events { |
| 808 | // Only look at announcements | 800 | // Only look at announcements |
| 809 | if event.kind != Kind::from(KIND_REPOSITORY_ANNOUNCEMENT) { | 801 | if event.kind != Kind::GitRepoAnnouncement { |
| 810 | continue; | 802 | continue; |
| 811 | } | 803 | } |
| 812 | 804 | ||
| @@ -838,7 +830,7 @@ impl AuthorizationContext { | |||
| 838 | pub fn is_state_authorized(&self, state_pubkey: &str, identifier: &str) -> bool { | 830 | pub fn is_state_authorized(&self, state_pubkey: &str, identifier: &str) -> bool { |
| 839 | for event in &self.events { | 831 | for event in &self.events { |
| 840 | // Only look at announcements | 832 | // Only look at announcements |
| 841 | if event.kind != Kind::from(KIND_REPOSITORY_ANNOUNCEMENT) { | 833 | if event.kind != Kind::GitRepoAnnouncement { |
| 842 | continue; | 834 | continue; |
| 843 | } | 835 | } |
| 844 | 836 | ||
| @@ -1093,7 +1085,7 @@ pub async fn get_event_commit_tag( | |||
| 1093 | // Query for PR (1618) and PR Update (1619) events with this ID | 1085 | // Query for PR (1618) and PR Update (1619) events with this ID |
| 1094 | let filter = Filter::new() | 1086 | let filter = Filter::new() |
| 1095 | .ids([*event_id]) | 1087 | .ids([*event_id]) |
| 1096 | .kinds([Kind::from(KIND_PR), Kind::from(KIND_PR_UPDATE)]); | 1088 | .kinds([Kind::GitPullRequest, Kind::GitPullRequestUpdate]); |
| 1097 | 1089 | ||
| 1098 | let events: Vec<Event> = database | 1090 | let events: Vec<Event> = database |
| 1099 | .query(filter) | 1091 | .query(filter) |
| @@ -1224,7 +1216,7 @@ mod tests { | |||
| 1224 | vec!["wss://example.com".to_string()], | 1216 | vec!["wss://example.com".to_string()], |
| 1225 | )); | 1217 | )); |
| 1226 | 1218 | ||
| 1227 | EventBuilder::new(Kind::from(KIND_REPOSITORY_ANNOUNCEMENT), "Test repo") | 1219 | EventBuilder::new(Kind::GitRepoAnnouncement, "Test repo") |
| 1228 | .tags(tags) | 1220 | .tags(tags) |
| 1229 | .sign_with_keys(keys) | 1221 | .sign_with_keys(keys) |
| 1230 | .unwrap() | 1222 | .unwrap() |
| @@ -1240,7 +1232,7 @@ mod tests { | |||
| 1240 | )); | 1232 | )); |
| 1241 | } | 1233 | } |
| 1242 | 1234 | ||
| 1243 | EventBuilder::new(Kind::from(KIND_REPOSITORY_STATE), "") | 1235 | EventBuilder::new(Kind::RepoState, "") |
| 1244 | .tags(tags) | 1236 | .tags(tags) |
| 1245 | .sign_with_keys(keys) | 1237 | .sign_with_keys(keys) |
| 1246 | .unwrap() | 1238 | .unwrap() |