damus

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

commit e981ae247e882866f04bffef2ae865ddb6281ff7
parent dcd7b5b1116334605d98636428ed2e30cb2f8b9f
Author: Daniel D’Aquino <daniel@daquino.me>
Date:   Tue,  7 May 2024 04:31:40 +0000

Mute: Add `user_keypair` to MutelistManager

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 <daniel@daquino.me>
Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
MDamusNotificationService/NotificationExtensionState.swift | 2+-
Mdamus/ContentView.swift | 2+-
Mdamus/Models/DamusState.swift | 2+-
Mdamus/Models/HomeModel.swift | 4++--
Mdamus/Models/MutelistManager.swift | 15++++++++++-----
Mdamus/Models/NotificationsManager.swift | 2+-
Mdamus/TestData.swift | 2+-
Mdamus/Views/DMChatView.swift | 2+-
Mdamus/Views/DirectMessagesView.swift | 2+-
MdamusTests/Mocking/MockDamusState.swift | 2+-
10 files changed, 20 insertions(+), 15 deletions(-)

diff --git 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 @@ -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 @@ -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 @@ -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 @@ -8,12 +8,17 @@ import Foundation class MutelistManager { + let user_keypair: Keypair private(set) var event: NostrEvent? = nil var users: Set<MuteItem> = [] var hashtags: Set<MuteItem> = [] var threads: Set<MuteItem> = [] var words: Set<MuteItem> = [] + + 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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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),