1
0
mirror of git://jb55.com/damus synced 2024-09-18 19:23:49 +00:00

ndb: move to uint32 for kind and created_at

This commit is contained in:
William Casarin 2023-07-25 15:20:51 -07:00
parent 54d40f7ffd
commit e3c04465fc
14 changed files with 32 additions and 33 deletions

View File

@ -10,7 +10,7 @@ import AVKit
struct TimestampedProfile { struct TimestampedProfile {
let profile: Profile let profile: Profile
let timestamp: Int64 let timestamp: UInt32
let event: NostrEvent 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 { if let last_event = last_event {
return last_event.created_at - 60 * 10 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") 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 return filters.map { filter in
let kinds = filter.kinds ?? [] let kinds = filter.kinds ?? []
let initial: Int64? = nil let initial: UInt32? = nil
let earliest = kinds.reduce(initial) { earliest, kind in let earliest = kinds.reduce(initial) { earliest, kind in
let last = last_of_kind[kind.rawValue] 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 earliest == nil {
if since == nil { if since == nil {
return nil return nil

View File

@ -66,7 +66,7 @@ class HomeModel {
var has_event: [String: Set<String>] = [:] var has_event: [String: Set<String>] = [:]
var deleted_events: Set<String> = Set() var deleted_events: Set<String> = Set()
var channels: [String: NostrEvent] = [:] 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 done_init: Bool = false
var incoming_dms: [NostrEvent] = [] var incoming_dms: [NostrEvent] = []
let dm_debouncer = Debouncer(interval: 0.5) 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) 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] } ?? [:] 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) 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 { guard let m = last_event_of_kind[relay_id] else {
last_event_of_kind[relay_id] = [:] last_event_of_kind[relay_id] = [:]
return nil return nil

View File

@ -10,7 +10,7 @@ import Foundation
class EventGroup { class EventGroup {
var events: [NostrEvent] var events: [NostrEvent]
var last_event_at: Int64 { var last_event_at: UInt32 {
guard let first = self.events.first else { guard let first = self.events.first else {
return 0 return 0
} }

View File

@ -12,7 +12,7 @@ class ZapGroup {
var msat_total: Int64 = 0 var msat_total: Int64 = 0
var zappers = Set<String>() var zappers = Set<String>()
var last_event_at: Int64 { var last_event_at: UInt32 {
guard let first = zaps.first else { guard let first = zaps.first else {
return 0 return 0
} }

View File

@ -51,7 +51,7 @@ enum NotificationItem {
} }
} }
var last_event_at: Int64 { var last_event_at: UInt32 {
switch self { switch self {
case .reaction(_, let evgrp): case .reaction(_, let evgrp):
return evgrp.last_event_at return evgrp.last_event_at

View File

@ -30,7 +30,7 @@ class SearchHomeModel: ObservableObject {
func get_base_filter() -> NostrFilter { func get_base_filter() -> NostrFilter {
var filter = NostrFilter(kinds: [.text, .chat]) var filter = NostrFilter(kinds: [.text, .chat])
filter.limit = self.limit filter.limit = self.limit
filter.until = Int64(Date.now.timeIntervalSince1970) filter.until = UInt32(Date.now.timeIntervalSince1970)
return filter return filter
} }

View File

@ -63,8 +63,8 @@ class NostrEventOld: Codable, Identifiable, CustomStringConvertible, Equatable,
var flags: Int = 0 var flags: Int = 0
let pubkey: String let pubkey: String
let created_at: Int64 let created_at: UInt32
let kind: Int let kind: UInt32
// cached stuff // cached stuff
private var _event_refs: [EventRef]? = nil private var _event_refs: [EventRef]? = nil
@ -665,8 +665,8 @@ func make_zap_request_event(keypair: FullKeypair, content: String, relays: [Rela
var kp = keypair var kp = keypair
let now = Int64(Date().timeIntervalSince1970) let now = UInt32(Date().timeIntervalSince1970)
var privzap_req: PrivateZapRequest? var privzap_req: PrivateZapRequest?
var message = content var message = content

View File

@ -12,8 +12,8 @@ struct NostrFilter: Codable, Equatable {
var kinds: [NostrKind]? var kinds: [NostrKind]?
var referenced_ids: [String]? var referenced_ids: [String]?
var pubkeys: [String]? var pubkeys: [String]?
var since: Int64? var since: UInt32?
var until: Int64? var until: UInt32?
var limit: UInt32? var limit: UInt32?
var authors: [String]? var authors: [String]?
var hashtag: [String]? var hashtag: [String]?
@ -32,7 +32,7 @@ struct NostrFilter: Codable, Equatable {
case limit 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.ids = ids
self.kinds = kinds self.kinds = kinds
self.referenced_ids = referenced_ids self.referenced_ids = referenced_ids

View File

@ -8,7 +8,7 @@
import Foundation import Foundation
enum NostrKind: Int, Codable { enum NostrKind: UInt32, Codable {
case metadata = 0 case metadata = 0
case text = 1 case text = 1
case contacts = 3 case contacts = 3

View File

@ -167,7 +167,7 @@ struct PayInvoiceRequest: Codable {
func make_wallet_connect_request<T>(req: WalletRequest<T>, to_pk: String, keypair: FullKeypair) -> NostrEvent? { func make_wallet_connect_request<T>(req: WalletRequest<T>, to_pk: String, keypair: FullKeypair) -> NostrEvent? {
let tags = [["p", to_pk]] let tags = [["p", to_pk]]
let created_at = Int64(Date().timeIntervalSince1970) let created_at = UInt32(Date().timeIntervalSince1970)
guard let content = encode_json(req) else { guard let content = encode_json(req) else {
return nil return nil
} }

View File

@ -216,7 +216,7 @@ enum Zapping {
} }
} }
var created_at: Int64 { var created_at: UInt32 {
switch self { switch self {
case .zap(let zap): case .zap(let zap):
return zap.event.created_at return zap.event.created_at

View File

@ -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 let privkey = keypair.privkey
guard let enc_content = encrypt_message(message: message, privkey: privkey, to_pk: to_pk) else { 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 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 { guard let keypair = keypair.to_full() else {
return nil return nil
} }

View File

@ -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))) 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 date = Date(timeIntervalSince1970: TimeInterval(created_at))
let dateFormatter = DateFormatter() let dateFormatter = DateFormatter()
dateFormatter.timeStyle = .short dateFormatter.timeStyle = .short

View File

@ -45,8 +45,8 @@ final class DMTests: XCTestCase {
let pubkey = "3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681" let pubkey = "3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681"
let model = DirectMessagesModel(our_pubkey: pubkey) 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)! 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]) handle_incoming_dms(prev_events: notif, dms: model, our_pubkey: alice.pubkey, evs: [alice_to_bob])