FollowingModel.swift (1628B)
1 // 2 // FollowingModel.swift 3 // damus 4 // 5 // Created by William Casarin on 2022-05-24. 6 // 7 8 import Foundation 9 10 class FollowingModel { 11 let damus_state: DamusState 12 var needs_sub: Bool = true 13 14 let contacts: [Pubkey] 15 let hashtags: [Hashtag] 16 17 let sub_id: String = UUID().description 18 19 init(damus_state: DamusState, contacts: [Pubkey], hashtags: [Hashtag]) { 20 self.damus_state = damus_state 21 self.contacts = contacts 22 self.hashtags = hashtags 23 } 24 25 func get_filter<Y>(txn: NdbTxn<Y>) -> NostrFilter { 26 var f = NostrFilter(kinds: [.metadata]) 27 f.authors = self.contacts.reduce(into: Array<Pubkey>()) { acc, pk in 28 // don't fetch profiles we already have 29 if damus_state.profiles.has_fresh_profile(id: pk, txn: txn) { 30 return 31 } 32 acc.append(pk) 33 } 34 return f 35 } 36 37 func subscribe<Y>(txn: NdbTxn<Y>) { 38 let filter = get_filter(txn: txn) 39 if (filter.authors?.count ?? 0) == 0 { 40 needs_sub = false 41 return 42 } 43 let filters = [filter] 44 //print_filters(relay_id: "following", filters: [filters]) 45 self.damus_state.pool.subscribe(sub_id: sub_id, filters: filters, handler: handle_event) 46 } 47 48 func unsubscribe() { 49 if !needs_sub { 50 return 51 } 52 print("unsubscribing from following \(sub_id)") 53 self.damus_state.pool.unsubscribe(sub_id: sub_id) 54 } 55 56 func handle_event(relay_id: RelayURL, ev: NostrConnectionEvent) { 57 // don't need to do anything here really 58 } 59 }