-
Notifications
You must be signed in to change notification settings - Fork 31
/
query.go
208 lines (170 loc) · 5.86 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
package stream_chat // nolint: golint
import (
"encoding/json"
"errors"
"net/http"
"net/url"
"strings"
)
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"`
FilterConditions map[string]interface{} `json:"filter_conditions,omitempty"`
Sort []*SortOption `json:"sort,omitempty"`
}
type queryUsersResponse struct {
Users []*User `json:"users"`
}
// QueryUsers returns list of users that match QueryOption.
// 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(q *QueryOption, sorters ...*SortOption) ([]*User, 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 := make(url.Values)
values.Set("payload", string(data))
var resp queryUsersResponse
err = c.makeRequest(http.MethodGet, "users", values, nil, &resp)
return resp.Users, err
}
type queryChannelResponse struct {
Channels []queryChannelResponseData `json:"channels"`
}
type queryChannelResponseData struct {
Channel *Channel `json:"channel"`
Messages []*Message `json:"messages"`
Read []*ChannelRead `json:"read"`
Members []*ChannelMember `json:"members"`
}
// 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(q *QueryOption, sort ...*SortOption) ([]*Channel, 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,
}
data, err := json.Marshal(&qp)
if err != nil {
return nil, err
}
values := make(url.Values)
values.Set("payload", string(data))
var resp queryChannelResponse
err = c.makeRequest(http.MethodGet, "channels", values, nil, &resp)
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 result, err
}
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 SearchResponse struct {
Results []SearchMessageResponse `json:"results"`
Next string `json:"next,omitempty"`
Previous string `json:"previous,omitempty"`
}
type SearchMessageResponse struct {
Message *Message `json:"message"`
}
// Search returns channels matching for given keyword.
func (c *Client) Search(request SearchRequest) ([]*Message, error) {
result, err := c.SearchWithFullResponse(request)
if err != nil {
return nil, err
}
messages := make([]*Message, 0, len(result.Results))
for _, res := range result.Results {
messages = append(messages, res.Message)
}
return messages, nil
}
// SearchWithFullResponse performs a search and returns the full results.
func (c *Client) SearchWithFullResponse(request SearchRequest) (*SearchResponse, 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 SearchResponse
if err := c.makeRequest(http.MethodGet, "search", values, nil, &result); err != nil {
return nil, err
}
return &result, nil
}
type queryMessageFlagsResponse struct {
Flags []*MessageFlag `json:"flags"`
}
// QueryMessageFlags returns list of message flags that match QueryOption.
func (c *Client) QueryMessageFlags(q *QueryOption) ([]*MessageFlag, error) {
qp := queryRequest{
FilterConditions: q.Filter,
Limit: q.Limit,
Offset: q.Offset,
}
data, err := json.Marshal(&qp)
if err != nil {
return nil, err
}
values := make(url.Values)
values.Set("payload", string(data))
var resp queryMessageFlagsResponse
err = c.makeRequest(http.MethodGet, "moderation/flags/message", values, nil, &resp)
return resp.Flags, err
}