ReplyCounter.swift (1242B)
1 // 2 // ReplyCounter.swift 3 // damus 4 // 5 // Created by William Casarin on 2023-04-04. 6 // 7 8 import Foundation 9 10 class ReplyCounter { 11 private var replies: [NoteId: Int] 12 private var counted: Set<NoteId> 13 private var our_replies: [NoteId: NostrEvent] 14 private let our_pubkey: Pubkey 15 16 init(our_pubkey: Pubkey) { 17 self.our_pubkey = our_pubkey 18 replies = [:] 19 counted = Set() 20 our_replies = [:] 21 } 22 23 func our_reply(_ evid: NoteId) -> NostrEvent? { 24 return our_replies[evid] 25 } 26 27 func get_replies(_ evid: NoteId) -> Int { 28 return replies[evid] ?? 0 29 } 30 31 func count_replies(_ event: NostrEvent, keypair: Keypair) { 32 guard event.is_textlike else { 33 return 34 } 35 36 if counted.contains(event.id) { 37 return 38 } 39 40 counted.insert(event.id) 41 42 for reply in event.direct_replies(keypair) { 43 if event.pubkey == our_pubkey { 44 self.our_replies[reply] = event 45 } 46 47 if replies[reply] != nil { 48 replies[reply] = replies[reply]! + 1 49 } else { 50 replies[reply] = 1 51 } 52 } 53 } 54 }