-
Notifications
You must be signed in to change notification settings - Fork 137
/
trigger_event.go
211 lines (190 loc) · 5.11 KB
/
trigger_event.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package job
import (
"errors"
"strings"
"github.com/cozy/cozy-stack/model/permission"
"github.com/cozy/cozy-stack/model/vfs"
"github.com/cozy/cozy-stack/pkg/consts"
"github.com/cozy/cozy-stack/pkg/couchdb"
"github.com/cozy/cozy-stack/pkg/logger"
"github.com/cozy/cozy-stack/pkg/realtime"
)
// EventTrigger implements Trigger for realtime triggered events
type EventTrigger struct {
*TriggerInfos
unscheduled chan struct{}
mask []permission.Rule
}
// NewEventTrigger returns a new instance of EventTrigger given the specified
// options.
func NewEventTrigger(infos *TriggerInfos) (*EventTrigger, error) {
args := strings.Split(infos.Arguments, " ")
rules := make([]permission.Rule, len(args))
for i, arg := range args {
rule, err := permission.UnmarshalRuleString(arg)
if err != nil {
return nil, err
}
rules[i] = rule
}
return &EventTrigger{
TriggerInfos: infos,
unscheduled: make(chan struct{}),
mask: rules,
}, nil
}
// Type implements the Type method of the Trigger interface.
func (t *EventTrigger) Type() string {
return t.TriggerInfos.Type
}
// Schedule implements the Schedule method of the Trigger interface.
func (t *EventTrigger) Schedule() <-chan *JobRequest {
ch := make(chan *JobRequest)
go func() {
sub := realtime.GetHub().Subscriber(t)
for _, m := range t.mask {
sub.Subscribe(m.Type)
}
defer func() {
sub.Close()
close(ch)
}()
for {
select {
case e := <-sub.Channel:
found := false
for _, m := range t.mask {
if eventMatchRule(e, &m) {
found = true
break
}
}
if found {
if evt, err := t.Infos().JobRequestWithEvent(e); err == nil {
ch <- evt
}
}
case <-t.unscheduled:
return
}
}
}()
return ch
}
// Unschedule implements the Unschedule method of the Trigger interface.
func (t *EventTrigger) Unschedule() {
close(t.unscheduled)
}
// Infos implements the Infos method of the Trigger interface.
func (t *EventTrigger) Infos() *TriggerInfos {
return t.TriggerInfos
}
// CombineRequest implements the CombineRequest method of the Trigger interface.
func (t *EventTrigger) CombineRequest() string {
return suppressPayload
}
func eventMatchRule(e *realtime.Event, rule *permission.Rule) bool {
if e.Doc.DocType() != rule.Type {
return false
}
if e.Verb == realtime.EventNotify {
return false
}
if !rule.Verbs.Contains(permission.Verb(e.Verb)) {
return false
}
if len(rule.Values) == 0 {
return true
}
if rule.Selector == "" {
if rule.ValuesContain(e.Doc.ID()) {
return true
}
if e.Doc.DocType() == consts.Files {
for _, value := range rule.Values {
var dir vfs.DirDoc
if err := couchdb.GetDoc(e, consts.Files, value, &dir); err != nil {
logger.WithDomain(e.Domain).
WithNamespace("event-trigger").
Debugf("Cannot find io.cozy.files %s for trigger rule: %s", value, err)
continue
}
// The trigger value was for a file, not a dir, and it should
// match only on ID, not on path.
if dir.Type != consts.DirType {
continue
}
if testPath(&dir, e.Doc) {
return true
}
if e.OldDoc != nil {
if testPath(&dir, e.OldDoc) {
return true
}
}
}
}
return false
}
if len(rule.Values) == 1 && rule.Values[0] == "!=" {
// Selector for a changed value
if e.Verb != realtime.EventUpdate {
return true // We consider that the value has changed on create and delete
}
if e.OldDoc == nil {
return false
}
if doc, ok := e.Doc.(permission.Fetcher); ok {
if old, ok := e.OldDoc.(permission.Fetcher); ok {
return rule.ValuesChanged(old, doc)
}
}
} else {
// Selector with normal values
if v, ok := e.Doc.(permission.Fetcher); ok {
if rule.ValuesMatch(v) {
return true
}
// Particular case where the new doc is not valid but the old one was.
if e.OldDoc != nil {
if vOld, okOld := e.OldDoc.(permission.Fetcher); okOld {
return rule.ValuesMatch(vOld)
}
}
}
}
return false
}
// DumpFilePather is a struct made for calling the Path method of a FileDoc and
// relying on the cached fullpath of this document (not trying to rebuild it)
type DumpFilePather struct{}
// FilePath only returns an error saying to not call this method
func (d DumpFilePather) FilePath(doc *vfs.FileDoc) (string, error) {
logger.WithNamespace("event-trigger").Warn("FilePath method of DumpFilePather has been called")
return "", errors.New("DumpFilePather FilePath should not have been called")
}
var dumpFilePather = DumpFilePather{}
func testPath(dir *vfs.DirDoc, doc realtime.Doc) bool {
if d, ok := doc.(*vfs.DirDoc); ok {
return strings.HasPrefix(d.Fullpath, dir.Fullpath+"/")
}
if f, ok := doc.(*vfs.FileDoc); ok {
if f.Trashed {
// XXX When a new file is uploaded, a document may be created in
// couchdb with trashed: true. We should ignore it.
if strings.HasPrefix(f.DocRev, "1-") {
return false
}
if f.RestorePath != "" {
return strings.HasPrefix(f.RestorePath, dir.Fullpath+"/")
}
}
p, err := f.Path(dumpFilePather)
if err != nil {
return false
}
return strings.HasPrefix(p, dir.Fullpath+"/")
}
return false
}
var _ Trigger = &EventTrigger{}