damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

EventRef.swift (3650B)


      1 //
      2 //  EventRef.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2022-05-08.
      6 //
      7 
      8 import Foundation
      9 
     10 enum EventRef: Equatable {
     11     case mention(Mention<NoteRef>)
     12     case thread_id(NoteRef)
     13     case reply(NoteRef)
     14     case reply_to_root(NoteRef)
     15 
     16     var is_mention: NoteRef? {
     17         if case .mention(let m) = self { return m.ref }
     18         return nil
     19     }
     20     
     21     var is_direct_reply: NoteRef? {
     22         switch self {
     23         case .mention:
     24             return nil
     25         case .thread_id:
     26             return nil
     27         case .reply(let refid):
     28             return refid
     29         case .reply_to_root(let refid):
     30             return refid
     31         }
     32     }
     33     
     34     var is_thread_id: NoteRef? {
     35         switch self {
     36         case .mention:
     37             return nil
     38         case .thread_id(let referencedId):
     39             return referencedId
     40         case .reply:
     41             return nil
     42         case .reply_to_root(let referencedId):
     43             return referencedId
     44         }
     45     }
     46     
     47     var is_reply: NoteRef? {
     48         switch self {
     49         case .mention:
     50             return nil
     51         case .thread_id:
     52             return nil
     53         case .reply(let refid):
     54             return refid
     55         case .reply_to_root(let refid):
     56             return refid
     57         }
     58     }
     59 }
     60 
     61 func build_mention_indices(_ blocks: BlocksSequence, type: MentionType) -> Set<Int> {
     62     return blocks.reduce(into: []) { acc, block in
     63         switch block {
     64         case .mention:
     65             return
     66         case .mention_index(let idx):
     67             return
     68         case .text:
     69             return
     70         case .hashtag:
     71             return
     72         case .url:
     73             return
     74         case .invoice:
     75             return
     76         }
     77     }
     78 }
     79 
     80 func interp_event_refs_without_mentions(_ refs: [NoteRef]) -> [EventRef] {
     81     if refs.count == 0 {
     82         return []
     83     }
     84 
     85     if refs.count == 1 {
     86         return [.reply_to_root(refs[0])]
     87     }
     88     
     89     var evrefs: [EventRef] = []
     90     var first: Bool = true
     91     for ref in refs {
     92         if first {
     93             evrefs.append(.thread_id(ref))
     94             first = false
     95         } else {
     96             evrefs.append(.reply(ref))
     97         }
     98     }
     99     return evrefs
    100 }
    101 
    102 func interp_event_refs_with_mentions(tags: Tags) -> [EventRef] {
    103     var mentions: [EventRef] = []
    104     var ev_refs: [NoteRef] = []
    105     var i: Int = 0
    106 
    107     for tag in tags {
    108         if let ref = NoteRef.from_tag(tag: tag) {
    109             ev_refs.append(ref)
    110         }
    111         i += 1
    112     }
    113     
    114     var replies = interp_event_refs_without_mentions(ev_refs)
    115     replies.append(contentsOf: mentions)
    116     return replies
    117 }
    118 
    119 func interpret_event_refs(tags: Tags) -> [EventRef] {
    120     if tags.count == 0 {
    121         return []
    122     }
    123     
    124     /// build a set of indices for each event mention
    125     //let mention_indices = build_mention_indices(blocks, type: .e)
    126 
    127     /// simpler case with no mentions
    128     //if mention_indices.count == 0 {
    129         //return interp_event_refs_without_mentions_ndb(References<NoteRef>(tags: tags))
    130     //}
    131 
    132     return interp_event_refs_with_mentions(tags: tags)
    133 }
    134 
    135 func ndb_interpret_event_refs(tags: Tags) -> [EventRef] {
    136     if tags.count == 0 {
    137         return []
    138     }
    139     
    140     /// build a set of indices for each event mention
    141     //let mention_indices = build_mention_indices(blocks, type: .e)
    142 
    143     /// simpler case with no mentions
    144     //if mention_indices.count == 0 {
    145         //return interp_event_refs_without_mentions_ndb(References<NoteRef>(tags: tags))
    146     //}
    147 
    148     return interp_event_refs_with_mentions(tags: tags)
    149 }
    150 
    151 
    152 func event_is_reply(_ refs: [EventRef]) -> Bool {
    153     return refs.contains { evref in
    154         return evref.is_reply != nil
    155     }
    156 }