Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions msg_post_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const (

// PostBuf .
type PostBuf struct {
Title string `json:"title"`
Content []PostElem `json:"content"`
Title string `json:"title"`
Content [][]PostElem `json:"content"`
}

// MsgPostBuilder for build text buf
Expand All @@ -63,7 +63,8 @@ func (pb *MsgPostBuilder) Locale(locale string) *MsgPostBuilder {
// WithLocale switches to locale and returns self
func (pb *MsgPostBuilder) WithLocale(locale string) *MsgPostBuilder {
if _, ok := pb.buf[locale]; !ok {
pb.buf[locale] = &PostBuf{}
buf := &PostBuf{Content: make([][]PostElem, 1)}
pb.buf[locale] = buf
}

pb.curLocale = locale
Expand All @@ -89,7 +90,7 @@ func (pb *MsgPostBuilder) TextTag(text string, lines int, unescape bool) *MsgPos
Lines: &lines,
UnEscape: &unescape,
}
pb.CurLocale().Content = append(pb.CurLocale().Content, pe)
pb.addElem(pe)
return pb
}

Expand All @@ -100,7 +101,7 @@ func (pb *MsgPostBuilder) LinkTag(text, href string) *MsgPostBuilder {
Text: &text,
Href: &href,
}
pb.CurLocale().Content = append(pb.CurLocale().Content, pe)
pb.addElem(pe)
return pb
}

Expand All @@ -111,7 +112,7 @@ func (pb *MsgPostBuilder) AtTag(text, userID string) *MsgPostBuilder {
Text: &text,
UserID: &userID,
}
pb.CurLocale().Content = append(pb.CurLocale().Content, pe)
pb.addElem(pe)
return pb
}

Expand All @@ -123,10 +124,27 @@ func (pb *MsgPostBuilder) ImageTag(imageKey string, imageWidth, imageHeight int)
ImageWidth: &imageWidth,
ImageHeight: &imageHeight,
}
pb.CurLocale().Content = append(pb.CurLocale().Content, pe)
pb.addElem(pe)
return pb
}

// NewLine starts a new line
func (pb *MsgPostBuilder) NewLine() *MsgPostBuilder {
// if pb.Len() == 0 then there's no need to start a new line
if pb.Len() > 0 {
buf := pb.CurLocale()
buf.Content = append(buf.Content, make([]PostElem, 0))
}
return pb
}

// addElem adds a PostElem to the latest line
func (pb *MsgPostBuilder) addElem(pe PostElem) {
buf := pb.CurLocale()
idx := len(buf.Content) - 1
buf.Content[idx] = append(buf.Content[idx], pe)
}

// Clear all message
func (pb *MsgPostBuilder) Clear() {
pb.curLocale = defaultLocale
Expand All @@ -139,13 +157,20 @@ func (pb *MsgPostBuilder) Render() *PostContent {
for locale, buf := range pb.buf {
content[locale] = PostBody{
Title: buf.Title,
Content: [][]PostElem{buf.Content},
Content: buf.Content,
}
}
return &content
}

// Len returns buf len
// Len returns the latest line buf len
func (pb MsgPostBuilder) Len() int {
buf := pb.CurLocale()
idx := len(buf.Content) - 1
return len(buf.Content[idx])
}

// Len returns the lines count
func (pb MsgPostBuilder) Lines() int {
return len(pb.CurLocale().Content)
}
45 changes: 30 additions & 15 deletions msg_post_builder_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lark

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -25,38 +26,38 @@ func TestPostTextTag(t *testing.T) {
pb := NewPostBuilder()
pb.TextTag("hello, world", 1, true)
buf := pb.CurLocale().Content
assert.Equal(t, "text", buf[0].Tag)
assert.Equal(t, "hello, world", *(buf[0].Text))
assert.Equal(t, 1, *(buf[0].Lines))
assert.Equal(t, true, *(buf[0].UnEscape))
assert.Equal(t, "text", buf[0][0].Tag)
assert.Equal(t, "hello, world", *(buf[0][0].Text))
assert.Equal(t, 1, *(buf[0][0].Lines))
assert.Equal(t, true, *(buf[0][0].UnEscape))
}

func TestPostLinkTag(t *testing.T) {
pb := NewPostBuilder()
pb.LinkTag("hello, world", "https://www.toutiao.com/")
buf := pb.CurLocale().Content
assert.Equal(t, "a", buf[0].Tag)
assert.Equal(t, "hello, world", *(buf[0].Text))
assert.Equal(t, "https://www.toutiao.com/", *(buf[0].Href))
assert.Equal(t, "a", buf[0][0].Tag)
assert.Equal(t, "hello, world", *(buf[0][0].Text))
assert.Equal(t, "https://www.toutiao.com/", *(buf[0][0].Href))
}

func TestPostAtTag(t *testing.T) {
pb := NewPostBuilder()
pb.AtTag("www", "123456")
buf := pb.CurLocale().Content
assert.Equal(t, "at", buf[0].Tag)
assert.Equal(t, "www", *(buf[0].Text))
assert.Equal(t, "123456", *(buf[0].UserID))
assert.Equal(t, "at", buf[0][0].Tag)
assert.Equal(t, "www", *(buf[0][0].Text))
assert.Equal(t, "123456", *(buf[0][0].UserID))
}

func TestPostImgTag(t *testing.T) {
pb := NewPostBuilder()
pb.ImageTag("d9f7d37e-c47c-411b-8ec6-9861132e6986", 320, 240)
buf := pb.CurLocale().Content
assert.Equal(t, "img", buf[0].Tag)
assert.Equal(t, "d9f7d37e-c47c-411b-8ec6-9861132e6986", *(buf[0].ImageKey))
assert.Equal(t, 240, *(buf[0].ImageHeight))
assert.Equal(t, 320, *(buf[0].ImageWidth))
assert.Equal(t, "img", buf[0][0].Tag)
assert.Equal(t, "d9f7d37e-c47c-411b-8ec6-9861132e6986", *(buf[0][0].ImageKey))
assert.Equal(t, 240, *(buf[0][0].ImageHeight))
assert.Equal(t, 320, *(buf[0][0].ImageWidth))
}

func TestPostClearAndLen(t *testing.T) {
Expand All @@ -81,7 +82,21 @@ func TestPostMultiLocaleContent(t *testing.T) {
assert.Equal(t, "en title", pb.CurLocale().Title)

content := pb.Render()
t.Log(content)
js, _ := json.Marshal(content)
t.Log(string(js))
assert.Equal(t, "中文标题", (*content)[LocaleZhCN].Title)
assert.Equal(t, "en title", (*content)[LocaleEnUS].Title)
}

func TestNewLine(t *testing.T) {
pb := NewPostBuilder()
pb.Title("中文标题")
pb.TextTag("你好世界", 1, true).TextTag("其他内容", 1, true)
pb.NewLine()
pb.TextTag("hello, world", 1, true).LinkTag("link", "https://www.toutiao.com/")
assert.Equal(t, 2, pb.Len())
assert.Equal(t, 2, pb.Lines())
content := pb.Render()
js, _ := json.Marshal(content)
t.Log(string(js))
}