Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 668daa7

Browse files
committed
突出显示某个评论,即评论详情
1 parent 71486a2 commit 668daa7

File tree

15 files changed

+810
-24
lines changed

15 files changed

+810
-24
lines changed

docs/api.docx

50 KB
Binary file not shown.

src/http/controller/app/base.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func getLogger(ctx echo.Context) *logger.Logger {
2525
func success(ctx echo.Context, data interface{}) error {
2626
result := map[string]interface{}{
2727
"code": 0,
28-
"msg": "操作成功",
28+
"msg": "ok",
2929
"data": data,
3030
}
3131

src/http/controller/app/index.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2018 The StudyGolang Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
// https://studygolang.com
5+
// Author: polaris [email protected]
6+
7+
package app
8+
9+
import (
10+
"logic"
11+
12+
"github.com/labstack/echo"
13+
"github.com/polaris1119/goutils"
14+
15+
. "http"
16+
)
17+
18+
type IndexController struct{}
19+
20+
// 注册路由
21+
func (self IndexController) RegisterRoute(g *echo.Group) {
22+
g.GET("/home", self.Home)
23+
}
24+
25+
// Home 首页
26+
func (IndexController) Home(ctx echo.Context) error {
27+
if len(logic.WebsiteSetting.IndexNavs) == 0 {
28+
return success(ctx, nil)
29+
}
30+
31+
tab := ctx.QueryParam("tab")
32+
if tab == "" {
33+
tab = GetFromCookie(ctx, "INDEX_TAB")
34+
}
35+
36+
if tab == "" {
37+
tab = logic.WebsiteSetting.IndexNavs[0].Tab
38+
}
39+
paginator := logic.NewPaginator(goutils.MustInt(ctx.QueryParam("p"), 1))
40+
41+
data := logic.DefaultIndex.FindData(ctx, tab, paginator)
42+
43+
SetCookie(ctx, "INDEX_TAB", data["tab"].(string))
44+
45+
data["all_nodes"] = logic.GenNodes()
46+
47+
if tab == "all" {
48+
data["total"] = paginator.GetTotal()
49+
50+
}
51+
return success(ctx, nil)
52+
}

src/http/controller/app/topic.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type TopicController struct{}
2323

2424
// 注册路由
2525
func (self TopicController) RegisterRoute(g *echo.Group) {
26-
g.GET("/topics", self.Topics)
26+
g.GET("/topics", self.TopicList)
2727
g.GET("/topics/no_reply", self.TopicsNoReply)
2828
g.GET("/topics/last", self.TopicsLast)
2929
g.GET("/topic/detail", self.Detail)
@@ -33,6 +33,18 @@ func (self TopicController) RegisterRoute(g *echo.Group) {
3333
g.Match([]string{"GET", "POST"}, "/topics/modify", self.Modify, middleware.NeedLogin(), middleware.Sensivite())
3434
}
3535

36+
func (self TopicController) TopicList(ctx echo.Context) error {
37+
tab := ctx.QueryParam("tab")
38+
if tab != "" && tab != "all" {
39+
nid := logic.GetNidByEname(tab)
40+
if nid > 0 {
41+
return self.topicList(ctx, tab, "topics.mtime DESC", "nid=? AND top!=1", nid)
42+
}
43+
}
44+
45+
return self.topicList(ctx, "all", "topics.mtime DESC", "top!=1")
46+
}
47+
3648
func (self TopicController) Topics(ctx echo.Context) error {
3749
return self.topicList(ctx, "", "topics.mtime DESC", "")
3850
}
@@ -45,17 +57,23 @@ func (self TopicController) TopicsLast(ctx echo.Context) error {
4557
return self.topicList(ctx, "last", "ctime DESC", "")
4658
}
4759

48-
func (TopicController) topicList(ctx echo.Context, view, orderBy, querystring string, args ...interface{}) error {
60+
func (TopicController) topicList(ctx echo.Context, tab, orderBy, querystring string, args ...interface{}) error {
4961
curPage := goutils.MustInt(ctx.QueryParam("p"), 1)
5062
paginator := logic.NewPaginator(curPage)
5163

64+
// 置顶的topic
65+
topTopics := logic.DefaultTopic.FindAll(ctx, paginator, "ctime DESC", "top=1")
66+
5267
topics := logic.DefaultTopic.FindAll(ctx, paginator, orderBy, querystring, args...)
5368
total := logic.DefaultTopic.Count(ctx, querystring, args...)
5469
hasMore := paginator.SetTotal(total).HasMorePage()
5570

71+
hotNodes := logic.DefaultTopic.FindHotNodes(ctx)
72+
5673
data := map[string]interface{}{
57-
"topics": topics,
58-
"view": view,
74+
"topics": append(topTopics, topics...),
75+
"tab": tab,
76+
"tab_list": hotNodes,
5977
"has_more": hasMore,
6078
}
6179

src/http/controller/comment.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package controller
88

99
import (
10+
"errors"
1011
"http/middleware"
1112
"logic"
1213
"model"
@@ -26,6 +27,9 @@ func (self CommentController) RegisterRoute(g *echo.Group) {
2627
g.Get("/at/users", self.AtUsers)
2728
g.Post("/comment/:objid", self.Create, middleware.NeedLogin(), middleware.Sensivite(), middleware.BalanceCheck(), middleware.PublishNotice())
2829
g.Get("/object/comments", self.CommentList)
30+
31+
g.Get("/topics/:objid/comment/:cid", self.TopicDetail)
32+
g.Get("/articles/:objid/comment/:cid", self.ArticleDetail)
2933
}
3034

3135
// AtUsers 评论或回复 @ 某人 suggest
@@ -77,3 +81,74 @@ func (CommentController) CommentList(ctx echo.Context) error {
7781

7882
return success(ctx, result)
7983
}
84+
85+
func (self CommentController) TopicDetail(ctx echo.Context) error {
86+
objid := goutils.MustInt(ctx.Param("objid"))
87+
cid := goutils.MustInt(ctx.Param("cid"))
88+
89+
topicMaps := logic.DefaultTopic.FindFullinfoByTids([]int{objid})
90+
if len(topicMaps) < 1 {
91+
return ctx.Redirect(http.StatusSeeOther, "/topics")
92+
}
93+
94+
topic := topicMaps[0]
95+
topic["node"] = logic.GetNode(topic["nid"].(int))
96+
97+
data := map[string]interface{}{
98+
"topic": topic,
99+
}
100+
data["appends"] = logic.DefaultTopic.FindAppend(ctx, objid)
101+
102+
err := self.fillCommentAndUser(ctx, data, cid, objid, model.TypeTopic)
103+
104+
if err != nil {
105+
return ctx.Redirect(http.StatusSeeOther, "/topics/"+strconv.Itoa(objid))
106+
}
107+
108+
return render(ctx, "topics/comment.html", data)
109+
}
110+
111+
func (self CommentController) ArticleDetail(ctx echo.Context) error {
112+
objid := goutils.MustInt(ctx.Param("objid"))
113+
cid := goutils.MustInt(ctx.Param("cid"))
114+
115+
article, err := logic.DefaultArticle.FindById(ctx, objid)
116+
if err != nil {
117+
return ctx.Redirect(http.StatusSeeOther, "/articles")
118+
}
119+
articleGCTT := logic.DefaultArticle.FindArticleGCTT(ctx, article)
120+
121+
data := map[string]interface{}{
122+
"article": article,
123+
"article_gctt": articleGCTT,
124+
}
125+
126+
err = self.fillCommentAndUser(ctx, data, cid, objid, model.TypeArticle)
127+
128+
if err != nil {
129+
return ctx.Redirect(http.StatusSeeOther, "/articles/"+strconv.Itoa(objid))
130+
}
131+
132+
return render(ctx, "articles/comment.html", data)
133+
}
134+
135+
func (CommentController) fillCommentAndUser(ctx echo.Context, data map[string]interface{}, cid, objid, objtype int) error {
136+
comment, comments := logic.DefaultComment.FindComment(ctx, cid, objid, model.TypeArticle)
137+
138+
if comment.Cid == 0 {
139+
return errors.New("comment not exists!")
140+
}
141+
142+
uids := make([]int, 1+len(comments))
143+
uids[0] = comment.Uid
144+
for i, comment := range comments {
145+
uids[i+1] = comment.Uid
146+
}
147+
users := logic.DefaultUser.FindUserInfos(ctx, uids)
148+
149+
data["comment"] = comment
150+
data["comments"] = comments
151+
data["users"] = users
152+
153+
return nil
154+
}

src/http/controller/user.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/labstack/echo"
1515
"github.com/polaris1119/goutils"
16+
"github.com/polaris1119/slices"
1617
)
1718

1819
type UserController struct{}
@@ -233,11 +234,17 @@ func (UserController) Comments(ctx echo.Context) error {
233234

234235
pageHtml := paginator.SetTotal(total).GetPageHtml(ctx.Request().URL().Path())
235236

236-
return render(ctx, "user/comments.html", map[string]interface{}{
237-
"username": username,
237+
data := map[string]interface{}{
238238
"comments": comments,
239239
"page": template.HTML(pageHtml),
240240
"total": total,
241-
})
241+
}
242+
243+
if username == "" {
244+
uids := slices.StructsIntSlice(comments, "Uid")
245+
data["users"] = logic.DefaultUser.FindUserInfos(ctx, uids)
246+
}
247+
248+
return render(ctx, "user/comments.html", data)
242249

243250
}

src/logic/comment.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,38 @@ func (self CommentLogic) FindObjectComments(ctx context.Context, objid, objtype
7575
}
7676

7777
for _, comment := range commentList {
78-
self.decodeCmtContentForShow(ctx, comment)
78+
self.decodeCmtContentForShow(ctx, comment, true)
7979
}
8080

8181
return
8282
}
8383

84+
// FindComment 获得评论和额外两个评论
85+
func (self CommentLogic) FindComment(ctx context.Context, cid, objid, objtype int) (*model.Comment, []*model.Comment) {
86+
objLog := GetLogger(ctx)
87+
88+
comment := &model.Comment{}
89+
_, err := MasterDB.Where("cid=?", cid).Get(comment)
90+
if err != nil {
91+
objLog.Errorln("CommentLogic FindComment error:", err)
92+
return comment, nil
93+
}
94+
self.decodeCmtContentForShow(ctx, comment, false)
95+
96+
comments := make([]*model.Comment, 0)
97+
err = MasterDB.Where("objid=? AND objtype=? AND cid!=?", objid, objtype, cid).
98+
Limit(2).Find(&comments)
99+
if err != nil {
100+
objLog.Errorln("CommentLogic FindComment Find more error:", err)
101+
return comment, nil
102+
}
103+
for _, cmt := range comments {
104+
self.decodeCmtContentForShow(ctx, cmt, false)
105+
}
106+
107+
return comment, comments
108+
}
109+
84110
// Total 评论总数(objtypes[0] 取某一类型的评论总数)
85111
func (CommentLogic) Total(objtypes ...int) int64 {
86112
var (
@@ -90,7 +116,6 @@ func (CommentLogic) Total(objtypes ...int) int64 {
90116
if len(objtypes) > 0 {
91117
total, err = MasterDB.Where("objtype=?", objtypes[0]).Count(new(model.Comment))
92118
} else {
93-
94119
total, err = MasterDB.Count(new(model.Comment))
95120
}
96121
if err != nil {
@@ -182,7 +207,7 @@ func (self CommentLogic) Publish(ctx context.Context, uid, objid int, form url.V
182207
objLog.Errorln("post comment service error:", err)
183208
return nil, err
184209
}
185-
self.decodeCmtContentForShow(ctx, comment)
210+
self.decodeCmtContentForShow(ctx, comment, true)
186211

187212
// 回调,不关心处理结果(有些对象可能不需要回调)
188213
if commenter, ok := commenters[objtype]; ok {
@@ -305,7 +330,7 @@ func (CommentLogic) decodeCmtContent(ctx context.Context, comment *model.Comment
305330
}
306331

307332
// decodeCmtContentForShow 采用引用的方式显示对其他楼层的回复
308-
func (CommentLogic) decodeCmtContentForShow(ctx context.Context, comment *model.Comment) {
333+
func (CommentLogic) decodeCmtContentForShow(ctx context.Context, comment *model.Comment, isEscape bool) {
309334
// 安全过滤
310335
content := template.HTMLEscapeString(comment.Content)
311336

@@ -349,7 +374,6 @@ func (self CommentLogic) FindAll(ctx context.Context, paginator *Paginator, orde
349374
cmtMap := make(map[int][]*model.Comment, len(model.PathUrlMap))
350375
for _, comment := range comments {
351376
self.decodeCmtContent(ctx, comment)
352-
353377
if _, ok := cmtMap[comment.Objtype]; !ok {
354378
cmtMap[comment.Objtype] = make([]*model.Comment, 0, 10)
355379
}

src/logic/github.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (self GithubLogic) IssueEvent(ctx context.Context, body []byte) error {
9797
}
9898

9999
gcttIssue.Label = label
100-
_, err = MasterDB.Id(id).Update(gcttIssue)
100+
_, err = MasterDB.Id(id).Cols("translator", "translating_at", "label").Update(gcttIssue)
101101
}
102102
} else if action == "closed" {
103103
closedAt := result.Get("issue.closed_at").Time().Unix()

src/logic/user.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,15 @@ func (self UserLogic) Update(ctx context.Context, me *model.Me, form url.Values)
114114
cols += ",email,status"
115115
user.Status = model.UserStatusNoAudit
116116
}
117-
_, err = MasterDB.Id(me.Uid).Cols(cols).Update(user)
117+
118+
session := MasterDB.NewSession()
119+
defer session.Close()
120+
session.Begin()
121+
122+
_, err = session.Id(me.Uid).Cols(cols).Update(user)
118123
if err != nil {
124+
session.Rollback()
125+
119126
objLog.Errorf("更新用户 【%d】 信息失败:%s", me.Uid, err)
120127
if strings.Contains(err.Error(), "Error 1062: Duplicate entry") {
121128
// TODO:被恶意注册?
@@ -126,6 +133,17 @@ func (self UserLogic) Update(ctx context.Context, me *model.Me, form url.Values)
126133
return
127134
}
128135

136+
_, err = session.Table(new(model.UserLogin)).
137+
Where("uid=?", me.Uid).Update(map[string]interface{}{"email": me.Email})
138+
if err != nil {
139+
session.Rollback()
140+
objLog.Errorf("更新用户 【%d】 信息失败:%s", me.Uid, err)
141+
errMsg = "对不起,服务器内部错误,请稍后再试!"
142+
return
143+
}
144+
145+
session.Commit()
146+
129147
// 修改用户资料,活跃度+1
130148
go self.IncrUserWeight("uid", me.Uid, 1)
131149

src/model/comment.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ const (
1616
TypeBook // 图书
1717
)
1818

19+
const (
20+
TopicURI = "topics"
21+
ArticleURI = "articles"
22+
ResourceURI = "resources"
23+
WikiURI = "wiki"
24+
ProjectURI = "p"
25+
BookURI = "book"
26+
)
27+
1928
var PathUrlMap = map[int]string{
2029
TypeTopic: "/topics/",
2130
TypeArticle: "/articles/",

0 commit comments

Comments
 (0)