Skip to content

Commit

Permalink
fix channelinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Feb 10, 2024
1 parent 499ef75 commit 566ca49
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion internal/chunk/control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ type Error struct {
}

func (e Error) Error() string {
return fmt.Sprintf("controller error in %s on %s: %v", e.Subroutine, e.Stage, e.Err)
return fmt.Sprintf("controller error in subroutine %s on stage %s: %v", e.Subroutine, e.Stage, e.Err)
}

func (c *Controller) Run(ctx context.Context, list *structures.EntityList) error {
Expand Down
7 changes: 4 additions & 3 deletions internal/chunk/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/slack-go/slack"
)

const ext = ".json.gz"
const chunkExt = ".json.gz"

// common filenames
const (
Expand Down Expand Up @@ -119,7 +119,7 @@ func (d *Directory) Channels() ([]slack.Channel, error) {
if err != nil {
return err
}
if !strings.HasSuffix(path, ext) {
if !strings.HasSuffix(path, chunkExt) {
return nil
} else if de.IsDir() {
return nil
Expand All @@ -145,6 +145,7 @@ func (d *Directory) Name() string {
return d.dir
}

// loadChanInfo loads the channel info from the file with full path.
func (d *Directory) loadChanInfo(fullpath string) ([]slack.Channel, error) {
f, err := d.openRAW(fullpath)
if err != nil {
Expand Down Expand Up @@ -255,7 +256,7 @@ func openfile(filename string) (*os.File, error) {

// filename returns the full path of the chunk file with the given fileID.
func (d *Directory) filename(id FileID) string {
return filepath.Join(d.dir, string(id)+ext)
return filepath.Join(d.dir, string(id)+chunkExt)
}

// Create creates the chunk file with the given name. Extension is appended
Expand Down
16 changes: 15 additions & 1 deletion internal/chunk/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ func caller(steps int) string {
return name
}

// ensure ensures that the file index was generated.
//
// TODO: maybe it shouldn't panic.
func (f *File) ensure() {
if f.idx == nil {
log.Panicf("internal error: %s called before File.Open", caller(1))
Expand Down Expand Up @@ -267,9 +270,20 @@ func (p *File) AllChannels() ([]slack.Channel, error) {
// info API.
func (f *File) AllChannelInfos() ([]slack.Channel, error) {
f.ensure()
return allForOffsets(f, f.idx.offsetsWithPrefix(chanInfoPrefix), func(c *Chunk) []slack.Channel {
chans, err := allForOffsets(f, f.idx.offsetsWithPrefix(chanInfoPrefix), func(c *Chunk) []slack.Channel {
return []slack.Channel{*c.Channel}
})
if err != nil {
return nil, err
}
for i := range chans {
members, err := f.ChannelUsers(chans[i].ID)
if err != nil {
return nil, err
}
chans[i].Members = members
}
return chans, nil
}

// AllChannelInfoWithMembers returns all channels with Members populated.
Expand Down
14 changes: 9 additions & 5 deletions internal/chunk/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,12 @@ func mkindex(rs io.ReadSeeker) index {
}

func TestFile_AllChannelInfos(t *testing.T) {
c0wus := *testChunks[0].Channel
c0wus.Members = testChunks[1].ChannelUsers

c6wus := *testChunks[6].Channel
c6wus.Members = testChunks[7].ChannelUsers

type fields struct {
rs io.ReadSeeker
}
Expand All @@ -728,8 +734,8 @@ func TestFile_AllChannelInfos(t *testing.T) {
rs: marshalChunks(testChunks...),
},
[]slack.Channel{
*testChunks[0].Channel,
*testChunks[6].Channel,
c0wus,
c6wus,
},
false,
},
Expand All @@ -745,9 +751,7 @@ func TestFile_AllChannelInfos(t *testing.T) {
t.Errorf("File.AllChannelInfos() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("File.AllChannelInfos() = %v, want %v", got, tt.want)
}
assert.Equal(t, tt.want, got)
})
}
}
Expand Down

0 comments on commit 566ca49

Please sign in to comment.