2022-11-17 06:15:30 -03:00
|
|
|
package nip10
|
|
|
|
|
|
|
|
import "github.com/nbd-wtf/go-nostr"
|
|
|
|
|
2025-03-10 02:33:29 -03:00
|
|
|
func GetThreadRoot(tags nostr.Tags) *nostr.EventPointer {
|
2022-11-17 06:15:30 -03:00
|
|
|
for _, tag := range tags {
|
|
|
|
if len(tag) >= 4 && tag[0] == "e" && tag[3] == "root" {
|
2025-03-10 02:33:29 -03:00
|
|
|
p, _ := nostr.EventPointerFromTag(tag)
|
|
|
|
return &p
|
2022-11-17 06:15:30 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-10 02:33:29 -03:00
|
|
|
firstE := tags.Find("e")
|
|
|
|
if firstE != nil {
|
|
|
|
return &nostr.EventPointer{
|
|
|
|
ID: firstE[1],
|
|
|
|
}
|
|
|
|
}
|
2022-11-17 06:15:30 -03:00
|
|
|
|
2025-03-10 02:33:29 -03:00
|
|
|
return nil
|
2025-01-23 17:52:26 -03:00
|
|
|
}
|
|
|
|
|
2025-03-10 02:33:29 -03:00
|
|
|
func GetImmediateParent(tags nostr.Tags) *nostr.EventPointer {
|
|
|
|
var parent nostr.Tag
|
|
|
|
var lastE nostr.Tag
|
2023-03-04 21:04:59 -03:00
|
|
|
|
2023-05-31 00:10:37 +02:00
|
|
|
for i := 0; i <= len(tags)-1; i++ {
|
2022-11-17 06:15:30 -03:00
|
|
|
tag := tags[i]
|
2023-03-04 21:04:59 -03:00
|
|
|
|
|
|
|
if len(tag) < 2 {
|
|
|
|
continue
|
2022-11-17 06:15:30 -03:00
|
|
|
}
|
2024-05-29 22:20:49 +02:00
|
|
|
if tag[0] != "e" && tag[0] != "a" {
|
2023-03-04 21:04:59 -03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tag) >= 4 {
|
|
|
|
if tag[3] == "reply" {
|
2025-03-10 02:35:02 -03:00
|
|
|
parent = tag
|
|
|
|
break
|
2023-03-04 21:04:59 -03:00
|
|
|
}
|
2025-03-10 02:35:02 -03:00
|
|
|
if tag[3] == "parent" {
|
2023-03-04 21:04:59 -03:00
|
|
|
// will be used as our first fallback
|
2025-03-10 02:35:02 -03:00
|
|
|
parent = tag
|
2023-03-04 21:04:59 -03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if tag[3] == "mention" {
|
|
|
|
// this invalidates this tag as a second fallback mechanism (clients that don't add markers)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-10 02:35:02 -03:00
|
|
|
lastE = tag // will be used as our second fallback (clients that don't add markers)
|
2023-03-04 21:04:59 -03:00
|
|
|
}
|
|
|
|
|
2025-03-10 02:35:02 -03:00
|
|
|
// 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 {
|
|
|
|
p, _ := nostr.EventPointerFromTag(parent)
|
|
|
|
return &p
|
2022-11-17 06:15:30 -03:00
|
|
|
}
|
|
|
|
|
2025-03-10 02:35:02 -03:00
|
|
|
if lastE != 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)
|
|
|
|
return &nostr.EventPointer{
|
|
|
|
ID: lastE[1],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-11-17 06:15:30 -03:00
|
|
|
}
|