Skip to content

Commit

Permalink
Merge pull request #367 from rusq/v3-beta
Browse files Browse the repository at this point in the history
Implementing #220 for V3
  • Loading branch information
rusq authored Nov 24, 2024
2 parents 14a4f89 + c66524c commit c6908db
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 24 deletions.
27 changes: 27 additions & 0 deletions channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,30 @@ func (s *Session) getChannels(ctx context.Context, chanTypes []string, cb func(t
}
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.cfg.limits.Tier4.Retries, 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
}
94 changes: 94 additions & 0 deletions channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"reflect"
"testing"

"github.com/rusq/fsadapter"
"github.com/rusq/slack"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"

"github.com/rusq/slackdump/v3/internal/network"
"github.com/rusq/slackdump/v3/internal/structures"
"github.com/rusq/slackdump/v3/types"
)

Expand Down Expand Up @@ -136,3 +138,95 @@ func TestSession_GetChannels(t *testing.T) {
})
}
}

func TestSession_GetChannelMembers(t *testing.T) {
type fields struct {
wspInfo *slack.AuthTestResponse
fs fsadapter.FS
Users types.Users
UserIndex structures.UserIndex
cfg config
}
type args struct {
ctx context.Context
channelID string
}
tests := []struct {
name string
fields fields
args args
expect func(mc *mockClienter)
want []string
wantErr bool
}{
{
"ok, single call",
fields{cfg: defConfig},
args{
context.Background(),
"chanID",
},
func(mc *mockClienter) {
mc.EXPECT().GetUsersInConversationContext(gomock.Any(), &slack.GetUsersInConversationParameters{
ChannelID: "chanID",
}).Return([]string{"user1", "user2"}, "", nil)
},
[]string{"user1", "user2"},
false,
},
{
"ok, two calls",
fields{cfg: defConfig},
args{
context.Background(),
"chanID",
},
func(mc *mockClienter) {
first := mc.EXPECT().GetUsersInConversationContext(gomock.Any(), &slack.GetUsersInConversationParameters{
ChannelID: "chanID",
}).Return([]string{"user1", "user2"}, "cursor", nil).Times(1)
_ = mc.EXPECT().GetUsersInConversationContext(gomock.Any(), &slack.GetUsersInConversationParameters{
ChannelID: "chanID",
Cursor: "cursor",
}).Return([]string{"user3"}, "", nil).After(first).Times(1)
},
[]string{"user1", "user2", "user3"},
false,
},
{
"error",
fields{cfg: defConfig},
args{
context.Background(),
"chanID",
},
func(mc *mockClienter) {
mc.EXPECT().GetUsersInConversationContext(gomock.Any(), &slack.GetUsersInConversationParameters{
ChannelID: "chanID",
}).Return([]string{}, "", errors.New("error fornicating corrugations"))
},
nil,
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mc := NewmockClienter(gomock.NewController(t))
tt.expect(mc)
sd := &Session{
client: mc,
wspInfo: tt.fields.wspInfo,
fs: tt.fields.fs,
cfg: tt.fields.cfg,
}
got, err := sd.GetChannelMembers(tt.args.ctx, tt.args.channelID)
if (err != nil) != tt.wantErr {
t.Errorf("Session.GetChannelMembers() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Session.GetChannelMembers() = %v, want %v", got, tt.want)
}
})
}
}
28 changes: 14 additions & 14 deletions cmd/slackdump/internal/emoji/emojidl/emoji_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions cmd/slackdump/internal/emoji/emojidl/emoji_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func Test_download(t *testing.T) {
name string
args args
fetchFn fetchFunc
expect func(m *Mockemojidumper)
expect func(m *MockEmojiDumper)
wantErr bool
}{
{
Expand All @@ -266,7 +266,7 @@ func Test_download(t *testing.T) {
failFast: true,
},
emptyFetchFn,
func(m *Mockemojidumper) {
func(m *MockEmojiDumper) {
m.EXPECT().
DumpEmojis(gomock.Any()).
Return(map[string]string{
Expand All @@ -283,7 +283,7 @@ func Test_download(t *testing.T) {
failFast: true,
},
emptyFetchFn,
func(m *Mockemojidumper) {
func(m *MockEmojiDumper) {
m.EXPECT().
DumpEmojis(gomock.Any()).
Return(map[string]string{
Expand All @@ -300,7 +300,7 @@ func Test_download(t *testing.T) {
failFast: true,
},
errorFetchFn,
func(m *Mockemojidumper) {
func(m *MockEmojiDumper) {
m.EXPECT().
DumpEmojis(gomock.Any()).
Return(map[string]string{
Expand All @@ -317,7 +317,7 @@ func Test_download(t *testing.T) {
failFast: false,
},
errorFetchFn,
func(m *Mockemojidumper) {
func(m *MockEmojiDumper) {
m.EXPECT().
DumpEmojis(gomock.Any()).
Return(nil, errors.New("no emojis for you, it's 1991."))
Expand All @@ -328,7 +328,7 @@ func Test_download(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
setGlobalFetchFn(tt.fetchFn)
sess := NewMockemojidumper(gomock.NewController(t))
sess := NewMockEmojiDumper(gomock.NewController(t))
tt.expect(sess)
fs, err := fsadapter.New(tt.args.output)
if err != nil {
Expand Down
85 changes: 85 additions & 0 deletions cmd/slackdump/internal/workspace/workspaceui/test_mock_manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/rusq/slackdump/v3/internal/cache"
)

//go:generate mockgen -package workspaceui -destination=test_mock_manager.go -source api.go manager
//go:generate mockgen -package workspaceui -destination=test_mock_manager.go -source workspaceui.go manager
type manager interface {
CreateAndSelect(ctx context.Context, p auth.Provider) (string, error)
Select(name string) error
Expand Down
Loading

0 comments on commit c6908db

Please sign in to comment.