Skip to content

Commit

Permalink
fix user display in export renders
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Mar 25, 2024
1 parent 3c6362a commit e7e208e
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 24 deletions.
94 changes: 93 additions & 1 deletion internal/structures/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"reflect"
"strings"

Expand Down Expand Up @@ -117,7 +118,7 @@ func (idx *ExportIndex) Marshal(fs fsadapter.FS) error {
return nil
}

func serializeToFS(fs fsadapter.FS, filename string, data interface{}) error {
func serializeToFS(fs fsadapter.FS, filename string, data any) error {
f, err := fs.Create(filename)
if err != nil {
return err
Expand All @@ -127,3 +128,94 @@ func serializeToFS(fs fsadapter.FS, filename string, data interface{}) error {
enc.SetIndent("", " ")
return enc.Encode(data)
}

// loadFromFS unmarshals the file with filename from the fsys into data.
func loadFromFS(fsys fs.FS, filename string, data any) error {
f, err := fsys.Open(filename)
if err != nil {
return err
}
defer f.Close()
dec := json.NewDecoder(f)
return dec.Decode(data)
}

func (idx *ExportIndex) Unmarshal(fsys fs.FS) error {
var newIdx ExportIndex

st := reflect.TypeOf(*idx)
val := reflect.ValueOf(&newIdx).Elem()
for i := 0; i < st.NumField(); i++ {
field := st.Field(i)
tg := field.Tag.Get("filename")
if tg == "" {
continue
}
filename, _, _ := strings.Cut(tg, ",")
if err := loadFromFS(fsys, filename, val.Field(i).Addr().Interface()); err != nil {
if errors.Is(err, fs.ErrNotExist) {
continue
}
return err
}
}
*idx = newIdx
return nil
}

// Restore restores the index to the original state (minus the lost DM
// data).
func (idx *ExportIndex) Restore() []slack.Channel {
me := idx.me()
var chans = make([]slack.Channel, 0, len(idx.Channels)+len(idx.Groups)+len(idx.MPIMs)+len(idx.DMs))
chans = append(chans, idx.Channels...)
chans = append(chans, idx.Groups...)
chans = append(chans, idx.MPIMs...)
for _, dm := range idx.DMs {
chans = append(chans, slack.Channel{
GroupConversation: slack.GroupConversation{
Conversation: slack.Conversation{
ID: dm.ID,
Created: slack.JSONTime(dm.Created),
IsIM: true,
User: notMe(me, dm.Members),
},
Members: dm.Members,
},
})
}
return chans
}

func notMe(me string, members []string) string {
for _, m := range members {
if m != me {
return m
}
}
return ""
}

// me attempts to identify the current user in the index. It uses the DMs of
// the index. If DMs are empty, or it's unable to identify the user, it
// returns an empty string. The user, who appears in "Members" slices the
// most, is considered the current user.
func (idx *ExportIndex) me() string {
var counts = make(map[string]int)
for _, dm := range idx.DMs {
for _, m := range dm.Members {
counts[m]++
}
}
var (
max int
id string
)
for k, v := range counts {
if v > max {
max = v
id = k
}
}
return id
}
2 changes: 1 addition & 1 deletion internal/viewer/renderer/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *Slack) Render(ctx context.Context, m *slack.Message) (v template.HTML)
var buf strings.Builder

if len(m.Blocks.BlockSet) == 0 {
s.RenderText(ctx, m.Text)
fmt.Fprint(&buf, parseSlackMd(m.Text))
} else {
s.renderBlocks(ctx, &buf, m.Timestamp, m.Blocks.BlockSet)
}
Expand Down
48 changes: 27 additions & 21 deletions internal/viewer/source/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,49 @@ import (

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

// Export implements viewer.Sourcer for the zip file Slack export format.
type Export struct {
fs fs.FS
channels []slack.Channel
chanNames map[string]string // maps the channel id to the channel name.
name string // name of the file
idx structures.ExportIndex
filestorage
}

func NewExport(fsys fs.FS, name string) (*Export, error) {
var idx structures.ExportIndex
if err := idx.Unmarshal(fsys); err != nil {
return nil, err
}
chans := idx.Restore()
z := &Export{
fs: fsys,
name: name,
fs: fsys,
name: name,
idx: idx,
channels: chans,
chanNames: make(map[string]string, len(chans)),
}

// initialise channels for quick lookup
c, err := z.Channels()
if err != nil {
return nil, err
}
z.chanNames = make(map[string]string, len(c))
for _, ch := range c {
z.chanNames[ch.ID] = ch.Name
for _, ch := range z.channels {
z.chanNames[ch.ID] = structures.NVL(ch.Name, ch.ID)
}
// determine files path
rslv, err := exportType(fsys)
fst, err := loadStorage(fsys)
if err != nil {
return nil, err
}
z.filestorage = rslv
z.filestorage = fst

return z, nil
}

// exportType determines the type of the file storage used.
func exportType(fsys fs.FS) (filestorage, error) {
// loadStorage determines the type of the file storage used and initialises
// appropriate filestorage implementation.
func loadStorage(fsys fs.FS) (filestorage, error) {
if _, err := fs.Stat(fsys, "__uploads"); err == nil {
return newMattermostStorage(fsys)
}
Expand All @@ -55,16 +61,11 @@ func exportType(fsys fs.FS) (filestorage, error) {
}

func (e *Export) Channels() ([]slack.Channel, error) {
cc, err := unmarshal[[]slack.Channel](e.fs, "channels.json")
if err != nil {
return nil, err
}
// TODO: check dms.json and groups.json
return cc, nil
return e.channels, nil
}

func (e *Export) Users() ([]slack.User, error) {
return unmarshal[[]slack.User](e.fs, "users.json")
return e.idx.Users, nil
}

func (e *Export) Close() error {
Expand All @@ -85,6 +86,11 @@ func (e *Export) AllMessages(channelID string) ([]slack.Message, error) {
if !ok {
return nil, fmt.Errorf("%w: %s", fs.ErrNotExist, channelID)
}
_, err := fs.Stat(e.fs, name)
if err != nil {
return nil, fmt.Errorf("%w: %s", fs.ErrNotExist, name)
}

var mm []slack.Message
if err := fs.WalkDir(e.fs, name, func(pth string, d fs.DirEntry, err error) error {
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/viewer/templates/file.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</div>
{{ else }}
{{ if (eq (mimetype $f.Mimetype) "image") }}
<img class="file-image" src="{{ $path }}" alt="{{ $f.Name }}" />
<a href="{{ $path }}" target="_blank"><img class="file-image" src="{{ $path }}" alt="{{ $f.Name }}" /></a>
{{ else if (eq (mimetype $f.Mimetype) "video") }}
<video class="file-video" controls>
<source src="{{ $path }}" type="{{ $f.Mimetype }}">
Expand Down

0 comments on commit e7e208e

Please sign in to comment.