commit fe4277e817fba60bd05f36e8f2fbd0f87b25311d
parent 6804fbb607f3dc6239cd8338f7411e8b42451210
Author: Bryan Montz <bryanmontz@me.com>
Date: Sun, 5 Mar 2023 00:28:08 -0600
simplified call site usage of NostrFilter
Diffstat:
17 files changed, 62 insertions(+), 84 deletions(-)
diff --git a/damus/ContentView.swift b/damus/ContentView.swift
@@ -692,7 +692,7 @@ func update_filters_with_since(last_of_kind: [Int: NostrEvent], filters: [NostrF
let kinds = filter.kinds ?? []
let initial: Int64? = nil
let earliest = kinds.reduce(initial) { earliest, kind in
- let last = last_of_kind[kind]
+ let last = last_of_kind[kind.rawValue]
let since: Int64? = get_since_time(last_event: last)
if earliest == nil {
@@ -746,10 +746,10 @@ func find_event(state: DamusState, evid: String, search_type: SearchType, find_f
var has_event = false
- var filter = search_type == .event ? NostrFilter.filter_ids([ evid ]) : NostrFilter.filter_authors([ evid ])
+ var filter = search_type == .event ? NostrFilter(ids: [evid]) : NostrFilter(authors: [evid])
if search_type == .profile {
- filter.kinds = [NostrKind.metadata.rawValue]
+ filter.kinds = [.metadata]
}
filter.limit = 1
diff --git a/damus/Models/EventsModel.swift b/damus/Models/EventsModel.swift
@@ -24,7 +24,7 @@ class EventsModel: ObservableObject {
}
private func get_filter() -> NostrFilter {
- var filter = NostrFilter.filter_kinds([kind.rawValue])
+ var filter = NostrFilter(kinds: [kind])
filter.referenced_ids = [target]
filter.limit = 500
return filter
diff --git a/damus/Models/FollowersModel.swift b/damus/Models/FollowersModel.swift
@@ -30,9 +30,8 @@ class FollowersModel: ObservableObject {
}
func get_filter() -> NostrFilter {
- var filter = NostrFilter.filter_contacts
- filter.pubkeys = [target]
- return filter
+ NostrFilter(kinds: [.contacts],
+ pubkeys: [target])
}
func subscribe() {
@@ -56,14 +55,13 @@ class FollowersModel: ObservableObject {
}
func load_profiles(relay_id: String) {
- var filter = NostrFilter.filter_profiles
let authors = find_profiles_to_fetch_from_keys(profiles: damus_state.profiles, pks: contacts ?? [])
if authors.isEmpty {
return
}
- filter.authors = authors
-
+ let filter = NostrFilter(kinds: [.metadata],
+ authors: authors)
damus_state.pool.subscribe_to(sub_id: profiles_id, filters: [filter], to: [relay_id], handler: handle_event)
}
diff --git a/damus/Models/FollowingModel.swift b/damus/Models/FollowingModel.swift
@@ -21,7 +21,7 @@ class FollowingModel {
}
func get_filter() -> NostrFilter {
- var f = NostrFilter.filter_kinds([NostrKind.metadata.rawValue])
+ var f = NostrFilter(kinds: [.metadata])
f.authors = self.contacts.reduce(into: Array<String>()) { acc, pk in
// don't fetch profiles we already have
if damus_state.profiles.has_fresh_profile(id: pk) {
diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift
@@ -397,10 +397,9 @@ class HomeModel: ObservableObject {
/// Send the initial filters, just our contact list mostly
func send_initial_filters(relay_id: String) {
- var filter = NostrFilter.filter_contacts
- filter.authors = [self.damus_state.pubkey]
- filter.limit = 1
-
+ var filter = NostrFilter(kinds: [.contacts],
+ limit: 1,
+ authors: [damus_state.pubkey])
pool.send(.subscribe(.init(filters: [filter], sub_id: init_subid)), to: [relay_id])
}
@@ -411,23 +410,19 @@ class HomeModel: ObservableObject {
var friends = damus_state.contacts.get_friend_list()
friends.append(damus_state.pubkey)
- var contacts_filter = NostrFilter.filter_kinds([NostrKind.metadata.rawValue])
+ var contacts_filter = NostrFilter(kinds: [.metadata])
contacts_filter.authors = friends
- var our_contacts_filter = NostrFilter.filter_kinds([NostrKind.contacts.rawValue, NostrKind.metadata.rawValue])
+ var our_contacts_filter = NostrFilter(kinds: [.contacts, .metadata])
our_contacts_filter.authors = [damus_state.pubkey]
- var our_blocklist_filter = NostrFilter.filter_kinds([NostrKind.list.rawValue])
+ var our_blocklist_filter = NostrFilter(kinds: [.list])
our_blocklist_filter.parameter = ["mute"]
our_blocklist_filter.authors = [damus_state.pubkey]
- var dms_filter = NostrFilter.filter_kinds([
- NostrKind.dm.rawValue,
- ])
+ var dms_filter = NostrFilter(kinds: [.dm])
- var our_dms_filter = NostrFilter.filter_kinds([
- NostrKind.dm.rawValue,
- ])
+ var our_dms_filter = NostrFilter(kinds: [.dm])
// friends only?...
//dms_filter.authors = friends
@@ -436,27 +431,27 @@ class HomeModel: ObservableObject {
our_dms_filter.authors = [ damus_state.pubkey ]
// TODO: separate likes?
- var home_filter_kinds = [
- NostrKind.text.rawValue,
- NostrKind.boost.rawValue
+ var home_filter_kinds: [NostrKind] = [
+ .text,
+ .boost
]
if !damus_state.settings.onlyzaps_mode {
- home_filter_kinds.append(NostrKind.like.rawValue)
+ home_filter_kinds.append(.like)
}
- var home_filter = NostrFilter.filter_kinds(home_filter_kinds)
+ var home_filter = NostrFilter(kinds: home_filter_kinds)
// include our pubkey as well even if we're not technically a friend
home_filter.authors = friends
home_filter.limit = 500
- var notifications_filter_kinds = [
- NostrKind.text.rawValue,
- NostrKind.boost.rawValue,
- NostrKind.zap.rawValue,
+ var notifications_filter_kinds: [NostrKind] = [
+ .text,
+ .boost,
+ .zap,
]
if !damus_state.settings.onlyzaps_mode {
- notifications_filter_kinds.append(NostrKind.like.rawValue)
+ notifications_filter_kinds.append(.like)
}
- var notifications_filter = NostrFilter.filter_kinds(notifications_filter_kinds)
+ var notifications_filter = NostrFilter(kinds: notifications_filter_kinds)
notifications_filter.pubkeys = [damus_state.pubkey]
notifications_filter.limit = 500
diff --git a/damus/Models/ProfileModel.swift b/damus/Models/ProfileModel.swift
@@ -69,16 +69,9 @@ class ProfileModel: ObservableObject, Equatable {
}
func subscribe() {
- var text_filter = NostrFilter.filter_kinds([
- NostrKind.text.rawValue,
- NostrKind.chat.rawValue,
- ])
+ var text_filter = NostrFilter(kinds: [.text, .chat])
- var profile_filter = NostrFilter.filter_kinds([
- NostrKind.contacts.rawValue,
- NostrKind.metadata.rawValue,
- NostrKind.boost.rawValue,
- ])
+ var profile_filter = NostrFilter(kinds: [.contacts, .metadata, .boost])
profile_filter.authors = [pubkey]
diff --git a/damus/Models/SearchHomeModel.swift b/damus/Models/SearchHomeModel.swift
@@ -27,7 +27,7 @@ class SearchHomeModel: ObservableObject {
}
func get_base_filter() -> NostrFilter {
- var filter = NostrFilter.filter_kinds([NostrKind.text.rawValue, NostrKind.chat.rawValue])
+ var filter = NostrFilter(kinds: [.text, .chat])
filter.limit = self.limit
filter.until = Int64(Date.now.timeIntervalSince1970)
return filter
@@ -126,16 +126,16 @@ enum PubkeysToLoad {
}
func load_profiles(profiles_subid: String, relay_id: String, load: PubkeysToLoad, damus_state: DamusState) {
- var filter = NostrFilter.filter_profiles
let authors = find_profiles_to_fetch(profiles: damus_state.profiles, load: load, cache: damus_state.events)
- filter.authors = authors
-
guard !authors.isEmpty else {
return
}
print("loading \(authors.count) profiles from \(relay_id)")
+ let filter = NostrFilter(kinds: [.metadata],
+ authors: authors)
+
damus_state.pool.subscribe_to(sub_id: profiles_subid, filters: [filter], to: [relay_id]) { sub_id, conn_ev in
let (sid, done) = handle_subid_event(pool: damus_state.pool, relay_id: relay_id, ev: conn_ev) { sub_id, ev in
guard sub_id == profiles_subid else {
diff --git a/damus/Models/SearchModel.swift b/damus/Models/SearchModel.swift
@@ -34,7 +34,7 @@ class SearchModel: ObservableObject {
func subscribe() {
// since 1 month
search.limit = self.limit
- search.kinds = [NostrKind.text.rawValue, NostrKind.like.rawValue]
+ search.kinds = [.text, .like]
//likes_filter.ids = ref_events.referenced_ids!
diff --git a/damus/Models/ThreadModel.swift b/damus/Models/ThreadModel.swift
@@ -56,16 +56,16 @@ class ThreadModel: ObservableObject {
let thread_id = event.thread_id(privkey: nil)
ref_events.referenced_ids = [thread_id, event.id]
- ref_events.kinds = [NostrKind.text.rawValue]
+ ref_events.kinds = [.text]
ref_events.limit = 1000
event_filter.ids = [thread_id, event.id]
meta_events.referenced_ids = [event.id]
- var kinds = [NostrKind.zap.rawValue, NostrKind.text.rawValue, NostrKind.boost.rawValue]
+ var kinds: [NostrKind] = [.zap, .text, .boost]
if !damus_state.settings.onlyzaps_mode {
- kinds.append(NostrKind.like.rawValue)
+ kinds.append(.like)
}
meta_events.kinds = kinds
diff --git a/damus/Models/ZapsModel.swift b/damus/Models/ZapsModel.swift
@@ -24,7 +24,7 @@ class ZapsModel: ObservableObject {
}
func subscribe() {
- var filter = NostrFilter.filter_kinds([NostrKind.zap.rawValue])
+ var filter = NostrFilter(kinds: [.zap])
switch target {
case .profile(let profile_id):
filter.pubkeys = [profile_id]
diff --git a/damus/Nostr/NostrFilter.swift b/damus/Nostr/NostrFilter.swift
@@ -9,15 +9,15 @@ import Foundation
struct NostrFilter: Codable, Equatable {
var ids: [String]?
- var kinds: [Int]?
+ var kinds: [NostrKind]?
var referenced_ids: [String]?
var pubkeys: [String]?
var since: Int64?
var until: Int64?
var limit: UInt32?
var authors: [String]?
- var hashtag: [String]? = nil
- var parameter: [String]? = nil
+ var hashtag: [String]?
+ var parameter: [String]?
private enum CodingKeys : String, CodingKey {
case ids
@@ -32,31 +32,23 @@ struct NostrFilter: Codable, Equatable {
case limit
}
- public static func copy(from: NostrFilter) -> NostrFilter {
- return NostrFilter(ids: from.ids, kinds: from.kinds, referenced_ids: from.referenced_ids, pubkeys: from.pubkeys, since: from.since, until: from.until, authors: from.authors, hashtag: from.hashtag)
- }
-
- public static func filter_hashtag(_ htags: [String]) -> NostrFilter {
- return NostrFilter(ids: nil, kinds: nil, referenced_ids: nil, pubkeys: nil, since: nil, until: nil, authors: nil, hashtag: htags.map { $0.lowercased() })
- }
-
- public static func filter_ids(_ ids: [String]) -> NostrFilter {
- return NostrFilter(ids: ids, kinds: nil, referenced_ids: nil, pubkeys: nil, since: nil, until: nil, authors: nil, hashtag: nil)
+ init(ids: [String]? = nil, kinds: [NostrKind]? = nil, referenced_ids: [String]? = nil, pubkeys: [String]? = nil, since: Int64? = nil, until: Int64? = nil, limit: UInt32? = nil, authors: [String]? = nil, hashtag: [String]? = nil) {
+ self.ids = ids
+ self.kinds = kinds
+ self.referenced_ids = referenced_ids
+ self.pubkeys = pubkeys
+ self.since = since
+ self.until = until
+ self.limit = limit
+ self.authors = authors
+ self.hashtag = hashtag
}
- public static var filter_profiles: NostrFilter {
- return filter_kinds([NostrKind.metadata.rawValue])
- }
-
- public static var filter_contacts: NostrFilter {
- return filter_kinds([NostrKind.contacts.rawValue])
+ public static func copy(from: NostrFilter) -> NostrFilter {
+ NostrFilter(ids: from.ids, kinds: from.kinds, referenced_ids: from.referenced_ids, pubkeys: from.pubkeys, since: from.since, until: from.until, authors: from.authors, hashtag: from.hashtag)
}
- public static func filter_authors(_ authors: [String]) -> NostrFilter {
- return NostrFilter(ids: nil, kinds: nil, referenced_ids: nil, pubkeys: nil, since: nil, until: nil, authors: authors)
- }
-
- public static func filter_kinds(_ kinds: [Int]) -> NostrFilter {
- return NostrFilter(ids: nil, kinds: kinds, referenced_ids: nil, pubkeys: nil, since: nil, until: nil, authors: nil)
+ public static func filter_hashtag(_ htags: [String]) -> NostrFilter {
+ NostrFilter(hashtag: htags.map { $0.lowercased() })
}
}
diff --git a/damus/Nostr/NostrKind.swift b/damus/Nostr/NostrKind.swift
@@ -8,7 +8,7 @@
import Foundation
-enum NostrKind: Int {
+enum NostrKind: Int, Codable {
case metadata = 0
case text = 1
case contacts = 3
diff --git a/damus/Nostr/NostrLink.swift b/damus/Nostr/NostrLink.swift
@@ -140,7 +140,7 @@ func decode_nostr_uri(_ s: String) -> NostrLink? {
}
if tag_is_hashtag(parts) {
- return .filter(NostrFilter.filter_hashtag([parts[1]]))
+ return .filter(NostrFilter(hashtag: [parts[1].lowercased()]))
}
if let rid = tag_to_refid(parts) {
diff --git a/damus/Util/WalletConnect.swift b/damus/Util/WalletConnect.swift
@@ -175,7 +175,7 @@ func make_wallet_connect_request<T>(req: WalletRequest<T>, to_pk: String, keypai
}
func subscribe_to_nwc(url: WalletConnectURL, pool: RelayPool) {
- var filter: NostrFilter = .filter_kinds([NostrKind.nwc_response.rawValue])
+ var filter = NostrFilter(kinds: [.nwc_response])
filter.authors = [url.pubkey]
filter.limit = 0
let sub = NostrSubscribe(filters: [filter], sub_id: "nwc")
diff --git a/damus/Views/Events/BuilderEventView.swift b/damus/Views/Events/BuilderEventView.swift
@@ -61,7 +61,7 @@ struct BuilderEventView: View {
subscribe(filters: [
NostrFilter(ids: [self.event_id], limit: 1),
NostrFilter(
- kinds: [NostrKind.zap.rawValue],
+ kinds: [.zap],
referenced_ids: [self.event_id]
)
])
diff --git a/damus/Views/SearchView.swift b/damus/Views/SearchView.swift
@@ -42,7 +42,7 @@ func describe_search(_ filter: NostrFilter) -> String {
struct SearchView_Previews: PreviewProvider {
static var previews: some View {
let test_state = test_damus_state()
- let filter = NostrFilter.filter_hashtag(["bitcoin"])
+ let filter = NostrFilter(hashtag: ["bitcoin"])
let model = SearchModel(state: test_state, search: filter)
diff --git a/damusTests/RequestTests.swift b/damusTests/RequestTests.swift
@@ -30,7 +30,7 @@ final class RequestTests: XCTestCase {
}
func testMakeSubscriptionRequest() {
- let filter = NostrFilter(kinds: [NostrKind.contacts.rawValue], limit: 1, authors: ["d9fa34214aa9d151c4f4db843e9c2af4f246bab4205137731f91bcfa44d66a62"])
+ let filter = NostrFilter(kinds: [.contacts], limit: 1, authors: ["d9fa34214aa9d151c4f4db843e9c2af4f246bab4205137731f91bcfa44d66a62"])
let subscribe = NostrSubscribe(filters: [filter], sub_id: "31C737B7-C8F9-41DD-8707-325974F279A4")
let result = make_nostr_req(.subscribe(subscribe))
let expectedResult = "[\"REQ\",\"31C737B7-C8F9-41DD-8707-325974F279A4\",{\"kinds\":[3],\"authors\":[\"d9fa34214aa9d151c4f4db843e9c2af4f246bab4205137731f91bcfa44d66a62\"],\"limit\":1}]"