File size: 3,574 Bytes
1e92f2d |
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 |
package mockslack
import (
"context"
"encoding/base64"
"math/rand"
"net/http"
"sort"
"strconv"
"strings"
)
// ConversationsListOpts contains parameters for the ConversationsList API call.
type ConversationsListOpts struct {
Cursor string
ExcludeArchived bool
Limit int
Types string
}
// ConversationsList returns a list of channel-like conversations in a workspace.
func (st *API) ConversationsList(ctx context.Context, opts ConversationsListOpts) ([]Channel, string, error) {
err := checkPermission(ctx, "bot", "channels:read", "groups:read", "im:read", "mpim:read")
if err != nil {
return nil, "", err
}
inclArchived := !opts.ExcludeArchived
inclPrivate := strings.Contains(opts.Types, "private_channel")
inclPublic := strings.Contains(opts.Types, "public_channel") || opts.Types == ""
if inclPublic && !hasScope(ctx, "bot", "channels:read") {
return nil, "", &response{Err: "invalid_types"}
}
if inclPrivate && !hasScope(ctx, "bot", "groups:read") {
return nil, "", &response{Err: "invalid_types"}
}
isBot := botID(ctx) != ""
uid := userID(ctx)
var cursorID string
if opts.Cursor != "" {
data, err := base64.URLEncoding.DecodeString(opts.Cursor)
if err != nil {
return nil, "", &response{Err: "invalid_cursor"}
}
cursorID = string(data)
opts.Cursor = ""
}
filter := func(ch *channelState) bool {
if ch == nil {
return false
}
if cursorID != "" && cursorID >= ch.ID {
return false
}
if ch.IsArchived && !inclArchived {
return false
}
if ch.IsGroup && !inclPrivate {
return false
}
if !ch.IsGroup && !inclPublic {
return false
}
if ch.IsGroup && !isBot && !contains(ch.Users, uid) {
return false
}
return true
}
if opts.Limit == 0 {
opts.Limit = 100
}
if opts.Limit > 1000 {
return nil, "", &response{Err: "invalid_limit"}
}
st.mx.Lock()
defer st.mx.Unlock()
ids := make([]string, 0, len(st.channels))
for id := range st.channels {
ids = append(ids, id)
}
sort.Strings(ids)
result := make([]Channel, 0, len(ids))
for _, id := range ids {
ch := st.channels[id]
if !filter(ch) {
continue
}
result = append(result, ch.Channel)
}
originalTotal := len(result)
if len(result) > opts.Limit {
result = result[:opts.Limit]
}
if len(result) > 1 {
// limit is never guaranteed (only as max) as per the docs
// so ensure it's handled by randomizing number of returned items
max := rand.Intn(len(result)) + 1
result = result[:max]
}
if originalTotal > len(result) && len(result) > 0 {
opts.Cursor = base64.URLEncoding.EncodeToString([]byte(result[len(result)-1].ID))
}
return result, opts.Cursor, nil
}
// ServeConversationsList serves a request to the `conversations.list` API call.
//
// https://api.slack.com/methods/conversations.list
func (s *Server) ServeConversationsList(w http.ResponseWriter, req *http.Request) {
var limit int
limitStr := req.FormValue("limit")
var err error
if limitStr != "" {
limit, err = strconv.Atoi(limitStr)
if err != nil {
respondWith(w, &response{Err: "invalid_limit"})
return
}
}
chans, cur, err := s.API().ConversationsList(req.Context(), ConversationsListOpts{
Cursor: req.FormValue("cursor"),
Limit: limit,
Types: req.FormValue("types"),
ExcludeArchived: req.FormValue("exclude_archived") == "true",
})
if respondErr(w, err) {
return
}
var resp struct {
response
Channels []Channel `json:"channels"`
}
resp.Meta.Cursor = cur
resp.Channels = chans
resp.OK = true
respondWith(w, resp)
}
|