/
push_filter.go
113 lines (86 loc) · 2.7 KB
/
push_filter.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
// Copyright 2019 Aporeto Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package elemental
import (
"fmt"
"net/url"
)
// A PushFilter represents an abstract filter for filtering out push notifications.
type PushFilter struct {
Identities map[string][]EventType `msgpack:"identities" json:"identities"`
Params url.Values `msgpack:"parameters" json:"parameters"`
}
// NewPushFilter returns a new PushFilter.
func NewPushFilter() *PushFilter {
return &PushFilter{
Identities: map[string][]EventType{},
}
}
// SetParameter sets the values of the parameter with the given key.
func (f *PushFilter) SetParameter(key string, values ...string) {
if f.Params == nil {
f.Params = url.Values{}
}
f.Params[key] = values
}
// Parameters returns a copy of all the parameters.
func (f *PushFilter) Parameters() url.Values {
if f.Params == nil {
return nil
}
out := url.Values{}
for k, v := range f.Params {
out[k] = v
}
return out
}
// FilterIdentity adds the given identity for the given eventTypes in the PushFilter.
func (f *PushFilter) FilterIdentity(identityName string, eventTypes ...EventType) {
f.Identities[identityName] = eventTypes
}
// IsFilteredOut returns true if the given Identity is not part of the PushFilter.
func (f *PushFilter) IsFilteredOut(identityName string, eventType EventType) bool {
// if the identities list is empty, we filter nothing.
if len(f.Identities) == 0 {
return false
}
// If it contains something, but not the identity, we filter out.
types, ok := f.Identities[identityName]
if !ok {
return true
}
// If there is no event types defined we don't filter
if len(types) == 0 {
return false
}
// If if there are some event types defined, we don't filter out
// if the current event type is in the list.
for _, t := range types {
if t == eventType {
return false
}
}
return true
}
// Duplicate duplicates the PushFilter.
func (f *PushFilter) Duplicate() *PushFilter {
nf := NewPushFilter()
for id, types := range f.Identities {
nf.FilterIdentity(id, types...)
}
for k, v := range f.Params {
nf.SetParameter(k, v...)
}
return nf
}
func (f *PushFilter) String() string {
return fmt.Sprintf("<pushfilter identities:%s>", f.Identities)
}