-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
379 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package expproc | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/rusq/slackdump/v2/internal/chunk" | ||
) | ||
|
||
type baseproc struct { | ||
dir string | ||
wf io.WriteCloser // processor recording | ||
*chunk.Recorder | ||
} | ||
|
||
func newBaseProc(dir string, filename string) (*baseproc, error) { | ||
if fi, err := os.Stat(dir); err != nil { | ||
return nil, err | ||
} else if !fi.IsDir() { | ||
return nil, fmt.Errorf("not a directory: %s", dir) | ||
} | ||
wf, err := os.Create(filepath.Join(dir, filename)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
r := chunk.NewRecorder(wf) | ||
return &baseproc{dir: dir, wf: wf, Recorder: r}, nil | ||
} | ||
|
||
func (p *baseproc) Close() error { | ||
return p.wf.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package expproc | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/slack-go/slack" | ||
) | ||
|
||
type Channels struct { | ||
*baseproc | ||
fn func(c []slack.Channel) error | ||
} | ||
|
||
func NewChannels(dir string, fn func(c []slack.Channel) error) (*Channels, error) { | ||
p, err := newBaseProc(dir, "channels.json") | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Channels{baseproc: p}, nil | ||
} | ||
|
||
// Channels is called for each channel chunk that is retrieved. Then, the | ||
// function calls the function passed in to the constructor for the channel | ||
// slice. | ||
func (cp *Channels) Channels(ctx context.Context, channels []slack.Channel) error { | ||
if err := cp.baseproc.Channels(ctx, channels); err != nil { | ||
return err | ||
} | ||
return cp.fn(channels) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package expproc | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
|
||
"github.com/rusq/slackdump/v2/internal/chunk" | ||
"github.com/slack-go/slack" | ||
) | ||
|
||
type Conversations struct { | ||
dir string | ||
cw map[string]*baseproc | ||
mu sync.RWMutex | ||
} | ||
|
||
func NewConversation(dir string) (*Conversations, error) { | ||
return &Conversations{dir: dir}, nil | ||
} | ||
|
||
func (p *Conversations) ensure(channelID string) error { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
if _, ok := p.cw[channelID]; ok { | ||
return nil | ||
} | ||
wf, err := os.Create(filepath.Join(p.dir, channelID+".json")) | ||
if err != nil { | ||
return err | ||
} | ||
r := chunk.NewRecorder(wf) | ||
p.cw[channelID] = &baseproc{dir: p.dir, wf: wf, Recorder: r} | ||
return nil | ||
} | ||
|
||
// ChannelInfo is called for each channel that is retrieved. | ||
func (p *Conversations) ChannelInfo(ctx context.Context, ci *slack.Channel, isThread bool) error { | ||
r, err := p.recorder(ci.ID) | ||
if err != nil { | ||
return err | ||
} | ||
return r.ChannelInfo(ctx, ci, isThread) | ||
} | ||
|
||
func (p *Conversations) recorder(channelID string) (*baseproc, error) { | ||
r, ok := p.cw[channelID] | ||
if ok { | ||
return r, nil | ||
} | ||
if err := p.ensure(channelID); err != nil { | ||
return nil, err | ||
} | ||
p.mu.RLock() | ||
defer p.mu.RUnlock() | ||
return p.cw[channelID], nil | ||
} | ||
|
||
// Messages is called for each message that is retrieved. | ||
func (p *Conversations) Messages(ctx context.Context, channelID string, isLast bool, mm []slack.Message) error { | ||
r, err := p.recorder(channelID) | ||
if err != nil { | ||
return err | ||
} | ||
return r.Messages(ctx, channelID, isLast, mm) | ||
} | ||
|
||
// Files is called for each file that is retrieved. The parent message is | ||
// passed in as well. | ||
func (p *Conversations) Files(ctx context.Context, channelID string, parent slack.Message, isThread bool, ff []slack.File) error { | ||
r, err := p.recorder(channelID) | ||
if err != nil { | ||
return err | ||
} | ||
return r.Files(ctx, channelID, parent, isThread, ff) | ||
} | ||
|
||
// ThreadMessages is called for each of the thread messages that are | ||
// retrieved. The parent message is passed in as well. | ||
func (p *Conversations) ThreadMessages(ctx context.Context, channelID string, parent slack.Message, isLast bool, tm []slack.Message) error { | ||
r, err := p.recorder(channelID) | ||
if err != nil { | ||
return err | ||
} | ||
return r.ThreadMessages(ctx, channelID, parent, isLast, tm) | ||
} | ||
|
||
func (p *Conversations) Finalise(channelID string) error { | ||
r, err := p.recorder(channelID) | ||
if err != nil { | ||
return err | ||
} | ||
if err := r.Close(); err != nil { | ||
return err | ||
} | ||
p.mu.Lock() | ||
delete(p.cw, channelID) | ||
p.mu.Unlock() | ||
return nil | ||
} | ||
|
||
func (p *Conversations) Close() error { | ||
for _, r := range p.cw { | ||
if err := r.Close(); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Package expproc implements the export processor interface. The processor | ||
// is responsible for writing the data to disk. | ||
package expproc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package expproc | ||
|
||
type Users struct { | ||
*baseproc | ||
} | ||
|
||
func NewUsers(dir string) (*Users, error) { | ||
p, err := newBaseProc(dir, "users.json") | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Users{baseproc: p}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.