damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit a5cb10ddaa6d48deca2c7b4af095cdeb1ed61f94
parent 4402d4bdf5844bf91f881e9c0535e3930aa83669
Author: William Casarin <jb55@jb55.com>
Date:   Fri,  5 May 2023 14:23:08 -0700

HomeView: refactor

Trying to clean this view up a bit

Diffstat:
Mdamus/ContentView.swift | 166+++++++++++++++++++++++++++++++++++++++++--------------------------------------
1 file changed, 86 insertions(+), 80 deletions(-)

diff --git a/damus/ContentView.swift b/damus/ContentView.swift @@ -370,81 +370,25 @@ struct ContentView: View { } } .onReceive(handle_notify(.unfollow)) { notif in - guard let privkey = self.privkey else { + guard let state = self.damus_state else { return } - - guard let damus = self.damus_state else { - return - } - - let target = notif.object as! FollowTarget - let pk = target.pubkey - - if let ev = unfollow_user(postbox: damus.postbox, - our_contacts: damus.contacts.event, - pubkey: damus.pubkey, - privkey: privkey, - unfollow: pk) { - notify(.unfollowed, pk) - - damus.contacts.event = ev - damus.contacts.remove_friend(pk) - //friend_events = friend_events.filter { $0.pubkey != pk } - } + handle_unfollow(state: state, notif: notif) } .onReceive(handle_notify(.follow)) { notif in - guard let privkey = self.privkey else { + guard let state = self.damus_state else { return } - - let fnotify = notif.object as! FollowTarget - guard let damus = self.damus_state else { - return - } - - if let ev = follow_user(pool: damus.pool, - our_contacts: damus.contacts.event, - pubkey: damus.pubkey, - privkey: privkey, - follow: ReferencedId(ref_id: fnotify.pubkey, relay_id: nil, key: "p")) { - notify(.followed, fnotify.pubkey) - - damus_state?.contacts.event = ev - - switch fnotify { - case .pubkey(let pk): - damus.contacts.add_friend_pubkey(pk) - case .contact(let ev): - damus.contacts.add_friend_contact(ev) - } - } + handle_follow(state: state, notif: notif) } - .onReceive(handle_notify(.post)) { obj in - guard let privkey = self.privkey else { - return + .onReceive(handle_notify(.post)) { notif in + guard let state = self.damus_state, + let keypair = state.keypair.to_full() else { + return } - - let post_res = obj.object as! NostrPostResult - switch post_res { - case .post(let post): - //let post = tup.0 - //let to_relays = tup.1 - print("post \(post.content)") - let new_ev = post_to_event(post: post, privkey: privkey, pubkey: pubkey) - guard let ds = self.damus_state else { - return - } - ds.postbox.send(new_ev) - for eref in new_ev.referenced_ids.prefix(3) { - // also broadcast at most 3 referenced events - if let ev = ds.events.lookup(eref.ref_id) { - ds.postbox.send(ev) - } - } - case .cancel: - active_sheet = nil - print("post cancelled") + + if !handle_post_notification(keypair: keypair, postbox: state.postbox, events: state.events, notif: notif) { + self.active_sheet = nil } } .onReceive(timer) { n in @@ -528,19 +472,11 @@ struct ContentView: View { } Button(NSLocalizedString("Yes, Overwrite", comment: "Text of button that confirms to overwrite the existing mutelist.")) { - guard let ds = damus_state else { - return - } - - guard let keypair = ds.keypair.to_full() else { - return - } - - guard let pubkey = muting else { - return - } - - guard let mutelist = create_or_update_mutelist(keypair: keypair, mprev: nil, to_add: pubkey) else { + guard let ds = damus_state, + let keypair = ds.keypair.to_full(), + let pubkey = muting, + let mutelist = create_or_update_mutelist(keypair: keypair, mprev: nil, to_add: pubkey) + else { return } @@ -848,3 +784,73 @@ func timeline_name(_ timeline: Timeline?) -> String { return NSLocalizedString("DMs", comment: "Toolbar label for DMs view, where DM is the English abbreviation for Direct Message.") } } + +func handle_unfollow(state: DamusState, notif: Notification) { + guard let privkey = state.keypair.privkey else { + return + } + + let target = notif.object as! FollowTarget + let pk = target.pubkey + + if let ev = unfollow_user(postbox: state.postbox, + our_contacts: state.contacts.event, + pubkey: state.pubkey, + privkey: privkey, + unfollow: pk) { + notify(.unfollowed, pk) + + state.contacts.event = ev + state.contacts.remove_friend(pk) + //friend_events = friend_events.filter { $0.pubkey != pk } + } +} + +func handle_follow(state: DamusState, notif: Notification) { + guard let privkey = state.keypair.privkey else { + return + } + + let fnotify = notif.object as! FollowTarget + + if let ev = follow_user(pool: state.pool, + our_contacts: state.contacts.event, + pubkey: state.pubkey, + privkey: privkey, + follow: ReferencedId(ref_id: fnotify.pubkey, relay_id: nil, key: "p")) { + notify(.followed, fnotify.pubkey) + + state.contacts.event = ev + + switch fnotify { + case .pubkey(let pk): + state.contacts.add_friend_pubkey(pk) + case .contact(let ev): + state.contacts.add_friend_contact(ev) + } + } +} + +func handle_post_notification(keypair: FullKeypair, postbox: PostBox, events: EventCache, notif: Notification) -> Bool { + let post_res = notif.object as! NostrPostResult + switch post_res { + case .post(let post): + //let post = tup.0 + //let to_relays = tup.1 + print("post \(post.content)") + let new_ev = post_to_event(post: post, privkey: keypair.privkey, pubkey: keypair.pubkey) + postbox.send(new_ev) + for eref in new_ev.referenced_ids.prefix(3) { + // also broadcast at most 3 referenced events + if let ev = events.lookup(eref.ref_id) { + postbox.send(ev) + } + } + return true + case .cancel: + print("post cancelled") + return false + } +} + +