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

Skip to content

Commit 83f401d

Browse files
authored
Merge pull request studygolang#69 from ParadeTo/master
完成 studygolang#61 修改评论的功能
2 parents a50b366 + b343b67 commit 83f401d

31 files changed

+449
-107
lines changed

src/http/controller/comment.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
. "http"
1818

1919
"github.com/labstack/echo"
20+
"github.com/polaris1119/echoutils"
2021
"github.com/polaris1119/goutils"
2122
"github.com/polaris1119/slices"
2223
)
@@ -27,6 +28,7 @@ func (self CommentController) RegisterRoute(g *echo.Group) {
2728
g.Get("/at/users", self.AtUsers)
2829
g.Post("/comment/:objid", self.Create, middleware.NeedLogin(), middleware.Sensivite(), middleware.BalanceCheck(), middleware.PublishNotice())
2930
g.Get("/object/comments", self.CommentList)
31+
g.Post("/object/comments/:cid", self.Modify, middleware.NeedLogin(), middleware.Sensivite(), middleware.PublishNotice())
3032

3133
g.Get("/topics/:objid/comment/:cid", self.TopicDetail)
3234
g.Get("/articles/:objid/comment/:cid", self.ArticleDetail)
@@ -57,6 +59,34 @@ func (CommentController) Create(ctx echo.Context) error {
5759
return success(ctx, comment)
5860
}
5961

62+
// 修改评论
63+
func (CommentController) Modify(ctx echo.Context) error {
64+
cid := goutils.MustInt(ctx.Param("cid"))
65+
content := ctx.FormValue("content")
66+
comment, err := logic.DefaultComment.FindById(cid)
67+
68+
if err != nil {
69+
return fail(ctx, 2, "评论不存在")
70+
}
71+
72+
if content == "" {
73+
return fail(ctx, 1, "内容不能为空")
74+
}
75+
76+
me := ctx.Get("user").(*model.Me)
77+
// CanEdit 已包含修改时间限制
78+
if !logic.CanEdit(me, comment) {
79+
return fail(ctx, 3, "没有修改权限")
80+
}
81+
82+
errMsg, err := logic.DefaultComment.Modify(echoutils.WrapEchoContext(ctx), cid, content)
83+
if err != nil {
84+
return fail(ctx, 4, errMsg)
85+
}
86+
87+
return success(ctx, map[string]interface{}{"cid": cid})
88+
}
89+
6090
// CommentList 获取某对象的评论信息
6191
func (CommentController) CommentList(ctx echo.Context) error {
6292
objid := goutils.MustInt(ctx.QueryParam("objid"))

src/http/controller/image.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
type ImageController struct{}
2828

2929
func (self ImageController) RegisterRoute(g *echo.Group) {
30+
// todo 这三个upload差不多啊
3031
g.POST("/image/upload", self.Upload)
3132
g.POST("/image/paste_upload", self.PasteUpload)
3233
g.POST("/image/quick_upload", self.QuickUpload)

src/logic/comment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,14 @@ func (CommentLogic) findByIds(cids []int) map[int]*model.Comment {
303303
return comments
304304
}
305305

306-
func (CommentLogic) findById(cid int) *model.Comment {
306+
func (CommentLogic) FindById(cid int) (*model.Comment, error) {
307307
comment := &model.Comment{}
308308
_, err := MasterDB.Where("cid=?", cid).Get(comment)
309309
if err != nil {
310310
logger.Errorln("CommentLogic findById error:", err)
311311
}
312312

313-
return comment
313+
return comment, err
314314
}
315315

316316
func (CommentLogic) decodeCmtContent(ctx context.Context, comment *model.Comment) string {

src/logic/common.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ func CanEdit(me *model.Me, curModel interface{}) bool {
6767
}
6868

6969
canEditTime := time.Duration(UserSetting["can_edit_time"]) * time.Second
70-
7170
switch entity := curModel.(type) {
7271
case *model.Topic:
7372
if me.Uid != entity.Uid && me.IsAdmin && roleCanEdit(model.TopicAdmin, me) {
@@ -134,14 +133,24 @@ func CanEdit(me *model.Me, curModel interface{}) bool {
134133
return false
135134
}
136135

136+
if me.Uid == entity.Uid {
137+
return true
138+
}
139+
case *model.Comment:
140+
if me.IsAdmin && roleCanEdit(model.Administrator, me) {
141+
return true
142+
}
143+
if time.Now().Sub(time.Time(entity.Ctime)) > canEditTime {
144+
return false
145+
}
146+
137147
if me.Uid == entity.Uid {
138148
return true
139149
}
140150
case map[string]interface{}:
141151
if adminCanEdit(entity, me) {
142152
return true
143153
}
144-
145154
if ctime, ok := entity["ctime"]; ok {
146155
if time.Now().Sub(time.Time(ctime.(model.OftenTime))) > canEditTime {
147156
return false

src/logic/observer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func (UserRichObserver) Update(action string, uid, objtype, objid int) {
184184
if action == actionPublish || action == actionComment {
185185
var comment *model.Comment
186186
if action == actionComment {
187-
comment = DefaultComment.findById(objid)
187+
comment, _ = DefaultComment.FindById(objid)
188188
if comment.Cid != objid {
189189
return
190190
}

src/logic/uploader.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ func (this *UploaderLogic) uploadLocalFile(localFile, key string) (err error) {
8989

9090
var ret io.PutRet
9191
var extra = &io.PutExtra{
92-
// Params: params,
93-
// MimeType: mieType,
94-
// Crc32: crc32,
95-
// CheckCrc: CheckCrc,
92+
// Params: params,
93+
// MimeType: mieType,
94+
// Crc32: crc32,
95+
// CheckCrc: CheckCrc,
9696
}
9797

9898
// ret 变量用于存取返回的信息,详情见 io.PutRet
@@ -119,10 +119,10 @@ func (this *UploaderLogic) uploadMemoryFile(r gio.Reader, key string, size int)
119119

120120
var ret io.PutRet
121121
var extra = &io.PutExtra{
122-
// Params: params,
123-
// MimeType: mieType,
124-
// Crc32: crc32,
125-
// CheckCrc: CheckCrc,
122+
// Params: params,
123+
// MimeType: mieType,
124+
// Crc32: crc32,
125+
// CheckCrc: CheckCrc,
126126
}
127127

128128
// ret 变量用于存取返回的信息,详情见 io.PutRet

static/css/main.css

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,26 @@ a.tab_current:hover {background-color: #445; color: #fff; text-decoration: none;
190190
.page .page-comment .comment-title:after { display: block;visibility: hidden;height: 0; content: '\0020'; clear: both; }
191191
.page .page-comment .comment-title h2 { font-size: 24px;color: #D55252;font-weight: normal;float: left; font-family: "microsoft yahei"; margin-top: 0px; }
192192
.page .page-comment .comment-title .h2-tip { font-size: 12px;margin-left: 8px;float: left;color: #505050;padding-top: 4px;font-family: "nsimsun"; margin-bottom: 10.5px;}
193-
.page .page-comment .md-toolbar ul { margin-bottom:2px;}
193+
/*.page .page-comment .md-toolbar ul { margin-bottom:2px;}
194194
.page .page-comment .md-toolbar ul a.op { -moz-border-radius: 8px;-webkit-border-radius: 8px;border-radius: 8px;padding: 0 5px;line-height: 18px;font-size: 12px;margin-right: 6px;text-shadow: 0;color: #444;border: 1px solid #fff;}
195195
.page .page-comment .md-toolbar ul a.op:hover { text-decoration: none;}
196196
.page .page-comment .md-toolbar ul .cur a.op { background: #fff;border: 1px solid #ddd;color: #666;}
197+
*/
198+
ul.comment-tab-menu { margin-bottom:2px; }
199+
ul.comment-tab-menu a.op {-moz-border-radius: 8px;-webkit-border-radius: 8px;border-radius: 8px;padding: 0 5px;line-height: 18px;font-size: 12px;margin-right: 6px;text-shadow: 0;color: #444;border: 1px solid #fff;}
200+
ul.comment-tab-menu a.op:hover { text-decoration: none; }
201+
ul.comment-tab-menu .cur a.op { background: #fff;border: 1px solid #ddd;color: #666;}
202+
197203
.page .page-comment .md-toolbar .upload-img { cursor: pointer;}
198204
.page .page-comment .submit {border-bottom: solid 1px #ECECEC;}
199-
.page .page-comment .submit textarea {resize: none;width: 100%;color: #000;font-size: 14px;border: solid 1px #E5E5E5;padding: 5px;}
200-
.page .page-comment .submit textarea:focus{border: 1px solid rgba(128, 128, 160, 0.6); outline: none;}
205+
/*.page .page-comment .submit textarea {resize: none;width: 100%;color: #000;font-size: 14px;border: solid 1px #E5E5E5;padding: 5px;}*/
206+
textarea.comment-textarea {resize: none;width: 100%;color: #000;font-size: 14px;border: solid 1px #E5E5E5;padding: 5px;}
207+
/*.page .page-comment .submit textarea:focus{border: 1px solid rgba(128, 128, 160, 0.6); outline: none;}*/
208+
textarea.comment-textarea:focus{border: 1px solid rgba(128, 128, 160, 0.6); outline: none;}
201209
.page .page-comment .submit .sub ul { padding-left: 30px; font-size:13px; line-height: 13px;}
202210
.page .page-comment .submit .sub .btn {padding: 6px 22px;}
203-
.page .page-comment .content-preview { margin-bottom: 5px; width: 100%;height: 200px;border: 1px solid #CCCCCC;border-radius: 3px 3px 3px 3px;-moz-border-radius: 3px 3px 3px 3px;display: none;padding: 10px;overflow: scroll; display: none; }
211+
/*.page .page-comment .content-preview { margin-bottom: 5px; width: 100%;height: 200px;border: 1px solid #CCCCCC;border-radius: 3px 3px 3px 3px;-moz-border-radius: 3px 3px 3px 3px;display: none;padding: 10px;overflow: scroll; display: none; }*/
212+
.comment-content-preview { margin-bottom: 5px; width: 100%;height: 200px;border: 1px solid #CCCCCC;border-radius: 3px 3px 3px 3px;-moz-border-radius: 3px 3px 3px 3px;display: none;padding: 10px;overflow: scroll; display: none; }
204213

205214
.footer {margin-top: 40px; margin-bottom: 20px;}
206215

static/dist/css/sg_styles.css

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,26 @@ a.tab_current:hover {background-color: #445; color: #fff; text-decoration: none;
293293
.page .page-comment .comment-title:after { display: block;visibility: hidden;height: 0; content: '\0020'; clear: both; }
294294
.page .page-comment .comment-title h2 { font-size: 24px;color: #D55252;font-weight: normal;float: left; font-family: "microsoft yahei"; margin-top: 0px; }
295295
.page .page-comment .comment-title .h2-tip { font-size: 12px;margin-left: 8px;float: left;color: #505050;padding-top: 4px;font-family: "nsimsun"; margin-bottom: 10.5px;}
296-
.page .page-comment .md-toolbar ul { margin-bottom:2px;}
296+
/*.page .page-comment .md-toolbar ul { margin-bottom:2px;}
297297
.page .page-comment .md-toolbar ul a.op { -moz-border-radius: 8px;-webkit-border-radius: 8px;border-radius: 8px;padding: 0 5px;line-height: 18px;font-size: 12px;margin-right: 6px;text-shadow: 0;color: #444;border: 1px solid #fff;}
298298
.page .page-comment .md-toolbar ul a.op:hover { text-decoration: none;}
299299
.page .page-comment .md-toolbar ul .cur a.op { background: #fff;border: 1px solid #ddd;color: #666;}
300+
*/
301+
ul.comment-tab-menu { margin-bottom:2px; }
302+
ul.comment-tab-menu a.op {-moz-border-radius: 8px;-webkit-border-radius: 8px;border-radius: 8px;padding: 0 5px;line-height: 18px;font-size: 12px;margin-right: 6px;text-shadow: 0;color: #444;border: 1px solid #fff;}
303+
ul.comment-tab-menu a.op:hover { text-decoration: none; }
304+
ul.comment-tab-menu .cur a.op { background: #fff;border: 1px solid #ddd;color: #666;}
305+
300306
.page .page-comment .md-toolbar .upload-img { cursor: pointer;}
301307
.page .page-comment .submit {border-bottom: solid 1px #ECECEC;}
302-
.page .page-comment .submit textarea {resize: none;width: 100%;color: #000;font-size: 14px;border: solid 1px #E5E5E5;padding: 5px;}
303-
.page .page-comment .submit textarea:focus{border: 1px solid rgba(128, 128, 160, 0.6); outline: none;}
308+
/*.page .page-comment .submit textarea {resize: none;width: 100%;color: #000;font-size: 14px;border: solid 1px #E5E5E5;padding: 5px;}*/
309+
textarea.comment-textarea {resize: none;width: 100%;color: #000;font-size: 14px;border: solid 1px #E5E5E5;padding: 5px;}
310+
/*.page .page-comment .submit textarea:focus{border: 1px solid rgba(128, 128, 160, 0.6); outline: none;}*/
311+
textarea.comment-textarea:focus{border: 1px solid rgba(128, 128, 160, 0.6); outline: none;}
304312
.page .page-comment .submit .sub ul { padding-left: 30px; font-size:13px; line-height: 13px;}
305313
.page .page-comment .submit .sub .btn {padding: 6px 22px;}
306-
.page .page-comment .content-preview { margin-bottom: 5px; width: 100%;height: 200px;border: 1px solid #CCCCCC;border-radius: 3px 3px 3px 3px;-moz-border-radius: 3px 3px 3px 3px;display: none;padding: 10px;overflow: scroll; display: none; }
314+
/*.page .page-comment .content-preview { margin-bottom: 5px; width: 100%;height: 200px;border: 1px solid #CCCCCC;border-radius: 3px 3px 3px 3px;-moz-border-radius: 3px 3px 3px 3px;display: none;padding: 10px;overflow: scroll; display: none; }*/
315+
.comment-content-preview { margin-bottom: 5px; width: 100%;height: 200px;border: 1px solid #CCCCCC;border-radius: 3px 3px 3px 3px;-moz-border-radius: 3px 3px 3px 3px;display: none;padding: 10px;overflow: scroll; display: none; }
307316

308317
.footer {margin-top: 40px; margin-bottom: 20px;}
309318

static/dist/css/sg_styles.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/dist/js/account.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/dist/js/articles.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/dist/js/books.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)