Switch over to use use blocks from nostrdb
This is still kind of broken until queries are switched over to nostrdb. Will do this next Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
committed by
Daniel D’Aquino
parent
208b3331ca
commit
28a06af534
@@ -18,22 +18,38 @@ enum MentionType: AsciiCharacter, TagKey {
|
||||
}
|
||||
}
|
||||
|
||||
enum MentionRef: TagKeys, TagConvertible, Equatable, Hashable {
|
||||
case pubkey(Pubkey)
|
||||
case note(NoteId)
|
||||
case nevent(NEvent)
|
||||
case nprofile(NProfile)
|
||||
case nrelay(String)
|
||||
case naddr(NAddr)
|
||||
extension UnsafePointer<UInt8> {
|
||||
func as_data(size: Int) -> Data {
|
||||
return Data(bytes: self, count: size)
|
||||
}
|
||||
}
|
||||
|
||||
struct MentionRef: TagKeys, TagConvertible, Equatable, Hashable {
|
||||
let nip19: Bech32Object
|
||||
|
||||
static func note(_ note_id: NoteId) -> MentionRef {
|
||||
return self.init(nip19: .note(note_id))
|
||||
}
|
||||
|
||||
init?(block: ndb_mention_bech32_block) {
|
||||
guard let bech32_obj = Bech32Object.init(block: block) else {
|
||||
return nil
|
||||
}
|
||||
self.nip19 = bech32_obj
|
||||
}
|
||||
|
||||
init(nip19: Bech32Object) {
|
||||
self.nip19 = nip19
|
||||
}
|
||||
|
||||
var key: MentionType {
|
||||
switch self {
|
||||
case .pubkey: return .p
|
||||
case .note: return .e
|
||||
case .nevent: return .e
|
||||
case .nprofile: return .p
|
||||
switch self.nip19 {
|
||||
case .note, .nevent: return .e
|
||||
case .nprofile, .npub: return .p
|
||||
case .nrelay: return .r
|
||||
case .naddr: return .a
|
||||
case .nscript: return .a
|
||||
case .nsec: return .p
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,38 +57,40 @@ enum MentionRef: TagKeys, TagConvertible, Equatable, Hashable {
|
||||
return Bech32Object.encode(toBech32Object())
|
||||
}
|
||||
|
||||
static func from_bech32(str: String) -> MentionRef? {
|
||||
switch Bech32Object.parse(str) {
|
||||
case .note(let noteid): return .note(noteid)
|
||||
case .npub(let pubkey): return .pubkey(pubkey)
|
||||
default: return nil
|
||||
init?(bech32_str: String) {
|
||||
guard let obj = Bech32Object.parse(bech32_str) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
self.nip19 = obj
|
||||
}
|
||||
|
||||
var pubkey: Pubkey? {
|
||||
switch self {
|
||||
case .pubkey(let pubkey): return pubkey
|
||||
switch self.nip19 {
|
||||
case .npub(let pubkey): return pubkey
|
||||
case .note: return nil
|
||||
case .nevent(let nevent): return nevent.author
|
||||
case .nprofile(let nprofile): return nprofile.author
|
||||
case .nrelay: return nil
|
||||
case .naddr: return nil
|
||||
case .nsec(let prv): return privkey_to_pubkey(privkey: prv)
|
||||
case .nscript(_): return nil
|
||||
}
|
||||
}
|
||||
|
||||
var tag: [String] {
|
||||
switch self {
|
||||
case .pubkey(let pubkey): return ["p", pubkey.hex()]
|
||||
switch self.nip19 {
|
||||
case .npub(let pubkey): return ["p", pubkey.hex()]
|
||||
case .note(let noteId): return ["e", noteId.hex()]
|
||||
case .nevent(let nevent):
|
||||
var tagBuilder = ["e", nevent.noteid.hex()]
|
||||
|
||||
let relay = nevent.relays.first
|
||||
if let author = nevent.author?.hex() {
|
||||
tagBuilder.append(relay ?? "")
|
||||
tagBuilder.append(relay?.absoluteString ?? "")
|
||||
tagBuilder.append(author)
|
||||
} else if let relay {
|
||||
tagBuilder.append(relay)
|
||||
tagBuilder.append(relay.absoluteString)
|
||||
}
|
||||
|
||||
return tagBuilder
|
||||
@@ -80,7 +98,7 @@ enum MentionRef: TagKeys, TagConvertible, Equatable, Hashable {
|
||||
var tagBuilder = ["p", nprofile.author.hex()]
|
||||
|
||||
if let relay = nprofile.relays.first {
|
||||
tagBuilder.append(relay)
|
||||
tagBuilder.append(relay.absoluteString)
|
||||
}
|
||||
|
||||
return tagBuilder
|
||||
@@ -89,10 +107,14 @@ enum MentionRef: TagKeys, TagConvertible, Equatable, Hashable {
|
||||
var tagBuilder = ["a", "\(naddr.kind.description):\(naddr.author.hex()):\(naddr.identifier.string())"]
|
||||
|
||||
if let relay = naddr.relays.first {
|
||||
tagBuilder.append(relay)
|
||||
tagBuilder.append(relay.absoluteString)
|
||||
}
|
||||
|
||||
return tagBuilder
|
||||
case .nsec(_):
|
||||
return []
|
||||
case .nscript(_):
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,10 +134,10 @@ enum MentionRef: TagKeys, TagConvertible, Equatable, Hashable {
|
||||
switch mention_type {
|
||||
case .p:
|
||||
guard let data = element.id() else { return nil }
|
||||
return .pubkey(Pubkey(data))
|
||||
return .init(nip19: .npub(Pubkey(data)))
|
||||
case .e:
|
||||
guard let data = element.id() else { return nil }
|
||||
return .note(NoteId(data))
|
||||
return .init(nip19: .note(NoteId(data)))
|
||||
case .a:
|
||||
let str = element.string()
|
||||
let data = str.split(separator: ":")
|
||||
@@ -124,26 +146,13 @@ enum MentionRef: TagKeys, TagConvertible, Equatable, Hashable {
|
||||
guard let pubkey = Pubkey(hex: String(data[1])) else { return nil }
|
||||
guard let kind = UInt32(data[0]) else { return nil }
|
||||
|
||||
return .naddr(NAddr(identifier: String(data[2]), author: pubkey, relays: [], kind: kind))
|
||||
case .r: return .nrelay(element.string())
|
||||
return .init(nip19: .naddr(NAddr(identifier: String(data[2]), author: pubkey, relays: [], kind: kind)))
|
||||
case .r: return .init(nip19: .nrelay(element.string()))
|
||||
}
|
||||
}
|
||||
|
||||
func toBech32Object() -> Bech32Object {
|
||||
switch self {
|
||||
case .pubkey(let pk):
|
||||
return .npub(pk)
|
||||
case .note(let noteid):
|
||||
return .note(noteid)
|
||||
case .naddr(let naddr):
|
||||
return .naddr(naddr)
|
||||
case .nevent(let nevent):
|
||||
return .nevent(nevent)
|
||||
case .nprofile(let nprofile):
|
||||
return .nprofile(nprofile)
|
||||
case .nrelay(let url):
|
||||
return .nrelay(url)
|
||||
}
|
||||
self.nip19
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,7 +194,6 @@ struct LightningInvoice<T> {
|
||||
let amount: T
|
||||
let string: String
|
||||
let expiry: UInt64
|
||||
let payment_hash: Data
|
||||
let created_at: UInt64
|
||||
|
||||
var abbreviated: String {
|
||||
@@ -213,8 +221,8 @@ struct LightningInvoice<T> {
|
||||
}
|
||||
}
|
||||
|
||||
func maybe_pointee<T>(_ p: UnsafeMutablePointer<T>!) -> T? {
|
||||
guard p != nil else {
|
||||
func maybe_pointee<T>(_ p: UnsafeMutablePointer<T>?) -> T? {
|
||||
guard let p else {
|
||||
return nil
|
||||
}
|
||||
return p.pointee
|
||||
@@ -282,7 +290,7 @@ func format_msats(_ msat: Int64, locale: Locale = Locale.current) -> String {
|
||||
return String(format: format, locale: locale, sats.decimalValue as NSDecimalNumber, formattedSats)
|
||||
}
|
||||
|
||||
func convert_invoice_description(b11: bolt11) -> InvoiceDescription? {
|
||||
func convert_invoice_description(b11: ndb_invoice) -> InvoiceDescription? {
|
||||
if let desc = b11.description {
|
||||
return .description(String(cString: desc))
|
||||
}
|
||||
@@ -307,3 +315,49 @@ func find_tag_ref(type: String, id: String, tags: [[String]]) -> Int? {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
struct PostTags {
|
||||
let blocks: [Block]
|
||||
let tags: [[String]]
|
||||
}
|
||||
|
||||
/// Convert
|
||||
func make_post_tags(post_blocks: [Block], tags: [[String]]) -> PostTags {
|
||||
var new_tags = tags
|
||||
|
||||
for post_block in post_blocks {
|
||||
switch post_block {
|
||||
case .mention(let mention):
|
||||
switch(mention.ref.nip19) {
|
||||
case .note, .nevent:
|
||||
continue
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
new_tags.append(mention.ref.tag)
|
||||
case .hashtag(let hashtag):
|
||||
new_tags.append(["t", hashtag.lowercased()])
|
||||
case .text: break
|
||||
case .invoice: break
|
||||
case .relay: break
|
||||
case .url(let url):
|
||||
new_tags.append(["r", url.absoluteString])
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return PostTags(blocks: post_blocks, tags: new_tags)
|
||||
}
|
||||
|
||||
func post_to_event(post: NostrPost, keypair: FullKeypair) -> NostrEvent? {
|
||||
let tags = post.references.map({ r in r.tag }) + post.tags
|
||||
guard let post_blocks = parse_post_blocks(content: post.content)?.blocks else {
|
||||
return nil
|
||||
}
|
||||
let post_tags = make_post_tags(post_blocks: post_blocks, tags: tags)
|
||||
let content = post_tags.blocks
|
||||
.map({ b in b.asString })
|
||||
.joined(separator: "")
|
||||
return NostrEvent(content: content, keypair: keypair.to_keypair(), kind: post.kind.rawValue, tags: post_tags.tags)
|
||||
}
|
||||
|
||||
@@ -778,13 +778,18 @@ func validate_event(ev: NostrEvent) -> ValidationResult {
|
||||
return ok ? .ok : .bad_sig
|
||||
}
|
||||
|
||||
func first_eref_mention(ev: NostrEvent, keypair: Keypair) -> Mention<NoteId>? {
|
||||
let blocks = ev.blocks(keypair).blocks.filter { block in
|
||||
func first_eref_mention(ndb: Ndb, ev: NostrEvent, keypair: Keypair) -> Mention<NoteId>? {
|
||||
guard let blocks_txn = ev.blocks(ndb: ndb) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let ndb_blocks = blocks_txn.unsafeUnownedValue
|
||||
let blocks = ndb_blocks.iter(note: ev).filter { block in
|
||||
guard case .mention(let mention) = block else {
|
||||
return false
|
||||
}
|
||||
|
||||
switch mention.ref {
|
||||
switch mention.bech32_type {
|
||||
case .note, .nevent:
|
||||
return true
|
||||
default:
|
||||
@@ -795,11 +800,13 @@ func first_eref_mention(ev: NostrEvent, keypair: Keypair) -> Mention<NoteId>? {
|
||||
/// MARK: - Preview
|
||||
if let firstBlock = blocks.first,
|
||||
case .mention(let mention) = firstBlock {
|
||||
switch mention.ref {
|
||||
case .note(let note_id):
|
||||
return .note(note_id)
|
||||
case .nevent(let nevent):
|
||||
return .note(nevent.noteid)
|
||||
switch mention.bech32_type {
|
||||
case .note:
|
||||
let data = mention.bech32.note.event_id.as_data(size: 32)
|
||||
return .note(NoteId(data))
|
||||
case .nevent:
|
||||
let data = mention.bech32.nevent.event_id.as_data(size: 32)
|
||||
return .note(NoteId(data))
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
@@ -807,9 +814,15 @@ func first_eref_mention(ev: NostrEvent, keypair: Keypair) -> Mention<NoteId>? {
|
||||
return nil
|
||||
}
|
||||
|
||||
func separate_invoices(ev: NostrEvent, keypair: Keypair) -> [Invoice]? {
|
||||
let invoiceBlocks: [Invoice] = ev.blocks(keypair).blocks.reduce(into: []) { invoices, block in
|
||||
guard case .invoice(let invoice) = block else {
|
||||
func separate_invoices(ndb: Ndb, ev: NostrEvent) -> [Invoice]? {
|
||||
guard let blocks_txn = ev.blocks(ndb: ndb) else {
|
||||
return nil
|
||||
}
|
||||
let ndb_blocks = blocks_txn.unsafeUnownedValue
|
||||
let invoiceBlocks: [Invoice] = ndb_blocks.iter(note: ev).reduce(into: []) { invoices, block in
|
||||
guard case .invoice(let invoice) = block,
|
||||
let invoice = invoice.as_invoice()
|
||||
else {
|
||||
return
|
||||
}
|
||||
invoices.append(invoice)
|
||||
|
||||
@@ -89,7 +89,7 @@ enum NostrResponse {
|
||||
free(data)
|
||||
return nil
|
||||
}
|
||||
let new_note = note_data.assumingMemoryBound(to: ndb_note.self)
|
||||
let new_note = ndb_note_ptr(ptr: OpaquePointer(note_data))
|
||||
let note = NdbNote(note: new_note, size: Int(len), owned: true, key: nil)
|
||||
|
||||
guard let subid = sized_cstr(cstr: tce.subid, len: tce.subid_len) else {
|
||||
|
||||
Reference in New Issue
Block a user