Skip to content

Commit

Permalink
fixes for the user display
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Mar 25, 2024
1 parent 058228e commit f2faeec
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 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(dir, listenAddr)
v, err := viewer.New(ctx, dir, listenAddr)
if err != nil {
base.SetExitStatus(base.SApplicationError)
return err
Expand Down
56 changes: 41 additions & 15 deletions internal/viewer/viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@
package viewer

import (
"context"
"embed"
"errors"
"html/template"
"net/http"
"strings"

"github.com/rusq/slack"
"github.com/rusq/slackdump/v3/internal/chunk"
"github.com/rusq/slackdump/v3/internal/structures"
"github.com/rusq/slackdump/v3/logger"
)

//go:embed templates
var fsys embed.FS

var tmpl = template.Must(template.New("").Funcs(
template.FuncMap{
"rendername": name,
},
).ParseFS(fsys, "templates/*.html"))

type Viewer struct {
ch channels
d *chunk.Directory
srv *http.Server
ch channels
um structures.UserIndex
d *chunk.Directory
srv *http.Server
lg logger.Interface
tmpl *template.Template
}

type channels struct {
Expand All @@ -33,7 +34,7 @@ type channels struct {
DM []slack.Channel
}

func New(dir *chunk.Directory, addr string) (*Viewer, error) {
func New(ctx context.Context, dir *chunk.Directory, addr string) (*Viewer, error) {
all, err := dir.Channels()
if err != nil {
return nil, err
Expand All @@ -52,10 +53,25 @@ func New(dir *chunk.Directory, addr string) (*Viewer, error) {
cc.Public = append(cc.Public, c)
}
}
uu, err := dir.Users()
if err != nil {
return nil, err
}

v := &Viewer{
d: dir,
ch: cc,
um: structures.NewUserIndex(uu),
lg: logger.FromContext(ctx),
}
// postinit
{
var tmpl = template.Must(template.New("").Funcs(
template.FuncMap{
"rendername": v.name,
},
).ParseFS(fsys, "templates/*.html"))
v.tmpl = tmpl
}

mux := http.NewServeMux()
Expand All @@ -79,12 +95,22 @@ func (v *Viewer) ListenAndServe() error {
}

func (v *Viewer) Close() error {
return v.srv.Close()
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)
}
v.lg.Debug("server closed")
if ee != nil {
v.lg.Printf("errors: %v", ee)
}
return ee
}

func (v *Viewer) indexHandler(w http.ResponseWriter, r *http.Request) {
err := tmpl.ExecuteTemplate(w, "index.html", v.ch)
if err != nil {
if err := v.tmpl.ExecuteTemplate(w, "index.html", v.ch); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
Expand Down Expand Up @@ -113,11 +139,11 @@ func channelType(ch slack.Channel) int {
}
}

func name(ch slack.Channel) (who string) {
func (v *Viewer) name(ch slack.Channel) (who string) {
t := channelType(ch)
switch t {
case CIM:
who = "@" + ch.NameNormalized
who = "@" + v.um.DisplayName(ch.User)
case CMPIM:
who = strings.Replace(ch.Purpose.Value, " messaging with", "", -1)
case CPrivate:
Expand Down

0 comments on commit f2faeec

Please sign in to comment.