ndb: implement eventref building from ndb notes
This commit is contained in:
@@ -39,6 +39,14 @@ class NdbNote {
|
||||
let count: Int
|
||||
let note: UnsafeMutablePointer<ndb_note>
|
||||
|
||||
// cached stuff (TODO: remove these)
|
||||
private var _event_refs: [EventRef]? = nil
|
||||
var decrypted_content: String? = nil
|
||||
private var _blocks: Blocks? = nil
|
||||
private lazy var inner_event: NdbNote? = {
|
||||
return NdbNote.owned_from_json_cstr(json: content_raw, json_len: content_len)
|
||||
}()
|
||||
|
||||
init(note: UnsafeMutablePointer<ndb_note>, owned_size: Int?) {
|
||||
self.note = note
|
||||
self.owned = owned_size != nil
|
||||
@@ -86,12 +94,20 @@ class NdbNote {
|
||||
}
|
||||
|
||||
static func owned_from_json(json: String, bufsize: Int = 2 << 18) -> NdbNote? {
|
||||
return json.withCString { cstr in
|
||||
return NdbNote.owned_from_json_cstr(
|
||||
json: cstr, json_len: UInt32(json.utf8.count), bufsize: bufsize)
|
||||
}
|
||||
}
|
||||
|
||||
static func owned_from_json_cstr(json: UnsafePointer<CChar>, json_len: UInt32, bufsize: Int = 2 << 18) -> NdbNote? {
|
||||
let data = malloc(bufsize)
|
||||
guard var json_cstr = json.cString(using: .utf8) else { return nil }
|
||||
|
||||
//guard var json_cstr = json.cString(using: .utf8) else { return nil }
|
||||
|
||||
//json_cs
|
||||
var note: UnsafeMutablePointer<ndb_note>?
|
||||
|
||||
let len = ndb_note_from_json(&json_cstr, Int32(json_cstr.count), ¬e, data, Int32(bufsize))
|
||||
let len = ndb_note_from_json(json, Int32(json_len), ¬e, data, Int32(bufsize))
|
||||
|
||||
if len == 0 {
|
||||
free(data)
|
||||
@@ -100,8 +116,8 @@ class NdbNote {
|
||||
|
||||
// Create new Data with just the valid bytes
|
||||
guard let note_data = realloc(data, Int(len)) else { return nil }
|
||||
|
||||
let new_note = note_data.assumingMemoryBound(to: ndb_note.self)
|
||||
|
||||
return NdbNote(note: new_note, owned_size: Int(len))
|
||||
}
|
||||
}
|
||||
@@ -152,26 +168,39 @@ extension NdbNote {
|
||||
|
||||
// TODO: References iterator
|
||||
public var referenced_ids: LazyFilterSequence<References> {
|
||||
References(note: self).lazy
|
||||
.filter() { ref in ref.key == "e" }
|
||||
References(tags: self.tags()).ids()
|
||||
}
|
||||
|
||||
public var referenced_pubkeys: LazyFilterSequence<References> {
|
||||
References(note: self).lazy
|
||||
.filter() { ref in ref.key == "p" }
|
||||
References(tags: self.tags()).pubkeys()
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
func event_refs(_ privkey: String?) -> [EventRef] {
|
||||
if let rs = _event_refs {
|
||||
return rs
|
||||
}
|
||||
let refs = interpret_event_refs(blocks: self.blocks(privkey).blocks, tags: self.tags)
|
||||
let refs = interpret_event_refs_ndb(blocks: self.blocks(privkey).blocks, tags: self.tags())
|
||||
self._event_refs = refs
|
||||
return refs
|
||||
}
|
||||
|
||||
func get_content(_ privkey: String?) -> String {
|
||||
if known_kind == .dm {
|
||||
return decrypted(privkey: privkey) ?? "*failed to decrypt content*"
|
||||
}
|
||||
|
||||
return content
|
||||
}
|
||||
|
||||
func blocks(_ privkey: String?) -> Blocks {
|
||||
if let bs = _blocks { return bs }
|
||||
|
||||
let blocks = get_blocks(content: self.get_content(privkey))
|
||||
self._blocks = blocks
|
||||
return blocks
|
||||
}
|
||||
|
||||
// NDBTODO: switch this to operating on bytes not strings
|
||||
func decrypted(privkey: String?) -> String? {
|
||||
if let decrypted_content = decrypted_content {
|
||||
return decrypted_content
|
||||
@@ -185,38 +214,32 @@ extension NdbNote {
|
||||
return nil
|
||||
}
|
||||
|
||||
var pubkey = self.pubkey
|
||||
// NDBTODO: don't hex encode
|
||||
var pubkey = hex_encode(self.pubkey)
|
||||
// This is our DM, we need to use the pubkey of the person we're talking to instead
|
||||
|
||||
if our_pubkey == pubkey {
|
||||
guard let refkey = self.referenced_pubkeys.first else {
|
||||
return nil
|
||||
}
|
||||
|
||||
pubkey = refkey.ref_id
|
||||
pubkey = refkey.ref_id.string()
|
||||
}
|
||||
|
||||
// NDBTODO: pass data to pubkey
|
||||
let dec = decrypt_dm(key, pubkey: pubkey, content: self.content, encoding: .base64)
|
||||
self.decrypted_content = dec
|
||||
|
||||
return dec
|
||||
}
|
||||
|
||||
func get_content(_ privkey: String?) -> String {
|
||||
if known_kind == .dm {
|
||||
return decrypted(privkey: privkey) ?? "*failed to decrypt content*"
|
||||
}
|
||||
|
||||
return content
|
||||
}
|
||||
/*
|
||||
|
||||
var description: String {
|
||||
return "NostrEvent { id: \(id) pubkey \(pubkey) kind \(kind) tags \(tags) content '\(content)' }"
|
||||
}
|
||||
|
||||
var known_kind: NostrKind? {
|
||||
return NostrKind.init(rawValue: kind)
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case id, sig, tags, pubkey, created_at, kind, content
|
||||
}
|
||||
|
||||
@@ -56,6 +56,10 @@ struct TagsSequence: Sequence {
|
||||
return nil
|
||||
}
|
||||
|
||||
func references() -> References {
|
||||
return References(tags: self)
|
||||
}
|
||||
|
||||
func makeIterator() -> TagsIterator {
|
||||
return .init(note: note)
|
||||
}
|
||||
|
||||
@@ -74,6 +74,72 @@ final class NdbTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func test_perf_old_decoding() {
|
||||
self.measure {
|
||||
let event = decode_nostr_event_json(test_contact_list_json)
|
||||
XCTAssertNotNil(event)
|
||||
}
|
||||
}
|
||||
|
||||
func test_perf_old_iter() {
|
||||
self.measure {
|
||||
let event = decode_nostr_event_json(test_contact_list_json)
|
||||
XCTAssertNotNil(event)
|
||||
}
|
||||
}
|
||||
|
||||
func longer_iter(_ n: Int = 1000) -> XCTMeasureOptions {
|
||||
let opts = XCTMeasureOptions()
|
||||
opts.iterationCount = n
|
||||
return opts
|
||||
}
|
||||
|
||||
func test_perf_interp_evrefs_old() {
|
||||
guard let event = decode_nostr_event_json(test_reply_json) else {
|
||||
return
|
||||
}
|
||||
self.measure(options: longer_iter()) {
|
||||
let blocks = event.blocks(nil).blocks
|
||||
let xs = interpret_event_refs(blocks: blocks, tags: event.tags)
|
||||
XCTAssertEqual(xs.count, 1)
|
||||
}
|
||||
}
|
||||
|
||||
func test_perf_interp_evrefs_ndb() {
|
||||
guard let note = NdbNote.owned_from_json(json: test_reply_json) else {
|
||||
return
|
||||
}
|
||||
self.measure(options: longer_iter()) {
|
||||
let blocks = note.blocks(nil).blocks
|
||||
let xs = interpret_event_refs_ndb(blocks: blocks, tags: note.tags())
|
||||
XCTAssertEqual(xs.count, 1)
|
||||
}
|
||||
}
|
||||
|
||||
func test_decoded_events_are_equal() {
|
||||
let event = decode_nostr_event_json(test_reply_json)
|
||||
let note = NdbNote.owned_from_json(json: test_reply_json)
|
||||
|
||||
XCTAssertNotNil(note)
|
||||
XCTAssertNotNil(event)
|
||||
guard let note else { return }
|
||||
guard let event else { return }
|
||||
|
||||
XCTAssertEqual(note.content_len, UInt32(event.content.utf8.count))
|
||||
XCTAssertEqual(hex_encode(note.pubkey), event.pubkey)
|
||||
XCTAssertEqual(hex_encode(note.id), event.id)
|
||||
|
||||
let ev_blocks = event.blocks(nil)
|
||||
let note_blocks = note.blocks(nil)
|
||||
|
||||
XCTAssertEqual(ev_blocks, note_blocks)
|
||||
|
||||
let event_refs = interpret_event_refs(blocks: ev_blocks.blocks, tags: event.tags)
|
||||
let note_refs = interpret_event_refs_ndb(blocks: note_blocks.blocks, tags: note.tags())
|
||||
|
||||
XCTAssertEqual(event_refs, note_refs)
|
||||
}
|
||||
|
||||
func test_iteration_perf() throws {
|
||||
guard let note = NdbNote.owned_from_json(json: test_contact_list_json) else {
|
||||
XCTAssert(false)
|
||||
|
||||
Reference in New Issue
Block a user