Skip to content

Commit

Permalink
split player and chunk file
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Apr 10, 2023
1 parent e088335 commit 937f7c5
Show file tree
Hide file tree
Showing 11 changed files with 1,091 additions and 1,029 deletions.
4 changes: 2 additions & 2 deletions cmd/slackdump/internal/diag/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ func runRecordState(ctx context.Context, _ *base.Command, args []string) error {
}
defer f.Close()

pl, err := chunk.NewPlayer(f)
cf, err := chunk.FromReader(f)
if err != nil {
return err
}
state, err := pl.State()
state, err := cf.State()
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/slackdump/internal/export/expproc/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ func transform(ctx context.Context, fsa fsadapter.FS, srcdir string, id string)
// return err
// }
// transform the chunk file
pl, err := chunk.NewPlayer(f)
cf, err := chunk.FromReader(f)
if err != nil {
return err
}

ci, err := pl.ChannelInfo(id)
ci, err := cf.ChannelInfo(id)
if err != nil {
return err
}
Expand All @@ -105,7 +105,7 @@ func transform(ctx context.Context, fsa fsadapter.FS, srcdir string, id string)
users = nil
}

if err := writeMessages(ctx, fsa, pl, ci, users); err != nil {
if err := writeMessages(ctx, fsa, cf, ci, users); err != nil {
return err
}

Expand All @@ -118,7 +118,7 @@ func LoadUsers(ctx context.Context, dir string) ([]slack.User, error) {
return nil, err
}
defer f.Close()
p, err := chunk.NewPlayer(f)
p, err := chunk.FromReader(f)
if err != nil {
return nil, err
}
Expand All @@ -136,7 +136,7 @@ func channelName(ch *slack.Channel) string {
return ch.Name
}

func writeMessages(ctx context.Context, fsa fsadapter.FS, pl *chunk.Player, ci *slack.Channel, users []slack.User) error {
func writeMessages(ctx context.Context, fsa fsadapter.FS, pl *chunk.File, ci *slack.Channel, users []slack.User) error {
uidx := types.Users(users).IndexByID()
dir := channelName(ci)
var (
Expand Down
29 changes: 17 additions & 12 deletions internal/chunk/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const (
CChannelInfo
)

var ErrUnsupChunkType = fmt.Errorf("unsupported chunk type")

// Chunk is a single chunk that was recorded. It contains the type of chunk,
// the timestamp of the chunk, the channel ID, and the number of messages or
// files that were recorded.
Expand Down Expand Up @@ -62,21 +64,26 @@ type Chunk struct {
Channels []slack.Channel `json:"ch,omitempty"` // Populated by Channels
}

// GroupID is a unique ID for a chunk group. It is used to group chunks of
// the same type together for indexing purposes. It may or may not be equal
// to the Slack ID of the entity.
type GroupID string

const (
userChunkID = "lusr"
channelChunkID = "lch"
userChunkID GroupID = "lusr"
channelChunkID GroupID = "lch"

threadPrefix = "t"
filePrefix = "f"
chanInfoPrefix = "ic"
threadChanInfoPrefix = "it"
)

// ID returns a unique ID for the chunk.
func (c *Chunk) ID() string {
// ID returns a Group ID for the chunk.
func (c *Chunk) ID() GroupID {
switch c.Type {
case CMessages:
return c.ChannelID
return GroupID(c.ChannelID)
case CThreadMessages:
return threadID(c.ChannelID, c.Parent.ThreadTimestamp)
case CFiles:
Expand All @@ -88,18 +95,18 @@ func (c *Chunk) ID() string {
case CChannels:
return channelChunkID // static, one team per chunk file.
}
return fmt.Sprintf("<unknown:%s>", c.Type)
return GroupID(fmt.Sprintf("<unknown:%s>", c.Type))
}

func id(prefix string, ids ...string) string {
return prefix + strings.Join(ids, ":")
func id(prefix string, ids ...string) GroupID {
return GroupID(prefix + strings.Join(ids, ":"))
}

func threadID(channelID, threadTS string) string {
func threadID(channelID, threadTS string) GroupID {
return id(threadPrefix, channelID, threadTS)
}

func channelInfoID(channelID string, isThread bool) string {
func channelInfoID(channelID string, isThread bool) GroupID {
if isThread {
return id(threadChanInfoPrefix, channelID)
}
Expand All @@ -110,8 +117,6 @@ func (c *Chunk) String() string {
return fmt.Sprintf("%s: %s", c.Type, c.ID())
}

var ErrUnsupChunkType = fmt.Errorf("unsupported chunk type")

// Timestamps returns the timestamps of the messages in the chunk. For files
// and other types of chunks, it returns ErrUnsupChunkType.
func (c *Chunk) Timestamps() ([]int64, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/chunk/chunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestChunk_ID(t *testing.T) {
tests := []struct {
name string
fields fields
want string
want GroupID
}{
{
name: "messages",
Expand Down
Loading

0 comments on commit 937f7c5

Please sign in to comment.