damus

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

commit 237c939639de2b2c13d7290d8f1d01489c52a331
parent 4f86361b638513a82e90c3450c27d5ad11420733
Author: Oleg Gordiichuk <oleghordiichuk@gmail.com>
Date:   Sun, 12 Feb 2023 22:45:47 +0100

Add the ability to unlike posts

Changelog-Added: Add ability to unlike posts
Closes: #580

Diffstat:
Mdamus/ContentView.swift | 6++++++
Mdamus/Models/HomeModel.swift | 28+++++++++++++++++++++-------
Mdamus/Models/LikeCounter.swift | 13+++++++++++++
Mdamus/Nostr/NostrEvent.swift | 10++++++++++
Mdamus/Views/ActionBar/EventActionBar.swift | 9+++++++++
5 files changed, 59 insertions(+), 7 deletions(-)

diff --git a/damus/ContentView.swift b/damus/ContentView.swift @@ -387,6 +387,12 @@ struct ContentView: View { } .onReceive(handle_notify(.like)) { like in } + .onReceive(handle_notify(.delete)) { delete in + guard let ev = delete.object as? NostrEvent else { + return + } + self.home.handle_unlike_event(ev) + } .onReceive(handle_notify(.deleted_account)) { notif in self.is_deleted_account = true } diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift @@ -167,6 +167,15 @@ class HomeModel: ObservableObject { } } + + func handle_delete_event(_ ev: NostrEvent) { + guard ev.is_valid else { + return + } + + self.deleted_events.insert(ev.id) + } + func handle_channel_create(_ ev: NostrEvent) { guard ev.is_valid else { @@ -185,13 +194,18 @@ class HomeModel: ObservableObject { self.notifications = notifications.filter { !damus_state.contacts.is_muted($0.pubkey) } } - func handle_delete_event(_ ev: NostrEvent) { - guard ev.is_valid else { - return - } - - self.deleted_events.insert(ev.id) - } + + func handle_unlike_event(_ ev: NostrEvent) { + guard ev.is_valid, let privkey = self.damus_state.keypair.privkey, let e = ev.last_refid() else { + return + } + + let delete = make_delete_event(pubkey: damus_state.keypair.pubkey, privkey: privkey, deleted_events: [ev.id]) + + pool.send(.event(delete)) + + damus_state.likes.remove_event(ev, target: e.ref_id) + } func handle_contact_event(sub_id: String, relay_id: String, ev: NostrEvent) { process_contact_event(state: self.damus_state, ev: ev) diff --git a/damus/Models/LikeCounter.swift b/damus/Models/LikeCounter.swift @@ -49,4 +49,17 @@ class EventCounter { return .success(counts[target]!) } + + func remove_event(_ ev: NostrEvent, target: String) { + let pubkey = ev.pubkey + + user_events[pubkey]?.remove(target) + + if counts[target] == nil { + counts[target] = 0 + return + } + + counts[target]! -= 1 + } } diff --git a/damus/Nostr/NostrEvent.swift b/damus/Nostr/NostrEvent.swift @@ -578,6 +578,16 @@ func make_like_event(pubkey: String, privkey: String, liked: NostrEvent) -> Nost return ev } +func make_delete_event(pubkey: String, privkey: String, deleted_events: Set<String>) -> NostrEvent { + + let tags: [[String]] = deleted_events.map{["e", $0]} + + let ev = NostrEvent(content: "Content delete", pubkey: pubkey, kind: NostrKind.delete.rawValue, tags: tags) + ev.calculate_id() + ev.sign(privkey: privkey) + return ev +} + func zap_target_to_tags(_ target: ZapTarget) -> [[String]] { switch target { case .profile(let pk): diff --git a/damus/Views/ActionBar/EventActionBar.swift b/damus/Views/ActionBar/EventActionBar.swift @@ -120,6 +120,15 @@ struct EventActionBar: View { self.bar.our_like = liked.event } } + .onReceive(handle_notify(.delete)) { delete in + guard let deleteRequest = delete.object as? NostrEvent, deleteRequest.tags.flatMap({$0}).contains(event.id), + deleteRequest.pubkey == damus_state.keypair.pubkey else { + return + } + + self.bar.our_like = nil + self.bar.likes -= 1 + } } func send_boost() {