matterbridge/vendor/github.com/nlopes/slack/search.go

157 lines
4.5 KiB
Go
Raw Normal View History

2016-09-05 14:34:37 +00:00
package slack
import (
2017-07-16 12:29:46 +00:00
"context"
2016-09-05 14:34:37 +00:00
"net/url"
"strconv"
)
const (
DEFAULT_SEARCH_SORT = "score"
DEFAULT_SEARCH_SORT_DIR = "desc"
DEFAULT_SEARCH_HIGHLIGHT = false
2018-08-09 22:38:19 +00:00
DEFAULT_SEARCH_COUNT = 20
2016-09-05 14:34:37 +00:00
DEFAULT_SEARCH_PAGE = 1
)
type SearchParameters struct {
Sort string
SortDirection string
Highlight bool
Count int
Page int
}
type CtxChannel struct {
2019-12-07 21:54:36 +00:00
ID string `json:"id"`
Name string `json:"name"`
IsExtShared bool `json:"is_ext_shared"`
IsMPIM bool `json:"is_mpim"`
ISOrgShared bool `json:"is_org_shared"`
IsPendingExtShared bool `json:"is_pending_ext_shared"`
IsPrivate bool `json:"is_private"`
IsShared bool `json:"is_shared"`
2016-09-05 14:34:37 +00:00
}
type CtxMessage struct {
User string `json:"user"`
Username string `json:"username"`
Text string `json:"text"`
Timestamp string `json:"ts"`
Type string `json:"type"`
}
type SearchMessage struct {
2018-08-09 22:38:19 +00:00
Type string `json:"type"`
Channel CtxChannel `json:"channel"`
User string `json:"user"`
Username string `json:"username"`
Timestamp string `json:"ts"`
2019-09-07 20:46:58 +00:00
Blocks Blocks `json:"blocks,omitempty"`
2018-08-09 22:38:19 +00:00
Text string `json:"text"`
Permalink string `json:"permalink"`
Attachments []Attachment `json:"attachments"`
Previous CtxMessage `json:"previous"`
Previous2 CtxMessage `json:"previous_2"`
Next CtxMessage `json:"next"`
Next2 CtxMessage `json:"next_2"`
2016-09-05 14:34:37 +00:00
}
type SearchMessages struct {
Matches []SearchMessage `json:"matches"`
Paging `json:"paging"`
Pagination `json:"pagination"`
Total int `json:"total"`
}
type SearchFiles struct {
Matches []File `json:"matches"`
Paging `json:"paging"`
Pagination `json:"pagination"`
Total int `json:"total"`
}
type searchResponseFull struct {
Query string `json:"query"`
SearchMessages `json:"messages"`
SearchFiles `json:"files"`
SlackResponse
}
func NewSearchParameters() SearchParameters {
return SearchParameters{
Sort: DEFAULT_SEARCH_SORT,
SortDirection: DEFAULT_SEARCH_SORT_DIR,
Highlight: DEFAULT_SEARCH_HIGHLIGHT,
Count: DEFAULT_SEARCH_COUNT,
Page: DEFAULT_SEARCH_PAGE,
}
}
2017-07-16 12:29:46 +00:00
func (api *Client) _search(ctx context.Context, path, query string, params SearchParameters, files, messages bool) (response *searchResponseFull, error error) {
2016-09-05 14:34:37 +00:00
values := url.Values{
2018-08-09 22:38:19 +00:00
"token": {api.token},
2016-09-05 14:34:37 +00:00
"query": {query},
}
if params.Sort != DEFAULT_SEARCH_SORT {
values.Add("sort", params.Sort)
}
if params.SortDirection != DEFAULT_SEARCH_SORT_DIR {
values.Add("sort_dir", params.SortDirection)
}
if params.Highlight != DEFAULT_SEARCH_HIGHLIGHT {
values.Add("highlight", strconv.Itoa(1))
}
if params.Count != DEFAULT_SEARCH_COUNT {
values.Add("count", strconv.Itoa(params.Count))
}
if params.Page != DEFAULT_SEARCH_PAGE {
values.Add("page", strconv.Itoa(params.Page))
}
2018-08-09 22:38:19 +00:00
2016-09-05 14:34:37 +00:00
response = &searchResponseFull{}
2019-09-07 20:46:58 +00:00
err := api.postMethod(ctx, path, values, response)
2016-09-05 14:34:37 +00:00
if err != nil {
return nil, err
}
2018-12-01 18:55:35 +00:00
return response, response.Err()
2016-09-05 14:34:37 +00:00
}
func (api *Client) Search(query string, params SearchParameters) (*SearchMessages, *SearchFiles, error) {
2017-07-16 12:29:46 +00:00
return api.SearchContext(context.Background(), query, params)
}
func (api *Client) SearchContext(ctx context.Context, query string, params SearchParameters) (*SearchMessages, *SearchFiles, error) {
response, err := api._search(ctx, "search.all", query, params, true, true)
2016-09-05 14:34:37 +00:00
if err != nil {
return nil, nil, err
}
return &response.SearchMessages, &response.SearchFiles, nil
}
func (api *Client) SearchFiles(query string, params SearchParameters) (*SearchFiles, error) {
2017-07-16 12:29:46 +00:00
return api.SearchFilesContext(context.Background(), query, params)
}
func (api *Client) SearchFilesContext(ctx context.Context, query string, params SearchParameters) (*SearchFiles, error) {
response, err := api._search(ctx, "search.files", query, params, true, false)
2016-09-05 14:34:37 +00:00
if err != nil {
return nil, err
}
return &response.SearchFiles, nil
}
func (api *Client) SearchMessages(query string, params SearchParameters) (*SearchMessages, error) {
2017-07-16 12:29:46 +00:00
return api.SearchMessagesContext(context.Background(), query, params)
}
func (api *Client) SearchMessagesContext(ctx context.Context, query string, params SearchParameters) (*SearchMessages, error) {
response, err := api._search(ctx, "search.messages", query, params, false, true)
2016-09-05 14:34:37 +00:00
if err != nil {
return nil, err
}
return &response.SearchMessages, nil
}