forked from keybase/client
/
interesting.go
154 lines (131 loc) · 3.3 KB
/
interesting.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package service
import (
"context"
"sort"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
)
type rankedList struct {
uids []keybase1.UID
ranks map[string]int
}
func newRankedList(uids []keybase1.UID) *rankedList {
r := &rankedList{
uids: uids,
ranks: make(map[string]int),
}
for index, uid := range uids {
r.ranks[uid.String()] = index
}
return r
}
func (r *rankedList) UIDs() []keybase1.UID {
return r.uids
}
func (r *rankedList) Rank(uid keybase1.UID) float64 {
var index int
var ok bool
if index, ok = r.ranks[uid.String()]; !ok {
return 0
}
total := float64(len(r.uids))
return ((total - float64(index)) / total)
}
type weightedRankedList struct {
*rankedList
weight float64
}
func newWeightedRankedList(rl *rankedList, weight float64) *weightedRankedList {
return &weightedRankedList{
rankedList: rl,
weight: weight,
}
}
func (w *weightedRankedList) Weight() float64 {
return w.weight
}
type linearWeightedSelector struct {
libkb.Contextified
lists []*weightedRankedList
}
func newLinearWeightedSelector(g *libkb.GlobalContext, lists []*weightedRankedList) *linearWeightedSelector {
return &linearWeightedSelector{
Contextified: libkb.NewContextified(g),
lists: lists,
}
}
func (w *linearWeightedSelector) getAllUIDs() (res []keybase1.UID) {
m := make(map[string]bool)
for _, list := range w.lists {
uids := list.UIDs()
for _, uid := range uids {
m[uid.String()] = true
}
}
for uid := range m {
res = append(res, keybase1.UID(uid))
}
return res
}
func (w *linearWeightedSelector) Select(maxUsers int) (res []keybase1.UID) {
type userScore struct {
uid keybase1.UID
score float64
}
var scores []userScore
for _, uid := range w.getAllUIDs() {
total := 0.0
// Get score in each list to get a total score
for _, list := range w.lists {
total += list.Rank(uid) * list.Weight()
}
scores = append(scores, userScore{
uid: uid,
score: total,
})
}
sort.Slice(scores, func(i, j int) bool { return scores[i].score > scores[j].score })
for _, score := range scores {
res = append(res, score.uid)
if len(res) > maxUsers {
break
}
}
return res
}
type interestingPeopleFn func(uid keybase1.UID) ([]keybase1.UID, error)
type interestingPeopleSource struct {
fn interestingPeopleFn
weight float64
}
type interestingPeople struct {
libkb.Contextified
sources []interestingPeopleSource
}
func newInterestingPeople(g *libkb.GlobalContext) *interestingPeople {
return &interestingPeople{
Contextified: libkb.NewContextified(g),
}
}
func (i *interestingPeople) AddSource(fn interestingPeopleFn, weight float64) {
i.sources = append(i.sources, interestingPeopleSource{
fn: fn,
weight: weight,
})
}
func (i interestingPeople) Get(ctx context.Context, maxUsers int) ([]keybase1.UID, error) {
uid := i.G().Env.GetUID()
if uid.IsNil() {
return nil, libkb.LoginRequiredError{}
}
var weightedLists []*weightedRankedList
for _, source := range i.sources {
ppl, err := source.fn(uid)
if err != nil {
i.G().Log.Debug("interestingPeople: failed to get list from source: %s", err.Error())
return nil, err
}
weightedLists = append(weightedLists, newWeightedRankedList(newRankedList(ppl), source.weight))
}
return newLinearWeightedSelector(i.G(), weightedLists).Select(maxUsers), nil
}