diff options
Diffstat (limited to 'src/nostr/policy/deletion.rs')
| -rw-r--r-- | src/nostr/policy/deletion.rs | 498 |
1 files changed, 498 insertions, 0 deletions
diff --git a/src/nostr/policy/deletion.rs b/src/nostr/policy/deletion.rs new file mode 100644 index 0000000..6457c90 --- /dev/null +++ b/src/nostr/policy/deletion.rs | |||
| @@ -0,0 +1,498 @@ | |||
| 1 | /// Deletion Policy - NIP-09 event deletion request handling | ||
| 2 | /// | ||
| 3 | /// Handles kind 5 (EventDeletion) events that request removal of purgatory entries | ||
| 4 | /// for repository announcements (kind 30617) and state events (kind 30618). | ||
| 5 | /// | ||
| 6 | /// ## NIP-09 Rules Enforced | ||
| 7 | /// | ||
| 8 | /// - Only the event author can delete their own events (pubkey must match) | ||
| 9 | /// - `e` tags reference specific event IDs to delete | ||
| 10 | /// - `a` tags reference addressable events by coordinate (`<kind>:<pubkey>:<d-identifier>`) | ||
| 11 | /// - When an `a` tag is used, all versions up to `created_at` of the deletion request | ||
| 12 | /// are considered deleted | ||
| 13 | /// | ||
| 14 | /// ## Purgatory Interaction | ||
| 15 | /// | ||
| 16 | /// - Kind 30617 (announcement) in purgatory: entry removed, bare repo deleted from disk | ||
| 17 | /// - Kind 30618 (state event) in purgatory: matching state event(s) removed by event ID | ||
| 18 | /// or by (author, identifier) coordinate | ||
| 19 | use nostr_relay_builder::prelude::{Event, WritePolicyResult}; | ||
| 20 | |||
| 21 | use super::PolicyContext; | ||
| 22 | |||
| 23 | /// Policy for handling NIP-09 event deletion requests | ||
| 24 | #[derive(Clone)] | ||
| 25 | pub struct DeletionPolicy { | ||
| 26 | ctx: PolicyContext, | ||
| 27 | } | ||
| 28 | |||
| 29 | impl DeletionPolicy { | ||
| 30 | pub fn new(ctx: PolicyContext) -> Self { | ||
| 31 | Self { ctx } | ||
| 32 | } | ||
| 33 | |||
| 34 | /// Process a kind 5 (EventDeletion) event. | ||
| 35 | /// | ||
| 36 | /// Checks whether the deletion request targets any purgatory announcements | ||
| 37 | /// and removes them if so. The deletion event itself is always accepted | ||
| 38 | /// (relays should store deletion requests per NIP-09). | ||
| 39 | /// | ||
| 40 | /// Only the event author can delete their own events — this is enforced by | ||
| 41 | /// checking that the purgatory entry's owner matches `event.pubkey`. | ||
| 42 | pub async fn handle(&self, event: &Event) -> WritePolicyResult { | ||
| 43 | // Process purgatory removals synchronously (no async needed) | ||
| 44 | self.remove_purgatory_targets(event); | ||
| 45 | |||
| 46 | // Always accept the deletion event itself so it is stored and | ||
| 47 | // can prevent re-acceptance of the deleted event in the future. | ||
| 48 | WritePolicyResult::Accept | ||
| 49 | } | ||
| 50 | |||
| 51 | /// Remove any purgatory entries targeted by this deletion event. | ||
| 52 | /// | ||
| 53 | /// Handles both reference styles from NIP-09: | ||
| 54 | /// - `e` tags: event ID references — match against announcement or state event IDs | ||
| 55 | /// - `a` tags: addressable coordinate references — `30617:…` or `30618:…` | ||
| 56 | /// | ||
| 57 | /// Only removes entries where the purgatory entry's author matches the deletion | ||
| 58 | /// event's pubkey (enforces author-only deletion). | ||
| 59 | fn remove_purgatory_targets(&self, event: &Event) { | ||
| 60 | let author = &event.pubkey; | ||
| 61 | |||
| 62 | for tag in event.tags.iter() { | ||
| 63 | let tag_vec = tag.as_slice(); | ||
| 64 | if tag_vec.len() < 2 { | ||
| 65 | continue; | ||
| 66 | } | ||
| 67 | |||
| 68 | match tag_vec[0].as_str() { | ||
| 69 | "e" => { | ||
| 70 | // Event ID reference: find purgatory announcement with this event ID | ||
| 71 | let target_id = &tag_vec[1]; | ||
| 72 | self.remove_by_event_id(author, target_id, event.created_at.as_secs()); | ||
| 73 | } | ||
| 74 | "a" => { | ||
| 75 | // Addressable coordinate reference: `<kind>:<pubkey>:<d-identifier>` | ||
| 76 | let coord = &tag_vec[1]; | ||
| 77 | self.remove_by_coordinate(author, coord, event.created_at.as_secs()); | ||
| 78 | } | ||
| 79 | _ => {} | ||
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | /// Remove a purgatory entry (announcement, state event, or PR event) matched by event ID. | ||
| 85 | /// | ||
| 86 | /// Checks in order: announcements (30617), state events (30618), PR/PR-update events. | ||
| 87 | /// Only removes entries whose author matches `author`. | ||
| 88 | fn remove_by_event_id( | ||
| 89 | &self, | ||
| 90 | author: &nostr_relay_builder::prelude::PublicKey, | ||
| 91 | target_id_hex: &str, | ||
| 92 | _deletion_created_at: u64, | ||
| 93 | ) { | ||
| 94 | // --- Check PR events (kind 1617/1618) first — O(1) direct lookup --- | ||
| 95 | // PR purgatory is keyed by event ID hex, so this is the cheapest check. | ||
| 96 | // Only remove if the entry has an actual event (not a placeholder) and the | ||
| 97 | // event's author matches the deletion request author. | ||
| 98 | if let Some(entry) = self.ctx.purgatory.find_pr(target_id_hex) { | ||
| 99 | if let Some(ref event) = entry.event { | ||
| 100 | if event.pubkey == *author { | ||
| 101 | tracing::info!( | ||
| 102 | event_id = %target_id_hex, | ||
| 103 | author = %author.to_hex(), | ||
| 104 | "Deletion request: removing purgatory PR event by event ID" | ||
| 105 | ); | ||
| 106 | self.ctx.purgatory.remove_pr(target_id_hex); | ||
| 107 | return; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | // Entry exists but is a placeholder or wrong author — don't remove | ||
| 111 | return; | ||
| 112 | } | ||
| 113 | |||
| 114 | // --- Check announcements (kind 30617) --- | ||
| 115 | // The DashMap doesn't expose a direct "find by event ID" method, so we use | ||
| 116 | // the announcements_for_sync snapshot to enumerate all (repo_id, _) pairs. | ||
| 117 | let all = self.ctx.purgatory.announcements_for_sync(); | ||
| 118 | for (repo_id, _) in all { | ||
| 119 | // repo_id format: "30617:{pubkey_hex}:{identifier}" | ||
| 120 | let parts: Vec<&str> = repo_id.splitn(3, ':').collect(); | ||
| 121 | if parts.len() != 3 { | ||
| 122 | continue; | ||
| 123 | } | ||
| 124 | let entry_pubkey_hex = parts[1]; | ||
| 125 | let identifier = parts[2]; | ||
| 126 | |||
| 127 | if entry_pubkey_hex != author.to_hex() { | ||
| 128 | continue; | ||
| 129 | } | ||
| 130 | |||
| 131 | if let Some(entry) = self.ctx.purgatory.find_announcement(author, identifier) { | ||
| 132 | if entry.event.id.to_hex() == target_id_hex { | ||
| 133 | tracing::info!( | ||
| 134 | event_id = %target_id_hex, | ||
| 135 | identifier = %identifier, | ||
| 136 | author = %author.to_hex(), | ||
| 137 | "Deletion request: removing purgatory announcement by event ID" | ||
| 138 | ); | ||
| 139 | self.evict_purgatory_entry(author, identifier); | ||
| 140 | return; // event IDs are unique | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | // --- Check state events (kind 30618) --- | ||
| 146 | // State events are keyed by identifier; scan all identifiers for a match. | ||
| 147 | let state_identifiers = self.ctx.purgatory.get_all_identifiers(); | ||
| 148 | for identifier in state_identifiers { | ||
| 149 | let entries = self.ctx.purgatory.find_state(&identifier); | ||
| 150 | for entry in entries { | ||
| 151 | if entry.author == *author && entry.event.id.to_hex() == target_id_hex { | ||
| 152 | tracing::info!( | ||
| 153 | event_id = %target_id_hex, | ||
| 154 | identifier = %identifier, | ||
| 155 | author = %author.to_hex(), | ||
| 156 | "Deletion request: removing purgatory state event by event ID" | ||
| 157 | ); | ||
| 158 | self.ctx.purgatory.remove_state_event(&identifier, &entry.event.id); | ||
| 159 | return; // event IDs are unique | ||
| 160 | } | ||
| 161 | } | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | /// Remove a purgatory entry matched by addressable coordinate. | ||
| 166 | /// | ||
| 167 | /// The coordinate format is `<kind>:<pubkey>:<d-identifier>`. | ||
| 168 | /// Handles kind 30617 (announcements) and kind 30618 (state events). | ||
| 169 | /// | ||
| 170 | /// Per NIP-09, all versions up to `deletion_created_at` are considered deleted. | ||
| 171 | fn remove_by_coordinate( | ||
| 172 | &self, | ||
| 173 | author: &nostr_relay_builder::prelude::PublicKey, | ||
| 174 | coordinate: &str, | ||
| 175 | deletion_created_at: u64, | ||
| 176 | ) { | ||
| 177 | // Parse coordinate: `<kind>:<pubkey>:<d-identifier>` | ||
| 178 | let parts: Vec<&str> = coordinate.splitn(3, ':').collect(); | ||
| 179 | if parts.len() != 3 { | ||
| 180 | return; | ||
| 181 | } | ||
| 182 | |||
| 183 | let kind_str = parts[0]; | ||
| 184 | let coord_pubkey_hex = parts[1]; | ||
| 185 | let identifier = parts[2]; | ||
| 186 | |||
| 187 | // The coordinate pubkey must match the deletion event author | ||
| 188 | if coord_pubkey_hex != author.to_hex() { | ||
| 189 | tracing::debug!( | ||
| 190 | coord_pubkey = %coord_pubkey_hex, | ||
| 191 | deletion_author = %author.to_hex(), | ||
| 192 | "Ignoring deletion: coordinate pubkey does not match deletion author" | ||
| 193 | ); | ||
| 194 | return; | ||
| 195 | } | ||
| 196 | |||
| 197 | match kind_str { | ||
| 198 | "30617" => { | ||
| 199 | // Announcement purgatory entry | ||
| 200 | if let Some(entry) = self.ctx.purgatory.find_announcement(author, identifier) { | ||
| 201 | if entry.event.created_at.as_secs() <= deletion_created_at { | ||
| 202 | tracing::info!( | ||
| 203 | identifier = %identifier, | ||
| 204 | author = %author.to_hex(), | ||
| 205 | "Deletion request: removing purgatory announcement by coordinate" | ||
| 206 | ); | ||
| 207 | self.evict_purgatory_entry(author, identifier); | ||
| 208 | } else { | ||
| 209 | tracing::debug!( | ||
| 210 | identifier = %identifier, | ||
| 211 | author = %author.to_hex(), | ||
| 212 | "Ignoring deletion: purgatory announcement is newer than deletion request" | ||
| 213 | ); | ||
| 214 | } | ||
| 215 | } | ||
| 216 | } | ||
| 217 | "30618" => { | ||
| 218 | // State event purgatory entries for this (author, identifier). | ||
| 219 | // Remove all entries authored by `author` with created_at ≤ deletion_created_at. | ||
| 220 | let entries = self.ctx.purgatory.find_state(identifier); | ||
| 221 | let mut removed = 0usize; | ||
| 222 | for entry in entries { | ||
| 223 | if entry.author == *author | ||
| 224 | && entry.event.created_at.as_secs() <= deletion_created_at | ||
| 225 | { | ||
| 226 | self.ctx.purgatory.remove_state_event(identifier, &entry.event.id); | ||
| 227 | removed += 1; | ||
| 228 | } | ||
| 229 | } | ||
| 230 | if removed > 0 { | ||
| 231 | tracing::info!( | ||
| 232 | identifier = %identifier, | ||
| 233 | author = %author.to_hex(), | ||
| 234 | removed = %removed, | ||
| 235 | "Deletion request: removed purgatory state event(s) by coordinate" | ||
| 236 | ); | ||
| 237 | } | ||
| 238 | } | ||
| 239 | _ => { | ||
| 240 | // Other kinds not handled | ||
| 241 | } | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | /// Remove a purgatory announcement and delete its bare repository from disk. | ||
| 246 | fn evict_purgatory_entry( | ||
| 247 | &self, | ||
| 248 | author: &nostr_relay_builder::prelude::PublicKey, | ||
| 249 | identifier: &str, | ||
| 250 | ) { | ||
| 251 | // Get repo path before removing | ||
| 252 | if let Some(entry) = self.ctx.purgatory.find_announcement(author, identifier) { | ||
| 253 | if entry.repo_path.exists() { | ||
| 254 | if let Err(e) = std::fs::remove_dir_all(&entry.repo_path) { | ||
| 255 | tracing::warn!( | ||
| 256 | path = %entry.repo_path.display(), | ||
| 257 | error = %e, | ||
| 258 | "Failed to delete bare repository during deletion request processing" | ||
| 259 | ); | ||
| 260 | } else { | ||
| 261 | tracing::info!( | ||
| 262 | path = %entry.repo_path.display(), | ||
| 263 | "Deleted bare repository for deletion-requested purgatory announcement" | ||
| 264 | ); | ||
| 265 | } | ||
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | self.ctx.purgatory.remove_announcement(author, identifier); | ||
| 270 | |||
| 271 | // Remove state events for this identifier only if no other owner's | ||
| 272 | // announcement remains in purgatory (state events are keyed by identifier alone) | ||
| 273 | let other_owners_remain = !self | ||
| 274 | .ctx | ||
| 275 | .purgatory | ||
| 276 | .get_announcements_by_identifier(identifier) | ||
| 277 | .is_empty(); | ||
| 278 | |||
| 279 | if !other_owners_remain { | ||
| 280 | self.ctx.purgatory.remove_state(identifier); | ||
| 281 | } | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | #[cfg(test)] | ||
| 286 | mod tests { | ||
| 287 | use super::*; | ||
| 288 | use crate::nostr::policy::PolicyContext; | ||
| 289 | use crate::purgatory::Purgatory; | ||
| 290 | use nostr_relay_builder::prelude::*; | ||
| 291 | use std::collections::HashSet; | ||
| 292 | use std::path::PathBuf; | ||
| 293 | use std::sync::Arc; | ||
| 294 | |||
| 295 | fn make_context() -> PolicyContext { | ||
| 296 | let db = Arc::new(MemoryDatabase::with_opts(MemoryDatabaseOptions { | ||
| 297 | events: true, | ||
| 298 | max_events: None, | ||
| 299 | })); | ||
| 300 | let purgatory = Arc::new(Purgatory::new(PathBuf::new())); | ||
| 301 | let config = crate::config::Config::for_testing(); | ||
| 302 | PolicyContext::new("test.example.com", db, PathBuf::new(), purgatory, config) | ||
| 303 | } | ||
| 304 | |||
| 305 | fn make_announcement_event(keys: &Keys, identifier: &str) -> Event { | ||
| 306 | EventBuilder::new(Kind::GitRepoAnnouncement, "") | ||
| 307 | .tags(vec![ | ||
| 308 | Tag::identifier(identifier), | ||
| 309 | Tag::custom(TagKind::custom("clone"), vec!["https://example.com/repo.git"]), | ||
| 310 | ]) | ||
| 311 | .sign_with_keys(keys) | ||
| 312 | .unwrap() | ||
| 313 | } | ||
| 314 | |||
| 315 | fn add_to_purgatory(ctx: &PolicyContext, event: &Event, identifier: &str) { | ||
| 316 | ctx.purgatory.add_announcement( | ||
| 317 | event.clone(), | ||
| 318 | identifier.to_string(), | ||
| 319 | event.pubkey, | ||
| 320 | PathBuf::new(), | ||
| 321 | HashSet::new(), | ||
| 322 | ); | ||
| 323 | } | ||
| 324 | |||
| 325 | #[tokio::test] | ||
| 326 | async fn test_deletion_by_event_id_removes_purgatory_entry() { | ||
| 327 | let ctx = make_context(); | ||
| 328 | let keys = Keys::generate(); | ||
| 329 | let identifier = "my-repo"; | ||
| 330 | |||
| 331 | let announcement = make_announcement_event(&keys, identifier); | ||
| 332 | add_to_purgatory(&ctx, &announcement, identifier); | ||
| 333 | |||
| 334 | assert!(ctx.purgatory.has_purgatory_announcement(&keys.public_key(), identifier)); | ||
| 335 | |||
| 336 | // Build kind 5 deletion event referencing the announcement by event ID | ||
| 337 | let deletion = EventBuilder::new(Kind::EventDeletion, "") | ||
| 338 | .tags(vec![ | ||
| 339 | Tag::event(announcement.id), | ||
| 340 | Tag::custom(TagKind::custom("k"), vec!["30617"]), | ||
| 341 | ]) | ||
| 342 | .sign_with_keys(&keys) | ||
| 343 | .unwrap(); | ||
| 344 | |||
| 345 | let policy = DeletionPolicy::new(ctx.clone()); | ||
| 346 | let result = policy.handle(&deletion).await; | ||
| 347 | |||
| 348 | assert!(matches!(result, WritePolicyResult::Accept)); | ||
| 349 | assert!( | ||
| 350 | !ctx.purgatory.has_purgatory_announcement(&keys.public_key(), identifier), | ||
| 351 | "Purgatory entry should have been removed" | ||
| 352 | ); | ||
| 353 | } | ||
| 354 | |||
| 355 | #[tokio::test] | ||
| 356 | async fn test_deletion_by_coordinate_removes_purgatory_entry() { | ||
| 357 | let ctx = make_context(); | ||
| 358 | let keys = Keys::generate(); | ||
| 359 | let identifier = "my-repo"; | ||
| 360 | |||
| 361 | let announcement = make_announcement_event(&keys, identifier); | ||
| 362 | add_to_purgatory(&ctx, &announcement, identifier); | ||
| 363 | |||
| 364 | assert!(ctx.purgatory.has_purgatory_announcement(&keys.public_key(), identifier)); | ||
| 365 | |||
| 366 | // Build kind 5 deletion event referencing the announcement by coordinate | ||
| 367 | let coord = format!("30617:{}:{}", keys.public_key().to_hex(), identifier); | ||
| 368 | let deletion = EventBuilder::new(Kind::EventDeletion, "") | ||
| 369 | .tags(vec![ | ||
| 370 | Tag::custom(TagKind::custom("a"), vec![coord]), | ||
| 371 | Tag::custom(TagKind::custom("k"), vec!["30617"]), | ||
| 372 | ]) | ||
| 373 | .sign_with_keys(&keys) | ||
| 374 | .unwrap(); | ||
| 375 | |||
| 376 | let policy = DeletionPolicy::new(ctx.clone()); | ||
| 377 | let result = policy.handle(&deletion).await; | ||
| 378 | |||
| 379 | assert!(matches!(result, WritePolicyResult::Accept)); | ||
| 380 | assert!( | ||
| 381 | !ctx.purgatory.has_purgatory_announcement(&keys.public_key(), identifier), | ||
| 382 | "Purgatory entry should have been removed" | ||
| 383 | ); | ||
| 384 | } | ||
| 385 | |||
| 386 | #[tokio::test] | ||
| 387 | async fn test_deletion_by_wrong_author_does_not_remove() { | ||
| 388 | let ctx = make_context(); | ||
| 389 | let owner_keys = Keys::generate(); | ||
| 390 | let attacker_keys = Keys::generate(); | ||
| 391 | let identifier = "my-repo"; | ||
| 392 | |||
| 393 | let announcement = make_announcement_event(&owner_keys, identifier); | ||
| 394 | add_to_purgatory(&ctx, &announcement, identifier); | ||
| 395 | |||
| 396 | // Attacker tries to delete by event ID | ||
| 397 | let deletion = EventBuilder::new(Kind::EventDeletion, "") | ||
| 398 | .tags(vec![ | ||
| 399 | Tag::event(announcement.id), | ||
| 400 | Tag::custom(TagKind::custom("k"), vec!["30617"]), | ||
| 401 | ]) | ||
| 402 | .sign_with_keys(&attacker_keys) | ||
| 403 | .unwrap(); | ||
| 404 | |||
| 405 | let policy = DeletionPolicy::new(ctx.clone()); | ||
| 406 | let result = policy.handle(&deletion).await; | ||
| 407 | |||
| 408 | assert!(matches!(result, WritePolicyResult::Accept)); | ||
| 409 | assert!( | ||
| 410 | ctx.purgatory.has_purgatory_announcement(&owner_keys.public_key(), identifier), | ||
| 411 | "Purgatory entry should NOT have been removed by wrong author" | ||
| 412 | ); | ||
| 413 | } | ||
| 414 | |||
| 415 | #[tokio::test] | ||
| 416 | async fn test_deletion_by_coordinate_wrong_author_does_not_remove() { | ||
| 417 | let ctx = make_context(); | ||
| 418 | let owner_keys = Keys::generate(); | ||
| 419 | let attacker_keys = Keys::generate(); | ||
| 420 | let identifier = "my-repo"; | ||
| 421 | |||
| 422 | let announcement = make_announcement_event(&owner_keys, identifier); | ||
| 423 | add_to_purgatory(&ctx, &announcement, identifier); | ||
| 424 | |||
| 425 | // Attacker tries to delete by coordinate using owner's pubkey in coord | ||
| 426 | // but signs with their own key — coord pubkey != deletion author | ||
| 427 | let coord = format!("30617:{}:{}", owner_keys.public_key().to_hex(), identifier); | ||
| 428 | let deletion = EventBuilder::new(Kind::EventDeletion, "") | ||
| 429 | .tags(vec![ | ||
| 430 | Tag::custom(TagKind::custom("a"), vec![coord]), | ||
| 431 | Tag::custom(TagKind::custom("k"), vec!["30617"]), | ||
| 432 | ]) | ||
| 433 | .sign_with_keys(&attacker_keys) | ||
| 434 | .unwrap(); | ||
| 435 | |||
| 436 | let policy = DeletionPolicy::new(ctx.clone()); | ||
| 437 | let result = policy.handle(&deletion).await; | ||
| 438 | |||
| 439 | assert!(matches!(result, WritePolicyResult::Accept)); | ||
| 440 | assert!( | ||
| 441 | ctx.purgatory.has_purgatory_announcement(&owner_keys.public_key(), identifier), | ||
| 442 | "Purgatory entry should NOT have been removed by wrong author" | ||
| 443 | ); | ||
| 444 | } | ||
| 445 | |||
| 446 | #[tokio::test] | ||
| 447 | async fn test_deletion_of_nonexistent_entry_is_accepted() { | ||
| 448 | let ctx = make_context(); | ||
| 449 | let keys = Keys::generate(); | ||
| 450 | |||
| 451 | // No purgatory entry exists — deletion should still be accepted | ||
| 452 | let deletion = EventBuilder::new(Kind::EventDeletion, "") | ||
| 453 | .tags(vec![ | ||
| 454 | Tag::custom(TagKind::custom("a"), vec![ | ||
| 455 | format!("30617:{}:nonexistent", keys.public_key().to_hex()) | ||
| 456 | ]), | ||
| 457 | ]) | ||
| 458 | .sign_with_keys(&keys) | ||
| 459 | .unwrap(); | ||
| 460 | |||
| 461 | let policy = DeletionPolicy::new(ctx.clone()); | ||
| 462 | let result = policy.handle(&deletion).await; | ||
| 463 | |||
| 464 | assert!(matches!(result, WritePolicyResult::Accept)); | ||
| 465 | } | ||
| 466 | |||
| 467 | #[tokio::test] | ||
| 468 | async fn test_deletion_by_coordinate_respects_created_at() { | ||
| 469 | let ctx = make_context(); | ||
| 470 | let keys = Keys::generate(); | ||
| 471 | let identifier = "my-repo"; | ||
| 472 | |||
| 473 | // Create announcement with a future timestamp | ||
| 474 | let future_ts = Timestamp::now().as_secs() + 3600; // 1 hour in the future | ||
| 475 | let announcement = EventBuilder::new(Kind::GitRepoAnnouncement, "") | ||
| 476 | .tags(vec![Tag::identifier(identifier)]) | ||
| 477 | .custom_created_at(Timestamp::from(future_ts)) | ||
| 478 | .sign_with_keys(&keys) | ||
| 479 | .unwrap(); | ||
| 480 | add_to_purgatory(&ctx, &announcement, identifier); | ||
| 481 | |||
| 482 | // Deletion event with current timestamp (older than announcement) | ||
| 483 | let coord = format!("30617:{}:{}", keys.public_key().to_hex(), identifier); | ||
| 484 | let deletion = EventBuilder::new(Kind::EventDeletion, "") | ||
| 485 | .tags(vec![Tag::custom(TagKind::custom("a"), vec![coord])]) | ||
| 486 | .sign_with_keys(&keys) | ||
| 487 | .unwrap(); | ||
| 488 | |||
| 489 | let policy = DeletionPolicy::new(ctx.clone()); | ||
| 490 | let result = policy.handle(&deletion).await; | ||
| 491 | |||
| 492 | assert!(matches!(result, WritePolicyResult::Accept)); | ||
| 493 | assert!( | ||
| 494 | ctx.purgatory.has_purgatory_announcement(&keys.public_key(), identifier), | ||
| 495 | "Purgatory entry should NOT be removed: entry is newer than deletion request" | ||
| 496 | ); | ||
| 497 | } | ||
| 498 | } | ||