56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
|
|
package matrix
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
type ViralReplySample struct {
|
||
|
|
Author string
|
||
|
|
Text string
|
||
|
|
}
|
||
|
|
|
||
|
|
type ViralPostSample struct {
|
||
|
|
Author string
|
||
|
|
LikeCount int
|
||
|
|
SearchTag string
|
||
|
|
Text string
|
||
|
|
Replies []ViralReplySample
|
||
|
|
}
|
||
|
|
|
||
|
|
func FormatViralSamples(posts []ViralPostSample) string {
|
||
|
|
if len(posts) == 0 {
|
||
|
|
return "(尚無海巡樣本,請依研究地圖與標籤發揮)"
|
||
|
|
}
|
||
|
|
var b strings.Builder
|
||
|
|
limit := 8
|
||
|
|
if len(posts) < limit {
|
||
|
|
limit = len(posts)
|
||
|
|
}
|
||
|
|
for i := 0; i < limit; i++ {
|
||
|
|
post := posts[i]
|
||
|
|
b.WriteString(fmt.Sprintf("\n[%d] @%s · %d讚 · 標籤:%s\n", i+1, post.Author, post.LikeCount, post.SearchTag))
|
||
|
|
text := strings.TrimSpace(post.Text)
|
||
|
|
if len([]rune(text)) > 160 {
|
||
|
|
text = string([]rune(text)[:160])
|
||
|
|
}
|
||
|
|
b.WriteString(text)
|
||
|
|
b.WriteString("\n")
|
||
|
|
if len(post.Replies) > 0 {
|
||
|
|
b.WriteString(" 熱門留言:")
|
||
|
|
for j, reply := range post.Replies {
|
||
|
|
if j >= 3 {
|
||
|
|
break
|
||
|
|
}
|
||
|
|
rt := strings.TrimSpace(reply.Text)
|
||
|
|
if len([]rune(rt)) > 60 {
|
||
|
|
rt = string([]rune(rt)[:60])
|
||
|
|
}
|
||
|
|
b.WriteString(fmt.Sprintf("\n - @%s: %s", reply.Author, rt))
|
||
|
|
}
|
||
|
|
b.WriteString("\n")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return strings.TrimSpace(b.String())
|
||
|
|
}
|