Skip to content

Commit e7fe4ab

Browse files
tyiudanieldaquino
authored andcommitted
Inverse hellthread_notifications_enabled to be hellthread_notifications_disabled and add hellthread_notifications_max_pubkeys setting
Signed-off-by: Terry Yiu <[email protected]>
1 parent c146bab commit e7fe4ab

9 files changed

+109
-39
lines changed

damus/Models/NotificationsManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func should_display_notification(state: HeadlessDamusState, event ev: NostrEvent
4141
return false
4242
}
4343

44-
if !state.settings.hellthread_notification && ev.is_hellthread {
44+
if state.settings.hellthread_notifications_disabled && ev.is_hellthread(max_pubkeys: state.settings.hellthread_notification_max_pubkeys) {
4545
return false
4646
}
4747

damus/Models/PushNotificationClient.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
import Foundation
99

10+
// Minimum threshold the hellthread pubkey tag count setting can go down to.
11+
let HELLTHREAD_MIN_PUBKEYS: Int = 6
12+
13+
// Maximum threshold the hellthread pubkey tag count setting can go up to.
14+
let HELLTHREAD_MAX_PUBKEYS: Int = 24
15+
1016
struct PushNotificationClient {
1117
let keypair: Keypair
1218
let settings: UserSettingsStore
@@ -181,10 +187,27 @@ extension PushNotificationClient {
181187
let reaction_notifications_enabled: Bool?
182188
let dm_notifications_enabled: Bool?
183189
let only_notifications_from_following_enabled: Bool?
184-
let hellthread_notifications_enabled: Bool?
190+
let hellthread_notifications_disabled: Bool?
191+
let hellthread_notifications_max_pubkeys: Int?
185192

186193
static func from(json_data: Data) -> Self? {
187194
guard let decoded = try? JSONDecoder().decode(Self.self, from: json_data) else { return nil }
195+
196+
// Normalize hellthread_notifications_max_pubkeys in case
197+
// it goes beyond the expected range supported on the client.
198+
if let max_pubkeys = decoded.hellthread_notifications_max_pubkeys, max_pubkeys < HELLTHREAD_MIN_PUBKEYS || max_pubkeys > HELLTHREAD_MAX_PUBKEYS {
199+
return NotificationSettings(
200+
zap_notifications_enabled: decoded.zap_notifications_enabled,
201+
mention_notifications_enabled: decoded.mention_notifications_enabled,
202+
repost_notifications_enabled: decoded.repost_notifications_enabled,
203+
reaction_notifications_enabled: decoded.reaction_notifications_enabled,
204+
dm_notifications_enabled: decoded.dm_notifications_enabled,
205+
only_notifications_from_following_enabled: decoded.only_notifications_from_following_enabled,
206+
hellthread_notifications_disabled: decoded.hellthread_notifications_disabled,
207+
hellthread_notifications_max_pubkeys: max(min(HELLTHREAD_MAX_PUBKEYS, max_pubkeys), HELLTHREAD_MIN_PUBKEYS)
208+
)
209+
}
210+
188211
return decoded
189212
}
190213

@@ -196,7 +219,8 @@ extension PushNotificationClient {
196219
reaction_notifications_enabled: settings.like_notification,
197220
dm_notifications_enabled: settings.dm_notification,
198221
only_notifications_from_following_enabled: settings.notification_only_from_following,
199-
hellthread_notifications_enabled: settings.hellthread_notification
222+
hellthread_notifications_disabled: settings.hellthread_notifications_disabled,
223+
hellthread_notifications_max_pubkeys: settings.hellthread_notification_max_pubkeys
200224
)
201225
}
202226

damus/Models/UserSettingsStore.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,11 @@ class UserSettingsStore: ObservableObject {
161161
@Setting(key: "notification_only_from_following", default_value: false)
162162
var notification_only_from_following: Bool
163163

164-
@Setting(key: "hellthread_notification", default_value: false)
165-
var hellthread_notification: Bool
164+
@Setting(key: "hellthread_notifications_disabled", default_value: false)
165+
var hellthread_notifications_disabled: Bool
166+
167+
@Setting(key: "hellthread_notification_max_pubkeys", default_value: DEFAULT_HELLTHREAD_MAX_PUBKEYS)
168+
var hellthread_notification_max_pubkeys: Int
166169

167170
@Setting(key: "translate_dms", default_value: false)
168171
var translate_dms: Bool

damus/Views/Notifications/NotificationsView.swift

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,26 @@ import SwiftUI
1010
class NotificationFilter: ObservableObject, Equatable {
1111
@Published var state: NotificationFilterState
1212
@Published var friend_filter: FriendFilter
13-
@Published var show_hellthreads: Bool = false
13+
@Published var hellthread_notifications_disabled: Bool
14+
@Published var hellthread_notification_max_pubkeys: Int
1415

1516
static func == (lhs: NotificationFilter, rhs: NotificationFilter) -> Bool {
16-
return lhs.state == rhs.state && lhs.friend_filter == rhs.friend_filter && lhs.show_hellthreads == rhs.show_hellthreads
17+
return lhs.state == rhs.state
18+
&& lhs.friend_filter == rhs.friend_filter
19+
&& lhs.hellthread_notifications_disabled == rhs.hellthread_notifications_disabled
20+
&& lhs.hellthread_notification_max_pubkeys == rhs.hellthread_notification_max_pubkeys
1721
}
1822

19-
init(state: NotificationFilterState = .all, friend_filter: FriendFilter = .all, show_hellthreads: Bool = false) {
23+
init(
24+
state: NotificationFilterState = .all,
25+
friend_filter: FriendFilter = .all,
26+
hellthread_notifications_disabled: Bool = false,
27+
hellthread_notification_max_pubkeys: Int = DEFAULT_HELLTHREAD_MAX_PUBKEYS
28+
) {
2029
self.state = state
2130
self.friend_filter = friend_filter
22-
self.show_hellthreads = show_hellthreads
31+
self.hellthread_notifications_disabled = hellthread_notifications_disabled
32+
self.hellthread_notification_max_pubkeys = hellthread_notification_max_pubkeys
2333
}
2434

2535
func filter(contacts: Contacts, items: [NotificationItem]) -> [NotificationItem] {
@@ -31,7 +41,7 @@ class NotificationFilter: ObservableObject, Equatable {
3141

3242
if let item = item.filter({ ev in
3343
self.friend_filter.filter(contacts: contacts, pubkey: ev.pubkey) &&
34-
(show_hellthreads || !ev.is_hellthread)
44+
(!hellthread_notifications_disabled || !ev.is_hellthread(max_pubkeys: hellthread_notification_max_pubkeys))
3545
}) {
3646
acc.append(item)
3747
}
@@ -71,7 +81,8 @@ struct NotificationsView: View {
7181
NotificationFilter(
7282
state: .all,
7383
friend_filter: filter.friend_filter,
74-
show_hellthreads: state.settings.hellthread_notification
84+
hellthread_notifications_disabled: state.settings.hellthread_notifications_disabled,
85+
hellthread_notification_max_pubkeys: state.settings.hellthread_notification_max_pubkeys
7586
)
7687
)
7788
.tag(NotificationFilterState.all)
@@ -80,7 +91,8 @@ struct NotificationsView: View {
8091
NotificationFilter(
8192
state: .zaps,
8293
friend_filter: filter.friend_filter,
83-
show_hellthreads: state.settings.hellthread_notification
94+
hellthread_notifications_disabled: state.settings.hellthread_notifications_disabled,
95+
hellthread_notification_max_pubkeys: state.settings.hellthread_notification_max_pubkeys
8496
)
8597
)
8698
.tag(NotificationFilterState.zaps)
@@ -89,7 +101,8 @@ struct NotificationsView: View {
89101
NotificationFilter(
90102
state: .replies,
91103
friend_filter: filter.friend_filter,
92-
show_hellthreads: state.settings.hellthread_notification
104+
hellthread_notifications_disabled: state.settings.hellthread_notifications_disabled,
105+
hellthread_notification_max_pubkeys: state.settings.hellthread_notification_max_pubkeys
93106
)
94107
)
95108
.tag(NotificationFilterState.replies)

damus/Views/Settings/NotificationSettingsView.swift

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ struct NotificationSettingsView: View {
1717

1818
@Environment(\.dismiss) var dismiss
1919

20-
let hellthread_notification_settings_text = pluralizedString(key: "hellthread_notification_settings", count: MIN_HELLTHREAD_PUBKEYS - 1)
21-
2220
func indicator_binding(_ val: NewEventsBits) -> Binding<Bool> {
2321
return Binding.init(get: {
2422
(settings.notification_indicators & val.rawValue) > 0
@@ -30,7 +28,15 @@ struct NotificationSettingsView: View {
3028
}
3129
})
3230
}
33-
31+
32+
var hellthread_notification_max_pubkeys_binding: Binding<Double> {
33+
Binding<Double>(get: {
34+
return Double(settings.hellthread_notification_max_pubkeys)
35+
}, set: {
36+
settings.hellthread_notification_max_pubkeys = Int($0)
37+
})
38+
}
39+
3440
func try_to_set_notifications_mode(new_value: UserSettingsStore.NotificationsMode) {
3541
notification_mode_setting_error = nil
3642
if new_value == .push {
@@ -113,7 +119,24 @@ struct NotificationSettingsView: View {
113119
}
114120

115121
// MARK: - View layout
116-
122+
123+
func hellthread_notification_settings_text() -> String {
124+
if !settings.hellthread_notifications_disabled {
125+
return NSLocalizedString("Hide notifications that tag many profiles", comment: "Label for notification settings toggle that hides notifications that tag many people.")
126+
}
127+
return pluralizedString(key: "hellthread_notifications_disabled", count: $settings.hellthread_notification_max_pubkeys.wrappedValue)
128+
}
129+
130+
var hellthread_notifications_max_pubkeys_view: some View {
131+
VStack(alignment: .leading) {
132+
Slider(
133+
value: self.notification_preference_binding(hellthread_notification_max_pubkeys_binding),
134+
in: Double(HELLTHREAD_MIN_PUBKEYS)...Double(HELLTHREAD_MAX_PUBKEYS),
135+
step: 1
136+
)
137+
}
138+
}
139+
117140
var body: some View {
118141
Form {
119142
if settings.enable_push_notifications {
@@ -177,8 +200,13 @@ struct NotificationSettingsView: View {
177200
.toggleStyle(.switch)
178201
Toggle(NSLocalizedString("Show only from users you follow", comment: "Setting to Show notifications only associated to users your follow"), isOn: self.notification_preference_binding($settings.notification_only_from_following))
179202
.toggleStyle(.switch)
180-
Toggle(hellthread_notification_settings_text, isOn: $settings.hellthread_notification)
181-
.toggleStyle(.switch)
203+
VStack {
204+
Toggle(hellthread_notification_settings_text(), isOn: self.notification_preference_binding($settings.hellthread_notifications_disabled))
205+
.toggleStyle(.switch)
206+
if settings.hellthread_notifications_disabled {
207+
hellthread_notifications_max_pubkeys_view
208+
}
209+
}
182210
}
183211

184212
Section(

damus/en-US.lproj/Localizable.stringsdict

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,52 +50,52 @@
5050
<string>Following</string>
5151
</dict>
5252
</dict>
53-
<key>imports_count</key>
53+
<key>hellthread_notifications_disabled</key>
5454
<dict>
5555
<key>NSStringLocalizedFormatKey</key>
56-
<string>%#@IMPORTS@</string>
57-
<key>IMPORTS</key>
56+
<string>%#@HELLTHREAD_PROFILES@</string>
57+
<key>HELLTHREAD_PROFILES</key>
5858
<dict>
5959
<key>NSStringFormatSpecTypeKey</key>
6060
<string>NSStringPluralRuleType</string>
6161
<key>NSStringFormatValueTypeKey</key>
6262
<string>d</string>
6363
<key>one</key>
64-
<string>Import</string>
64+
<string>Hide notifications that tag more than %d profile</string>
6565
<key>other</key>
66-
<string>Imports</string>
66+
<string>Hide notifications that tag more than %d profiles</string>
6767
</dict>
6868
</dict>
69-
<key>people_reposted_count</key>
69+
<key>imports_count</key>
7070
<dict>
7171
<key>NSStringLocalizedFormatKey</key>
72-
<string>%#@REPOSTED@</string>
73-
<key>REPOSTED</key>
72+
<string>%#@IMPORTS@</string>
73+
<key>IMPORTS</key>
7474
<dict>
7575
<key>NSStringFormatSpecTypeKey</key>
7676
<string>NSStringPluralRuleType</string>
7777
<key>NSStringFormatValueTypeKey</key>
7878
<string>d</string>
7979
<key>one</key>
80-
<string>%2$@ and %1$d other reposted</string>
80+
<string>Import</string>
8181
<key>other</key>
82-
<string>%2$@ and %1$d others reposted</string>
82+
<string>Imports</string>
8383
</dict>
8484
</dict>
85-
<key>hellthread_notification_settings</key>
85+
<key>people_reposted_count</key>
8686
<dict>
8787
<key>NSStringLocalizedFormatKey</key>
88-
<string>%#@HELLTHREAD_PROFILES@</string>
89-
<key>HELLTHREAD_PROFILES</key>
88+
<string>%#@REPOSTED@</string>
89+
<key>REPOSTED</key>
9090
<dict>
9191
<key>NSStringFormatSpecTypeKey</key>
9292
<string>NSStringPluralRuleType</string>
9393
<key>NSStringFormatValueTypeKey</key>
9494
<string>d</string>
9595
<key>one</key>
96-
<string>Show notifications that mention more than %d profile</string>
96+
<string>%2$@ and %1$d other reposted</string>
9797
<key>other</key>
98-
<string>Show notifications that mention more than %d profiles</string>
98+
<string>%2$@ and %1$d others reposted</string>
9999
</dict>
100100
</dict>
101101
<key>quoted_reposts_count</key>

damusTests/LargeEventTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ final class LargeEventTests: XCTestCase {
4343

4444
XCTAssertEqual(subid, "subid")
4545
XCTAssertTrue(ev.should_show_event)
46-
XCTAssertTrue(ev.is_hellthread)
46+
XCTAssertTrue(ev.is_hellthread(max_pubkeys: 10))
4747
XCTAssertTrue(validate_event(ev: ev) == .ok)
4848
}
4949

damusTests/LocalizationUtilTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ final class LocalizationUtilTests: XCTestCase {
1717
let keys = [
1818
["followers_count", "Followers", "Follower", "Followers"],
1919
["following_count", "Following", "Following", "Following"],
20+
["hellthread_notifications_disabled", "Hide notifications that tag more than 0 profiles", "Hide notifications that tag more than 1 profile", "Hide notifications that tag more than 2 profiles"],
2021
["imports_count", "Imports", "Import", "Imports"],
21-
["hellthread_notification_settings", "Show notifications that mention more than 0 profiles", "Show notifications that mention more than 1 profile", "Show notifications that mention more than 2 profiles"],
2222
["quoted_reposts_count", "Quotes", "Quote", "Quotes"],
2323
["reactions_count", "Reactions", "Reaction", "Reactions"],
2424
["relays_count", "Relays", "Relay", "Relays"],

nostrdb/NdbNote.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import secp256k1_implementation
1313
import CryptoKit
1414

1515
let MAX_NOTE_SIZE: Int = 2 << 18
16-
let MIN_HELLTHREAD_PUBKEYS = 11
16+
17+
// Default threshold of the hellthread pubkey tag count setting if it is not set.
18+
let DEFAULT_HELLTHREAD_MAX_PUBKEYS: Int = 10
1719

1820
struct NdbStr {
1921
let note: NdbNote
@@ -300,10 +302,10 @@ extension NdbNote {
300302
return !too_big
301303
}
302304

303-
var is_hellthread: Bool {
305+
func is_hellthread(max_pubkeys: Int) -> Bool {
304306
switch known_kind {
305307
case .text, .boost, .like, .zap:
306-
Set(referenced_pubkeys).count >= MIN_HELLTHREAD_PUBKEYS
308+
Set(referenced_pubkeys).count > max_pubkeys
307309
default:
308310
false
309311
}

0 commit comments

Comments
 (0)