fetch following contacts if we are missing any
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
70
damus/Models/FollowingModel.swift
Normal file
70
damus/Models/FollowingModel.swift
Normal file
@@ -0,0 +1,70 @@
|
||||
//
|
||||
// FollowingModel.swift
|
||||
// damus
|
||||
//
|
||||
// Created by William Casarin on 2022-05-24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class FollowingModel: ObservableObject {
|
||||
let damus_state: DamusState
|
||||
var needs_sub: Bool = true
|
||||
|
||||
var has_contact: Set<String> = Set()
|
||||
let contacts: [String]
|
||||
|
||||
let sub_id: String = UUID().description
|
||||
|
||||
init(damus_state: DamusState, contacts: [String]) {
|
||||
self.damus_state = damus_state
|
||||
self.contacts = contacts
|
||||
}
|
||||
|
||||
func get_filter() -> NostrFilter {
|
||||
var f = NostrFilter.filter_kinds([0])
|
||||
f.authors = self.contacts.reduce(into: Array<String>()) { acc, pk in
|
||||
// don't fetch profiles we already have
|
||||
if damus_state.profiles.lookup(id: pk) != nil {
|
||||
return
|
||||
}
|
||||
acc.append(pk)
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func subscribe() {
|
||||
let filter = get_filter()
|
||||
if (filter.authors?.count ?? 0) == 0 {
|
||||
needs_sub = false
|
||||
return
|
||||
}
|
||||
let filters = [filter]
|
||||
print_filters(relay_id: "following", filters: [filters])
|
||||
self.damus_state.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.pool.unsubscribe(sub_id: sub_id)
|
||||
}
|
||||
|
||||
func handle_event(relay_id: String, ev: NostrConnectionEvent) {
|
||||
switch ev {
|
||||
case .ws_event:
|
||||
break
|
||||
case .nostr_event(let nev):
|
||||
switch nev {
|
||||
case .event(_, let ev):
|
||||
if ev.kind == 0 {
|
||||
process_metadata_event(profiles: damus_state.profiles, ev: ev)
|
||||
}
|
||||
case .notice(let msg):
|
||||
print("followingmodel notice: \(msg)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,8 +69,7 @@ class HomeModel: ObservableObject {
|
||||
}
|
||||
|
||||
func handle_contact_event(sub_id: String, relay_id: String, ev: NostrEvent) {
|
||||
load_our_contacts(contacts: self.damus_state.contacts, our_pubkey: self.damus_state.pubkey, ev: ev)
|
||||
add_contact_if_friend(contacts: self.damus_state.contacts, ev: ev)
|
||||
process_contact_event(contacts: damus_state.contacts, pubkey: damus_state.pubkey, ev: ev)
|
||||
|
||||
if sub_id == init_subid {
|
||||
pool.send(.unsubscribe(init_subid), to: [relay_id])
|
||||
@@ -246,21 +245,7 @@ class HomeModel: ObservableObject {
|
||||
}
|
||||
|
||||
func handle_metadata_event(_ ev: NostrEvent) {
|
||||
guard let profile: Profile = decode_data(Data(ev.content.utf8)) else {
|
||||
return
|
||||
}
|
||||
|
||||
if let mprof = damus_state.profiles.lookup_with_timestamp(id: ev.pubkey) {
|
||||
if mprof.timestamp > ev.created_at {
|
||||
// skip if we already have an newer profile
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
let tprof = TimestampedProfile(profile: profile, timestamp: ev.created_at)
|
||||
damus_state.profiles.add(id: ev.pubkey, profile: tprof)
|
||||
|
||||
notify(.profile_updated, ProfileUpdate(pubkey: ev.pubkey, profile: profile))
|
||||
process_metadata_event(profiles: damus_state.profiles, ev: ev)
|
||||
}
|
||||
|
||||
func get_last_event_of_kind(relay_id: String, kind: Int) -> NostrEvent? {
|
||||
@@ -392,3 +377,26 @@ func print_filters(relay_id: String?, filters groups: [[NostrFilter]]) {
|
||||
}
|
||||
print("-----")
|
||||
}
|
||||
|
||||
func process_metadata_event(profiles: Profiles, ev: NostrEvent) {
|
||||
guard let profile: Profile = decode_data(Data(ev.content.utf8)) else {
|
||||
return
|
||||
}
|
||||
|
||||
if let mprof = profiles.lookup_with_timestamp(id: ev.pubkey) {
|
||||
if mprof.timestamp > ev.created_at {
|
||||
// skip if we already have an newer profile
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
let tprof = TimestampedProfile(profile: profile, timestamp: ev.created_at)
|
||||
profiles.add(id: ev.pubkey, profile: tprof)
|
||||
|
||||
notify(.profile_updated, ProfileUpdate(pubkey: ev.pubkey, profile: profile))
|
||||
}
|
||||
|
||||
func process_contact_event(contacts: Contacts, pubkey: String, ev: NostrEvent) {
|
||||
load_our_contacts(contacts: contacts, our_pubkey: pubkey, ev: ev)
|
||||
add_contact_if_friend(contacts: contacts, ev: ev)
|
||||
}
|
||||
|
||||
@@ -39,19 +39,14 @@ class ProfileModel: ObservableObject {
|
||||
var profile_filter = NostrFilter.filter_kinds([
|
||||
NostrKind.text.rawValue,
|
||||
NostrKind.boost.rawValue,
|
||||
NostrKind.metadata.rawValue,
|
||||
NostrKind.contacts.rawValue,
|
||||
NostrKind.like.rawValue
|
||||
])
|
||||
profile_filter.authors = [pubkey]
|
||||
|
||||
var contact_pks = (contacts?.referenced_pubkeys.map { $0.ref_id }) ?? []
|
||||
contact_pks.append(pubkey)
|
||||
|
||||
var contacts_filter = NostrFilter.filter_kinds([0,3])
|
||||
contacts_filter.authors = contact_pks
|
||||
|
||||
profile_filter.limit = 1000
|
||||
|
||||
let filters = [profile_filter, contacts_filter]
|
||||
let filters = [profile_filter]
|
||||
|
||||
print("subscribing to profile \(pubkey) with sub_id \(sub_id)")
|
||||
print_filters(relay_id: "profile", filters: [filters])
|
||||
|
||||
Reference in New Issue
Block a user