Skip to content

Commit

Permalink
decouple chunk.Directory
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Mar 25, 2024
1 parent 2a33960 commit b757b23
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 31 deletions.
2 changes: 1 addition & 1 deletion cmd/slackdump/internal/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func RunView(ctx context.Context, cmd *base.Command, args []string) error {
}
defer dir.Close()

v, err := viewer.New(ctx, dir, listenAddr)
v, err := viewer.New(ctx, viewer.NewChunkRetriever(dir), listenAddr)
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
Expand Down
72 changes: 72 additions & 0 deletions internal/viewer/adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package viewer

import (
"github.com/rusq/slack"
"github.com/rusq/slackdump/v3/internal/chunk"
)

// Retriever is an interface for retrieving data from different sources.
type Retriever interface {
// Name should return the name of the retriever underlying media, i.e.
// directory or archive.
Name() string
// Channels should return all channels.
Channels() ([]slack.Channel, error)
// Users should return all users.
Users() ([]slack.User, error)
// AllMessages should return all messages for the given channel id.
AllMessages(channelID string) ([]slack.Message, error)
// AllThreadMessages should return all messages for the given tuple
// (channelID, threadID).
AllThreadMessages(channelID, threadID string) ([]slack.Message, error)
// ChannelInfo should return the channel information for the given channel
// id.
ChannelInfo(channelID string) (*slack.Channel, error)
}

type ChunkRetriever struct {
d *chunk.Directory
}

func NewChunkRetriever(d *chunk.Directory) *ChunkRetriever {
return &ChunkRetriever{d: d}
}

func (c *ChunkRetriever) AllMessages(channelID string) ([]slack.Message, error) {
f, err := c.d.Open(chunk.ToFileID(channelID, "", false))
if err != nil {
return nil, err
}
defer f.Close()
return f.AllMessages(channelID)
}

func (c *ChunkRetriever) AllThreadMessages(channelID, threadID string) ([]slack.Message, error) {
f, err := c.d.Open(chunk.ToFileID(channelID, "", false))
if err != nil {
return nil, err
}
defer f.Close()
return f.AllThreadMessages(channelID, threadID)
}

func (c *ChunkRetriever) ChannelInfo(channelID string) (*slack.Channel, error) {
f, err := c.d.Open(chunk.ToFileID(channelID, "", false))
if err != nil {
return nil, err
}
defer f.Close()
return f.ChannelInfo(channelID)
}

func (c *ChunkRetriever) Channels() ([]slack.Channel, error) {
return c.d.Channels()
}

func (c *ChunkRetriever) Name() string {
return c.d.Name()
}

func (c *ChunkRetriever) Users() ([]slack.User, error) {
return c.d.Users()
}
26 changes: 9 additions & 17 deletions internal/viewer/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/davecgh/go-spew/spew"
"github.com/rusq/slack"
"github.com/rusq/slackdump/v3/internal/chunk"
"golang.org/x/exp/slices"
)

Expand All @@ -16,7 +15,7 @@ func (v *Viewer) indexHandler(w http.ResponseWriter, r *http.Request) {
channels
}{
Conversation: slack.Channel{}, //blank.
Name: v.d.Name(),
Name: v.rtr.Name(),
channels: v.ch,
}
if err := v.tmpl.ExecuteTemplate(w, "index.html", page); err != nil {
Expand All @@ -25,26 +24,19 @@ func (v *Viewer) indexHandler(w http.ResponseWriter, r *http.Request) {
}
}

