contacts: generalize following to allow any reference
I noticed we are not using the PostBox when following new users. Not good! This is probably why following users sometimes does not work. Changelog-Fixed: Fixed a bug where following a user might not work due to poor connectivity
This commit is contained in:
@@ -880,49 +880,45 @@ func timeline_name(_ timeline: Timeline?) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handle_unfollow(state: DamusState, notif: Notification) {
|
func handle_unfollow(state: DamusState, notif: Notification) {
|
||||||
guard let privkey = state.keypair.privkey else {
|
guard let keypair = state.keypair.to_full() else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let target = notif.object as! FollowTarget
|
let target = notif.object as! FollowTarget
|
||||||
let pk = target.pubkey
|
let pk = target.pubkey
|
||||||
let old_contacts = state.contacts.event
|
let old_contacts = state.contacts.event
|
||||||
|
|
||||||
if let ev = unfollow_user(postbox: state.postbox,
|
|
||||||
our_contacts: old_contacts,
|
|
||||||
pubkey: state.pubkey,
|
|
||||||
privkey: privkey,
|
|
||||||
unfollow: pk) {
|
|
||||||
notify(.unfollowed, pk)
|
|
||||||
|
|
||||||
state.contacts.event = ev
|
guard let ev = unfollow_reference(postbox: state.postbox, our_contacts: old_contacts, keypair: keypair, unfollow: .p(pk))
|
||||||
state.contacts.remove_friend(pk)
|
else { return }
|
||||||
state.user_search_cache.updateOwnContactsPetnames(id: state.pubkey, oldEvent: old_contacts, newEvent: ev)
|
|
||||||
}
|
notify(.unfollowed, pk)
|
||||||
|
|
||||||
|
state.contacts.event = ev
|
||||||
|
state.contacts.remove_friend(pk)
|
||||||
|
state.user_search_cache.updateOwnContactsPetnames(id: state.pubkey, oldEvent: old_contacts, newEvent: ev)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handle_follow(state: DamusState, notif: Notification) {
|
func handle_follow(state: DamusState, notif: Notification) {
|
||||||
guard let privkey = state.keypair.privkey else {
|
guard let keypair = state.keypair.to_full() else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let fnotify = notif.object as! FollowTarget
|
let fnotify = notif.object as! FollowTarget
|
||||||
|
|
||||||
if let ev = follow_user(pool: state.pool,
|
guard let ev = follow_reference(box: state.postbox, our_contacts: state.contacts.event, keypair: keypair, follow: .p(fnotify.pubkey))
|
||||||
our_contacts: state.contacts.event,
|
else {
|
||||||
pubkey: state.pubkey,
|
return
|
||||||
privkey: privkey,
|
}
|
||||||
follow: ReferencedId(ref_id: fnotify.pubkey, relay_id: nil, key: "p")) {
|
|
||||||
notify(.followed, fnotify.pubkey)
|
notify(.followed, fnotify.pubkey)
|
||||||
|
|
||||||
state.contacts.event = ev
|
state.contacts.event = ev
|
||||||
|
|
||||||
switch fnotify {
|
switch fnotify {
|
||||||
case .pubkey(let pk):
|
case .pubkey(let pk):
|
||||||
state.contacts.add_friend_pubkey(pk)
|
state.contacts.add_friend_pubkey(pk)
|
||||||
case .contact(let ev):
|
case .contact(let ev):
|
||||||
state.contacts.add_friend_contact(ev)
|
state.contacts.add_friend_contact(ev)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -118,37 +118,36 @@ class Contacts {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func follow_user(pool: RelayPool, our_contacts: NostrEvent?, pubkey: String, privkey: String, follow: ReferencedId) -> NostrEvent? {
|
func follow_reference(box: PostBox, our_contacts: NostrEvent?, keypair: FullKeypair, follow: ReferencedId) -> NostrEvent? {
|
||||||
guard let ev = follow_user_event(our_contacts: our_contacts, our_pubkey: pubkey, follow: follow) else {
|
guard let ev = follow_user_event(our_contacts: our_contacts, our_pubkey: keypair.pubkey, follow: follow) else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ev.calculate_id()
|
ev.calculate_id()
|
||||||
ev.sign(privkey: privkey)
|
ev.sign(privkey: keypair.privkey)
|
||||||
|
|
||||||
|
box.send(ev)
|
||||||
pool.send(.event(ev))
|
|
||||||
|
|
||||||
return ev
|
return ev
|
||||||
}
|
}
|
||||||
|
|
||||||
func unfollow_user(postbox: PostBox, our_contacts: NostrEvent?, pubkey: String, privkey: String, unfollow: String) -> NostrEvent? {
|
func unfollow_reference(postbox: PostBox, our_contacts: NostrEvent?, keypair: FullKeypair, unfollow: ReferencedId) -> NostrEvent? {
|
||||||
guard let cs = our_contacts else {
|
guard let cs = our_contacts else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
let ev = unfollow_user_event(our_contacts: cs, our_pubkey: pubkey, unfollow: unfollow)
|
let ev = unfollow_reference_event(our_contacts: cs, our_pubkey: keypair.pubkey, unfollow: unfollow)
|
||||||
ev.calculate_id()
|
ev.calculate_id()
|
||||||
ev.sign(privkey: privkey)
|
ev.sign(privkey: keypair.privkey)
|
||||||
|
|
||||||
postbox.send(ev)
|
postbox.send(ev)
|
||||||
|
|
||||||
return ev
|
return ev
|
||||||
}
|
}
|
||||||
|
|
||||||
func unfollow_user_event(our_contacts: NostrEvent, our_pubkey: String, unfollow: String) -> NostrEvent {
|
func unfollow_reference_event(our_contacts: NostrEvent, our_pubkey: String, unfollow: ReferencedId) -> NostrEvent {
|
||||||
let tags = our_contacts.tags.filter { tag in
|
let tags = our_contacts.tags.filter { tag in
|
||||||
if tag.count >= 2 && tag[0] == "p" && tag[1] == unfollow {
|
if tag.count >= 2 && tag[0] == unfollow.key && tag[1] == unfollow.ref_id {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
@@ -220,12 +219,16 @@ func ensure_relay_info(relays: [RelayDescriptor], content: String) -> [String: R
|
|||||||
return relay_info
|
return relay_info
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func is_already_following(contacts: NostrEvent, follow: ReferencedId) -> Bool {
|
||||||
|
return contacts.references(id: follow.ref_id, key: follow.key)
|
||||||
|
}
|
||||||
|
|
||||||
func follow_with_existing_contacts(our_pubkey: String, our_contacts: NostrEvent, follow: ReferencedId) -> NostrEvent? {
|
func follow_with_existing_contacts(our_pubkey: String, our_contacts: NostrEvent, follow: ReferencedId) -> NostrEvent? {
|
||||||
// don't update if we're already following
|
// don't update if we're already following
|
||||||
if our_contacts.references(id: follow.ref_id, key: "p") {
|
if is_already_following(contacts: our_contacts, follow: follow) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
let kind = NostrKind.contacts.rawValue
|
let kind = NostrKind.contacts.rawValue
|
||||||
var tags = our_contacts.tags
|
var tags = our_contacts.tags
|
||||||
tags.append(refid_to_tag(follow))
|
tags.append(refid_to_tag(follow))
|
||||||
|
|||||||
Reference in New Issue
Block a user