Skip to content

Commit

Permalink
export v3 early draft
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Mar 15, 2023
1 parent 56a6985 commit ab926b2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 17 deletions.
18 changes: 9 additions & 9 deletions cmd/slackdump/internal/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,23 @@ func runExport(ctx context.Context, cmd *base.Command, args []string) error {
if err != nil {
return err
}

return exportV2(ctx, prov, list)
}

func exportV2(ctx context.Context, prov auth.Provider, list *structures.EntityList) error {
options.List = list
options.Logger = dlog.FromContext(ctx)
sess, err := slackdump.New(ctx, prov, cfg.SlackConfig)
if err != nil {
return err
}
defer sess.Close()

options.List = list
options.Logger = dlog.FromContext(ctx)

return exportV2(ctx, sess, list, options)
}

func exportV2(ctx context.Context, sess *slackdump.Session, list *structures.EntityList, options export.Config) error {
exp := export.New(sess, options)
return exp.Run(ctx)
}

// func exportV3(ctx context.Context, prov auth.Provider, list *structures.EntityList) error {

// func exportV3(ctx context.Context, sess *slackdump.Session, list *structures.EntityList, options export.Config) error {
// s := sess.Stream(slackdump.WithOldest(options.Oldest), slackdump.WithLatest(options.Latest))
// }
7 changes: 6 additions & 1 deletion internal/chunk/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,15 @@ func (p *Player) HasMoreChannels() bool {
return p.hasMore(channelChunkID)
}

func (p *Player) HasMoreUsers() bool {
// HasUsers returns true if there is at least one user chunk in the file.
func (p *Player) HasUsers() bool {
return p.hasMore(userChunkID)
}

func (p *Player) HasChannels() bool {
return p.hasMore(channelChunkID)
}

// Thread returns the messages for the given thread.
func (p *Player) Thread(channelID string, threadTS string) ([]slack.Message, error) {
id := threadID(channelID, threadTS)
Expand Down
75 changes: 70 additions & 5 deletions internal/chunk/transform/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ package transform

import (
"archive/zip"
"context"
"fmt"
"io"
"os"
"path"

"github.com/rusq/fsadapter"

"github.com/rusq/slackdump/v2/export"
"github.com/rusq/slackdump/v2/internal/chunk"
"github.com/rusq/slackdump/v2/internal/chunk/state"
)

type Export struct{}
type Export struct {
fsa fsadapter.FS
}

func NewExport() *Export { return &Export{} }
func NewExport(fsa fsadapter.FS) *Export { return &Export{fsa: fsa} }

type ExportInfo struct {
IsComplete bool
Expand All @@ -20,14 +28,14 @@ type ExportInfo struct {
Filename string
}

func (e *Export) RestoreState(loc string) (*state.State, error) {
func ExportState(loc string) (*state.State, error) {
zf, err := zip.OpenReader(loc)
if err != nil {
return nil, err
}
defer zf.Close()

var ei = ExportInfo{
ei := ExportInfo{
State: state.New(""),
}

Expand All @@ -36,7 +44,7 @@ func (e *Export) RestoreState(loc string) (*state.State, error) {
case "channels.json":
case "mpims.json":
case "dms.json":
case "users.jsone":
case "users.json":
ei.IsComplete = true
default:
// TODO: handle other files
Expand All @@ -46,3 +54,60 @@ func (e *Export) RestoreState(loc string) (*state.State, error) {

return nil, nil
}

func loadState(st *state.State, basePath string) (io.ReadSeekCloser, error) {
if st == nil {
return nil, fmt.Errorf("fatal: nil state")
}
if !st.IsComplete {
return nil, fmt.Errorf("fatal: incomplete state")
}
rsc, err := st.OpenChunks(basePath)
if err != nil {
return nil, err
}
return rsc, nil
}

func (e *Export) Transform(ctx context.Context, basePath string, st *state.State) error {
// check if the base directory exists
if err := checkDir(basePath); err != nil {
return err
}

rsc, err := loadState(st, basePath)
if err != nil {
return err
}
defer rsc.Close()

pl, err := chunk.NewPlayer(rsc)
if err != nil {
return err
}

// check if it has users
if pl.HasUsers() {
// generate users.json
}
// check if it has channels
if pl.HasChannels() {
// generate channels.json, mpims.json, dms.json
}
// check if it has conversations
channels := pl.AllChannelIDs()
if len(channels) != 0 {
// process channels
}

return nil
}

func checkDir(dir string) error {
if fi, err := os.Stat(dir); err != nil {
return err
} else if !fi.IsDir() {
return fmt.Errorf("%q is not a directory", dir)
}
return nil
}
3 changes: 1 addition & 2 deletions internal/chunk/transform/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (

func TestExport_RestoreState(t *testing.T) {
t.Run("manual", func(t *testing.T) {
e := NewExport()
_, err := e.RestoreState("../../../tmp/kiwi1.zip")
_, err := ExportState("../../../tmp/kiwi1.zip")
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit ab926b2

Please sign in to comment.