-
Notifications
You must be signed in to change notification settings - Fork 31
/
ban.go
169 lines (140 loc) · 4.7 KB
/
ban.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
package stream_chat
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/url"
"time"
)
// BanUser bans targetID.
func (c *Client) BanUser(ctx context.Context, targetID, bannedBy string, options ...BanOption) (*Response, error) {
switch {
case targetID == "":
return nil, errors.New("targetID should not be empty")
case bannedBy == "":
return nil, errors.New("bannedBy should not be empty")
}
opts := &banOptions{
TargetUserID: targetID,
BannedBy: bannedBy,
}
for _, fn := range options {
fn(opts)
}
var resp Response
err := c.makeRequest(ctx, http.MethodPost, "moderation/ban", nil, opts, &resp)
return &resp, err
}
// UnBanUser removes the ban for targetID.
func (c *Client) UnBanUser(ctx context.Context, targetID string) (*Response, error) {
if targetID == "" {
return nil, errors.New("targetID should not be empty")
}
params := url.Values{}
params.Set("target_user_id", targetID)
var resp Response
err := c.makeRequest(ctx, http.MethodDelete, "moderation/ban", params, nil, &resp)
return &resp, err
}
// ShadowBan shadow bans targetID.
func (c *Client) ShadowBan(ctx context.Context, targetID, bannedByID string, options ...BanOption) (*Response, error) {
options = append(options, banWithShadow())
return c.BanUser(ctx, targetID, bannedByID, options...)
}
// BanUser bans targetID on the channel ch.
func (ch *Channel) BanUser(ctx context.Context, targetID, bannedBy string, options ...BanOption) (*Response, error) {
options = append(options, banFromChannel(ch.Type, ch.ID))
return ch.client.BanUser(ctx, targetID, bannedBy, options...)
}
// UnBanUser removes the ban for targetID from the channel ch.
func (ch *Channel) UnBanUser(ctx context.Context, targetID string) (*Response, error) {
if targetID == "" {
return nil, errors.New("targetID should not be empty")
}
params := url.Values{}
params.Set("target_user_id", targetID)
params.Set("id", ch.ID)
params.Set("type", ch.Type)
var resp Response
err := ch.client.makeRequest(ctx, http.MethodDelete, "moderation/ban", params, nil, &resp)
return &resp, err
}
// ShadowBan shadow bans targetID on the channel ch.
func (ch *Channel) ShadowBan(ctx context.Context, targetID, bannedByID string, options ...BanOption) (*Response, error) {
options = append(options, banWithShadow(), banFromChannel(ch.Type, ch.ID))
return ch.client.ShadowBan(ctx, targetID, bannedByID, options...)
}
type QueryBannedUsersOptions struct {
*QueryOption
}
type QueryBannedUsersResponse struct {
Bans []*Ban `json:"bans"`
Response
}
type Ban struct {
Channel *Channel `json:"channel,omitempty"`
User *User `json:"user"`
Expires *time.Time `json:"expires,omitempty"`
Reason string `json:"reason,omitempty"`
Shadow bool `json:"shadow,omitempty"`
BannedBy *User `json:"banned_by,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
// QueryBannedUsers filters and returns a list of banned users.
// Banned users can be retrieved in different ways:
// 1) Using the dedicated query bans endpoint
// 2) User Search: you can add the banned:true condition to your search. Please note that
// this will only return users that were banned at the app-level and not the ones
// that were banned only on channels.
func (c *Client) QueryBannedUsers(ctx context.Context, q *QueryBannedUsersOptions, sorters ...*SortOption) (*QueryBannedUsersResponse, error) {
qp := queryRequest{
FilterConditions: q.Filter,
Limit: q.Limit,
Offset: q.Offset,
Sort: sorters,
}
data, err := json.Marshal(&qp)
if err != nil {
return nil, err
}
values := url.Values{}
values.Set("payload", string(data))
var resp QueryBannedUsersResponse
err = c.makeRequest(ctx, http.MethodGet, "query_banned_users", values, nil, &resp)
return &resp, err
}
type banOptions struct {
Reason string `json:"reason,omitempty"`
Expiration int `json:"timeout,omitempty"`
TargetUserID string `json:"target_user_id"`
BannedBy string `json:"user_id"`
Shadow bool `json:"shadow"`
// ID and Type of the channel when acting on a channel member.
ID string `json:"id"`
Type string `json:"type"`
}
type BanOption func(*banOptions)
func BanWithReason(reason string) func(*banOptions) {
return func(opt *banOptions) {
opt.Reason = reason
}
}
// BanWithExpiration set when the ban will expire. Should be in minutes.
// eg. to ban during one hour: BanWithExpiration(60).
func BanWithExpiration(expiration int) func(*banOptions) {
return func(opt *banOptions) {
opt.Expiration = expiration
}
}
func banWithShadow() func(*banOptions) {
return func(opt *banOptions) {
opt.Shadow = true
}
}
func banFromChannel(_type, id string) func(*banOptions) {
return func(opt *banOptions) {
opt.Type = _type
opt.ID = id
}
}