-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
main.go
162 lines (145 loc) · 4.06 KB
/
main.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
159
160
161
162
// Command slackutil is an utility that provides some useful functions for
// testing, i.e. deletion of the threads, or generation of large threads.
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"github.com/joho/godotenv"
"github.com/rusq/osenv/v2"
"github.com/schollz/progressbar/v3"
"github.com/slack-go/slack"
"github.com/rusq/slackdump/v2"
"github.com/rusq/slackdump/v2/internal/network"
"github.com/rusq/slackdump/v2/internal/structures"
"github.com/rusq/slackdump/v2/logger"
)
var _ = godotenv.Load()
var (
token = flag.String("token", osenv.Secret("TOKEN", ""), "slack app token")
channel = flag.String("channel", osenv.Value("CHANNEL", ""), "channel to generate thread in")
numThreadMsg = flag.Int("num", 2, "number of messages to generate in the thread")
delThread = flag.String("del", "", "`URL` of the thread to delete")
)
func main() {
flag.Parse()
if *token == "" {
flag.Usage()
logger.Default.Fatal("token not set")
}
if *delThread != "" {
if err := runDelete(*token, *delThread); err != nil {
log.Fatal(err)
}
} else {
if err := runGenerate(*token, *channel, *numThreadMsg); err != nil {
log.Fatal(err)
}
}
}
func runDelete(token, url string) error {
if err := deleteThread(context.Background(), slack.New(token), url); err != nil {
return err
}
return nil
}
func runGenerate(token string, channelID string, numMsg int) error {
if channelID == "" {
return errors.New("channel ID not set")
}
if err := generateThread(context.Background(), slack.New(token), channelID, numMsg); err != nil {
return err
}
return nil
}
func generateThread(ctx context.Context, client *slack.Client, channelID string, numMsg int) error {
msg := []slack.Block{
slack.NewHeaderBlock(
slack.NewTextBlockObject("plain_text", fmt.Sprintf("Very long thread (%d messages)", numMsg), true, false),
),
}
_, ts, err := client.PostMessageContext(
ctx,
channelID,
slack.MsgOptionBlocks(msg...),
)
if err != nil {
return fmt.Errorf("failed to send initial message: %w", err)
}
l := network.NewLimiter(network.Tier3, slackdump.DefOptions.Tier3Burst, int(slackdump.DefOptions.Tier3Boost))
pb := progressbar.Default(int64(numMsg))
pb.Describe("posting messages")
defer pb.Finish()
for i := 0; i < numMsg; i++ {
if err := network.WithRetry(ctx, l, 3, func() error {
_, _, err := client.PostMessageContext(ctx, channelID, slack.MsgOptionTS(ts), slack.MsgOptionText(fmt.Sprintf("message: %d", i), false))
return err
}); err != nil {
return fmt.Errorf("failed to post message to the thread: %w", err)
}
if err := pb.Add(1); err != nil {
// what a shame
return err
}
}
return nil
}
func deleteThread(ctx context.Context, client *slack.Client, url string) error {
ui, err := structures.ParseURL(url)
if err != nil {
return err
}
msgs, err := getMessages(ctx, client, ui)
if err != nil {
return err
}
if err := delMessages(ctx, client, ui.Channel, msgs); err != nil {
return err
}
return nil
}
func delMessages(ctx context.Context, client *slack.Client, channelID string, msgs []slack.Message) error {
pb := progressbar.Default(int64(len(msgs)))
pb.Describe("deleting messages")
defer pb.Finish()
l := network.NewLimiter(network.Tier3, slackdump.DefOptions.Tier3Burst, int(slackdump.DefOptions.Tier3Boost))
for _, m := range msgs {
err := network.WithRetry(ctx, l, 3, func() error {
_, _, err := client.DeleteMessageContext(ctx, channelID, m.Timestamp)
return err
})
if err != nil {
return err
}
_ = pb.Add(1)
}
return nil
}
func getMessages(ctx context.Context, client *slack.Client, ui *structures.SlackLink) ([]slack.Message, error) {
var msgs []slack.Message
cursor := ""
for {
var (
chunk []slack.Message
hasmore bool
err error
)
chunk, hasmore, cursor, err = client.GetConversationRepliesContext(
ctx,
&slack.GetConversationRepliesParameters{
ChannelID: ui.Channel,
Timestamp: ui.ThreadTS,
Cursor: cursor,
})
if err != nil {
return nil, err
}
msgs = append(msgs, chunk...)
if !hasmore {
break
}
}
return msgs, nil
}