Skip to content

Commit

Permalink
organise data
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Mar 25, 2024
1 parent 8fafda8 commit 2a33960
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 34 deletions.
13 changes: 6 additions & 7 deletions internal/viewer/renderer/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *Slack) Render(ctx context.Context, m *slack.Message) (v template.HTML)

var buf strings.Builder
for _, b := range m.Blocks.BlockSet {
fn, ok := blockAction[b.BlockType()]
fn, ok := blockTypeHandlers[b.BlockType()]
if !ok {
slog.WarnContext(ctx, "unhandled block type", "block_type", b.BlockType(), attrMsgID)
maybeprint(b)
Expand All @@ -83,12 +83,6 @@ func maybeprint(b slack.Block) {
}
}

var blockAction = map[slack.MessageBlockType]func(*Slack, slack.Block) (string, error){
slack.MBTRichText: (*Slack).mbtRichText,
slack.MBTImage: (*Slack).mbtImage,
slack.MBTContext: (*Slack).mbtContext,
}

const stackframe = 1

type ErrIncorrectBlockType struct {
Expand Down Expand Up @@ -124,3 +118,8 @@ func NewErrMissingHandler(t any) error {
Type: t,
}
}

// classes
func div(class string, s string) string {
return fmt.Sprintf(`<div class=\"%s\">%s</div>`, class, s)
}
5 changes: 4 additions & 1 deletion internal/viewer/renderer/slack_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ func (*Slack) mbtImage(ib slack.Block) (string, error) {
if !ok {
return "", NewErrIncorrectType(&slack.ImageBlock{}, ib)
}
return fmt.Sprintf(`<figure><img src="%[1]s" alt="%[2]s"><figcaption>%[2]s</figcaption></figure>`, b.ImageURL, b.AltText), nil
return fmt.Sprintf(
`<figure class="slack-image-block"><img class="slack-image" src="%[1]s" alt="%[2]s"><figcaption class="slack-image-caption">%[2]s</figcaption></figure>`,
slack.MBTImage, b.ImageURL, b.AltText,
), nil
}
42 changes: 42 additions & 0 deletions internal/viewer/renderer/slack_image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package renderer

import (
"testing"

"github.com/rusq/slack"
)

func TestSlack_mbtImage(t *testing.T) {
type fields struct {
uu map[string]slack.User
cc map[string]slack.Channel
}
type args struct {
ib slack.Block
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Slack{
uu: tt.fields.uu,
cc: tt.fields.cc,
}
got, err := s.mbtImage(tt.args.ib)
if (err != nil) != tt.wantErr {
t.Errorf("Slack.mbtImage() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Slack.mbtImage() = %v, want %v", got, tt.want)
}
})
}
}
32 changes: 7 additions & 25 deletions internal/viewer/renderer/slack_rich_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ import (
"github.com/rusq/slack"
)

var rteTypeHandlers = map[slack.RichTextElementType]func(*Slack, slack.RichTextElement) (string, error){}

func init() {
rteTypeHandlers[slack.RTESection] = (*Slack).rteSection
rteTypeHandlers[slack.RTEList] = (*Slack).rteList
rteTypeHandlers[slack.RTEQuote] = (*Slack).rteQuote
rteTypeHandlers[slack.RTEPreformatted] = (*Slack).rtePreformatted
}

