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

Skip to content

Commit 7e8abcb

Browse files
author
studygolang
committed
增加回复别人评论的功能;
增加@功能
1 parent f55bc9f commit 7e8abcb

File tree

18 files changed

+268
-146
lines changed

18 files changed

+268
-146
lines changed

websites/code/studygolang/src/controller/comment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func CommentHandler(rw http.ResponseWriter, req *http.Request) {
2121
vars := mux.Vars(req)
2222
user, _ := filter.CurrentUser(req)
2323
// 入库
24-
err := service.PostComment(util.MustInt(vars["objid"]), util.MustInt(req.FormValue("objtype")), user["uid"].(int), req.FormValue("content"), req.FormValue("objname"))
24+
err := service.PostComment(user["uid"].(int), util.MustInt(vars["objid"]), req.Form)
2525
if err != nil {
2626
fmt.Fprint(rw, `{"errno": 1, "error":"服务器内部错误"}`)
2727
return

websites/code/studygolang/src/controller/like.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,10 @@ package controller
99
// 喜欢系统
1010

1111
import (
12-
"filter"
13-
"fmt"
14-
"github.com/studygolang/mux"
1512
"net/http"
16-
"service"
17-
"util"
1813
)
1914

2015
// 喜欢(或取消喜欢)
2116
// uri: /like/{objid:[0-9]+}.json
2217
func LikeHandler(rw http.ResponseWriter, req *http.Request) {
23-
vars := mux.Vars(req)
24-
user, _ := filter.CurrentUser(req)
25-
// 入库
26-
err := service.PostComment(util.MustInt(vars["objid"]), util.MustInt(req.FormValue("objtype")), user["uid"].(int), req.FormValue("content"), req.FormValue("objname"))
27-
if err != nil {
28-
fmt.Fprint(rw, `{"errno": 1, "error":"服务器内部错误"}`)
29-
return
30-
}
31-
fmt.Fprint(rw, `{"errno": 0, "error":""}`)
3218
}

websites/code/studygolang/src/filter/view.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,14 @@ func (this *ViewFilter) PostFilter(rw http.ResponseWriter, req *http.Request) bo
105105
if contentHtml == "" {
106106
return true
107107
}
108+
contentHtmls := strings.Split(contentHtml, ",")
109+
for i, contentHtml := range contentHtmls {
110+
contentHtmls[i] = config.ROOT + strings.TrimSpace(contentHtml)
111+
}
112+
108113
// 为了使用自定义的模板函数,首先New一个以第一个模板文件名为模板名。
109114
// 这样,在ParseFiles时,新返回的*Template便还是原来的模板实例
110-
tpl, err := template.New(this.baseTplName).Funcs(funcMap).ParseFiles(append(this.commonHtmlFiles, config.ROOT+contentHtml)...)
115+
tpl, err := template.New(this.baseTplName).Funcs(funcMap).ParseFiles(append(this.commonHtmlFiles, contentHtmls...)...)
111116
if err != nil {
112117
logger.Errorf("解析模板出错(ParseFiles):[%q] %s\n", req.RequestURI, err)
113118
return false

websites/code/studygolang/src/model/comment.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ const (
1818
TYPE_WIKI // WIKI
1919
)
2020

21+
var PathUrlMap = map[int]string{
22+
TYPE_TOPIC: "/topics/",
23+
TYPE_BLOG: "/blog/",
24+
TYPE_RESOURCE: "/resources/",
25+
TYPE_WIKI: "/wiki/",
26+
}
27+
2128
// 评论信息(通用)
2229
type Comment struct {
2330
Cid int `json:"cid"`

websites/code/studygolang/src/service/comment.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
package service
88

99
import (
10+
"fmt"
11+
"html/template"
1012
"logger"
1113
"model"
14+
"net/url"
15+
"regexp"
1216
"strconv"
1317
"time"
1418
"util"
@@ -43,12 +47,28 @@ func FindObjComments(objid, objtype string, owner, lastCommentUid int /*, page,
4347
for _, comment := range commentList {
4448
tmpMap := make(map[string]interface{})
4549
util.Struct2Map(tmpMap, comment)
50+
tmpMap["content"] = decodeCmtContent(comment)
4651
tmpMap["user"] = userMap[comment.Uid]
4752
comments = append(comments, tmpMap)
4853
}
4954
return
5055
}
5156

57+
func decodeCmtContent(comment *model.Comment) template.HTML {
58+
// 安全过滤
59+
content := template.HTMLEscapeString(comment.Content)
60+
// @别人
61+
reg := regexp.MustCompile(`@([^\s@]{4,20})`)
62+
content = reg.ReplaceAllString(content, `<a href="/user/$1" title="@$1">@$1</a>`)
63+
64+
// 回复某一楼层
65+
reg = regexp.MustCompile(`#(\d+)楼`)
66+
url := fmt.Sprintf("%s%d#comment", model.PathUrlMap[comment.Objtype], comment.Objid)
67+
content = reg.ReplaceAllString(content, `<a href="`+url+`$1" title="$1">#$1<span>楼</span></a>`)
68+
69+
return template.HTML(content)
70+
}
71+
5272
// 获得某人在某种类型最近的评论
5373
func FindRecentComments(uid, objtype int) []*model.Comment {
5474
comments, err := model.NewComment().Where("uid=" + strconv.Itoa(uid) + " AND objtype=" + strconv.Itoa(objtype)).Order("ctime DESC").Limit("0, 5").FindAll()
@@ -114,12 +134,14 @@ func RegisterCommentObject(objname string, commenter Commenter) {
114134

115135
// 发表评论(或回复)。
116136
// objname 注册的评论对象名
117-
func PostComment(objid, objtype, uid int, content string, objname string) error {
137+
// uid 评论人
138+
func PostComment(uid, objid int, form url.Values) error {
118139
comment := model.NewComment()
119140
comment.Objid = objid
141+
objtype := util.MustInt(form.Get("objtype"))
120142
comment.Objtype = objtype
121143
comment.Uid = uid
122-
comment.Content = content
144+
comment.Content = form.Get("content")
123145

124146
// TODO:评论楼层怎么处理,避免冲突?最后的楼层信息保存在内存中?
125147

@@ -140,7 +162,7 @@ func PostComment(objid, objtype, uid int, content string, objname string) error
140162
return err
141163
}
142164
// 回调,不关心处理结果(有些对象可能不需要回调)
143-
if commenter, ok := commenters[objname]; ok {
165+
if commenter, ok := commenters[form.Get("objname")]; ok {
144166
logger.Debugf("评论[objid:%d] [objtype:%d] [uid:%d] 成功,通知被评论者更新", objid, objtype, uid)
145167
go commenter.UpdateComment(cid, objid, uid, time.Now().Format("2006-01-02 15:04:05"))
146168
}
@@ -157,7 +179,8 @@ func PostComment(objid, objtype, uid int, content string, objname string) error
157179
}
158180
go SendSystemMsgTo(0, objtype, ext)
159181

160-
// TODO: @某人 发系统消息?
182+
// @某人 发系统消息
183+
go SendSysMsgAtUids(form.Get("uid"), ext)
161184

162185
return nil
163186
}

websites/code/studygolang/src/service/message.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"logger"
1111
"model"
1212
"strconv"
13+
"strings"
1314
"util"
1415
)
1516

@@ -66,6 +67,37 @@ func SendSystemMsgTo(to, msgtype int, ext map[string]interface{}) bool {
6667
return true
6768
}
6869

70+
// 给被@的用户发系统消息
71+
func SendSysMsgAtUids(uids string, ext map[string]interface{}) bool {
72+
if uids == "" {
73+
return true
74+
}
75+
message := model.NewSystemMessage()
76+
message.Msgtype = model.MsgtypeAtMe
77+
message.SetExt(ext)
78+
79+
msg := NewMessage(WsMsgNotify, 1)
80+
81+
uidSlice := strings.Split(uids, ",")
82+
for _, uidStr := range uidSlice {
83+
uid, _ := strconv.Atoi(strings.TrimSpace(uidStr))
84+
if from, ok := ext["uid"]; ok {
85+
// 自己的动作不发系统消息
86+
if uid == from.(int) {
87+
continue
88+
}
89+
}
90+
message.To = uid
91+
if _, err := message.Insert(); err != nil {
92+
logger.Errorln("message service SendSysMsgAtUids Error:", err)
93+
continue
94+
}
95+
// 通过 WebSocket 通知对方
96+
go Book.PostMessage(uid, msg)
97+
}
98+
return true
99+
}
100+
69101
// 获得某人的系统消息
70102
// 系统消息类型不同,在ext中存放的字段也不一样,如下:
71103
// model.MsgtypeTopicReply/MsgtypeResourceComment/MsgtypeWikiComment存放都为:
@@ -156,19 +188,22 @@ func FindSysMsgsByUid(uid string) []map[string]interface{} {
156188
objUrl = "/wiki/" + strconv.Itoa(wikiMap[objid].Id)
157189
title = "评论了你的Wiki页:"
158190
case model.MsgtypeAtMe:
191+
title = "评论时提到了你,在"
159192
switch int(ext["objtype"].(float64)) {
160193
case model.TYPE_TOPIC:
161194
objTitle = topicMap[objid].Title
162195
objUrl = "/topics/" + strconv.Itoa(topicMap[objid].Tid)
196+
title += "主题:"
163197
case model.TYPE_BLOG:
164198
case model.TYPE_RESOURCE:
165199
objTitle = resourceMap[objid].Title
166200
objUrl = "/resources/" + strconv.Itoa(resourceMap[objid].Id)
201+
title += "资源:"
167202
case model.TYPE_WIKI:
168203
objTitle = wikiMap[objid].Title
169204
objUrl = "/wiki/" + strconv.Itoa(wikiMap[objid].Id)
205+
title += "wiki:"
170206
}
171-
title = "评论提到了你,在:"
172207
}
173208
tmpMap["objtitle"] = objTitle
174209
tmpMap["objurl"] = objUrl
@@ -183,7 +218,7 @@ func FindSysMsgsByUid(uid string) []map[string]interface{} {
183218
if val, ok := ext["content"]; ok {
184219
tmpMap["content"] = val.(string)
185220
} else if val, ok := ext["cid"]; ok {
186-
tmpMap["content"] = commentMap[int(val.(float64))].Content
221+
tmpMap["content"] = decodeCmtContent(commentMap[int(val.(float64))])
187222
}
188223
tmpMap["title"] = title
189224
result[i] = tmpMap

websites/code/studygolang/src/service/topic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func FindRecentReplies(comments []*model.Comment) []map[string]interface{} {
272272
oneReply := make(map[string]interface{})
273273
oneReply["tid"] = topic.Tid
274274
oneReply["title"] = topic.Title
275-
oneReply["cmt_content"] = commentMap[topic.Tid].Content
275+
oneReply["cmt_content"] = decodeCmtContent(commentMap[topic.Tid])
276276
oneReply["replytime"] = commentMap[topic.Tid].Ctime
277277
result[i] = oneReply
278278
}

0 commit comments

Comments
 (0)