damus

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

ZapGroup.swift (1407B)


      1 //
      2 //  ZapGroup.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2023-02-21.
      6 //
      7 
      8 import Foundation
      9 
     10 class ZapGroup {
     11     var zaps: [Zapping] = []
     12     var msat_total: Int64 = 0
     13     var zappers = Set<Pubkey>()
     14 
     15     var last_event_at: UInt32 {
     16         guard let first = zaps.first else {
     17             return 0
     18         }
     19         
     20         return first.created_at
     21     }
     22     
     23     func zap_requests() -> [NostrEvent] {
     24         zaps.map { z in z.request.ev }
     25     }
     26     
     27     func would_filter(_ isIncluded: (NostrEvent) -> Bool) -> Bool {
     28         for zap in zaps {
     29             if !isIncluded(zap.request.ev) {
     30                 return true
     31             }
     32         }
     33         
     34         return false
     35     }
     36     
     37     func filter(_ isIncluded: (NostrEvent) -> Bool) -> ZapGroup? {
     38         let new_zaps = zaps.filter { isIncluded($0.request.ev) }
     39         guard new_zaps.count > 0 else {
     40             return nil
     41         }
     42         let grp = ZapGroup()
     43         for zap in new_zaps {
     44             grp.insert(zap)
     45         }
     46         return grp
     47     }
     48     
     49     @discardableResult
     50     func insert(_ zap: Zapping) -> Bool {
     51         if !insert_uniq_sorted_zap_by_created(zaps: &zaps, new_zap: zap) {
     52             return false
     53         }
     54         
     55         msat_total += zap.amount
     56         
     57         if !zappers.contains(zap.request.ev.pubkey)  {
     58             zappers.insert(zap.request.ev.pubkey)
     59         }
     60         
     61         return true
     62     }
     63 }
     64