-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathrepository_cache.go
More file actions
144 lines (128 loc) · 3.47 KB
/
repository_cache.go
File metadata and controls
144 lines (128 loc) · 3.47 KB
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
package brutalinks
import (
"path/filepath"
vocab "github.com/go-ap/activitypub"
"github.com/go-ap/cache"
)
func caches(enabled bool) *cc {
c := cache.New(enabled)
return &cc{c}
}
type cc struct {
c cache.CanStore
}
func accum(toRemove *vocab.IRIs, iri vocab.IRI, col vocab.CollectionPath) {
if repl := col.IRI(iri); !toRemove.Contains(repl) {
*toRemove = append(*toRemove, repl)
}
}
func accumItem(it vocab.Item, toRemove *vocab.IRIs, col vocab.CollectionPath) {
if vocab.IsNil(it) {
return
}
if vocab.IsItemCollection(it) {
vocab.OnItemCollection(it, func(c *vocab.ItemCollection) error {
for _, ob := range c.Collection() {
accum(toRemove, ob.GetLink(), col)
}
return nil
})
} else {
accum(toRemove, it.GetLink(), col)
}
}
func (c *cc) removeRelated(items ...vocab.Item) {
toRemove := make(vocab.IRIs, 0)
for _, it := range items {
if vocab.IsNil(it) {
continue
}
if vocab.IsObject(it) || vocab.IsItemCollection(it) && len(it.GetLink()) > 0 {
typ := it.GetType()
if vocab.ActivityTypes.Match(typ) || vocab.IntransitiveActivityTypes.Match(typ) {
vocab.OnActivity(it, c.accumActivityIRIs(&toRemove))
} else {
vocab.OnObject(it, c.accumObjectIRIs(&toRemove))
}
}
if aIRI := it.GetLink(); len(aIRI) > 0 && !toRemove.Contains(aIRI) {
toRemove = append(toRemove, aIRI)
}
}
c.remove(toRemove...)
}
func (c *cc) accumRecipientIRIs(r vocab.Item, toRemove *vocab.IRIs) {
iri := r.GetLink()
if iri.Equals(vocab.PublicNS, false) {
return
}
_, col := vocab.Split(iri)
toDeref := vocab.CollectionPaths{vocab.Followers, vocab.Following}
if toDeref.Contains(col) {
if iris := c.get(iri); !vocab.IsNil(iris) {
vocab.OnCollectionIntf(iris, func(col vocab.CollectionInterface) error {
for _, it := range col.Collection() {
accumItem(it.GetLink(), toRemove, vocab.Outbox)
}
return nil
})
}
return
}
toAppend := vocab.CollectionPaths{vocab.Inbox, vocab.Outbox}
if toAppend.Contains(col) {
if toRemove.Contains(iri) {
*toRemove = append(*toRemove, iri)
}
return
}
accumItem(r, toRemove, vocab.Inbox)
}
func (c *cc) accumActivityIRIs(toRemove *vocab.IRIs) func(activity *vocab.Activity) error {
return func(a *vocab.Activity) error {
for _, r := range a.Recipients() {
c.accumRecipientIRIs(r, toRemove)
}
if destCol := vocab.Outbox.IRI(a.Actor); !toRemove.Contains(destCol) {
*toRemove = append(*toRemove, destCol)
}
typ := a.Type
withSideEffects := vocab.ActivityVocabularyTypes{vocab.UpdateType, vocab.UndoType, vocab.DeleteType}
if withSideEffects.Match(typ) {
base := filepath.Dir(a.Object.GetLink().String())
*toRemove = append(*toRemove, vocab.IRI(base), a.Object.GetLink())
}
return vocab.OnObject(a.Object, c.accumObjectIRIs(toRemove))
}
}
func (c *cc) accumObjectIRIs(toRemove *vocab.IRIs) func(*vocab.Object) error {
return func(ob *vocab.Object) error {
if ob == nil {
return nil
}
if !ob.IsObject() {
return nil
}
if obIRI := ob.GetLink(); len(obIRI) > 0 && !toRemove.Contains(obIRI) {
*toRemove = append(*toRemove, obIRI)
}
for _, r := range ob.Recipients() {
c.accumRecipientIRIs(r, toRemove)
}
accumItem(ob.InReplyTo, toRemove, vocab.Replies)
accumItem(ob.AttributedTo, toRemove, vocab.Outbox)
return nil
}
}
func (c *cc) remove(iris ...vocab.IRI) {
if len(iris) == 0 {
return
}
c.c.Delete(iris...)
}
func (c *cc) add(iri vocab.IRI, it vocab.Item) {
c.c.Store(iri, it)
}
func (c *cc) get(iri vocab.IRI) vocab.Item {
return c.c.Load(iri)
}