ndb: implement eventref building from ndb notes

This commit is contained in:
William Casarin
2023-07-24 10:55:34 -07:00
parent c8e236b6d5
commit 1e9e4a7f3a
8 changed files with 209 additions and 31 deletions

View File

@@ -73,3 +73,70 @@ func parse_note_content_ndb(note: NdbNote) -> Blocks {
return Blocks(words: words, blocks: out)
}
func interpret_event_refs_ndb(blocks: [Block], tags: TagsSequence) -> [EventRef] {
if tags.count == 0 {
return []
}
/// build a set of indices for each event mention
let mention_indices = build_mention_indices(blocks, type: .event)
/// simpler case with no mentions
if mention_indices.count == 0 {
let ev_refs = References(tags: tags).ids()
return interp_event_refs_without_mentions_ndb(ev_refs)
}
return interp_event_refs_with_mentions_ndb(tags: tags, mention_indices: mention_indices)
}
func interp_event_refs_without_mentions_ndb(_ ev_tags: LazyFilterSequence<References>) -> [EventRef] {
var count = 0
var evrefs: [EventRef] = []
var first: Bool = true
var first_ref: Reference? = nil
for ref in ev_tags {
if first {
first_ref = ref
evrefs.append(.thread_id(ref.to_referenced_id()))
first = false
} else {
evrefs.append(.reply(ref.to_referenced_id()))
}
count += 1
}
if let first_ref, count == 1 {
let r = first_ref.to_referenced_id()
return [.reply_to_root(r)]
}
return evrefs
}
func interp_event_refs_with_mentions_ndb(tags: TagsSequence, mention_indices: Set<Int>) -> [EventRef] {
var mentions: [EventRef] = []
var ev_refs: [ReferencedId] = []
var i: Int = 0
for tag in tags {
if tag.count >= 2, let t = tag[0], t.matches_char("e"),
let ref = tag_to_refid_ndb(tag)
{
if mention_indices.contains(i) {
let mention = Mention(index: i, type: .event, ref: ref)
mentions.append(.mention(mention))
} else {
ev_refs.append(ref)
}
}
i += 1
}
var replies = interp_event_refs_without_mentions(ev_refs)
replies.append(contentsOf: mentions)
return replies
}