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

ndb: implement a few more event things

We're basically done. Time to try the switch-over
This commit is contained in:
William Casarin 2023-07-24 12:40:27 -07:00
parent b3c87bdc07
commit 04e408bfea

View File

@ -6,6 +6,7 @@
// //
import Foundation import Foundation
import NaturalLanguage
struct NdbStr { struct NdbStr {
@ -233,20 +234,17 @@ extension NdbNote {
return dec return dec
} }
/* /*
var description: String { var description: String {
return "NostrEvent { id: \(id) pubkey \(pubkey) kind \(kind) tags \(tags) content '\(content)' }" return "NostrEvent { id: \(id) pubkey \(pubkey) kind \(kind) tags \(tags) content '\(content)' }"
} }
private enum CodingKeys: String, CodingKey { // Not sure I should implement this
case id, sig, tags, pubkey, created_at, kind, content
}
private func get_referenced_ids(key: String) -> [ReferencedId] { private func get_referenced_ids(key: String) -> [ReferencedId] {
return damus.get_referenced_ids(tags: self.tags, key: key) return damus.get_referenced_ids(tags: self.tags, key: key)
} }
*/
public func direct_replies(_ privkey: String?) -> [ReferencedId] { public func direct_replies(_ privkey: String?) -> [ReferencedId] {
return event_refs(privkey).reduce(into: []) { acc, evref in return event_refs(privkey).reduce(into: []) { acc, evref in
@ -256,6 +254,7 @@ extension NdbNote {
} }
} }
// NDBTODO: just use Id
public func thread_id(privkey: String?) -> String { public func thread_id(privkey: String?) -> String {
for ref in event_refs(privkey) { for ref in event_refs(privkey) {
if let thread_id = ref.is_thread_id { if let thread_id = ref.is_thread_id {
@ -263,32 +262,18 @@ extension NdbNote {
} }
} }
return self.id return hex_encode(self.id)
} }
public func last_refid() -> ReferencedId? { public func last_refid() -> ReferencedId? {
var mlast: Int? = nil return self.referenced_ids.last?.to_referenced_id()
var i: Int = 0
for tag in tags {
if tag.count >= 2 && tag[0] == "e" {
mlast = i
}
i += 1
}
guard let last = mlast else {
return nil
}
return tag_to_refid(tags[last])
} }
public func references(id: String, key: String) -> Bool { // NDBTODO: id -> data
for tag in tags { public func references(id: String, key: AsciiCharacter) -> Bool {
if tag.count >= 2 && tag[0] == key { for ref in References(tags: self.tags()) {
if tag[1] == id { if ref.key == key && ref.id.string() == id {
return true return true
}
} }
} }
@ -299,36 +284,31 @@ extension NdbNote {
return event_is_reply(self.event_refs(privkey)) return event_is_reply(self.event_refs(privkey))
} }
func note_language(_ privkey: String?) -> String? { func note_language(_ privkey: String?) async -> String? {
// Rely on Apple's NLLanguageRecognizer to tell us which language it thinks the note is in let t = Task.detached {
// and filter on only the text portions of the content as URLs and hashtags confuse the language recognizer. // Rely on Apple's NLLanguageRecognizer to tell us which language it thinks the note is in
let originalBlocks = blocks(privkey).blocks // and filter on only the text portions of the content as URLs and hashtags confuse the language recognizer.
let originalOnlyText = originalBlocks.compactMap { $0.is_text }.joined(separator: " ") let originalBlocks = self.blocks(privkey).blocks
let originalOnlyText = originalBlocks.compactMap { $0.is_text }.joined(separator: " ")
// Only accept language recognition hypothesis if there's at least a 50% probability that it's accurate. // Only accept language recognition hypothesis if there's at least a 50% probability that it's accurate.
let languageRecognizer = NLLanguageRecognizer() let languageRecognizer = NLLanguageRecognizer()
languageRecognizer.processString(originalOnlyText) languageRecognizer.processString(originalOnlyText)
guard let locale = languageRecognizer.languageHypotheses(withMaximum: 1).first(where: { $0.value >= 0.5 })?.key.rawValue else { guard let locale = languageRecognizer.languageHypotheses(withMaximum: 1).first(where: { $0.value >= 0.5 })?.key.rawValue else {
return nil let nstr: String? = nil
return nstr
}
// Remove the variant component and just take the language part as translation services typically only supports the variant-less language.
// Moreover, speakers of one variant can generally understand other variants.
return localeToLanguage(locale)
} }
// Remove the variant component and just take the language part as translation services typically only supports the variant-less language. return await t.value
// Moreover, speakers of one variant can generally understand other variants.
return localeToLanguage(locale)
} }
public var referenced_ids: [ReferencedId] { /*
return get_referenced_ids(key: "e")
}
public var referenced_pubkeys: [ReferencedId] {
return get_referenced_ids(key: "p")
}
public var is_local: Bool {
return (self.flags & 1) != 0
}
func calculate_id() { func calculate_id() {
self.id = calculate_event_id(ev: self) self.id = calculate_event_id(ev: self)
@ -349,4 +329,12 @@ extension LazyFilterSequence {
var first: Element? { var first: Element? {
self.first(where: { _ in true }) self.first(where: { _ in true })
} }
var last: Element? {
var ev: Element? = nil
for e in self {
ev = e
}
return ev
}
} }