Debian Codegraph @

search.go

raw symbols SCIP
1 package slack
2
3 import (
4 "context"
5 "net/url"
6 "strconv"
7 )
8
9 const (
10 DEFAULT_SEARCH_SORT = "score"
11 DEFAULT_SEARCH_SORT_DIR = "desc"
12 DEFAULT_SEARCH_HIGHLIGHT = false
13 DEFAULT_SEARCH_COUNT = 20
14 DEFAULT_SEARCH_PAGE = 1
15 )
16
17 type SearchParameters struct {
18 Sort string
19 SortDirection string
20 Highlight bool
21 Count int
22 Page int
23 }
24
25 type CtxChannel struct {
26 ID string `json:"id"`
27 Name string `json:"name"`
28 IsExtShared bool `json:"is_ext_shared"`
29 IsMPIM bool `json:"is_mpim"`
30 ISOrgShared bool `json:"is_org_shared"`
31 IsPendingExtShared bool `json:"is_pending_ext_shared"`
32 IsPrivate bool `json:"is_private"`
33 IsShared bool `json:"is_shared"`
34 }
35
36 type CtxMessage struct {
37 User string `json:"user"`
38 Username string `json:"username"`
39 Text string `json:"text"`
40 Timestamp string `json:"ts"`
41 Type string `json:"type"`
42 }
43
44 type SearchMessage struct {
45 Type string `json:"type"`
46 Channel CtxChannel `json:"channel"`
47 User string `json:"user"`
48 Username string `json:"username"`
49 Timestamp string `json:"ts"`
50 Blocks Blocks `json:"blocks,omitempty"`
51 Text string `json:"text"`
52 Permalink string `json:"permalink"`
53 Attachments []Attachment `json:"attachments"`
54 Previous CtxMessage `json:"previous"`
55 Previous2 CtxMessage `json:"previous_2"`
56 Next CtxMessage `json:"next"`
57 Next2 CtxMessage `json:"next_2"`
58 }
59
60 type SearchMessages struct {
61 Matches []SearchMessage `json:"matches"`
62 Paging `json:"paging"`
63 Pagination `json:"pagination"`
64 Total int `json:"total"`
65 }
66
67 type SearchFiles struct {
68 Matches []File `json:"matches"`
69 Paging `json:"paging"`
70 Pagination `json:"pagination"`
71 Total int `json:"total"`
72 }
73
74 type searchResponseFull struct {
75 Query string `json:"query"`
76 SearchMessages `json:"messages"`
77 SearchFiles `json:"files"`
78 SlackResponse
79 }
80
81 func NewSearchParameters() SearchParameters {
82 return SearchParameters{
83 Sort: DEFAULT_SEARCH_SORT,
84 SortDirection: DEFAULT_SEARCH_SORT_DIR,
85 Highlight: DEFAULT_SEARCH_HIGHLIGHT,
86 Count: DEFAULT_SEARCH_COUNT,
87 Page: DEFAULT_SEARCH_PAGE,
88 }
89 }
90
91 func (api *Client) _search(ctx context.Context, path, query string, params SearchParameters, files, messages bool) (response *searchResponseFull, error error) {
92 values := url.Values{
93 "token": {api.token},
94 "query": {query},
95 }
96 if params.Sort != DEFAULT_SEARCH_SORT {
97 values.Add("sort", params.Sort)
98 }
99 if params.SortDirection != DEFAULT_SEARCH_SORT_DIR {
100 values.Add("sort_dir", params.SortDirection)
101 }
102 if params.Highlight != DEFAULT_SEARCH_HIGHLIGHT {
103 values.Add("highlight", strconv.Itoa(1))
104 }
105 if params.Count != DEFAULT_SEARCH_COUNT {
106 values.Add("count", strconv.Itoa(params.Count))
107 }
108 if params.Page != DEFAULT_SEARCH_PAGE {
109 values.Add("page", strconv.Itoa(params.Page))
110 }
111
112 response = &searchResponseFull{}
113 err := api.postMethod(ctx, path, values, response)
114 if err != nil {
115 return nil, err
116 }
117
118 return response, response.Err()
119
120 }
121
122 func (api *Client) Search(query string, params SearchParameters) (*SearchMessages, *SearchFiles, error) {
123 return api.SearchContext(context.Background(), query, params)
124 }
125
126 func (api *Client) SearchContext(ctx context.Context, query string, params SearchParameters) (*SearchMessages, *SearchFiles, error) {
127 response, err := api._search(ctx, "search.all", query, params, true, true)
128 if err != nil {
129 return nil, nil, err
130 }
131 return &response.SearchMessages, &response.SearchFiles, nil
132 }
133
134 func (api *Client) SearchFiles(query string, params SearchParameters) (*SearchFiles, error) {
135 return api.SearchFilesContext(context.Background(), query, params)
136 }
137
138 func (api *Client) SearchFilesContext(ctx context.Context, query string, params SearchParameters) (*SearchFiles, error) {
139 response, err := api._search(ctx, "search.files", query, params, true, false)
140 if err != nil {
141 return nil, err
142 }
143 return &response.SearchFiles, nil
144 }
145
146 func (api *Client) SearchMessages(query string, params SearchParameters) (*SearchMessages, error) {
147 return api.SearchMessagesContext(context.Background(), query, params)
148 }
149
150 func (api *Client) SearchMessagesContext(ctx context.Context, query string, params SearchParameters) (*SearchMessages, error) {
151 response, err := api._search(ctx, "search.messages", query, params, false, true)
152 if err != nil {
153 return nil, err
154 }
155 return &response.SearchMessages, nil
156 }