Skip to content

Commit

Permalink
Merge pull request #426 from rusq/view-rev
Browse files Browse the repository at this point in the history
fix reversal of messages on thread link and thread link
  • Loading branch information
rusq authored Jan 31, 2025
2 parents b9b5b7b + 5825eff commit 6e6287d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
46 changes: 30 additions & 16 deletions internal/viewer/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package viewer

import (
"errors"
"fmt"
"io/fs"
"net/http"
"os"
Expand Down Expand Up @@ -54,6 +55,25 @@ func (v *Viewer) newFileHandler(fn func(w http.ResponseWriter, r *http.Request,
}
}

func maybeReverse(mm []slack.Message) error {
if len(mm) == 0 {
return nil
}

first, err := fasttime.TS2int(mm[0].Timestamp)
if err != nil {
return fmt.Errorf("TS2int at 0: %w", err)
}
last, err := fasttime.TS2int(mm[len(mm)-1].Timestamp)
if err != nil {
return fmt.Errorf("TS2int at -1: %w", err)
}
if first > last {
slices.Reverse(mm)
}
return nil
}

func (v *Viewer) channelHandler(w http.ResponseWriter, r *http.Request, id string) {
ctx := r.Context()
lg := v.lg.With("in", "channelHandler", "channel", id)
Expand All @@ -67,22 +87,11 @@ func (v *Viewer) channelHandler(w http.ResponseWriter, r *http.Request, id strin
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if len(mm) > 0 {
first, err := fasttime.TS2int(mm[0].Timestamp)
if err != nil {
lg.ErrorContext(ctx, "TS2int", "idx", 0, "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
last, err := fasttime.TS2int(mm[len(mm)-1].Timestamp)
if err != nil {
lg.ErrorContext(ctx, "TS2int", "idx", -1, "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if first > last {
slices.Reverse(mm)
}

if err := maybeReverse(mm); err != nil {
lg.ErrorContext(ctx, "maybeReverse", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

lg.DebugContext(ctx, "conversation", "id", id, "message_count", len(mm))
Expand Down Expand Up @@ -159,6 +168,11 @@ func (v *Viewer) threadHandler(w http.ResponseWriter, r *http.Request, id string
lg.ErrorContext(ctx, "AllMessages", "error", err, "template", template)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
if err := maybeReverse(msg); err != nil {
lg.ErrorContext(ctx, "maybeReverse", "error", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
page.Messages = msg
}
if err := v.tmpl.ExecuteTemplate(w, template, page); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/viewer/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ <h2>{{ rendername .Conversation }}</h2>
{{define "hx_thread"}}
<h2>Thread: {{ .ThreadID }}</h2>
<p><a id="close-thread" href="#">[X]</a></p>
<p><a id="thread-link" href="{{.Conversation.ID}}/{{ .ThreadID }}">Link to this thread</a></p>
<p><a id="thread-link" href="/archives/{{.Conversation.ID}}/{{ .ThreadID }}#{{.ThreadID}}">Link to this thread</a></p>
{{ range $i, $el := .ThreadMessages }}
<article class="message">
{{ template "render_message" $el }}
Expand Down

0 comments on commit 6e6287d

Please sign in to comment.