Skip to content

Commit

Permalink
implement "closing" tags, add color and usergroup support
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Jul 12, 2024
1 parent fde69ee commit 4d54c9e
Show file tree
Hide file tree
Showing 14 changed files with 337 additions and 91 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
github.com/rusq/fsadapter v1.0.2
github.com/rusq/osenv/v2 v2.0.1
github.com/rusq/rbubbles v0.0.2
github.com/rusq/slack v0.9.6-0.20240326123841-3c46349abfdc
github.com/rusq/slack v0.9.6-0.20240712095442-5a0e2e405a99
github.com/rusq/slackauth v0.1.1
github.com/rusq/tracer v1.0.1
github.com/schollz/progressbar/v3 v3.13.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ github.com/rusq/secure v0.0.4 h1:svpiZHfHnx89eEDCCFI9OXG1Y8hL9kUWUG6fJbrWUOI=
github.com/rusq/secure v0.0.4/go.mod h1:F1QilMKreuFRjov0UY7DZSIXn77/8RqMVGu2zV0RtqY=
github.com/rusq/slack v0.9.6-0.20240326123841-3c46349abfdc h1:lWnXSrpjXMwZFRyfxb5yfm753Fz4NGpsbEpNv/21AH0=
github.com/rusq/slack v0.9.6-0.20240326123841-3c46349abfdc/go.mod h1:9O0zQAFN6W47z4KpTQbe6vOHOzBO76vMg1+gthPwaTI=
github.com/rusq/slack v0.9.6-0.20240712095442-5a0e2e405a99 h1:dqEcNs9hMc2PiMwhw8+Zi3wF8GNUHIK5OItZ01iM0Vk=
github.com/rusq/slack v0.9.6-0.20240712095442-5a0e2e405a99/go.mod h1:9O0zQAFN6W47z4KpTQbe6vOHOzBO76vMg1+gthPwaTI=
github.com/rusq/slackauth v0.1.1 h1:nTAjZQ6UHoGzfV02IQQUyUrAR0JeEqfy6snD7D6cMzk=
github.com/rusq/slackauth v0.1.1/go.mod h1:0fWxVftSfAgULWCcflvy4q20/qY39aB8v0jLDaDLejM=
github.com/rusq/tracer v1.0.1 h1:5u4PCV8NGO97VuAINQA4gOVRkPoqHimLE2jpezRVNMU=
Expand Down
3 changes: 2 additions & 1 deletion internal/viewer/renderer/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ func (s *Slack) renderBlocks(ctx context.Context, buf *strings.Builder, msgTS st
maybeprint(b)
continue
}
html, err := fn(s, b)
html, cl, err := fn(s, b)
if err != nil {
slog.ErrorContext(ctx, "error rendering block", "error", err, "block_type", b.BlockType(), attrMsgID)
maybeprint(b)
continue
}
buf.WriteString(html)
buf.WriteString(cl)
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/viewer/renderer/slack_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
},
*/

