Skip to content

Commit

Permalink
recorder
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed May 2, 2023
1 parent 7d41103 commit 7087ab8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ dist/
/tmp
*.dot

# outputs
slackdump_2*
26 changes: 13 additions & 13 deletions cmd/slackdump/internal/man/assets/chunk.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,16 @@ Sample chunk JSON message:
### t: Chunk type

Each JSON object can contain the following "chunk" of information, denoted as
unsigned 8-bit integer:
unsigned 8-bit integer, each chunk type is a direct mapping to the Slack API method that was used to
retrieve the data:

- **Type 0**: slice of channel messages;
- **Type 1**: slice of channel message replies (a thread);
- **Type 2**: slice of files that were uploaded to the workspace (only definitions);
- **Type 3**: slice of channels;
- **Type 4**: slice of users;
- **Type 5**: workspace information.

Each chunk type is a direct mapping to the Slack API method that was used to
retrieve the data:

- **Type 0**: [conversations.history](https://api.slack.com/methods/conversations.history);
- **Type 1**: [conversations.replies](https://api.slack.com/methods/conversations.replies);
- **Type 2**: [files.list](https://api.slack.com/methods/files.list);
Expand All @@ -93,6 +92,7 @@ The channel ID is a string that contains the ID of the channel that the chunk
belongs to. It is only populated for chunks of type 0, 1, and 2.

### n: Number of messages or files

The number of messages or files is an integer that contains the number of
messages or files that are contained in the chunk. It is only populated for
chunks of type 0, 1, and 2.
Expand Down Expand Up @@ -125,24 +125,24 @@ It is only populated for chunks of type 1 and 2.

### m: Messages

The messages contains a chunk of messages as returned by the API. It is only
populated for chunks of type 0 and 1. This slice size can be in range from 1
to 1000 for message type chunks.
The messages contains a chunk of messages. It is only populated for chunks of
type 0 and 1. This slice size can be in range from 1 to 1000 for message type
chunks.

### f: Files

The files contains a chunk of files as returned by the API. It is only
populated for chunks of type 2.
The files contains a chunk of files. It is only populated for chunks of type
2.

### u: Users

The users contains a chunk of users as returned by the API. It is only
populated for chunks of type 4.
The users contains a chunk of users. It is only populated for chunks of type
4.

### ch: Channels

The channels contains a chunk of channels as returned by the API. It is only
populated for chunks of type 3.
The channels contains a chunk of channels. It is only populated for chunks of
type 3.

### w: Workspace information

Expand Down
52 changes: 51 additions & 1 deletion cmd/slackdump/internal/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ package record
import (
"context"
_ "embed"
"strings"
"time"

"github.com/rusq/slackdump/v2"
"github.com/rusq/slackdump/v2/auth"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/cfg"
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/golang/base"
"github.com/rusq/slackdump/v2/internal/chunk"
"github.com/rusq/slackdump/v2/internal/chunk/control"
"github.com/rusq/slackdump/v2/internal/structures"
"github.com/rusq/slackdump/v2/logger"
)

//go:embed assets/record.md
Expand All @@ -23,11 +30,54 @@ var CmdRecord = &base.Command{
}

func RunRecord(ctx context.Context, cmd *base.Command, args []string) error {
list, err := structures.NewEntityList(args)
if err != nil {
base.SetExitStatus(base.SUserError)
return err
}

prov, err := auth.FromContext(ctx)
if err != nil {
base.SetExitStatus(base.SAuthError)
return err
}
_ = prov

// hack
cfg.BaseLocation = strings.TrimSuffix(cfg.BaseLocation, ".zip")

cd, err := chunk.CreateDir(cfg.BaseLocation)
if err != nil {
base.SetExitStatus(base.SGenericError)
return err
}

sess, err := slackdump.New(ctx, prov, slackdump.WithLogger(logger.FromContext(ctx)))
if err != nil {
base.SetExitStatus(base.SGenericError)
return err
}

stream := sess.Stream(slackdump.OptLatest(time.Time(cfg.Latest)), slackdump.OptOldest(time.Time(cfg.Oldest)))

if err := record(ctx, stream, cd, list); err != nil {
base.SetExitStatus(base.SApplicationError)
return err
}
return nil
}

func resultLogger(lg logger.Interface) func(sr slackdump.StreamResult) error {
return func(sr slackdump.StreamResult) error {
lg.Printf("%s", sr)
return nil
}
}

func record(ctx context.Context, stream control.Streamer, cd *chunk.Directory, list *structures.EntityList) error {
lg := logger.FromContext(ctx)
ctrl := control.New(cd, stream, control.WithLogger(lg), control.WithResultFn(resultLogger(lg)))
if err := ctrl.Run(ctx, list); err != nil {
return err
}
return nil
}

0 comments on commit 7087ab8

Please sign in to comment.