-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdummycurator.go
132 lines (106 loc) · 2.88 KB
/
dummycurator.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 main
import (
"fmt"
"log"
"strconv"
"time"
"github.com/boltdb/bolt"
)
// DummyCurator is a placeholder curation module. Currently it
// registers all content, but does no filtering nor ranking
type DummyCurator struct{}
var curationDB *bolt.DB
const curationDBPath = "./data/curation.db"
const curPostBucket = "exampleCuration_post"
const curFlagBucket = "exampleCuration_flags"
// Init initializes boltdb which simply keeps track of saved hashes
// and their arrivaltime
func (c *DummyCurator) Init() error {
Info.Println("Init Curation DB")
config := &bolt.Options{Timeout: 2 * time.Second}
var err error
curationDB, err = bolt.Open(curationDBPath, 0600, config)
if err != nil {
Error.Println("FATAL", err)
log.Fatal(err)
}
// Create Buckets if they don't exists
err = curationDB.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(curPostBucket))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
return nil
})
if err != nil {
return err
}
return curationDB.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(curFlagBucket))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
return nil
})
}
func (c *DummyCurator) OnPostAdded(obj *Post, isWhitelabeled bool) bool {
err := curationDB.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(B(curPostBucket))
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
return bucket.Put(B(obj.Hash), B(timestamp))
})
if err != nil {
Error.Println("Error on adding content to curation", err)
return false
}
return true
}
func (c *DummyCurator) OnCommentAdded(obj *Comment, isWhitelabeled bool) bool {
return true
}
func (c *DummyCurator) GetContent(params map[string]interface{}) []string {
hashes := []string{}
err := curationDB.View(func(tx *bolt.Tx) error {
b := tx.Bucket(B(curPostBucket))
b.ForEach(func(k, _ []byte) error {
// Check if content is flagged
fB := tx.Bucket(B(curFlagBucket))
isFlagged := fB.Get(k)
if isFlagged != nil && string(isFlagged) == "true" {
// Skip flagged elemens
return nil
}
hashes = append(hashes, string(k))
return nil
})
return nil
})
if err != nil {
return []string{}
}
return hashes
}
func (c *DummyCurator) FlagContent(hash string, isFlagged bool) {
Info.Println(hash, "flagged", isFlagged)
err := curationDB.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(B(curFlagBucket))
if isFlagged {
return bucket.Put(B(hash), B("true"))
} else {
return bucket.Put(B(hash), B("false"))
}
})
if err != nil {
Warning.Println("Error on adding content to curation", err)
}
}
func (c *DummyCurator) UpvoteContent(hash string) {
Info.Println(hash, "upvoted")
}
func (c *DummyCurator) DownvoteContent(hash string) {
Info.Println(hash, "donwvoted")
}
func (c *DummyCurator) Close() error {
Info.Println("Destroy curation module")
return nil
}