damus

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

commit e3c04465fc4e3801a95baa71d6487c4e2b8df19c
parent 54d40f7ffd163a593a009c5dcd34cdddc409facd
Author: William Casarin <jb55@jb55.com>
Date:   Tue, 25 Jul 2023 15:20:51 -0700

ndb: move to uint32 for kind and created_at

Diffstat:
Mdamus/ContentView.swift | 14+++++++-------
Mdamus/Models/HomeModel.swift | 6+++---
Mdamus/Models/Notifications/EventGroup.swift | 2+-
Mdamus/Models/Notifications/ZapGroup.swift | 2+-
Mdamus/Models/NotificationsModel.swift | 2+-
Mdamus/Models/SearchHomeModel.swift | 2+-
Mdamus/Nostr/NostrEvent.swift | 8++++----
Mdamus/Nostr/NostrFilter.swift | 6+++---
Mdamus/Nostr/NostrKind.swift | 2+-
Mdamus/Util/WalletConnect.swift | 2+-
Mdamus/Util/Zap.swift | 2+-
Mdamus/Views/DMChatView.swift | 9++++-----
Mdamus/Views/EventView.swift | 4++--
MdamusTests/DMTests.swift | 4++--
14 files changed, 32 insertions(+), 33 deletions(-)

diff --git a/damus/ContentView.swift b/damus/ContentView.swift @@ -10,7 +10,7 @@ import AVKit struct TimestampedProfile { let profile: Profile - let timestamp: Int64 + let timestamp: UInt32 let event: NostrEvent } @@ -662,7 +662,7 @@ struct ContentView_Previews: PreviewProvider { } } -func get_since_time(last_event: NostrEvent?) -> Int64? { +func get_since_time(last_event: NostrEvent?) -> UInt32? { if let last_event = last_event { return last_event.created_at - 60 * 10 } @@ -705,15 +705,15 @@ func save_last_event(_ ev: NostrEvent, timeline: Timeline) { UserDefaults.standard.set(String(ev.created_at), forKey: "last_\(str)_time") } -func update_filters_with_since(last_of_kind: [Int: NostrEvent], filters: [NostrFilter]) -> [NostrFilter] { - +func update_filters_with_since(last_of_kind: [UInt32: NostrEvent], filters: [NostrFilter]) -> [NostrFilter] { + return filters.map { filter in let kinds = filter.kinds ?? [] - let initial: Int64? = nil + let initial: UInt32? = nil let earliest = kinds.reduce(initial) { earliest, kind in let last = last_of_kind[kind.rawValue] - let since: Int64? = get_since_time(last_event: last) - + let since: UInt32? = get_since_time(last_event: last) + if earliest == nil { if since == nil { return nil diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift @@ -66,7 +66,7 @@ class HomeModel { var has_event: [String: Set<String>] = [:] var deleted_events: Set<String> = Set() var channels: [String: NostrEvent] = [:] - var last_event_of_kind: [String: [Int: NostrEvent]] = [:] + var last_event_of_kind: [String: [UInt32: NostrEvent]] = [:] var done_init: Bool = false var incoming_dms: [NostrEvent] = [] let dm_debouncer = Debouncer(interval: 0.5) @@ -494,7 +494,7 @@ class HomeModel { pool.send(.subscribe(.init(filters: dms_filters, sub_id: dms_subid)), to: relay_ids) } - func get_last_of_kind(relay_id: String?) -> [Int: NostrEvent] { + func get_last_of_kind(relay_id: String?) -> [UInt32: NostrEvent] { return relay_id.flatMap { last_event_of_kind[$0] } ?? [:] } @@ -566,7 +566,7 @@ class HomeModel { process_metadata_event(events: damus_state.events, our_pubkey: damus_state.pubkey, profiles: damus_state.profiles, ev: ev) } - func get_last_event_of_kind(relay_id: String, kind: Int) -> NostrEvent? { + func get_last_event_of_kind(relay_id: String, kind: UInt32) -> NostrEvent? { guard let m = last_event_of_kind[relay_id] else { last_event_of_kind[relay_id] = [:] return nil diff --git a/damus/Models/Notifications/EventGroup.swift b/damus/Models/Notifications/EventGroup.swift @@ -10,7 +10,7 @@ import Foundation class EventGroup { var events: [NostrEvent] - var last_event_at: Int64 { + var last_event_at: UInt32 { guard let first = self.events.first else { return 0 } diff --git a/damus/Models/Notifications/ZapGroup.swift b/damus/Models/Notifications/ZapGroup.swift @@ -12,7 +12,7 @@ class ZapGroup { var msat_total: Int64 = 0 var zappers = Set<String>() - var last_event_at: Int64 { + var last_event_at: UInt32 { guard let first = zaps.first else { return 0 } diff --git a/damus/Models/NotificationsModel.swift b/damus/Models/NotificationsModel.swift @@ -51,7 +51,7 @@ enum NotificationItem { } } - var last_event_at: Int64 { + var last_event_at: UInt32 { switch self { case .reaction(_, let evgrp): return evgrp.last_event_at diff --git a/damus/Models/SearchHomeModel.swift b/damus/Models/SearchHomeModel.swift @@ -30,7 +30,7 @@ class SearchHomeModel: ObservableObject { func get_base_filter() -> NostrFilter { var filter = NostrFilter(kinds: [.text, .chat]) filter.limit = self.limit - filter.until = Int64(Date.now.timeIntervalSince1970) + filter.until = UInt32(Date.now.timeIntervalSince1970) return filter } diff --git a/damus/Nostr/NostrEvent.swift b/damus/Nostr/NostrEvent.swift @@ -63,8 +63,8 @@ class NostrEventOld: Codable, Identifiable, CustomStringConvertible, Equatable, var flags: Int = 0 let pubkey: String - let created_at: Int64 - let kind: Int + let created_at: UInt32 + let kind: UInt32 // cached stuff private var _event_refs: [EventRef]? = nil @@ -665,8 +665,8 @@ func make_zap_request_event(keypair: FullKeypair, content: String, relays: [Rela var kp = keypair - let now = Int64(Date().timeIntervalSince1970) - + let now = UInt32(Date().timeIntervalSince1970) + var privzap_req: PrivateZapRequest? var message = content diff --git a/damus/Nostr/NostrFilter.swift b/damus/Nostr/NostrFilter.swift @@ -12,8 +12,8 @@ struct NostrFilter: Codable, Equatable { var kinds: [NostrKind]? var referenced_ids: [String]? var pubkeys: [String]? - var since: Int64? - var until: Int64? + var since: UInt32? + var until: UInt32? var limit: UInt32? var authors: [String]? var hashtag: [String]? @@ -32,7 +32,7 @@ struct NostrFilter: Codable, Equatable { case limit } - 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) { + init(ids: [String]? = nil, kinds: [NostrKind]? = nil, referenced_ids: [String]? = nil, pubkeys: [String]? = nil, since: UInt32? = nil, until: UInt32? = nil, limit: UInt32? = nil, authors: [String]? = nil, hashtag: [String]? = nil) { self.ids = ids self.kinds = kinds self.referenced_ids = referenced_ids diff --git a/damus/Nostr/NostrKind.swift b/damus/Nostr/NostrKind.swift @@ -8,7 +8,7 @@ import Foundation -enum NostrKind: Int, Codable { +enum NostrKind: UInt32, Codable { case metadata = 0 case text = 1 case contacts = 3 diff --git a/damus/Util/WalletConnect.swift b/damus/Util/WalletConnect.swift @@ -167,7 +167,7 @@ struct PayInvoiceRequest: Codable { func make_wallet_connect_request<T>(req: WalletRequest<T>, to_pk: String, keypair: FullKeypair) -> NostrEvent? { let tags = [["p", to_pk]] - let created_at = Int64(Date().timeIntervalSince1970) + let created_at = UInt32(Date().timeIntervalSince1970) guard let content = encode_json(req) else { return nil } diff --git a/damus/Util/Zap.swift b/damus/Util/Zap.swift @@ -216,7 +216,7 @@ enum Zapping { } } - var created_at: Int64 { + var created_at: UInt32 { switch self { case .zap(let zap): return zap.event.created_at diff --git a/damus/Views/DMChatView.swift b/damus/Views/DMChatView.swift @@ -209,8 +209,7 @@ func encrypt_message(message: String, privkey: String, to_pk: String, encoding: } -func create_encrypted_event(_ message: String, to_pk: String, tags: [[String]], keypair: FullKeypair, created_at: Int64, kind: Int) -> NostrEvent? { - +func create_encrypted_event(_ message: String, to_pk: String, tags: [[String]], keypair: FullKeypair, created_at: UInt32, kind: UInt32) -> NostrEvent? { let privkey = keypair.privkey guard let enc_content = encrypt_message(message: message, privkey: privkey, to_pk: to_pk) else { @@ -224,10 +223,10 @@ func create_encrypted_event(_ message: String, to_pk: String, tags: [[String]], return ev } -func create_dm(_ message: String, to_pk: String, tags: [[String]], keypair: Keypair, created_at: Int64? = nil) -> NostrEvent? +func create_dm(_ message: String, to_pk: String, tags: [[String]], keypair: Keypair, created_at: UInt32? = nil) -> NostrEvent? { - let created = created_at ?? Int64(Date().timeIntervalSince1970) - + let created = created_at ?? UInt32(Date().timeIntervalSince1970) + guard let keypair = keypair.to_full() else { return nil } diff --git a/damus/Views/EventView.swift b/damus/Views/EventView.swift @@ -83,12 +83,12 @@ extension View { } } -func format_relative_time(_ created_at: Int64) -> String +func format_relative_time(_ created_at: UInt32) -> String { return time_ago_since(Date(timeIntervalSince1970: Double(created_at))) } -func format_date(_ created_at: Int64) -> String { +func format_date(_ created_at: UInt32) -> String { let date = Date(timeIntervalSince1970: TimeInterval(created_at)) let dateFormatter = DateFormatter() dateFormatter.timeStyle = .short diff --git a/damusTests/DMTests.swift b/damusTests/DMTests.swift @@ -45,8 +45,8 @@ final class DMTests: XCTestCase { let pubkey = "3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681" let model = DirectMessagesModel(our_pubkey: pubkey) - let now = Int64(Date().timeIntervalSince1970) - + let now = UInt32(Date().timeIntervalSince1970) + let alice_to_bob = create_dm("hi bob", to_pk: bob.pubkey, tags: [["p", bob.pubkey]], keypair: alice, created_at: now)! handle_incoming_dms(prev_events: notif, dms: model, our_pubkey: alice.pubkey, evs: [alice_to_bob])