Skip to content

Commit

Permalink
first draft of the renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Mar 25, 2024
1 parent 48d22a2 commit 90d6fbe
Show file tree
Hide file tree
Showing 6 changed files with 529 additions and 147 deletions.
133 changes: 133 additions & 0 deletions internal/viewer/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package viewer

import (
"net/http"

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

func (v *Viewer) indexHandler(w http.ResponseWriter, r *http.Request) {
var page = struct {
Conversation slack.Channel
Name string
channels
}{
Conversation: slack.Channel{}, //blank.
Name: v.d.Name(),
channels: v.ch,
}
if err := v.tmpl.ExecuteTemplate(w, "index.html", page); err != nil {
v.lg.Printf("error: %v", err)
//http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

func (v *Viewer) newFileHandler(fn func(w http.ResponseWriter, r *http.Request, id string, f *chunk.File)) 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)
}
}

func (v *Viewer) channelHandler(w http.ResponseWriter, r *http.Request, id string, f *chunk.File) {
mm, err := f.AllMessages(id)
if err != nil {
v.lg.Printf("%s: error: %v", id, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
slices.Reverse(mm)

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

ci, err := f.ChannelInfo(id)
if err != nil {
v.lg.Printf("error: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

var page = struct {
Conversation slack.Channel
Messages []slack.Message
}{
Conversation: *ci,
Messages: mm,
}
if err := v.tmpl.ExecuteTemplate(w, "hx_conversation", page); err != nil {
v.lg.Printf("error: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

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

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

ci, err := f.ChannelInfo(id)
if err != nil {
v.lg.Printf("error: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var page = struct {
Conversation slack.Channel
Messages []slack.Message
ThreadID string
}{
ThreadID: ts,
Conversation: *ci,
Messages: mm,
}
if err := v.tmpl.ExecuteTemplate(w, "hx_thread", page); err != nil {
v.lg.Printf("error: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

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
}
spew.Dump(u)

if err := v.tmpl.ExecuteTemplate(w, "hx_user", u); err != nil {
v.lg.Printf("error: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
129 changes: 129 additions & 0 deletions internal/viewer/renderer/slack_fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,133 @@ const (
]
}
`
fxtrMsgNestedLists = `{
"client_msg_id": "229a6d45-a202-4c1f-86bc-24bded55cc0a",
"type": "message",
"user": "UHSD97ZA5",
"text": "Enumerated:\n1. First (1)\n2. Second(2)\n a. Nested (2.a)\n b. Nested (2.b)\n ▪︎ Nexted bullet point\n • Another nested bullet\n a. Nested enumeration",
"ts": "1710144832.176569",
"team": "THY5HTZ8U",
"replace_original": false,
"delete_original": false,
"metadata": {
"event_type": "",
"event_payload": null
},
"blocks": [
{
"type": "rich_text",
"block_id": "hqvjh",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "Enumerated:\n"
}
]
},
{
"type": "rich_text_list",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "First (1)"
}
]
},
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "Second(2)"
}
]
}
],
"style": "ordered",
"indent": 0
},
{
"type": "rich_text_list",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "Nested (2.a)"
}
]
},
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "Nested (2.b)"
}
]
}
],
"style": "ordered",
"indent": 1
},
{
"type": "rich_text_list",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "Nexted bullet point"
}
]
}
],
"style": "bullet",
"indent": 2
},
{
"type": "rich_text_list",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "Another nested bullet"
}
]
}
],
"style": "bullet",
"indent": 3
},
{
"type": "rich_text_list",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "Nested enumeration"
}
]
}
],
"style": "ordered",
"indent": 4
}
]
}
]
}`
)
Loading

0 comments on commit 90d6fbe

Please sign in to comment.