1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16
17 package index
18
19 import (
20 "net/url"
21 "time"
22
23 "perkeep.org/pkg/schema"
24 "perkeep.org/pkg/types/camtypes"
25 )
26
27 var urle = url.QueryEscape
28
29 func urld(s string) string {
30 d, _ := url.QueryUnescape(s)
31 return d
32 }
33
34 type dupSkipper struct {
35 m map[string]bool
36 }
37
38
39 func (s *dupSkipper) Dup(v string) bool {
40 if s.m == nil {
41 s.m = make(map[string]bool)
42 }
43 if s.m[v] {
44 return true
45 }
46 s.m[v] = true
47 return false
48 }
49
50
51
52
53 func claimPtrsAttrValue(claims []*camtypes.Claim, attr string, at time.Time, signerFilter SignerRefSet) string {
54 return claimsIntfAttrValue(claimPtrSlice(claims), attr, at, signerFilter)
55 }
56
57 type claimsIntf interface {
58 Len() int
59 Claim(i int) *camtypes.Claim
60 }
61
62 type claimSlice []camtypes.Claim
63
64 func (s claimSlice) Len() int { return len(s) }
65 func (s claimSlice) Claim(i int) *camtypes.Claim { return &s[i] }
66
67 type claimPtrSlice []*camtypes.Claim
68
69 func (s claimPtrSlice) Len() int { return len(s) }
70 func (s claimPtrSlice) Claim(i int) *camtypes.Claim { return s[i] }
71
72
73
74
75
76 func claimsIntfAttrValue(claims claimsIntf, attr string, at time.Time, signerFilter SignerRefSet) string {
77 if claims == nil {
78 panic("nil claims argument in claimsIntfAttrValue")
79 }
80
81 if at.IsZero() {
82 at = time.Now()
83 }
84
85
86
87
88 var buf [8]string
89 v := buf[:][:0]
90
91 for i := 0; i < claims.Len(); i++ {
92 cl := claims.Claim(i)
93 if cl.Attr != attr || cl.Date.After(at) {
94 continue
95 }
96
97 if len(signerFilter) > 0 {
98 if !signerFilter.blobMatches(cl.Signer) {
99 continue
100 }
101 }
102
103 switch cl.Type {
104 case string(schema.DelAttributeClaim):
105 if cl.Value == "" {
106 v = v[:0]
107 } else {
108 i := 0
109 for _, w := range v {
110 if w != cl.Value {
111 v[i] = w
112 i++
113 }
114 }
115 v = v[:i]
116 }
117 case string(schema.SetAttributeClaim):
118 v = append(v[:0], cl.Value)
119 case string(schema.AddAttributeClaim):
120 v = append(v, cl.Value)
121 }
122 }
123 if len(v) != 0 {
124 return v[0]
125 }
126 return ""
127 }