Skip to content

Commit

Permalink
add user handler
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Mar 25, 2024
1 parent 6764e7c commit 9a5fe94
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 46 deletions.
1 change: 0 additions & 1 deletion internal/viewer/parser.go

This file was deleted.

57 changes: 57 additions & 0 deletions internal/viewer/renderer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package viewer

import (
"html"
"html/template"
"log/slog"
"strings"

"github.com/rusq/slack"
"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
"github.com/yuin/goldmark/extension"
gparser "github.com/yuin/goldmark/parser"
ghtml "github.com/yuin/goldmark/renderer/html"
)

type Renderer interface {
RenderText(s string) (v template.HTML)
Render(m *slack.Message) (v template.HTML)
}

type goldmrk struct {
r goldmark.Markdown
}

func newGold() *goldmrk {
md := goldmark.New(
goldmark.WithExtensions(extension.GFM, emoji.Emoji, extension.DefinitionList),
goldmark.WithParserOptions(
gparser.WithAutoHeadingID(),
),
goldmark.WithRendererOptions(
ghtml.WithHardWraps(),
ghtml.WithXHTML(),
),
)
return &goldmrk{r: md}
}

func (g *goldmrk) Render(s string) (v template.HTML) {
var buf strings.Builder
if err := g.r.Convert([]byte(s), &buf); err != nil {
slog.Debug("error", "error", err)
return template.HTML(s)
}
return template.HTML(buf.String())
}

type debugrender struct{}

func (d *debugrender) RenderText(s string) (v template.HTML) {
return template.HTML("<pre>" + html.EscapeString(s) + "</pre>")
}

func (d *debugrender) Render(m *slack.Message) (v template.HTML) {
return template.HTML("<pre>" + html.EscapeString(m.Text) + "</pre>")
}
35 changes: 34 additions & 1 deletion internal/viewer/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ <h2>{{ rendername .Conversation }}</h2>
// Load thread messages into .thread div here
});
});
document.querySelectorAll('.message-sender a').forEach(function (link) {
link.addEventListener('click', function (e) {
e.preventDefault();
// document.querySelector('.conversations').style.display = 'none';
// document.querySelector('.thread').style.display = 'block';
document.querySelector('.conversations').style.flex = '0 0 40%';
document.querySelector('.thread').style.display = 'block';
document.querySelector('.thread').style.flex = '1';
// Load thread messages into .thread div here
});
});
</script>
{{ end }}

Expand All @@ -109,7 +120,9 @@ <h2>Thread: {{ .ThreadID }}</h2>

{{ define "render_message" }}
<div class="message-header">
<span class="message-sender">{{ displayname .User }}</span>
<span class="message-sender">
<a href="#" hx-get="/team/{{ .User }}" hx-target="#thread"> {{ displayname .User }} </a>
</span>
<span class="message-timestamp">{{ time $.Timestamp }}</span>
</div>
<div class="message-content">
Expand All @@ -123,6 +136,7 @@ <h2>Thread: {{ .ThreadID }}</h2>
display: flex;
height: 97vh;
}

.subtitle {
display: block;
margin-bottom: 1em;
Expand All @@ -146,3 +160,22 @@ <h2>Thread: {{ .ThreadID }}</h2>
}
</style>
{{ end }}

{{ define "hx_user" }}
<h2>User: {{ displayname .ID }}</h2>
<p>Close: <a id="close-user" href="#">X</a></p>
<article class="user">
{{ if . }}
{{ displayname .ID }}
{{ else }}
<p>Unknown</p>
{{ end }}
</article>
<script>
document.getElementById('close-user').addEventListener('click', function (e) {
e.preventDefault();
document.querySelector('.conversations').style.flex = '1';
document.querySelector('.thread').style.display = 'none';
});
</script>
{{ end }}
64 changes: 20 additions & 44 deletions internal/viewer/viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"embed"
"errors"
"html"
"html/template"
"log/slog"
"net/http"
Expand All @@ -14,11 +13,6 @@ import (

"github.com/go-chi/chi/v5/middleware"
"github.com/rusq/slack"
"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
"github.com/yuin/goldmark/extension"
gparser "github.com/yuin/goldmark/parser"
ghtml "github.com/yuin/goldmark/renderer/html"
"golang.org/x/exp/slices"

"github.com/rusq/slackdump/v3/internal/chunk"
Expand Down Expand Up @@ -87,7 +81,7 @@ func New(ctx context.Context, dir *chunk.Directory, addr string) (*Viewer, error
"rendername": v.name,
"displayname": v.um.DisplayName,
"time": localtime,
"markdown": v.r.Render,
"markdown": v.r.RenderText,
"is_thread_start": st.IsThreadStart,
},
).ParseFS(fsys, "templates/*.html"))
Expand All @@ -102,6 +96,7 @@ func New(ctx context.Context, dir *chunk.Directory, addr string) (*Viewer, error
// https: //ora600.slack.com/archives/DHMAB25DY/p1710063528879959
mux.HandleFunc("/archives/{id}/{ts}", v.newFileHandler(v.threadHandler))
mux.HandleFunc("/files/{id}", v.fileHandler)
mux.HandleFunc("/team/{user_id}", v.userHandler)
v.srv = &http.Server{
Addr: addr,
Handler: middleware.Logger(mux),
Expand All @@ -110,43 +105,6 @@ func New(ctx context.Context, dir *chunk.Directory, addr string) (*Viewer, error
return v, nil
}

type goldmrk struct {
r goldmark.Markdown
}

func newGold() *goldmrk {
md := goldmark.New(
goldmark.WithExtensions(extension.GFM, emoji.Emoji, extension.DefinitionList),
goldmark.WithParserOptions(
gparser.WithAutoHeadingID(),
),
goldmark.WithRendererOptions(
ghtml.WithHardWraps(),
ghtml.WithXHTML(),
),
)
return &goldmrk{r: md}
}

type Renderer interface {
Render(s string) (v template.HTML)
}

func (g *goldmrk) Render(s string) (v template.HTML) {
var buf strings.Builder
if err := g.r.Convert([]byte(s), &buf); err != nil {
slog.Debug("error", "error", err)
return template.HTML(s)
}
return template.HTML(buf.String())
}

type debugrender struct{}

func (d *debugrender) Render(s string) (v template.HTML) {
return template.HTML("<pre>" + html.EscapeString(s) + "</pre>")
}

func init() {
slog.SetLogLoggerLevel(slog.LevelDebug)
}
Expand Down Expand Up @@ -290,6 +248,24 @@ func (v *Viewer) threadHandler(w http.ResponseWriter, r *http.Request, id string
func (v *Viewer) fileHandler(w http.ResponseWriter, r *http.Request) {
}

func (v *Viewer) userHandler(w http.ResponseWriter, r *http.Request) {
uid := r.PathValue("user_id")
if uid == "" {
http.NotFound(w, r)
return
}
u, found := v.um[uid]
if !found {
http.NotFound(w, r)
return
}

if err := v.tmpl.ExecuteTemplate(w, "hx_user", u); err != nil {
v.lg.Printf("error: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

func localtime(ts string) string {
t, err := st.ParseSlackTS(ts)
if err != nil {
Expand Down

0 comments on commit 9a5fe94

Please sign in to comment.