forked from OpenBazaar/openbazaar-go
/
inventory.go
132 lines (109 loc) · 3.43 KB
/
inventory.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
package core
import (
"encoding/json"
"errors"
peer "gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
"time"
"github.com/OpenBazaar/openbazaar-go/repo"
)
var (
ipfsInventoryCacheMaxDuration = 1 * time.Hour
// ErrInventoryNotFoundForSlug - inventory not found error
ErrInventoryNotFoundForSlug = errors.New("could not find slug in inventory")
)
// InventoryListing is the listing representation stored on IPFS
type InventoryListing struct {
Inventory int64 `json:"inventory"`
LastUpdated string `json:"lastUpdated"`
}
// Inventory is the complete inventory representation stored on IPFS
// It maps slug -> quantity information
type Inventory map[string]*InventoryListing
// GetLocalInventory gets the inventory from the database
func (n *OpenBazaarNode) GetLocalInventory() (Inventory, error) {
listings, err := n.Datastore.Inventory().GetAll()
if err != nil {
return nil, err
}
inventory := make(Inventory, len(listings))
var totalCount int64
for slug, variants := range listings {
totalCount = 0
for _, variantCount := range variants {
totalCount += variantCount
}
inventory[slug] = &InventoryListing{
Inventory: totalCount,
LastUpdated: time.Now().UTC().Format(time.RFC3339),
}
}
return inventory, nil
}
// GetLocalInventoryForSlug gets the local inventory for the given slug
func (n *OpenBazaarNode) GetLocalInventoryForSlug(slug string) (*InventoryListing, error) {
variants, err := n.Datastore.Inventory().Get(slug)
if err != nil {
return nil, err
}
var inventory *InventoryListing
var totalCount int64
for _, variantCount := range variants {
totalCount += variantCount
}
inventory = &InventoryListing{
Inventory: totalCount,
LastUpdated: time.Now().UTC().Format(time.RFC3339),
}
return inventory, nil
}
// PublishInventory stores an inventory on IPFS
func (n *OpenBazaarNode) PublishInventory() error {
// TODO: [cp] need to refactor the inventory publishing and getting as we've discussed before
// now is a good time to do that.
/*inventory, err := n.GetLocalInventory()
if err != nil {
return err
}
n.Broadcast <- repo.StatusNotification{Status: "publishing"}
go func() {
hash, err := repo.PublishObjectToIPFS(n.IpfsNode, n.RepoPath, "inventory", inventory)
if err != nil {
log.Error(err)
n.Broadcast <- repo.StatusNotification{Status: "error publishing"}
return
}
n.Broadcast <- repo.StatusNotification{Status: "publish complete"}
err = n.sendToPushNodes(hash)
if err != nil {
log.Error(err)
}
}()*/
return nil
}
// GetPublishedInventoryBytes gets a byte slice representing the given peer's
// inventory that it published to IPFS
func (n *OpenBazaarNode) GetPublishedInventoryBytes(p peer.ID, useCache bool) ([]byte, error) {
var cacheLength time.Duration
if useCache {
cacheLength = ipfsInventoryCacheMaxDuration
}
return repo.GetObjectFromIPFS(n.IpfsNode, p, "inventory", cacheLength)
}
// GetPublishedInventoryBytesForSlug gets a byte slice representing the given
// slug's inventory from IPFS
func (n *OpenBazaarNode) GetPublishedInventoryBytesForSlug(p peer.ID, slug string, useCache bool) ([]byte, error) {
bytes, err := n.GetPublishedInventoryBytes(p, useCache)
if err != nil {
return nil, err
}
inventory := Inventory{}
err = json.Unmarshal(bytes, &inventory)
if err != nil {
return nil, err
}
listingInventory, ok := inventory[slug]
if !ok {
return nil, ErrInventoryNotFoundForSlug
}
return json.Marshal(listingInventory)
}