Skip to content

Commit

Permalink
Merge pull request #350 from rusq/i324
Browse files Browse the repository at this point in the history
Do not attempt to read channel users for archived channels
  • Loading branch information
rusq authored Nov 5, 2024
2 parents 24eb419 + a8cabd1 commit 9808f30
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
14 changes: 10 additions & 4 deletions internal/chunk/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ func (f *File) AllChannelInfos() ([]slack.Channel, error) {
return nil, err
}
for i := range chans {
if chans[i].IsArchived {
logger.Default.Debugf("skipping archived channel %s", chans[i].ID)
continue
}
members, err := f.ChannelUsers(chans[i].ID)
if err != nil {
if errors.Is(err, ErrNotFound) {
Expand Down Expand Up @@ -335,11 +339,13 @@ func (f *File) ChannelInfo(channelID string) (*slack.Channel, error) {
if err != nil {
return nil, err
}
users, err := f.ChannelUsers(channelID)
if err != nil {
return nil, fmt.Errorf("failed getting channel users for %q: %w", channelID, err)
if !info.IsArchived {
users, err := f.ChannelUsers(channelID)
if err != nil {
return nil, fmt.Errorf("failed getting channel users for %q: %w", channelID, err)
}
info.Members = users
}
info.Members = users
return info, nil
}

Expand Down
66 changes: 66 additions & 0 deletions internal/chunk/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ var testThreadsIndex = index{
"tC1234567890:1234567890.123458": []int64{619},
}

var archivedChannel = []Chunk{
{Type: CChannelInfo, ChannelID: TestChannelID, Channel: &slack.Channel{GroupConversation: slack.GroupConversation{IsArchived: true, Conversation: slack.Conversation{ID: TestChannelID}}}},
{Type: CMessages, ChannelID: TestChannelID, Messages: []slack.Message{
{Msg: slack.Msg{Timestamp: "1234567890.100000", Text: "message1"}},
{Msg: slack.Msg{Timestamp: "1234567890.200000", Text: "message2"}},
{Msg: slack.Msg{Timestamp: "1234567890.300000", Text: "message3"}},
{Msg: slack.Msg{Timestamp: "1234567890.400000", Text: "message4"}},
{Msg: slack.Msg{Timestamp: "1234567890.500000", Text: "message5"}},
}},
{Type: CMessages, ChannelID: TestChannelID, Messages: []slack.Message{
{Msg: slack.Msg{Timestamp: "1234567890.600000", Text: "Hello, again!"}},
{Msg: slack.Msg{Timestamp: "1234567890.700000", Text: "And again!"}},
}},
}

var testChunks = []Chunk{
{Type: CChannelInfo, ChannelID: TestChannelID, Channel: &slack.Channel{GroupConversation: slack.GroupConversation{Conversation: slack.Conversation{ID: TestChannelID}}}},
{Type: CChannelUsers, ChannelID: TestChannelID, ChannelUsers: []string{"user1", "user2"}},
Expand Down Expand Up @@ -804,3 +819,54 @@ func TestFile_AllChannelInfoWithMembers(t *testing.T) {
})
}
}

func TestFile_ChannelInfo(t *testing.T) {
chanWithUsers := *testChunks[0].Channel
chanWithUsers.Members = testChunks[1].ChannelUsers
type args struct {
channelID string
}
tests := []struct {
name string
file *File
args args
want *slack.Channel
wantErr bool
}{
{
name: "normal channel",
file: &File{
rs: marshalChunks(testChunks...),
idx: mkindex(marshalChunks(testChunks...)),
},
args: args{
channelID: TestChannelID,
},
want: &chanWithUsers,
wantErr: false,
},
{
name: "archived channel",
file: &File{
rs: marshalChunks(archivedChannel...),
idx: mkindex(marshalChunks(archivedChannel...)),
},
args: args{
channelID: TestChannelID,
},
want: archivedChannel[0].Channel,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := tt.file
got, err := f.ChannelInfo(tt.args.channelID)
if (err != nil) != tt.wantErr {
t.Errorf("File.ChannelInfo() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.Equal(t, tt.want, got)
})
}
}
10 changes: 9 additions & 1 deletion internal/structures/user_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func (idx UserIndex) IsDeleted(id string) bool {
// ChannelName return the "beautified" name of the channel.
func (idx UserIndex) ChannelName(ch slack.Channel) (who string) {
t := ChannelType(ch)

switch t {
case CIM:
who = "@" + idx.Username(ch.User)
Expand All @@ -106,5 +107,12 @@ func (idx UserIndex) ChannelName(ch slack.Channel) (who string) {
default:
who = "#" + NVL(ch.NameNormalized, ch.Name)
}
return who
return who + iftrue(ch.IsArchived, " (archived)", "")
}

func iftrue[T any](b bool, t, f T) T {
if b {
return t
}
return f
}

0 comments on commit 9808f30

Please sign in to comment.