-
Notifications
You must be signed in to change notification settings - Fork 31
/
channel_type.go
156 lines (116 loc) · 3.43 KB
/
channel_type.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
package stream_chat // nolint: golint
import (
"errors"
"net/http"
"net/url"
"path"
"time"
)
const (
AutoModDisabled modType = "disabled"
AutoModSimple modType = "simple"
AutoModAI modType = "AI"
ModBehaviourFlag modBehaviour = "flag"
ModBehaviourBlock modBehaviour = "block"
defaultMessageLength = 5000
MessageRetentionForever = "infinite"
)
var defaultChannelTypes = []string{
"messaging",
"team",
"livestream",
"commerce",
"gaming",
}
type (
modType string
modBehaviour string
)
type Permission struct {
Name string `json:"name"` // required
Action string `json:"action"` // one of: Deny Allow
Resources []string `json:"resources"` // required
Roles []string `json:"roles"`
Owner bool `json:"owner"`
Priority int `json:"priority"` // required
}
type ChannelType struct {
ChannelConfig
Commands []*Command `json:"commands"`
Permissions []*Permission `json:"permissions"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func (ct *ChannelType) toRequest() channelTypeRequest {
req := channelTypeRequest{ChannelType: ct}
if len(req.Commands) == 0 {
req.Commands = []string{"all"}
}
return req
}
// NewChannelType returns initialized ChannelType with default values.
func NewChannelType(name string) *ChannelType {
ct := &ChannelType{ChannelConfig: DefaultChannelConfig}
ct.Name = name
return ct
}
type channelTypeRequest struct {
*ChannelType
Commands []string `json:"commands"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
type channelTypeResponse struct {
ChannelTypes map[string]*ChannelType `json:"channel_types"`
}
// CreateChannelType adds new channel type.
func (c *Client) CreateChannelType(chType *ChannelType) (*ChannelType, error) {
if chType == nil {
return nil, errors.New("channel type is nil")
}
var resp channelTypeRequest
err := c.makeRequest(http.MethodPost, "channeltypes", nil, chType.toRequest(), &resp)
if err != nil {
return nil, err
}
if resp.ChannelType == nil {
return nil, errors.New("unexpected error: channel type response is nil")
}
for _, cmd := range resp.Commands {
resp.ChannelType.Commands = append(resp.ChannelType.Commands, &Command{Name: cmd})
}
return resp.ChannelType, nil
}
// GetChannelType returns information about channel type.
func (c *Client) GetChannelType(chanType string) (*ChannelType, error) {
if chanType == "" {
return nil, errors.New("channel type is empty")
}
p := path.Join("channeltypes", url.PathEscape(chanType))
ct := ChannelType{}
err := c.makeRequest(http.MethodGet, p, nil, nil, &ct)
return &ct, err
}
// ListChannelTypes returns all channel types.
func (c *Client) ListChannelTypes() (map[string]*ChannelType, error) {
var resp channelTypeResponse
err := c.makeRequest(http.MethodGet, "channeltypes", nil, nil, &resp)
return resp.ChannelTypes, err
}
func (c *Client) UpdateChannelType(name string, options map[string]interface{}) error {
switch {
case name == "":
return errors.New("channel type name is empty")
case len(options) == 0:
return errors.New("options are empty")
}
p := path.Join("channeltypes", url.PathEscape(name))
return c.makeRequest(http.MethodPut, p, nil, options, nil)
}
func (c *Client) DeleteChannelType(name string) error {
if name == "" {
return errors.New("channel type name is empty")
}
p := path.Join("channeltypes", url.PathEscape(name))
return c.makeRequest(http.MethodDelete, p, nil, nil, nil)
}