2023-07-21 21:54:03 +00:00
|
|
|
//
|
|
|
|
// NdbNote.swift
|
|
|
|
// damus
|
|
|
|
//
|
|
|
|
// Created by William Casarin on 2023-07-21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2023-07-24 19:40:27 +00:00
|
|
|
import NaturalLanguage
|
2023-07-21 21:54:03 +00:00
|
|
|
|
2023-07-23 18:55:36 +00:00
|
|
|
|
|
|
|
struct NdbStr {
|
|
|
|
let note: NdbNote
|
|
|
|
let str: UnsafePointer<CChar>
|
|
|
|
}
|
|
|
|
|
|
|
|
struct NdbId {
|
|
|
|
let note: NdbNote
|
|
|
|
let id: Data
|
|
|
|
}
|
|
|
|
|
|
|
|
enum NdbData {
|
|
|
|
case id(NdbId)
|
|
|
|
case str(NdbStr)
|
|
|
|
|
|
|
|
init(note: NdbNote, str: ndb_str) {
|
|
|
|
guard str.flag == NDB_PACKED_ID else {
|
|
|
|
self = .str(NdbStr(note: note, str: str.str))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let buffer = UnsafeBufferPointer(start: str.id, count: 32)
|
|
|
|
self = .id(NdbId(note: note, id: Data(buffer: buffer)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
class NdbNote: Encodable, Equatable, Hashable {
|
2023-07-21 23:01:28 +00:00
|
|
|
// we can have owned notes, but we can also have lmdb virtual-memory mapped notes so its optional
|
2023-07-23 18:55:36 +00:00
|
|
|
private let owned: Bool
|
|
|
|
let count: Int
|
2023-07-21 21:54:03 +00:00
|
|
|
let note: UnsafeMutablePointer<ndb_note>
|
|
|
|
|
2023-07-24 17:55:34 +00:00
|
|
|
// 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)
|
|
|
|
}()
|
|
|
|
|
2023-07-23 18:55:36 +00:00
|
|
|
init(note: UnsafeMutablePointer<ndb_note>, owned_size: Int?) {
|
2023-07-21 23:01:28 +00:00
|
|
|
self.note = note
|
2023-07-23 18:55:36 +00:00
|
|
|
self.owned = owned_size != nil
|
|
|
|
self.count = owned_size ?? 0
|
2023-07-26 15:46:44 +00:00
|
|
|
|
2023-08-06 16:07:09 +00:00
|
|
|
#if DEBUG_NOTE_SIZE
|
2023-07-26 15:46:44 +00:00
|
|
|
if let owned_size {
|
|
|
|
NdbNote.total_ndb_size += Int(owned_size)
|
|
|
|
NdbNote.notes_created += 1
|
|
|
|
|
|
|
|
print("\(NdbNote.notes_created) ndb_notes, \(NdbNote.total_ndb_size) bytes")
|
|
|
|
}
|
2023-08-06 16:07:09 +00:00
|
|
|
#endif
|
2023-07-26 15:46:44 +00:00
|
|
|
|
2023-07-22 14:40:20 +00:00
|
|
|
}
|
2023-07-23 18:55:36 +00:00
|
|
|
|
2023-07-22 14:40:20 +00:00
|
|
|
var content: String {
|
2023-07-23 00:15:36 +00:00
|
|
|
String(cString: content_raw, encoding: .utf8) ?? ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var content_raw: UnsafePointer<CChar> {
|
|
|
|
ndb_note_content(note)
|
|
|
|
}
|
|
|
|
|
|
|
|
var content_len: UInt32 {
|
|
|
|
ndb_note_content_length(note)
|
2023-07-22 14:40:20 +00:00
|
|
|
}
|
|
|
|
|
2023-07-25 15:58:06 +00:00
|
|
|
/// NDBTODO: make this into data
|
2023-07-26 15:46:44 +00:00
|
|
|
var id: NoteId {
|
|
|
|
.init(Data(bytes: ndb_note_id(note), count: 32))
|
|
|
|
}
|
|
|
|
|
|
|
|
var sig: Signature {
|
|
|
|
.init(Data(bytes: ndb_note_sig(note), count: 64))
|
2023-07-21 21:54:03 +00:00
|
|
|
}
|
2023-07-23 00:15:36 +00:00
|
|
|
|
2023-07-25 15:58:06 +00:00
|
|
|
/// NDBTODO: make this into data
|
2023-07-26 15:46:44 +00:00
|
|
|
var pubkey: Pubkey {
|
|
|
|
.init(Data(bytes: ndb_note_pubkey(note), count: 32))
|
2023-07-21 23:01:28 +00:00
|
|
|
}
|
2023-07-23 00:15:36 +00:00
|
|
|
|
|
|
|
var created_at: UInt32 {
|
|
|
|
ndb_note_created_at(note)
|
|
|
|
}
|
|
|
|
|
|
|
|
var kind: UInt32 {
|
|
|
|
ndb_note_kind(note)
|
|
|
|
}
|
2023-07-23 18:55:36 +00:00
|
|
|
|
2023-07-24 20:08:18 +00:00
|
|
|
var tags: TagsSequence {
|
|
|
|
.init(note: self)
|
2023-07-21 21:54:03 +00:00
|
|
|
}
|
2023-07-23 18:55:36 +00:00
|
|
|
|
|
|
|
deinit {
|
|
|
|
if self.owned {
|
2023-08-06 16:07:09 +00:00
|
|
|
#if DEBUG_NOTE_SIZE
|
2023-07-26 15:46:44 +00:00
|
|
|
NdbNote.total_ndb_size -= Int(count)
|
|
|
|
NdbNote.notes_created -= 1
|
|
|
|
|
|
|
|
print("\(NdbNote.notes_created) ndb_notes, \(NdbNote.total_ndb_size) bytes")
|
2023-08-06 16:07:09 +00:00
|
|
|
#endif
|
2023-07-23 18:55:36 +00:00
|
|
|
free(note)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-24 20:09:27 +00:00
|
|
|
static func == (lhs: NdbNote, rhs: NdbNote) -> Bool {
|
|
|
|
return lhs.id == rhs.id
|
|
|
|
}
|
|
|
|
|
2023-07-25 15:58:06 +00:00
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
hasher.combine(id)
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
private enum CodingKeys: String, CodingKey {
|
|
|
|
case id, sig, tags, pubkey, created_at, kind, content
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement the `Encodable` protocol
|
|
|
|
func encode(to encoder: Encoder) throws {
|
|
|
|
var container = encoder.container(keyedBy: CodingKeys.self)
|
|
|
|
|
|
|
|
try container.encode(hex_encode(id.id), forKey: .id)
|
|
|
|
try container.encode(hex_encode(sig.data), forKey: .sig)
|
|
|
|
try container.encode(pubkey, forKey: .pubkey)
|
|
|
|
try container.encode(created_at, forKey: .created_at)
|
|
|
|
try container.encode(kind, forKey: .kind)
|
|
|
|
try container.encode(content, forKey: .content)
|
|
|
|
try container.encode(tags, forKey: .tags)
|
|
|
|
}
|
|
|
|
|
2023-08-06 16:07:09 +00:00
|
|
|
#if DEBUG_NOTE_SIZE
|
2023-07-26 15:46:44 +00:00
|
|
|
static var total_ndb_size: Int = 0
|
|
|
|
static var notes_created: Int = 0
|
2023-08-06 16:07:09 +00:00
|
|
|
#endif
|
2023-07-25 15:58:06 +00:00
|
|
|
|
2023-07-25 23:22:25 +00:00
|
|
|
init?(content: String, keypair: Keypair, kind: UInt32 = 1, tags: [[String]] = [], createdAt: UInt32 = UInt32(Date().timeIntervalSince1970)) {
|
2023-07-25 15:58:06 +00:00
|
|
|
|
|
|
|
var builder = ndb_builder()
|
|
|
|
let buflen = MAX_NOTE_SIZE
|
|
|
|
let buf = malloc(buflen)
|
|
|
|
|
|
|
|
ndb_builder_init(&builder, buf, Int32(buflen))
|
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
var pk_raw = keypair.pubkey.bytes
|
2023-07-25 15:58:06 +00:00
|
|
|
|
|
|
|
ndb_builder_set_pubkey(&builder, &pk_raw)
|
|
|
|
ndb_builder_set_kind(&builder, UInt32(kind))
|
2023-08-25 19:32:30 +00:00
|
|
|
ndb_builder_set_created_at(&builder, UInt64(createdAt))
|
2023-07-25 15:58:06 +00:00
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
var ok = true
|
2023-07-25 15:58:06 +00:00
|
|
|
for tag in tags {
|
|
|
|
ndb_builder_new_tag(&builder);
|
|
|
|
for elem in tag {
|
2023-07-26 15:46:44 +00:00
|
|
|
ok = elem.withCString({ eptr in
|
|
|
|
return ndb_builder_push_tag_str(&builder, eptr, Int32(elem.utf8.count)) > 0
|
|
|
|
})
|
|
|
|
if !ok {
|
|
|
|
return nil
|
2023-07-25 15:58:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
ok = content.withCString { cptr in
|
|
|
|
return ndb_builder_set_content(&builder, cptr, Int32(content.utf8.count)) > 0
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return nil
|
2023-07-25 15:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var n = UnsafeMutablePointer<ndb_note>?(nil)
|
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
|
|
|
|
var the_kp: ndb_keypair? = nil
|
|
|
|
|
|
|
|
if let sec = keypair.privkey {
|
2023-07-25 15:58:06 +00:00
|
|
|
var kp = ndb_keypair()
|
2023-07-26 15:46:44 +00:00
|
|
|
memcpy(&kp.secret.0, sec.id.bytes, 32);
|
|
|
|
|
|
|
|
if ndb_create_keypair(&kp) <= 0 {
|
|
|
|
print("bad keypair")
|
|
|
|
} else {
|
|
|
|
the_kp = kp
|
2023-07-25 15:58:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var len: Int32 = 0
|
2023-07-26 15:46:44 +00:00
|
|
|
if var the_kp {
|
|
|
|
len = ndb_builder_finalize(&builder, &n, &the_kp)
|
2023-07-25 15:58:06 +00:00
|
|
|
} else {
|
|
|
|
len = ndb_builder_finalize(&builder, &n, nil)
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
if len <= 0 {
|
|
|
|
free(buf)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//guard let n else { return nil }
|
2023-07-25 15:58:06 +00:00
|
|
|
|
|
|
|
self.owned = true
|
|
|
|
self.count = Int(len)
|
2023-07-26 15:46:44 +00:00
|
|
|
//self.note = n
|
|
|
|
let r = realloc(buf, Int(len))
|
|
|
|
guard let r else {
|
|
|
|
free(buf)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
self.note = r.assumingMemoryBound(to: ndb_note.self)
|
2023-07-25 15:58:06 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 21:54:03 +00:00
|
|
|
static func owned_from_json(json: String, bufsize: Int = 2 << 18) -> NdbNote? {
|
2023-07-24 17:55:34 +00:00
|
|
|
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? {
|
2023-07-23 18:55:36 +00:00
|
|
|
let data = malloc(bufsize)
|
2023-07-24 17:55:34 +00:00
|
|
|
//guard var json_cstr = json.cString(using: .utf8) else { return nil }
|
|
|
|
|
|
|
|
//json_cs
|
2023-07-21 21:54:03 +00:00
|
|
|
var note: UnsafeMutablePointer<ndb_note>?
|
2023-07-23 00:15:36 +00:00
|
|
|
|
2023-07-24 17:55:34 +00:00
|
|
|
let len = ndb_note_from_json(json, Int32(json_len), ¬e, data, Int32(bufsize))
|
2023-07-23 18:55:36 +00:00
|
|
|
|
|
|
|
if len == 0 {
|
|
|
|
free(data)
|
|
|
|
return nil
|
2023-07-21 21:54:03 +00:00
|
|
|
}
|
2023-07-23 18:55:36 +00:00
|
|
|
|
2023-07-21 21:54:03 +00:00
|
|
|
// Create new Data with just the valid bytes
|
2023-07-23 18:55:36 +00:00
|
|
|
guard let note_data = realloc(data, Int(len)) else { return nil }
|
|
|
|
let new_note = note_data.assumingMemoryBound(to: ndb_note.self)
|
2023-07-24 17:55:34 +00:00
|
|
|
|
2023-07-23 18:55:36 +00:00
|
|
|
return NdbNote(note: new_note, owned_size: Int(len))
|
2023-07-23 00:15:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NostrEvent compat
|
|
|
|
extension NdbNote {
|
|
|
|
var is_textlike: Bool {
|
|
|
|
return kind == 1 || kind == 42 || kind == 30023
|
|
|
|
}
|
|
|
|
|
|
|
|
var known_kind: NostrKind? {
|
2023-07-25 15:58:06 +00:00
|
|
|
return NostrKind.init(rawValue: kind)
|
2023-07-23 00:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var too_big: Bool {
|
|
|
|
return known_kind != .longform && self.content_len > 16000
|
|
|
|
}
|
|
|
|
|
|
|
|
var should_show_event: Bool {
|
|
|
|
return !too_big
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
func get_blocks(privkey: Privkey?) -> Blocks {
|
|
|
|
return parse_note_content(content: .init(note: self, privkey: privkey))
|
2023-07-23 00:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func get_inner_event(cache: EventCache) -> NostrEvent? {
|
|
|
|
guard self.known_kind == .boost else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
if self.content_len == 0, let id = self.referenced_ids.first {
|
2023-07-23 18:55:36 +00:00
|
|
|
// TODO: raw id cache lookups
|
|
|
|
return cache.lookup(id)
|
2023-07-23 00:15:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
return self.inner_event
|
2023-07-23 00:15:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-23 18:55:36 +00:00
|
|
|
// TODO: References iterator
|
2023-07-26 15:46:44 +00:00
|
|
|
public var referenced_ids: References<NoteId> {
|
|
|
|
References<NoteId>(tags: self.tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
public var referenced_noterefs: References<NoteRef> {
|
|
|
|
References<NoteRef>(tags: self.tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
public var referenced_follows: References<FollowRef> {
|
|
|
|
References<FollowRef>(tags: self.tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
public var referenced_pubkeys: References<Pubkey> {
|
|
|
|
References<Pubkey>(tags: self.tags)
|
2023-07-23 18:55:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
public var referenced_hashtags: References<Hashtag> {
|
|
|
|
References<Hashtag>(tags: self.tags)
|
2023-07-23 18:55:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
public var referenced_params: References<ReplaceableParam> {
|
|
|
|
References<ReplaceableParam>(tags: self.tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
public var references: References<RefId> {
|
|
|
|
References<RefId>(tags: self.tags)
|
2023-07-25 15:58:06 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 10:57:26 +00:00
|
|
|
func event_refs(_ privkey: Privkey?) -> [EventRef] {
|
2023-07-23 00:15:36 +00:00
|
|
|
if let rs = _event_refs {
|
|
|
|
return rs
|
|
|
|
}
|
2023-07-24 20:08:18 +00:00
|
|
|
let refs = interpret_event_refs_ndb(blocks: self.blocks(privkey).blocks, tags: self.tags)
|
2023-07-23 00:15:36 +00:00
|
|
|
self._event_refs = refs
|
|
|
|
return refs
|
|
|
|
}
|
|
|
|
|
2023-07-31 10:57:26 +00:00
|
|
|
func get_content(_ privkey: Privkey?) -> String {
|
2023-07-24 17:55:34 +00:00
|
|
|
if known_kind == .dm {
|
|
|
|
return decrypted(privkey: privkey) ?? "*failed to decrypt content*"
|
|
|
|
}
|
|
|
|
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
2023-07-31 10:57:26 +00:00
|
|
|
func blocks(_ privkey: Privkey?) -> Blocks {
|
2023-07-24 17:55:34 +00:00
|
|
|
if let bs = _blocks { return bs }
|
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
let blocks = get_blocks(privkey: privkey)
|
2023-07-24 17:55:34 +00:00
|
|
|
self._blocks = blocks
|
|
|
|
return blocks
|
|
|
|
}
|
|
|
|
|
|
|
|
// NDBTODO: switch this to operating on bytes not strings
|
2023-07-31 10:57:26 +00:00
|
|
|
func decrypted(privkey: Privkey?) -> String? {
|
|
|
|
if let decrypted_content {
|
2023-07-23 00:15:36 +00:00
|
|
|
return decrypted_content
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
guard let privkey,
|
|
|
|
let our_pubkey = privkey_to_pubkey(privkey: privkey) else {
|
2023-07-23 00:15:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-24 17:55:34 +00:00
|
|
|
// NDBTODO: don't hex encode
|
2023-07-25 15:58:06 +00:00
|
|
|
var pubkey = self.pubkey
|
2023-07-23 00:15:36 +00:00
|
|
|
// This is our DM, we need to use the pubkey of the person we're talking to instead
|
2023-07-24 17:55:34 +00:00
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
if our_pubkey == pubkey, let pk = self.referenced_pubkeys.first {
|
|
|
|
pubkey = pk
|
2023-07-23 00:15:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-24 17:55:34 +00:00
|
|
|
// NDBTODO: pass data to pubkey
|
2023-07-26 15:46:44 +00:00
|
|
|
let dec = decrypt_dm(privkey, pubkey: pubkey, content: self.content, encoding: .base64)
|
2023-07-23 00:15:36 +00:00
|
|
|
self.decrypted_content = dec
|
|
|
|
|
|
|
|
return dec
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
public func direct_replies(_ privkey: Privkey?) -> [NoteId] {
|
2023-07-23 00:15:36 +00:00
|
|
|
return event_refs(privkey).reduce(into: []) { acc, evref in
|
|
|
|
if let direct_reply = evref.is_direct_reply {
|
2023-07-26 15:46:44 +00:00
|
|
|
acc.append(direct_reply.note_id)
|
2023-07-23 00:15:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-24 19:40:27 +00:00
|
|
|
// NDBTODO: just use Id
|
2023-07-31 10:57:26 +00:00
|
|
|
public func thread_id(privkey: Privkey?) -> NoteId {
|
2023-07-23 00:15:36 +00:00
|
|
|
for ref in event_refs(privkey) {
|
|
|
|
if let thread_id = ref.is_thread_id {
|
2023-07-26 15:46:44 +00:00
|
|
|
return thread_id.note_id
|
2023-07-23 00:15:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-25 15:58:06 +00:00
|
|
|
return self.id
|
2023-07-23 00:15:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
public func last_refid() -> NoteId? {
|
|
|
|
return self.referenced_ids.last
|
2023-07-23 00:15:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-24 19:40:27 +00:00
|
|
|
// NDBTODO: id -> data
|
2023-07-26 15:46:44 +00:00
|
|
|
/*
|
2023-07-24 19:40:27 +00:00
|
|
|
public func references(id: String, key: AsciiCharacter) -> Bool {
|
2023-07-26 15:46:44 +00:00
|
|
|
var matcher: (Reference) -> Bool = { ref in ref.ref_id.matches_str(id) }
|
|
|
|
if id.count == 64, let decoded = hex_decode(id) {
|
|
|
|
matcher = { ref in ref.ref_id.matches_id(decoded) }
|
|
|
|
}
|
2023-07-24 20:08:18 +00:00
|
|
|
for ref in References(tags: self.tags) {
|
2023-07-26 15:46:44 +00:00
|
|
|
if ref.key == key && matcher(ref) {
|
2023-07-24 19:40:27 +00:00
|
|
|
return true
|
2023-07-23 00:15:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2023-07-26 15:46:44 +00:00
|
|
|
*/
|
2023-07-23 00:15:36 +00:00
|
|
|
|
2023-07-31 10:57:26 +00:00
|
|
|
func is_reply(_ privkey: Privkey?) -> Bool {
|
2023-07-24 19:39:41 +00:00
|
|
|
return event_is_reply(self.event_refs(privkey))
|
2023-07-23 00:15:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
func note_language(_ privkey: Privkey?) -> String? {
|
|
|
|
assert(!Thread.isMainThread, "This function must not be run on the main thread.")
|
2023-07-23 00:15:36 +00:00
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
// Rely on Apple's NLLanguageRecognizer to tell us which language it thinks the note is in
|
|
|
|
// and filter on only the text portions of the content as URLs and hashtags confuse the language recognizer.
|
|
|
|
let originalBlocks = self.blocks(privkey).blocks
|
2023-08-26 02:03:19 +00:00
|
|
|
let originalOnlyText = originalBlocks.compactMap { $0.is_text }.joined(separator: " ")
|
2023-07-23 00:15:36 +00:00
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
// Only accept language recognition hypothesis if there's at least a 50% probability that it's accurate.
|
|
|
|
let languageRecognizer = NLLanguageRecognizer()
|
|
|
|
languageRecognizer.processString(originalOnlyText)
|
2023-07-23 00:15:36 +00:00
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
guard let locale = languageRecognizer.languageHypotheses(withMaximum: 1).first(where: { $0.value >= 0.5 })?.key.rawValue else {
|
|
|
|
let nstr: String? = nil
|
|
|
|
return nstr
|
2023-07-24 19:40:27 +00:00
|
|
|
}
|
2023-07-23 00:15:36 +00:00
|
|
|
|
2023-07-26 15:46:44 +00:00
|
|
|
// 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)
|
2023-07-23 00:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var age: TimeInterval {
|
|
|
|
let event_date = Date(timeIntervalSince1970: TimeInterval(created_at))
|
|
|
|
return Date.now.timeIntervalSince(event_date)
|
|
|
|
}
|
2023-07-23 18:55:36 +00:00
|
|
|
}
|