-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathchannel_type.go
182 lines (142 loc) · 4.32 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
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
package stream_chat
import (
"context"
"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 ChannelTypePermission 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"`
// Deprecated: Use Permissions V2 API instead,
// that can be found in permission_client.go.
// See https://getstream.io/chat/docs/go-golang/migrating_from_legacy/?language=go
Permissions []*ChannelTypePermission `json:"permissions"`
Grants map[string][]string `json:"grants"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func (ct *ChannelType) toRequest() channelTypeRequest {
req := channelTypeRequest{ChannelType: ct}
for _, cmd := range ct.Commands {
req.Commands = append(req.Commands, cmd.Name)
}
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 {
*ChannelType
Commands []string `json:"commands"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
Response
}
// CreateChannelType adds new channel type.
func (c *Client) CreateChannelType(ctx context.Context, chType *ChannelType) (*ChannelTypeResponse, error) {
if chType == nil {
return nil, errors.New("channel type is nil")
}
var resp ChannelTypeResponse
err := c.makeRequest(ctx, 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, nil
}
type GetChannelTypeResponse struct {
*ChannelType
Response
}
// GetChannelType returns information about channel type.
func (c *Client) GetChannelType(ctx context.Context, chanType string) (*GetChannelTypeResponse, error) {
if chanType == "" {
return nil, errors.New("channel type is empty")
}
p := path.Join("channeltypes", url.PathEscape(chanType))
var resp GetChannelTypeResponse
err := c.makeRequest(ctx, http.MethodGet, p, nil, nil, &resp)
return &resp, err
}
type ChannelTypesResponse struct {
ChannelTypes map[string]*ChannelType `json:"channel_types"`
Response
}
// ListChannelTypes returns all channel types.
func (c *Client) ListChannelTypes(ctx context.Context) (*ChannelTypesResponse, error) {
var resp ChannelTypesResponse
err := c.makeRequest(ctx, http.MethodGet, "channeltypes", nil, nil, &resp)
return &resp, err
}
// UpdateChannelType updates channel type.
func (c *Client) UpdateChannelType(ctx context.Context, name string, options map[string]interface{}) (*Response, error) {
switch {
case name == "":
return nil, errors.New("channel type name is empty")
case len(options) == 0:
return nil, errors.New("options are empty")
}
p := path.Join("channeltypes", url.PathEscape(name))
var resp Response
err := c.makeRequest(ctx, http.MethodPut, p, nil, options, &resp)
return &resp, err
}
// DeleteChannelType deletes channel type.
func (c *Client) DeleteChannelType(ctx context.Context, name string) (*Response, error) {
if name == "" {
return nil, errors.New("channel type name is empty")
}
p := path.Join("channeltypes", url.PathEscape(name))
var resp Response
err := c.makeRequest(ctx, http.MethodDelete, p, nil, nil, &resp)
return &resp, err
}