damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

EventGroup.swift (999B)


      1 //
      2 //  ReactionGroup.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2023-02-21.
      6 //
      7 
      8 import Foundation
      9 
     10 class EventGroup {
     11     var events: [NostrEvent]
     12     
     13     var last_event_at: UInt32 {
     14         guard let first = self.events.first else {
     15             return 0
     16         }
     17         
     18         return first.created_at
     19     }
     20     
     21     init(events: [NostrEvent] = []) {
     22         self.events = events
     23     }
     24     
     25     func insert(_ ev: NostrEvent) -> Bool {
     26         return insert_uniq_sorted_event_created(events: &events, new_ev: ev)
     27     }
     28     
     29     func would_filter(_ isIncluded: (NostrEvent) -> Bool) -> Bool {
     30         for ev in events {
     31             if !isIncluded(ev) {
     32                 return true
     33             }
     34         }
     35         
     36         return false
     37     }
     38     
     39     func filter(_ isIncluded: (NostrEvent) -> Bool) -> EventGroup? {
     40         let new_evs = events.filter(isIncluded)
     41         guard new_evs.count > 0 else {
     42             return nil
     43         }
     44         return EventGroup(events: new_evs)
     45     }
     46 }