1package event23import "encoding/hex"45// RoomReplyRoot reads NIP-10 marked references and the older unmarked6// NIP-29 thread convention. A mention or q tag never makes a reply.7func RoomReplyRoot(e Event) string {8 if e.Kind != KIND_CHAT && e.Kind != KIND_THREAD_REPLY {9 return ""10 }11 var root, reply, first string12 marked := false13 for _, tag := range e.Tags {14 if len(tag) < 2 || tag[0] != "e" || len(tag[1]) != 64 {15 continue16 }17 if _, err := hex.DecodeString(tag[1]); err != nil {18 continue19 }20 marker := ""21 if len(tag) > 3 {22 marker = tag[3]23 }24 if marker != "" {25 marked = true26 }27 switch marker {28 case "root":29 root = tag[1]30 case "reply":31 reply = tag[1]32 case "":33 if first == "" {34 first = tag[1]35 }36 }37 }38 if root != "" {39 return root40 }41 if reply != "" {42 return reply43 }44 if !marked {45 return first46 }47 return ""48}