-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope.go
99 lines (83 loc) · 2.18 KB
/
scope.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
package dazeus
import "errors"
// Scope for which a request is sent
type Scope struct {
Network, Receiver, Sender *string
}
// NewUniversalScope returns a universal scope
func NewUniversalScope() Scope {
return Scope{
Network: nil,
Receiver: nil,
Sender: nil,
}
}
// NewNetworkScope returns a scope limited to a network
func NewNetworkScope(network string) Scope {
return Scope{
Network: &network,
Receiver: nil,
Sender: nil,
}
}
// NewReceiverScope returns a scope limited to a network and a specific receiver (channel) in that network
func NewReceiverScope(network string, receiver string) Scope {
return Scope{
Network: &network,
Receiver: &receiver,
Sender: nil,
}
}
// NewSenderScope returns a scope limited to a network and a specific sender (user) in that network
func NewSenderScope(network string, sender string) Scope {
return Scope{
Network: &network,
Receiver: nil,
Sender: &sender,
}
}
// NewScope returns a scope limited on a specific sender and receiver combination in a specific network
func NewScope(network string, receiver string, sender string) Scope {
return Scope{
Network: &network,
Receiver: &receiver,
Sender: &sender,
}
}
// IsAll indicates if this scope is global
func (scope Scope) IsAll() bool {
return scope.Network == nil && scope.Receiver == nil && scope.Sender == nil
}
// ToSlice returns a slice for usage with permissions and properties
func (scope Scope) ToSlice() []string {
s := make([]string, 0)
if scope.Network != nil {
s = append(s, *scope.Network)
if scope.Receiver != nil {
s = append(s, *scope.Receiver)
if scope.Sender != nil {
s = append(s, *scope.Sender)
}
}
}
return s
}
// ToCommandSlice returns a slice for usage when sending with a command subscription
func (scope Scope) ToCommandSlice() ([]interface{}, error) {
s := make([]interface{}, 0)
if scope.Receiver != nil && scope.Sender != nil {
return s, errors.New("")
}
if scope.Network != nil {
s = append(s, *scope.Network)
if scope.Receiver != nil {
s = append(s, false)
s = append(s, *scope.Receiver)
}
if scope.Sender != nil {
s = append(s, false)
s = append(s, *scope.Sender)
}
}
return s, nil
}