forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repointer.go
49 lines (44 loc) · 1 KB
/
repointer.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
package net
import (
"github.com/OpenBazaar/openbazaar-go/ipfs"
"github.com/OpenBazaar/openbazaar-go/repo"
"github.com/ipfs/go-ipfs/core"
"golang.org/x/net/context"
"time"
)
type PointerRepublisher struct {
ipfsNode *core.IpfsNode
db repo.Datastore
}
func NewPointerRepublisher(node *core.IpfsNode, database repo.Datastore) *PointerRepublisher {
return &PointerRepublisher{
ipfsNode: node,
db: database,
}
}
func (r *PointerRepublisher) Run() {
tick := time.NewTicker(time.Hour * 24)
defer tick.Stop()
go r.republish()
for range tick.C {
go r.republish()
}
}
func (r *PointerRepublisher) republish() {
pointers, err := r.db.Pointers().GetAll()
if err != nil {
return
}
ctx := context.Background()
for _, p := range pointers {
if p.Purpose != ipfs.MESSAGE {
ipfs.RePublishPointer(r.ipfsNode, ctx, p)
} else {
if time.Now().Sub(p.Timestamp) > time.Hour*24*30 {
r.db.Pointers().Delete(p.Value.ID)
} else {
ipfs.RePublishPointer(r.ipfsNode, ctx, p)
}
}
}
}