-
Notifications
You must be signed in to change notification settings - Fork 13
/
votes.go
125 lines (104 loc) · 2.18 KB
/
votes.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
package brutalinks
import (
"time"
vocab "github.com/go-ap/activitypub"
"github.com/go-ap/errors"
)
const (
ScoreMultiplier = 1
ScoreMaxK = 10000.0
ScoreMaxM = 10000000.0
ScoreMaxB = 10000000000.0
)
var ValidAppreciationTypes = vocab.ActivityVocabularyTypes{
vocab.LikeType,
vocab.DislikeType,
}
type VoteCollection []Vote
type VoteMetadata struct {
IRI string `json:"-"`
OriginalIRI string `json:"-"`
}
type Vote struct {
SubmittedBy *Account `json:"-"`
SubmittedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
Weight int `json:"weight"`
Item *Item `json:"on"`
Flags FlagBits `json:"-"`
Metadata *VoteMetadata `json:"-"`
Pub *vocab.Activity `json:"-"`
}
func (v *Vote) ID() Hash {
if v == nil {
return AnonymousHash
}
return HashFromString(v.Metadata.IRI)
}
// HasMetadata
func (v Vote) HasMetadata() bool {
return v.Metadata != nil
}
// IsValid
func (v *Vote) IsValid() bool {
return v != nil && v.Item.IsValid()
}
// IsYay returns true if current vote is a Yay
func (v Vote) IsYay() bool {
if v.Pub == nil {
return false
}
return v.Pub.GetType() == vocab.LikeType
}
// IsNay returns true if current vote is a Nay
func (v Vote) IsNay() bool {
if v.Pub == nil {
return false
}
return v.Pub.GetType() == vocab.DislikeType
}
// AP returns the underlying actvitypub item
func (v *Vote) AP() vocab.Item {
return v.Pub
}
// Type
func (v *Vote) Type() RenderType {
return AppreciationType
}
// Date
func (v Vote) Date() time.Time {
return v.SubmittedAt
}
func (v *Vote) Children() *RenderableList {
return nil
}
func (v VoteCollection) Contains(vot Vote) bool {
for _, vv := range v {
if !vv.HasMetadata() || !vot.HasMetadata() {
continue
}
if vv.Metadata.IRI == vot.Metadata.IRI {
return true
}
}
return false
}
type ScoreType int
const (
ScoreItem = ScoreType(iota)
ScoreAccount
)
func (v VoteCollection) First() (*Vote, error) {
for _, vv := range v {
return &vv, nil
}
return nil, errors.Errorf("empty %T", v)
}
// Score
func (v VoteCollection) Score() int {
score := 0
for _, vot := range v {
score += vot.Weight
}
return score
}