76 lines
1.6 KiB
Go
76 lines
1.6 KiB
Go
package nip10
|
|
|
|
import "fiatjaf.com/nostr"
|
|
|
|
func GetThreadRoot(tags nostr.Tags) nostr.Pointer {
|
|
for _, tag := range tags {
|
|
if len(tag) >= 4 && tag[0] == "e" && tag[3] == "root" {
|
|
p, _ := nostr.EventPointerFromTag(tag)
|
|
return p
|
|
}
|
|
}
|
|
|
|
firstE := tags.Find("e")
|
|
if firstE != nil {
|
|
p, _ := nostr.EventPointerFromTag(firstE)
|
|
return p
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func GetImmediateParent(tags nostr.Tags) nostr.Pointer {
|
|
var parent nostr.Tag
|
|
var last nostr.Tag
|
|
|
|
for i := 0; i <= len(tags)-1; i++ {
|
|
tag := tags[i]
|
|
|
|
if len(tag) < 2 {
|
|
continue
|
|
}
|
|
if tag[0] != "e" && tag[0] != "a" {
|
|
continue
|
|
}
|
|
|
|
if len(tag) >= 4 {
|
|
if tag[3] == "reply" {
|
|
parent = tag
|
|
break
|
|
}
|
|
if tag[3] == "mention" {
|
|
// this invalidates this tag as a second fallback mechanism (clients that don't add markers)
|
|
continue
|
|
}
|
|
}
|
|
|
|
last = tag // will be used as our second fallback (clients that don't add markers)
|
|
}
|
|
|
|
// if we reached this point we don't have a "reply", but if we have a "parent"
|
|
// that means this event is a direct reply to the parent
|
|
if parent != nil {
|
|
if parent[0] == "e" {
|
|
p, _ := nostr.EventPointerFromTag(parent)
|
|
return p
|
|
} else {
|
|
a, _ := nostr.EntityPointerFromTag(parent)
|
|
return a
|
|
}
|
|
}
|
|
|
|
if last != nil {
|
|
// if we reached this point and we have at least one "e" we'll use that (the last)
|
|
// (we don't bother looking for relay or author hints because these clients don't add these anyway)
|
|
if last[0] == "e" {
|
|
p, _ := nostr.EventPointerFromTag(last)
|
|
return p
|
|
} else {
|
|
a, _ := nostr.EntityPointerFromTag(last)
|
|
return a
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|