-
Notifications
You must be signed in to change notification settings - Fork 31
/
query.go
256 lines (214 loc) · 7.55 KB
/
query.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package stream_chat
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/url"
"strings"
"time"
)
type QueryOption struct {
// https://getstream.io/chat/docs/#query_syntax
Filter map[string]interface{} `json:"filter_conditions,omitempty"`
Sort []*SortOption `json:"sort,omitempty"`
UserID string `json:"user_id,omitempty"`
Limit int `json:"limit,omitempty"` // pagination option: limit number of results
Offset int `json:"offset,omitempty"` // pagination option: offset to return items from
MessageLimit *int `json:"message_limit,omitempty"`
MemberLimit *int `json:"member_limit,omitempty"`
}
type SortOption struct {
Field string `json:"field"` // field name to sort by,from json tags(in camel case), for example created_at
Direction int `json:"direction"` // [-1, 1]
}
type queryRequest struct {
Watch bool `json:"watch"`
State bool `json:"state"`
Presence bool `json:"presence"`
UserID string `json:"user_id,omitempty"`
Limit int `json:"limit,omitempty"`
Offset int `json:"offset,omitempty"`
MemberLimit *int `json:"member_limit,omitempty"`
MessageLimit *int `json:"message_limit,omitempty"`
IncludeDeactivatedUsers bool `json:"include_deactivated_users,omitempty"`
FilterConditions map[string]interface{} `json:"filter_conditions,omitempty"`
Sort []*SortOption `json:"sort,omitempty"`
}
type QueryFlagReportsRequest struct {
FilterConditions map[string]interface{} `json:"filter_conditions,omitempty"`
Limit int `json:"limit,omitempty"`
Offset int `json:"offset,omitempty"`
}
type FlagReport struct {
ID string `json:"id"`
Message *Message `json:"message"`
FlagsCount int `json:"flags_count"`
MessageUserID string `json:"message_user_id"`
ChannelCid string `json:"channel_cid"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type QueryUsersOptions struct {
QueryOption
IncludeDeactivatedUsers bool `json:"include_deactivated_users"`
}
type QueryUsersResponse struct {
Users []*User `json:"users"`
Response
}
// QueryUsers returns list of users that match QueryUsersOptions.
// If any number of SortOption are set, result will be sorted by field and direction in the order of sort options.
func (c *Client) QueryUsers(ctx context.Context, q *QueryUsersOptions, sorters ...*SortOption) (*QueryUsersResponse, error) {
qp := queryRequest{
FilterConditions: q.Filter,
Limit: q.Limit,
Offset: q.Offset,
IncludeDeactivatedUsers: q.IncludeDeactivatedUsers,
Sort: sorters,
}
data, err := json.Marshal(&qp)
if err != nil {
return nil, err
}
values := url.Values{}
values.Set("payload", string(data))
var resp QueryUsersResponse
err = c.makeRequest(ctx, http.MethodGet, "users", values, nil, &resp)
return &resp, err
}
type queryChannelResponse struct {
Channels []queryChannelResponseData `json:"channels"`
Response
}
type queryChannelResponseData struct {
Channel *Channel `json:"channel"`
Messages []*Message `json:"messages"`
Read []*ChannelRead `json:"read"`
Members []*ChannelMember `json:"members"`
}
type QueryChannelsResponse struct {
Channels []*Channel
Response
}
// QueryChannels returns list of channels with members and messages, that match QueryOption.
// If any number of SortOption are set, result will be sorted by field and direction in oder of sort options.
func (c *Client) QueryChannels(ctx context.Context, q *QueryOption, sort ...*SortOption) (*QueryChannelsResponse, error) {
qp := queryRequest{
State: true,
FilterConditions: q.Filter,
Sort: sort,
UserID: q.UserID,
Limit: q.Limit,
Offset: q.Offset,
MemberLimit: q.MemberLimit,
MessageLimit: q.MessageLimit,
}
var resp queryChannelResponse
if err := c.makeRequest(ctx, http.MethodPost, "channels", nil, qp, &resp); err != nil {
return nil, err
}
result := make([]*Channel, len(resp.Channels))
for i, data := range resp.Channels {
result[i] = data.Channel
result[i].Members = data.Members
result[i].Messages = data.Messages
result[i].Read = data.Read
result[i].client = c
}
return &QueryChannelsResponse{Channels: result, Response: resp.Response}, nil
}
type SearchRequest struct {
// Required
Query string `json:"query"`
Filters map[string]interface{} `json:"filter_conditions"`
MessageFilters map[string]interface{} `json:"message_filter_conditions"`
// Pagination, optional
Limit int `json:"limit,omitempty"`
Offset int `json:"offset,omitempty"`
Next string `json:"next,omitempty"`
// Sort, optional
Sort []SortOption `json:"sort,omitempty"`
}
type SearchFullResponse struct {
Results []SearchMessageResponse `json:"results"`
Next string `json:"next,omitempty"`
Previous string `json:"previous,omitempty"`
Response
}
type SearchMessageResponse struct {
Message *Message `json:"message"`
}
type SearchResponse struct {
Messages []*Message
Response
}
// Search returns messages matching for given keyword.
func (c *Client) Search(ctx context.Context, request SearchRequest) (*SearchResponse, error) {
result, err := c.SearchWithFullResponse(ctx, request)
if err != nil {
return nil, err
}
messages := make([]*Message, 0, len(result.Results))
for _, res := range result.Results {
messages = append(messages, res.Message)
}
resp := SearchResponse{
Messages: messages,
Response: result.Response,
}
return &resp, nil
}
// SearchWithFullResponse performs a search and returns the full results.
func (c *Client) SearchWithFullResponse(ctx context.Context, request SearchRequest) (*SearchFullResponse, error) {
if request.Offset != 0 {
if len(request.Sort) > 0 || request.Next != "" {
return nil, errors.New("cannot use Offset with Next or Sort parameters")
}
}
if request.Query != "" && len(request.MessageFilters) != 0 {
return nil, errors.New("can only specify Query or MessageFilters, not both")
}
var buf strings.Builder
if err := json.NewEncoder(&buf).Encode(request); err != nil {
return nil, err
}
values := url.Values{}
values.Set("payload", buf.String())
var result SearchFullResponse
if err := c.makeRequest(ctx, http.MethodGet, "search", values, nil, &result); err != nil {
return nil, err
}
return &result, nil
}
type QueryMessageFlagsResponse struct {
Flags []*MessageFlag `json:"flags"`
Response
}
// QueryMessageFlags returns list of message flags that match QueryOption.
func (c *Client) QueryMessageFlags(ctx context.Context, q *QueryOption) (*QueryMessageFlagsResponse, error) {
qp := queryRequest{
FilterConditions: q.Filter,
Limit: q.Limit,
Offset: q.Offset,
}
data, err := json.Marshal(&qp)
if err != nil {
return nil, err
}
values := url.Values{}
values.Set("payload", string(data))
var resp QueryMessageFlagsResponse
err = c.makeRequest(ctx, http.MethodGet, "moderation/flags/message", values, nil, &resp)
return &resp, err
}
type QueryFlagReportsResponse struct {
Response
FlagReports []*FlagReport `json:"flag_reports"`
}
// QueryFlagReports returns list of flag reports that match the filter.
func (c *Client) QueryFlagReports(ctx context.Context, q *QueryFlagReportsRequest) (*QueryFlagReportsResponse, error) {
resp := &QueryFlagReportsResponse{}
err := c.makeRequest(ctx, http.MethodPost, "moderation/reports", nil, q, &resp)
return resp, err
}