From e981ae247e882866f04bffef2ae865ddb6281ff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=E2=80=99Aquino?= Date: Tue, 7 May 2024 04:31:40 +0000 Subject: [PATCH] Mute: Add `user_keypair` to MutelistManager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The user keypair is necessary to determine whether or not a given event is muted or not. Previously, the user keypair had to be passed on each function call as an optional parameter, and word filtering would not work unless the caller remembered to add the keypair parameter. All usages of MutelistManager functions indicate that the desired base keypair is always the same as the DamusState's keypair that owns the MutelistManager. Therefore, it is simpler and less error prone to simply pass the keypair to MutelistManager during its initialization. Signed-off-by: Daniel D’Aquino Signed-off-by: William Casarin --- .../NotificationExtensionState.swift | 2 +- damus/ContentView.swift | 2 +- damus/Models/DamusState.swift | 2 +- damus/Models/HomeModel.swift | 4 ++-- damus/Models/MutelistManager.swift | 15 ++++++++++----- damus/Models/NotificationsManager.swift | 2 +- damus/TestData.swift | 2 +- damus/Views/DMChatView.swift | 2 +- damus/Views/DirectMessagesView.swift | 2 +- damusTests/Mocking/MockDamusState.swift | 2 +- 10 files changed, 20 insertions(+), 15 deletions(-) diff --git a/DamusNotificationService/NotificationExtensionState.swift b/DamusNotificationService/NotificationExtensionState.swift index d5cb989f..0cfb8e6a 100644 --- a/DamusNotificationService/NotificationExtensionState.swift +++ b/DamusNotificationService/NotificationExtensionState.swift @@ -28,7 +28,7 @@ struct NotificationExtensionState: HeadlessDamusState { self.settings = UserSettingsStore() self.contacts = Contacts(our_pubkey: keypair.pubkey) - self.mutelist_manager = MutelistManager() + self.mutelist_manager = MutelistManager(user_keypair: keypair) self.keypair = keypair self.profiles = Profiles(ndb: ndb) self.zaps = Zaps(our_pubkey: keypair.pubkey) diff --git a/damus/ContentView.swift b/damus/ContentView.swift index b8619102..b3846a57 100644 --- a/damus/ContentView.swift +++ b/damus/ContentView.swift @@ -699,7 +699,7 @@ struct ContentView: View { likes: EventCounter(our_pubkey: pubkey), boosts: EventCounter(our_pubkey: pubkey), contacts: Contacts(our_pubkey: pubkey), - mutelist_manager: MutelistManager(), + mutelist_manager: MutelistManager(user_keypair: keypair), profiles: Profiles(ndb: ndb), dms: home.dms, previews: PreviewCache(), diff --git a/damus/Models/DamusState.swift b/damus/Models/DamusState.swift index ed4cf268..13679e45 100644 --- a/damus/Models/DamusState.swift +++ b/damus/Models/DamusState.swift @@ -112,7 +112,7 @@ class DamusState: HeadlessDamusState { likes: EventCounter(our_pubkey: empty_pub), boosts: EventCounter(our_pubkey: empty_pub), contacts: Contacts(our_pubkey: empty_pub), - mutelist_manager: MutelistManager(), + mutelist_manager: MutelistManager(user_keypair: kp), profiles: Profiles(ndb: .empty), dms: DirectMessagesModel(our_pubkey: empty_pub), previews: PreviewCache(), diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift index 82d5bde1..5f040dc6 100644 --- a/damus/Models/HomeModel.swift +++ b/damus/Models/HomeModel.swift @@ -1148,8 +1148,8 @@ func should_show_event(event: NostrEvent, damus_state: DamusState) -> Bool { ) } -func should_show_event(state: DamusState, ev: NostrEvent, keypair: Keypair? = nil) -> Bool { - let event_muted = state.mutelist_manager.is_event_muted(ev, keypair: keypair) +func should_show_event(state: DamusState, ev: NostrEvent) -> Bool { + let event_muted = state.mutelist_manager.is_event_muted(ev) if event_muted { return false } diff --git a/damus/Models/MutelistManager.swift b/damus/Models/MutelistManager.swift index 90a640fe..88ef8f90 100644 --- a/damus/Models/MutelistManager.swift +++ b/damus/Models/MutelistManager.swift @@ -8,12 +8,17 @@ import Foundation class MutelistManager { + let user_keypair: Keypair private(set) var event: NostrEvent? = nil var users: Set = [] var hashtags: Set = [] var threads: Set = [] var words: Set = [] + + init(user_keypair: Keypair) { + self.user_keypair = user_keypair + } func refresh_sets() { guard let referenced_mute_items = event?.referenced_mute_items else { return } @@ -55,8 +60,8 @@ class MutelistManager { } } - func is_event_muted(_ ev: NostrEvent, keypair: Keypair? = nil) -> Bool { - return event_muted_reason(ev, keypair: keypair) != nil + func is_event_muted(_ ev: NostrEvent) -> Bool { + return self.event_muted_reason(ev) != nil } func set_mutelist(_ ev: NostrEvent) { @@ -120,9 +125,9 @@ class MutelistManager { /// /// - Parameter ev: The ``NostrEvent`` that you want to check the muted reason for. /// - Returns: The ``MuteItem`` that matched the event. Or `nil` if the event is not muted. - func event_muted_reason(_ ev: NostrEvent, keypair: Keypair? = nil) -> MuteItem? { + func event_muted_reason(_ ev: NostrEvent) -> MuteItem? { // Events from the current user should not be muted. - guard keypair?.pubkey != ev.pubkey else { return nil } + guard self.user_keypair.pubkey != ev.pubkey else { return nil } // Check if user is muted let check_user_item = MuteItem.user(ev.pubkey, nil) @@ -147,7 +152,7 @@ class MutelistManager { } // Check if word is muted - if let keypair, let content: String = ev.maybe_get_content(keypair)?.lowercased() { + if let content: String = ev.maybe_get_content(self.user_keypair)?.lowercased() { for word in words { if case .word(let string, _) = word { if content.contains(string.lowercased()) { diff --git a/damus/Models/NotificationsManager.swift b/damus/Models/NotificationsManager.swift index d59527d4..8fcaa554 100644 --- a/damus/Models/NotificationsManager.swift +++ b/damus/Models/NotificationsManager.swift @@ -37,7 +37,7 @@ func should_display_notification(state: HeadlessDamusState, event ev: NostrEvent } // Don't show notifications that match mute list. - if state.mutelist_manager.is_event_muted(ev, keypair: state.keypair) { + if state.mutelist_manager.is_event_muted(ev) { return false } diff --git a/damus/TestData.swift b/damus/TestData.swift index 1de6ac02..d7e9c977 100644 --- a/damus/TestData.swift +++ b/damus/TestData.swift @@ -73,7 +73,7 @@ var test_damus_state: DamusState = ({ likes: .init(our_pubkey: our_pubkey), boosts: .init(our_pubkey: our_pubkey), contacts: .init(our_pubkey: our_pubkey), - mutelist_manager: MutelistManager(), + mutelist_manager: MutelistManager(user_keypair: test_keypair), profiles: .init(ndb: ndb), dms: .init(our_pubkey: our_pubkey), previews: .init(), diff --git a/damus/Views/DMChatView.swift b/damus/Views/DMChatView.swift index 32a39fbc..fcf17362 100644 --- a/damus/Views/DMChatView.swift +++ b/damus/Views/DMChatView.swift @@ -20,7 +20,7 @@ struct DMChatView: View, KeyboardReadable { ScrollViewReader { scroller in ScrollView { LazyVStack(alignment: .leading) { - ForEach(Array(zip(dms.events, dms.events.indices)).filter { should_show_event(state: damus_state, ev: $0.0, keypair: damus_state.keypair)}, id: \.0.id) { (ev, ind) in + ForEach(Array(zip(dms.events, dms.events.indices)).filter { should_show_event(state: damus_state, ev: $0.0)}, id: \.0.id) { (ev, ind) in DMView(event: dms.events[ind], damus_state: damus_state) .contextMenu{MenuItems(damus_state: damus_state, event: ev, target_pubkey: ev.pubkey, profileModel: ProfileModel(pubkey: ev.pubkey, damus: damus_state))} } diff --git a/damus/Views/DirectMessagesView.swift b/damus/Views/DirectMessagesView.swift index 2f41aead..9f3c8b4b 100644 --- a/damus/Views/DirectMessagesView.swift +++ b/damus/Views/DirectMessagesView.swift @@ -55,7 +55,7 @@ struct DirectMessagesView: View { func MaybeEvent(_ model: DirectMessageModel) -> some View { Group { - if let ev = model.events.last(where: { should_show_event(state: damus_state, ev: $0, keypair: damus_state.keypair) }) { + if let ev = model.events.last(where: { should_show_event(state: damus_state, ev: $0) }) { EventView(damus: damus_state, event: ev, pubkey: model.pubkey, options: options) .onTapGesture { self.model.set_active_dm_model(model) diff --git a/damusTests/Mocking/MockDamusState.swift b/damusTests/Mocking/MockDamusState.swift index fb02cd62..e554f2fe 100644 --- a/damusTests/Mocking/MockDamusState.swift +++ b/damusTests/Mocking/MockDamusState.swift @@ -25,7 +25,7 @@ func generate_test_damus_state( return profiles }() - let mutelist_manager = MutelistManager() + let mutelist_manager = MutelistManager(user_keypair: test_keypair) let damus = DamusState(pool: pool, keypair: test_keypair, likes: .init(our_pubkey: our_pubkey),