func (*Slack) mbtAction(ib slack.Block) (string, error) {
func (*Slack) mbtAction(ib slack.Block) (string, string, error) {
b, ok := ib.(*slack.ActionBlock)
if !ok {
return "", NewErrIncorrectType(&slack.ActionBlock{}, ib)
return "", "", NewErrIncorrectType(&slack.ActionBlock{}, ib)
}
var buf strings.Builder
for _, e := range b.Elements.ElementSet {
Expand All @@ -38,5 +38,5 @@ func (*Slack) mbtAction(ib slack.Block) (string, error) {
fmt.Fprintf(&buf, "[ELEMENT: %T]", e)
}
}
return div("slack-actions", buf.String()), nil
return div("slack-actions", buf.String()), "", nil
}
29 changes: 15 additions & 14 deletions internal/viewer/renderer/slack_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,45 @@ import (
"github.com/rusq/slack"
)

func (s *Slack) mbtContext(ib slack.Block) (string, error) {
func (s *Slack) mbtContext(ib slack.Block) (string, string, error) {
b, ok := ib.(*slack.ContextBlock)
if !ok {
return "", NewErrIncorrectType(&slack.ContextBlock{}, ib)
return "", "", NewErrIncorrectType(&slack.ContextBlock{}, ib)
}
var buf strings.Builder
var buf, cbuf strings.Builder
for _, el := range b.ContextElements.Elements {
fn, ok := contextElementHandlers[el.MixedElementType()]
if !ok {
return "", NewErrMissingHandler(el.MixedElementType())
return "", "", NewErrMissingHandler(el.MixedElementType())
}
s, err := fn(s, el)
s, cl, err := fn(s, el)
if err != nil {
return "", err
return "", "", err
}
buf.WriteString(s)
cbuf.WriteString(cl)
}

return buf.String(), nil
return buf.String(), cbuf.String(), nil
}

var contextElementHandlers = map[slack.MixedElementType]func(*Slack, slack.MixedElement) (string, error){
var contextElementHandlers = map[slack.MixedElementType]func(*Slack, slack.MixedElement) (string, string, error){
slack.MixedElementImage: (*Slack).metImage,
slack.MixedElementText: (*Slack).metText,
}

func (*Slack) metImage(ie slack.MixedElement) (string, error) {
func (*Slack) metImage(ie slack.MixedElement) (string, string, error) {
e, ok := ie.(*slack.ImageBlockElement)
if !ok {
return "", NewErrIncorrectType(&slack.ImageBlockElement{}, ie)
return "", "", NewErrIncorrectType(&slack.ImageBlockElement{}, ie)
}
return fmt.Sprintf(`<img src="%s" alt="%s">`, e.ImageURL, e.AltText), nil
return fmt.Sprintf(`<img src="%s" alt="%s">`, e.ImageURL, e.AltText), "", nil
}

func (*Slack) metText(ie slack.MixedElement) (string, error) {
func (*Slack) metText(ie slack.MixedElement) (string, string, error) {
e, ok := ie.(*slack.TextBlockObject)
if !ok {
return "", NewErrIncorrectType(&slack.TextBlockObject{}, ie)
return "", "", NewErrIncorrectType(&slack.TextBlockObject{}, ie)
}
return e.Text, nil
return e.Text, "", nil
}
11 changes: 10 additions & 1 deletion internal/viewer/renderer/slack_fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/rusq/slack"
)

func load(t *testing.T, s string) *slack.Message {
func loadmsg(t *testing.T, s string) *slack.Message {
t.Helper()
var m slack.Message
if err := json.Unmarshal([]byte(s), &m); err != nil {
Expand All @@ -16,6 +16,15 @@ func load(t *testing.T, s string) *slack.Message {
return &m
}

func load[T any](t *testing.T, s string) T {
t.Helper()
var v T
if err := json.Unmarshal([]byte(s), &v); err != nil {
t.Fatal(err)
}
return v
}

// integration tests fixtures
const (
fxtrRtseText = `
Expand Down
6 changes: 3 additions & 3 deletions internal/viewer/renderer/slack_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"github.com/rusq/slack"
)

func (*Slack) mbtImage(ib slack.Block) (string, error) {
func (*Slack) mbtImage(ib slack.Block) (string, string, error) {
b, ok := ib.(*slack.ImageBlock)
if !ok {
return "", NewErrIncorrectType(&slack.ImageBlock{}, ib)
return "", "", NewErrIncorrectType(&slack.ImageBlock{}, ib)
}
return figure(
blockTypeClass[slack.MBTImage],
fmt.Sprintf(
`<img src="%[1]s" alt="%[2]s"><figcaption class="slack-image-caption">%[2]s</figcaption>`,
b.ImageURL, b.AltText,
),
), nil
), "", nil
}
6 changes: 5 additions & 1 deletion internal/viewer/renderer/slack_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestSlack_mbtImage(t *testing.T) {
fields fields
args args
want string
want1 string
wantErr bool
}{
// TODO: Add test cases.
Expand All @@ -29,14 +30,17 @@ func TestSlack_mbtImage(t *testing.T) {
uu: tt.fields.uu,
cc: tt.fields.cc,
}
got, err := s.mbtImage(tt.args.ib)
got, got1, 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)
}
if got1 != tt.want1 {
t.Errorf("Slack.mbtImage() = %v, want %v", got1, tt.want1)
}
})
}
}
Loading

0 comments on commit 4d54c9e

Please sign in to comment.