ContentParsing.swift (2132B)
1 // 2 // ContentParsing.swift 3 // damus 4 // 5 // Created by William Casarin on 2023-07-22. 6 // 7 8 import Foundation 9 10 enum NoteContent { 11 case note(NostrEvent) 12 case content(String, TagsSequence?) 13 14 init(note: NostrEvent, keypair: Keypair) { 15 if note.known_kind == .dm || note.known_kind == .highlight { 16 self = .content(note.get_content(keypair), note.tags) 17 } else { 18 self = .note(note) 19 } 20 } 21 } 22 23 func interpret_event_refs(tags: TagsSequence) -> ThreadReply? { 24 // migration is long over, lets just do this to fix tests 25 return interpret_event_refs_ndb(tags: tags) 26 } 27 28 func interpret_event_refs_ndb(tags: TagsSequence) -> ThreadReply? { 29 if tags.count == 0 { 30 return nil 31 } 32 33 return interp_event_refs_without_mentions_ndb(References<NoteRef>(tags: tags)) 34 } 35 36 func interp_event_refs_without_mentions_ndb(_ ev_tags: References<NoteRef>) -> ThreadReply? { 37 var first: Bool = true 38 var root_id: NoteRef? = nil 39 var reply_id: NoteRef? = nil 40 var mention: NoteRef? = nil 41 var any_marker: Bool = false 42 43 for ref in ev_tags { 44 if let marker = ref.marker { 45 any_marker = true 46 switch marker { 47 case .root: root_id = ref 48 case .reply: reply_id = ref 49 case .mention: mention = ref 50 } 51 // deprecated form, only activate if we don't have any markers set 52 } else if !any_marker { 53 if first { 54 root_id = ref 55 first = false 56 } else { 57 reply_id = ref 58 } 59 } 60 } 61 62 // If either reply or root_id is blank while the other is not, then this is 63 // considered reply-to-root. We should always have a root and reply tag, if they 64 // are equal this is reply-to-root 65 if reply_id == nil && root_id != nil { 66 reply_id = root_id 67 } else if root_id == nil && reply_id != nil { 68 root_id = reply_id 69 } 70 71 guard let reply_id, let root_id else { 72 return nil 73 } 74 75 return ThreadReply(root: root_id, reply: reply_id, mention: mention.map { m in .noteref(m) }) 76 }