Files
damus/damus/Models/FollowingModel.swift
T
Daniel D’Aquino 3a0acfaba1 Implement NostrNetworkManager and UserRelayListManager
This commit implements a new layer called NostrNetworkManager,
responsible for managing interactions with the Nostr network, and
providing a higher level API that is easier and more secure to use for
the layer above it.

It also integrates it with the rest of the app, by moving RelayPool and PostBox
into NostrNetworkManager, along with all their usages.

Changelog-Added: Added NIP-65 relay list support
Changelog-Changed: Improved robustness of relay list handling
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-04-16 11:48:52 -07:00

60 lines
1.6 KiB
Swift

//
// FollowingModel.swift
// damus
//
// Created by William Casarin on 2022-05-24.
//
import Foundation
class FollowingModel {
let damus_state: DamusState
var needs_sub: Bool = true
let contacts: [Pubkey]
let hashtags: [Hashtag]
let sub_id: String = UUID().description
init(damus_state: DamusState, contacts: [Pubkey], hashtags: [Hashtag]) {
self.damus_state = damus_state
self.contacts = contacts
self.hashtags = hashtags
}
func get_filter<Y>(txn: NdbTxn<Y>) -> NostrFilter {
var f = NostrFilter(kinds: [.metadata])
f.authors = self.contacts.reduce(into: Array<Pubkey>()) { acc, pk in
// don't fetch profiles we already have
if damus_state.profiles.has_fresh_profile(id: pk, txn: txn) {
return
}
acc.append(pk)
}
return f
}
func subscribe<Y>(txn: NdbTxn<Y>) {
let filter = get_filter(txn: txn)
if (filter.authors?.count ?? 0) == 0 {
needs_sub = false
return
}
let filters = [filter]
//print_filters(relay_id: "following", filters: [filters])
self.damus_state.nostrNetwork.pool.subscribe(sub_id: sub_id, filters: filters, handler: handle_event)
}
func unsubscribe() {
if !needs_sub {
return
}
print("unsubscribing from following \(sub_id)")
self.damus_state.nostrNetwork.pool.unsubscribe(sub_id: sub_id)
}
func handle_event(relay_id: RelayURL, ev: NostrConnectionEvent) {
// don't need to do anything here really
}
}