-
Notifications
You must be signed in to change notification settings - Fork 15
/
import.go
135 lines (118 loc) Β· 3.27 KB
/
import.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/fiatjaf/eventstore"
"github.com/nbd-wtf/go-nostr"
)
const layout = "2006-01-02"
var (
ownerImportedNotes = 0
taggedImportedNotes = 0
)
func importOwnerNotes() {
ctx := context.Background()
wdb := eventstore.RelayWrapper{Store: outboxDB}
startTime, err := time.Parse(layout, config.ImportStartDate)
if err != nil {
fmt.Println("Error parsing start date:", err)
return
}
endTime := startTime.Add(240 * time.Hour)
for {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
startTimestamp := nostr.Timestamp(startTime.Unix())
endTimestamp := nostr.Timestamp(endTime.Unix())
filters := []nostr.Filter{{
Authors: []string{nPubToPubkey(config.OwnerNpub)},
Since: &startTimestamp,
Until: &endTimestamp,
}}
for ev := range pool.SubManyEose(ctx, config.ImportSeedRelays, filters) {
wdb.Publish(ctx, *ev.Event)
ownerImportedNotes++
}
log.Println("π¦ imported", ownerImportedNotes, "owner notes")
time.Sleep(5 * time.Second)
startTime = startTime.Add(240 * time.Hour)
endTime = endTime.Add(240 * time.Hour)
if startTime.After(time.Now()) {
log.Println("β
owner note import complete! ")
break
}
}
}
func importTaggedNotes() {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 2*time.Minute)
defer cancel()
wdb := eventstore.RelayWrapper{Store: inboxDB}
filters := []nostr.Filter{{
Tags: nostr.TagMap{
"p": {nPubToPubkey(config.OwnerNpub)},
},
}}
log.Println("π¦ importing inbox notes, please wait 2 minutes")
taggedImportedNotes = 0
for ev := range pool.SubMany(ctx, config.ImportSeedRelays, filters) {
if !wotMap[ev.Event.PubKey] {
continue
}
for _, tag := range ev.Event.Tags.GetAll([]string{"p"}) {
if len(tag) < 2 {
continue
}
if tag[1] == nPubToPubkey(config.OwnerNpub) {
wdb.Publish(ctx, *ev.Event)
taggedImportedNotes++
}
}
}
log.Println("π¦ imported", taggedImportedNotes, "tagged notes")
log.Println("β
tagged import complete. please restart the relay")
}
func subscribeInbox() {
ctx := context.Background()
wdb := eventstore.RelayWrapper{Store: inboxDB}
startTime := nostr.Timestamp(time.Now().Add(-time.Minute * 5).Unix())
filters := []nostr.Filter{{
Tags: nostr.TagMap{
"p": {nPubToPubkey(config.OwnerNpub)},
},
Since: &startTime,
}}
log.Println("π’ subscribing to inbox")
for ev := range pool.SubMany(ctx, config.ImportSeedRelays, filters) {
if !wotMap[ev.Event.PubKey] {
continue
}
for _, tag := range ev.Event.Tags.GetAll([]string{"p"}) {
if len(tag) < 2 {
continue
}
if tag[1] == nPubToPubkey(config.OwnerNpub) {
wdb.Publish(ctx, *ev.Event)
switch ev.Event.Kind {
case nostr.KindTextNote:
log.Println("π° new note in your inbox")
case nostr.KindReaction:
log.Println(ev.Event.Content, "new reaction in your inbox")
case nostr.KindZap:
log.Println("β‘οΈ new zap in your inbox")
case nostr.KindEncryptedDirectMessage:
log.Println("π new encrypted message in your inbox")
case nostr.KindRepost:
log.Println("π new repost in your inbox")
case nostr.KindFollowList:
// do nothing
default:
log.Println("π¦ new event in your inbox")
}
taggedImportedNotes++
}
}
}
}