8
8
"strings"
9
9
"sync"
10
10
11
+ "github.com/avast/retry-go/v4"
11
12
"github.com/google/uuid"
13
+ "github.com/sanity-io/litter"
12
14
"github.com/sirupsen/logrus"
13
15
"github.com/slack-go/slack"
14
16
"github.com/slack-go/slack/slackevents"
@@ -531,10 +533,10 @@ func (b *SocketSlack) send(ctx context.Context, event slackMessage, in interacti
531
533
}
532
534
533
535
// Upload message as a file if too long
534
- var file * slack.File
536
+ var file * slack.FileSummary
535
537
var err error
536
538
if len (markdown ) >= slackMaxMessageSize {
537
- file , err = uploadFileToSlack (ctx , event .Channel , resp , b . client , event .ThreadTimeStamp )
539
+ file , err = b . uploadFileToSlack (ctx , event .Channel , resp , event .ThreadTimeStamp )
538
540
if err != nil {
539
541
return err
540
542
}
@@ -564,7 +566,7 @@ func (b *SocketSlack) send(ctx context.Context, event slackMessage, in interacti
564
566
// if the message should be sent in thread, but thread is not yet started, then use the root message timestamp
565
567
event .ThreadTimeStamp = event .RootMessageTimeStamp
566
568
}
567
- if ts := b .getThreadOptionIfNeeded (event , file ); ts != nil {
569
+ if ts := b .getThreadOptionIfNeeded (ctx , event , file ); ts != nil {
568
570
options = append (options , ts )
569
571
}
570
572
@@ -707,7 +709,7 @@ func resolveBlockActionCommand(act slack.BlockAction) (string, command.Origin) {
707
709
return cmd , cmdOrigin
708
710
}
709
711
710
- func (b * SocketSlack ) getThreadOptionIfNeeded (event slackMessage , file * slack.File ) slack.MsgOption {
712
+ func (b * SocketSlack ) getThreadOptionIfNeeded (ctx context. Context , event slackMessage , file * slack.FileSummary ) slack.MsgOption {
711
713
//if the message is from thread then add an option to return the response to the thread
712
714
if event .ThreadTimeStamp != "" {
713
715
return slack .MsgOptionTS (event .ThreadTimeStamp )
@@ -717,11 +719,45 @@ func (b *SocketSlack) getThreadOptionIfNeeded(event slackMessage, file *slack.Fi
717
719
return nil
718
720
}
719
721
720
- // If the message was already as a file attachment, reply it a given thread
721
- for _ , share := range file .Shares .Public {
722
- if len (share ) >= 1 && share [0 ].Ts != "" {
723
- return slack .MsgOptionTS (share [0 ].Ts )
722
+ var ts string
723
+ err := b .withRetry (ctx , func () error {
724
+ info , _ , _ , err := b .client .GetFileInfoContext (ctx , file .ID , 100 , 1 )
725
+ if err != nil {
726
+ fmt .Println (err )
727
+ return err
728
+ }
729
+
730
+ for _ , share := range info .Shares .Public {
731
+ if len (share ) >= 1 && share [0 ].Ts != "" {
732
+ ts = share [0 ].Ts
733
+ return nil
734
+ }
724
735
}
736
+
737
+ litter .Dump (info )
738
+ return fmt .Errorf ("file was not yet processed" )
739
+ })
740
+ if err != nil {
741
+ b .log .WithError (err ).Error ("Cannot get file share info. Sending message without thread option." )
742
+ return nil
743
+ }
744
+
745
+ if ts != "" {
746
+ return slack .MsgOptionTS (ts )
747
+ }
748
+
749
+ return nil
750
+ }
751
+
752
+ func (b * SocketSlack ) withRetry (ctx context.Context , fn func () error ) error {
753
+ err := retry .Do (
754
+ fn ,
755
+ retry .Attempts (maxRetries ),
756
+ retry .LastErrorOnly (true ),
757
+ retry .Context (ctx ),
758
+ )
759
+ if err != nil {
760
+ return fmt .Errorf ("while retrying: %w" , err )
725
761
}
726
762
727
763
return nil
@@ -766,17 +802,19 @@ func (b *SocketSlack) GetStatus() health.PlatformStatus {
766
802
}
767
803
}
768
804
769
- func uploadFileToSlack (ctx context.Context , channel string , resp interactive.CoreMessage , client * slack.Client , ts string ) (* slack.File , error ) {
770
- params := slack.FileUploadParameters {
805
+ func (b * SocketSlack ) uploadFileToSlack (ctx context.Context , channel string , resp interactive.CoreMessage , ts string ) (* slack.FileSummary , error ) {
806
+ content := interactive .MessageToPlaintext (resp , interactive .NewlineFormatter )
807
+ params := slack.UploadFileV2Parameters {
771
808
Filename : "Response.txt" ,
772
809
Title : "Response.txt" ,
773
810
InitialComment : resp .Description ,
774
- Content : interactive .MessageToPlaintext (resp , interactive .NewlineFormatter ),
775
- Channels : []string {channel },
811
+ Content : content ,
812
+ FileSize : len (content ),
813
+ Channel : channel ,
776
814
ThreadTimestamp : ts ,
777
815
}
778
816
779
- file , err := client .UploadFileContext (ctx , params )
817
+ file , err := b . client .UploadFileV2Context (ctx , params )
780
818
if err != nil {
781
819
return nil , fmt .Errorf ("while uploading file: %w" , err )
782
820
}
0 commit comments