forked from GetStream/stream-chat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
158 lines (127 loc) · 4.36 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
package stream_chat // nolint: golint
import (
"encoding/json"
"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"`
// Pagination, optional
Limit int `json:"limit,omitempty"`
Offset int `json:"offset,omitempty"`
}
type searchResponse struct {
Results []searchMessageResponse `json:"results"`
}
type searchMessageResponse struct {
Message *Message `json:"message"`
}
// Search returns channels matching for given keyword.
func (c *Client) Search(request SearchRequest) ([]*Message, error) {
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
}
messages := make([]*Message, 0, len(result.Results))
for _, res := range result.Results {
messages = append(messages, res.Message)
}
return messages, nil
}