-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
msg_post_builder.go
151 lines (131 loc) · 3.41 KB
/
msg_post_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package lark
// PostContent .
type PostContent map[string]PostBody
// PostBody .
type PostBody struct {
Title string `json:"title"`
Content [][]PostElem `json:"content"`
}
// PostElem .
type PostElem struct {
Tag string `json:"tag"`
// For Text
UnEscape *bool `json:"un_escape,omitempty"`
Text *string `json:"text,omitempty"`
Lines *int `json:"lines,omitempty"`
// For Link
Href *string `json:"href,omitempty"`
// For At
UserID *string `json:"user_id,omitempty"`
// For Image
ImageKey *string `json:"image_key,omitempty"`
ImageWidth *int `json:"width,omitempty"`
ImageHeight *int `json:"height,omitempty"`
}
const (
msgPostText = "text"
msgPostLink = "a"
msgPostAt = "at"
msgPostImage = "img"
)
// PostBuf .
type PostBuf struct {
Title string `json:"title"`
Content []PostElem `json:"content"`
}
// MsgPostBuilder for build text buf
type MsgPostBuilder struct {
buf map[string]*PostBuf
curLocale string
}
const defaultLocale = LocaleZhCN
// NewPostBuilder creates a text builder
func NewPostBuilder() *MsgPostBuilder {
return &MsgPostBuilder{
buf: make(map[string]*PostBuf),
curLocale: defaultLocale,
}
}
// Locale renamed to WithLocale but still available
func (pb *MsgPostBuilder) Locale(locale string) *MsgPostBuilder {
return pb.WithLocale(locale)
}
// WithLocale switches to locale and returns self
func (pb *MsgPostBuilder) WithLocale(locale string) *MsgPostBuilder {
if _, ok := pb.buf[locale]; !ok {
pb.buf[locale] = &PostBuf{}
}
pb.curLocale = locale
return pb
}
// CurLocale switches to locale and returns the buffer of that locale
func (pb *MsgPostBuilder) CurLocale() *PostBuf {
return pb.WithLocale(pb.curLocale).buf[pb.curLocale]
}
// Title sets title
func (pb *MsgPostBuilder) Title(title string) *MsgPostBuilder {
pb.CurLocale().Title = title
return pb
}
// TextTag creates a text tag
func (pb *MsgPostBuilder) TextTag(text string, lines int, unescape bool) *MsgPostBuilder {
pe := PostElem{
Tag: msgPostText,
Text: &text,
Lines: &lines,
UnEscape: &unescape,
}
pb.CurLocale().Content = append(pb.CurLocale().Content, pe)
return pb
}
// LinkTag creates a link tag
func (pb *MsgPostBuilder) LinkTag(text, href string) *MsgPostBuilder {
pe := PostElem{
Tag: msgPostLink,
Text: &text,
Href: &href,
}
pb.CurLocale().Content = append(pb.CurLocale().Content, pe)
return pb
}
// AtTag creates an at tag
func (pb *MsgPostBuilder) AtTag(text, userID string) *MsgPostBuilder {
pe := PostElem{
Tag: msgPostAt,
Text: &text,
UserID: &userID,
}
pb.CurLocale().Content = append(pb.CurLocale().Content, pe)
return pb
}
// ImageTag creates an image tag
func (pb *MsgPostBuilder) ImageTag(imageKey string, imageWidth, imageHeight int) *MsgPostBuilder {
pe := PostElem{
Tag: msgPostImage,
ImageKey: &imageKey,
ImageWidth: &imageWidth,
ImageHeight: &imageHeight,
}
pb.CurLocale().Content = append(pb.CurLocale().Content, pe)
return pb
}
// Clear all message
func (pb *MsgPostBuilder) Clear() {
pb.curLocale = defaultLocale
pb.buf = make(map[string]*PostBuf)
}
// Render message
func (pb *MsgPostBuilder) Render() *PostContent {
content := make(PostContent)
for locale, buf := range pb.buf {
content[locale] = PostBody{
Title: buf.Title,
Content: [][]PostElem{buf.Content},
}
}
return &content
}
// Len returns buf len
func (pb MsgPostBuilder) Len() int {
return len(pb.CurLocale().Content)
}