func (v *Viewer) newFileHandler(fn func(w http.ResponseWriter, r *http.Request, id string, f *chunk.File)) http.HandlerFunc {
func (v *Viewer) newFileHandler(fn func(w http.ResponseWriter, r *http.Request, id string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if id == "" {
http.NotFound(w, r)
return
}
f, err := v.d.Open(chunk.ToFileID(id, "", false))
if err != nil {
v.lg.Printf("%s: error: %v", id, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer f.Close()
fn(w, r, id, f)
fn(w, r, id)
}
}

func (v *Viewer) channelHandler(w http.ResponseWriter, r *http.Request, id string, f *chunk.File) {
mm, err := f.AllMessages(id)
func (v *Viewer) channelHandler(w http.ResponseWriter, r *http.Request, id string) {
mm, err := v.rtr.AllMessages(id)
if err != nil {
v.lg.Printf("%s: error: %v", id, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -54,7 +46,7 @@ func (v *Viewer) channelHandler(w http.ResponseWriter, r *http.Request, id strin

v.lg.Debugf("conversation: %s, got %d messages", id, len(mm))

ci, err := f.ChannelInfo(id)
ci, err := v.rtr.ChannelInfo(id)
if err != nil {
v.lg.Printf("error: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -74,13 +66,13 @@ func (v *Viewer) channelHandler(w http.ResponseWriter, r *http.Request, id strin
}
}

func (v *Viewer) threadHandler(w http.ResponseWriter, r *http.Request, id string, f *chunk.File) {
func (v *Viewer) threadHandler(w http.ResponseWriter, r *http.Request, id string) {
ts := r.PathValue("ts")
if ts == "" {
http.NotFound(w, r)
return
}
mm, err := f.AllThreadMessages(id, ts)
mm, err := v.rtr.AllThreadMessages(id, ts)
if err != nil {
v.lg.Printf("%s: error: %v", id, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -89,7 +81,7 @@ func (v *Viewer) threadHandler(w http.ResponseWriter, r *http.Request, id string

v.lg.Debugf("conversation: %s, thread: %s, got %d messages", id, ts, len(mm))

ci, err := f.ChannelInfo(id)
ci, err := v.rtr.ChannelInfo(id)
if err != nil {
v.lg.Printf("error: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
22 changes: 9 additions & 13 deletions internal/viewer/viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/go-chi/chi/v5/middleware"
"github.com/rusq/slack"

"github.com/rusq/slackdump/v3/internal/chunk"
st "github.com/rusq/slackdump/v3/internal/structures"
"github.com/rusq/slackdump/v3/internal/viewer/renderer"
"github.com/rusq/slackdump/v3/logger"
Expand All @@ -27,7 +26,7 @@ type Viewer struct {
// data
ch channels
um st.UserIndex
d *chunk.Directory
rtr Retriever
tmpl *template.Template

// handles
Expand All @@ -43,8 +42,8 @@ type channels struct {
DM []slack.Channel
}

func New(ctx context.Context, dir *chunk.Directory, addr string) (*Viewer, error) {
all, err := dir.Channels()
func New(ctx context.Context, r Retriever, addr string) (*Viewer, error) {
all, err := r.Channels()
if err != nil {
return nil, err
}
Expand All @@ -62,18 +61,18 @@ func New(ctx context.Context, dir *chunk.Directory, addr string) (*Viewer, error
cc.Public = append(cc.Public, c)
}
}
uu, err := dir.Users()
uu, err := r.Users()
if err != nil {
return nil, err
}
sr := renderer.NewSlack(renderer.WithUsers(indexusers(uu)), renderer.WithChannels(indexchannels(all)))
// sr := &renderer.Debug{}
v := &Viewer{
d: dir,
ch: cc,
um: st.NewUserIndex(uu),
lg: logger.FromContext(ctx),
r: sr,
rtr: r,
ch: cc,
um: st.NewUserIndex(uu),
lg: logger.FromContext(ctx),
r: sr,
}
// postinit
{
Expand Down Expand Up @@ -117,9 +116,6 @@ func (v *Viewer) ListenAndServe() error {

func (v *Viewer) Close() error {
var ee error
if err := v.d.Close(); err != nil {
ee = errors.Join(err)
}
if err := v.srv.Close(); err != nil {
ee = errors.Join(err)
}
Expand Down

0 comments on commit b757b23

Please sign in to comment.