This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tracker.go
86 lines (79 loc) · 2.23 KB
/
tracker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package sdk
import (
"net/url"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/nostr-sdk/hints"
)
func (sys *System) trackEventHints(ie nostr.IncomingEvent) {
if IsVirtualRelay(ie.Relay.URL) {
return
}
switch ie.Kind {
case nostr.KindRelayListMetadata:
for _, tag := range ie.Tags {
if len(tag) < 2 || tag[0] != "r" {
continue
}
if len(tag) == 2 || (tag[2] == "" || tag[2] == "write") {
sys.Hints.Save(ie.PubKey, tag[1], hints.LastInRelayList, ie.CreatedAt)
}
}
case nostr.KindContactList:
sys.Hints.Save(ie.PubKey, ie.Relay.URL, hints.MostRecentEventFetched, ie.CreatedAt)
for _, tag := range ie.Tags {
if len(tag) < 3 {
continue
}
if IsVirtualRelay(tag[2]) {
continue
}
if p, err := url.Parse(tag[2]); err != nil || (p.Scheme != "wss" && p.Scheme != "ws") {
continue
}
if tag[0] == "p" && nostr.IsValidPublicKey(tag[1]) {
sys.Hints.Save(tag[1], tag[2], hints.LastInTag, ie.CreatedAt)
}
}
case nostr.KindTextNote:
sys.Hints.Save(ie.PubKey, ie.Relay.URL, hints.MostRecentEventFetched, ie.CreatedAt)
for _, tag := range ie.Tags {
if len(tag) < 3 {
continue
}
if IsVirtualRelay(tag[2]) {
continue
}
if p, err := url.Parse(tag[2]); err != nil || (p.Scheme != "wss" && p.Scheme != "ws") {
continue
}
if tag[0] == "p" && nostr.IsValidPublicKey(tag[1]) {
sys.Hints.Save(tag[1], tag[2], hints.LastInTag, ie.CreatedAt)
}
}
for _, ref := range ParseReferences(ie.Event) {
if ref.Profile != nil {
for _, relay := range ref.Profile.Relays {
if IsVirtualRelay(relay) {
continue
}
if p, err := url.Parse(relay); err != nil || (p.Scheme != "wss" && p.Scheme != "ws") {
continue
}
if nostr.IsValidPublicKey(ref.Profile.PublicKey) {
sys.Hints.Save(ref.Profile.PublicKey, relay, hints.LastInNprofile, ie.CreatedAt)
}
}
} else if ref.Event != nil && nostr.IsValidPublicKey(ref.Event.Author) {
for _, relay := range ref.Event.Relays {
if IsVirtualRelay(relay) {
continue
}
if p, err := url.Parse(relay); err != nil || (p.Scheme != "wss" && p.Scheme != "ws") {
continue
}
sys.Hints.Save(ref.Event.Author, relay, hints.LastInNevent, ie.CreatedAt)
}
}
}
}
}