-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
channels.go
127 lines (109 loc) · 3.4 KB
/
channels.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
package slackdump
// In this file: channel/conversations and thread related code.
import (
"context"
"runtime/trace"
"time"
"github.com/slack-go/slack"
"github.com/rusq/slackdump/v2/internal/network"
"github.com/rusq/slackdump/v2/types"
)
// GetChannels list all conversations for a user. `chanTypes` specifies the
// type of messages to fetch. See github.com/rusq/slack docs for possible
// values. If large number of channels is to be returned, consider using
// StreamChannels.
func (sd *Session) GetChannels(ctx context.Context, chanTypes ...string) (types.Channels, error) {
var allChannels types.Channels
if err := sd.getChannels(ctx, chanTypes, func(cc types.Channels) error {
allChannels = append(allChannels, cc...)
return nil
}); err != nil {
return allChannels, err
}
return allChannels, nil
}
// StreamChannels requests the channels from the API and calls the callback
// function cb for each.
func (sd *Session) StreamChannels(ctx context.Context, chanTypes []string, cb func(ch slack.Channel) error) error {
return sd.getChannels(ctx, chanTypes, func(chans types.Channels) error {
for _, ch := range chans {
if err := cb(ch); err != nil {
return err
}
}
return nil
})
}
// getChannels list all conversations for a user. `chanTypes` specifies
// the type of messages to fetch. See github.com/rusq/slack docs for possible
// values
func (sd *Session) getChannels(ctx context.Context, chanTypes []string, cb func(types.Channels) error) error {
ctx, task := trace.NewTask(ctx, "getChannels")
defer task.End()
limiter := network.NewLimiter(network.Tier2, sd.options.Tier2Burst, int(sd.options.Tier2Boost))
if chanTypes == nil {
chanTypes = AllChanTypes
}
params := &slack.GetConversationsParameters{Types: chanTypes, Limit: sd.options.ChannelsPerReq}
fetchStart := time.Now()
var total int
for i := 1; ; i++ {
var (
chans []slack.Channel
nextcur string
)
reqStart := time.Now()
if err := network.WithRetry(ctx, limiter, sd.options.Tier3Retries, func() error {
var err error
trace.WithRegion(ctx, "GetConversationsContext", func() {
chans, nextcur, err = sd.client.GetConversationsContext(ctx, params)
})
return err
}); err != nil {
return err
}
if err := cb(chans); err != nil {
return err
}
total += len(chans)
sd.l().Printf("channels request #%5d, fetched: %4d, total: %8d (speed: %6.2f/sec, avg: %6.2f/sec)\n",
i, len(chans), total,
float64(len(chans))/float64(time.Since(reqStart).Seconds()),
float64(total)/float64(time.Since(fetchStart).Seconds()),
)
if nextcur == "" {
sd.l().Printf("channels fetch complete, total: %d channels", total)
break
}
params.Cursor = nextcur
if err := limiter.Wait(ctx); err != nil {
return err
}
}
return nil
}
// GetChannelMembers returns a list of all members in a channel.
func (sd *Session) GetChannelMembers(ctx context.Context, channelID string) ([]string, error) {
var ids []string
var cursor string
for {
var uu []string
var next string
if err := network.WithRetry(ctx, sd.limiter(network.Tier4), sd.options.Tier4Retries, func() error {
var err error
uu, next, err = sd.client.GetUsersInConversationContext(ctx, &slack.GetUsersInConversationParameters{
ChannelID: channelID,
Cursor: cursor,
})
return err
}); err != nil {
return nil, err
}
ids = append(ids, uu...)
if next == "" {
break
}
cursor = next
}
return ids, nil
}