Repository navigation
Expand file tree
/
Copy pathtopic_render.go
More file actions
97 lines (82 loc) · 2.3 KB
/
Copy pathtopic_render.go
File metadata and controls
97 lines (82 loc) · 2.3 KB
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
package render
import (
"bbs-go/model"
"bbs-go/model/constants"
"bbs-go/pkg/markdown"
"bbs-go/services"
"html"
"github.com/mlogclub/simple"
)
func BuildTopic(topic *model.Topic) *model.TopicResponse {
return _buildTopic(topic, true)
}
func BuildSimpleTopic(topic *model.Topic) *model.TopicResponse {
buildContent := topic.Type == constants.TopicTypeTweet // 动态时渲染内容
return _buildTopic(topic, buildContent)
}
func BuildSimpleTopics(topics []model.Topic, currentUser *model.User) []model.TopicResponse {
if len(topics) == 0 {
return nil
}
var likedTopicIds []int64
if currentUser != nil {
var topicIds []int64
for _, topic := range topics {
topicIds = append(topicIds, topic.Id)
}
likedTopicIds = services.UserLikeService.IsLiked(currentUser.Id, constants.EntityTopic, topicIds)
}
var responses []model.TopicResponse
for _, topic := range topics {
var (
liked = simple.Contains(topic.Id, likedTopicIds)
item = BuildSimpleTopic(&topic)
)
item.Liked = liked
responses = append(responses, *item)
}
return responses
}
func _buildTopic(topic *model.Topic, buildContent bool) *model.TopicResponse {
if topic == nil {
return nil
}
rsp := &model.TopicResponse{}
rsp.TopicId = topic.Id
rsp.Type = topic.Type
rsp.Title = topic.Title
rsp.User = BuildUserInfoDefaultIfNull(topic.UserId)
rsp.LastCommentTime = topic.LastCommentTime
rsp.CreateTime = topic.CreateTime
rsp.ViewCount = topic.ViewCount
rsp.CommentCount = topic.CommentCount
rsp.LikeCount = topic.LikeCount
rsp.Recommend = topic.Recommend
rsp.RecommendTime = topic.RecommendTime
// 构建内容
if buildContent {
if topic.Type == constants.TopicTypeTopic {
content := markdown.ToHTML(topic.Content)
rsp.Content = handleHtmlContent(content)
} else {
rsp.Content = html.EscapeString(topic.Content)
}
} else {
rsp.Summary = markdown.GetSummary(topic.Content, 128)
}
if topic.Type == constants.TopicTypeTweet {
if simple.IsBlank(topic.Content) {
rsp.Content = "分享图片"
} else {
rsp.Content = html.EscapeString(topic.Content)
}
rsp.ImageList = buildImageList(topic.ImageList)
}
if topic.NodeId > 0 {
node := services.TopicNodeService.Get(topic.NodeId)
rsp.Node = BuildNode(node)
}
tags := services.TopicService.GetTopicTags(topic.Id)
rsp.Tags = BuildTags(tags)
return rsp
}