func (s *Slack) mbtRichText(ib slack.Block) (string, error) {
b, ok := ib.(*slack.RichTextBlock)
if !ok {
Expand Down Expand Up @@ -46,7 +37,7 @@ func (s *Slack) rteSection(ie slack.RichTextElement) (string, error) {
}
var buf strings.Builder
for _, el := range e.Elements {
fn, ok := rtseHandlers[el.RichTextSectionElementType()]
fn, ok := rtseTypeHandlers[el.RichTextSectionElementType()]
if !ok {
return "", NewErrMissingHandler(el.RichTextSectionElementType())
}
Expand All @@ -60,14 +51,6 @@ func (s *Slack) rteSection(ie slack.RichTextElement) (string, error) {
return buf.String(), nil
}

var rtseHandlers = map[slack.RichTextSectionElementType]func(*Slack, slack.RichTextSectionElement) (string, error){
slack.RTSEText: (*Slack).rtseText,
slack.RTSELink: (*Slack).rtseLink,
slack.RTSEUser: (*Slack).rtseUser,
slack.RTSEEmoji: (*Slack).rtseEmoji,
slack.RTSEChannel: (*Slack).rtseChannel,
}

func (s *Slack) rtseText(ie slack.RichTextSectionElement) (string, error) {
e, ok := ie.(*slack.RichTextSectionTextElement)
if !ok {
Expand Down Expand Up @@ -148,7 +131,7 @@ func (s *Slack) rteQuote(ie slack.RichTextElement) (string, error) {
var buf strings.Builder
buf.WriteString("<blockquote>")
for _, el := range e.Elements {
fn, ok := rtseHandlers[el.RichTextSectionElementType()]
fn, ok := rtseTypeHandlers[el.RichTextSectionElementType()]
if !ok {
return "", NewErrMissingHandler(el.RichTextSectionElementType())
}
Expand All @@ -170,7 +153,7 @@ func (s *Slack) rtePreformatted(ie slack.RichTextElement) (string, error) {
var buf strings.Builder
buf.WriteString("<pre>")
for _, el := range e.Elements {
fn, ok := rtseHandlers[el.RichTextSectionElementType()]
fn, ok := rtseTypeHandlers[el.RichTextSectionElementType()]
if !ok {
return "", NewErrMissingHandler(el.RichTextSectionElementType())
}
Expand All @@ -190,8 +173,8 @@ func (s *Slack) rtseUser(ie slack.RichTextSectionElement) (string, error) {
return "", NewErrIncorrectType(&slack.RichTextSectionUserElement{}, ie)
}
var name string
u, ok := s.uu[e.UserID]
if ok {

if u, ok := s.uu[e.UserID]; s.uu != nil && ok {
name = u.Name
} else {
slog.Warn("user not found", "user_id", e.UserID, "user", u)
Expand All @@ -218,13 +201,12 @@ func (s *Slack) rtseChannel(ie slack.RichTextSectionElement) (string, error) {
return "", NewErrIncorrectType(&slack.RichTextSectionChannelElement{}, ie)
}
var name string
c, ok := s.uu[e.ChannelID]
if ok {
if c, ok := s.cc[e.ChannelID]; s.cc != nil && ok {
name = c.Name
} else {
slog.Warn("channel not found", "channel_id", e.ChannelID)
name = e.ChannelID
}

return applyStyle(fmt.Sprintf("<#%s>", name), e.Style), nil
return div("slack-rtse-channel", applyStyle(fmt.Sprintf("<#%s>", name), e.Style)), nil
}
10 changes: 9 additions & 1 deletion internal/viewer/renderer/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ func TestSlack_Render(t *testing.T) {
args args
wantV template.HTML
}{
{
"simple message",
&Slack{},
args{
m: load(t, fxtrRtseText),
},
template.HTML("New message"),
},
{
"nested lists",
&Slack{},
args{
m: nestedLists,
},
template.HTML(`numerated:<br><ol><li>First (1)</li><li>Second(2)</li></ol><ol><ol><li>Nested (2.a)</li><li>Nested (2.b)</li></ol></ol><ul><ul><ul><li>Nexted bullet point</li></ul></ul></ul><ul><ul><ul><ul><li>Another nested bullet</li></ul></ul></ul></ul><ol><ol><ol><ol><ol><li>Nested enumeration</li></ol></ol></ol></ol></ol>`),
template.HTML(`Enumerated:<br><ol><li>First (1)</li><li>Second(2)</li></ol><ol><ol><li>Nested (2.a)</li><li>Nested (2.b)</li></ol></ol><ul><ul><ul><li>Nexted bullet point</li></ul></ul></ul><ul><ul><ul><ul><li>Another nested bullet</li></ul></ul></ul></ul><ol><ol><ol><ol><ol><li>Nested enumeration</li></ol></ol></ol></ol></ol>`),
},
}
for _, tt := range tests {
Expand Down
55 changes: 55 additions & 0 deletions internal/viewer/renderer/slack_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package renderer

import "github.com/rusq/slack"

var (
blockTypeHandlers = map[slack.MessageBlockType]func(*Slack, slack.Block) (string, error){
slack.MBTRichText: (*Slack).mbtRichText,
slack.MBTImage: (*Slack).mbtImage,
slack.MBTContext: (*Slack).mbtContext,
}

blockTypeClass = map[slack.MessageBlockType]string{
slack.MBTRichText: "slack-rich-text-block",
slack.MBTImage: "slack-image-block",
slack.MBTContext: "slack-context-block",
}
)

// rte - rich text element
var (
rteTypeHandlers = map[slack.RichTextElementType]func(*Slack, slack.RichTextElement) (string, error){}

rteTypeClass = map[slack.RichTextElementType]string{
slack.RTESection: "slack-rich-text-section",
slack.RTEList: "slack-rich-text-list",
slack.RTEQuote: "slack-rich-text-quote",
slack.RTEPreformatted: "slack-rich-text-preformatted",
}
)

func init() {
rteTypeHandlers[slack.RTESection] = (*Slack).rteSection
rteTypeHandlers[slack.RTEList] = (*Slack).rteList
rteTypeHandlers[slack.RTEQuote] = (*Slack).rteQuote
rteTypeHandlers[slack.RTEPreformatted] = (*Slack).rtePreformatted
}

// rtse - rich text section element
var (
rtseTypeHandlers = map[slack.RichTextSectionElementType]func(*Slack, slack.RichTextSectionElement) (string, error){
slack.RTSEText: (*Slack).rtseText,
slack.RTSELink: (*Slack).rtseLink,
slack.RTSEUser: (*Slack).rtseUser,
slack.RTSEEmoji: (*Slack).rtseEmoji,
slack.RTSEChannel: (*Slack).rtseChannel,
}

rtseTypeClass = map[slack.RichTextSectionElementType]string{
slack.RTSEText: "slack-rich-text-section-text",
slack.RTSELink: "slack-rich-text-section-link",
slack.RTSEUser: "slack-rich-text-section-user",
slack.RTSEEmoji: "slack-rich-text-section-emoji",
slack.RTSEChannel: "slack-rich-text-section-channel",
}
)

0 comments on commit 2a33960

Please sign in to comment.