Skip to content

Commit 4ea1357

Browse files
committed
extract actions on update/delete comment from models to comment service
1 parent b702650 commit 4ea1357

File tree

4 files changed

+79
-65
lines changed

4 files changed

+79
-65
lines changed

models/issue_comment.go

+3-61
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ func (c *Comment) checkInvalidation(doer *User, repo *git.Repository, branch str
440440
}
441441
if c.CommitSHA != "" && c.CommitSHA != commit.ID.String() {
442442
c.Invalidated = true
443-
return UpdateComment(doer, c, "")
443+
return UpdateComment(c, doer)
444444
}
445445
return nil
446446
}
@@ -899,7 +899,7 @@ func FindComments(opts FindCommentsOptions) ([]*Comment, error) {
899899
}
900900

901901
// UpdateComment updates information of comment.
902-
func UpdateComment(doer *User, c *Comment, oldContent string) error {
902+
func UpdateComment(c *Comment, doer *User) error {
903903
sess := x.NewSession()
904904
defer sess.Close()
905905
if err := sess.Begin(); err != nil {
@@ -921,38 +921,12 @@ func UpdateComment(doer *User, c *Comment, oldContent string) error {
921921
if err := sess.Commit(); err != nil {
922922
return fmt.Errorf("Commit: %v", err)
923923
}
924-
sess.Close()
925-
926-
if err := c.LoadPoster(); err != nil {
927-
return err
928-
}
929-
if err := c.Issue.LoadAttributes(); err != nil {
930-
return err
931-
}
932-
933-
mode, _ := AccessLevel(doer, c.Issue.Repo)
934-
if err := PrepareWebhooks(c.Issue.Repo, HookEventIssueComment, &api.IssueCommentPayload{
935-
Action: api.HookIssueCommentEdited,
936-
Issue: c.Issue.APIFormat(),
937-
Comment: c.APIFormat(),
938-
Changes: &api.ChangesPayload{
939-
Body: &api.ChangesFromPayload{
940-
From: oldContent,
941-
},
942-
},
943-
Repository: c.Issue.Repo.APIFormat(mode),
944-
Sender: doer.APIFormat(),
945-
}); err != nil {
946-
log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
947-
} else {
948-
go HookQueue.Add(c.Issue.Repo.ID)
949-
}
950924

951925
return nil
952926
}
953927

954928
// DeleteComment deletes the comment
955-
func DeleteComment(doer *User, comment *Comment) error {
929+
func DeleteComment(comment *Comment, doer *User) error {
956930
sess := x.NewSession()
957931
defer sess.Close()
958932
if err := sess.Begin(); err != nil {
@@ -981,38 +955,6 @@ func DeleteComment(doer *User, comment *Comment) error {
981955
if err := sess.Commit(); err != nil {
982956
return err
983957
}
984-
sess.Close()
985-
986-
if err := comment.LoadPoster(); err != nil {
987-
return err
988-
}
989-
if err := comment.LoadIssue(); err != nil {
990-
return err
991-
}
992-
993-
if err := comment.Issue.LoadAttributes(); err != nil {
994-
return err
995-
}
996-
if err := comment.loadPoster(x); err != nil {
997-
return err
998-
}
999-
if err := comment.neuterCrossReferences(x); err != nil {
1000-
return err
1001-
}
1002-
1003-
mode, _ := AccessLevel(doer, comment.Issue.Repo)
1004-
1005-
if err := PrepareWebhooks(comment.Issue.Repo, HookEventIssueComment, &api.IssueCommentPayload{
1006-
Action: api.HookIssueCommentDeleted,
1007-
Issue: comment.Issue.APIFormat(),
1008-
Comment: comment.APIFormat(),
1009-
Repository: comment.Issue.Repo.APIFormat(mode),
1010-
Sender: doer.APIFormat(),
1011-
}); err != nil {
1012-
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
1013-
} else {
1014-
go HookQueue.Add(comment.Issue.Repo.ID)
1015-
}
1016958

1017959
return nil
1018960
}

routers/api/v1/repo/issue_comment.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)
300300

301301
oldContent := comment.Content
302302
comment.Content = form.Body
303-
if err := models.UpdateComment(ctx.User, comment, oldContent); err != nil {
303+
if err := comment_service.UpdateComment(comment, ctx.User, oldContent); err != nil {
304304
ctx.Error(500, "UpdateComment", err)
305305
return
306306
}
@@ -391,7 +391,7 @@ func deleteIssueComment(ctx *context.APIContext) {
391391
return
392392
}
393393

394-
if err = models.DeleteComment(ctx.User, comment); err != nil {
394+
if err = comment_service.DeleteComment(comment, ctx.User); err != nil {
395395
ctx.Error(500, "DeleteCommentByID", err)
396396
return
397397
}

routers/repo/issue.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ func UpdateCommentContent(ctx *context.Context) {
13401340
})
13411341
return
13421342
}
1343-
if err = models.UpdateComment(ctx.User, comment, oldContent); err != nil {
1343+
if err = comment_service.UpdateComment(comment, ctx.User, oldContent); err != nil {
13441344
ctx.ServerError("UpdateComment", err)
13451345
return
13461346
}
@@ -1373,7 +1373,7 @@ func DeleteComment(ctx *context.Context) {
13731373
return
13741374
}
13751375

1376-
if err = models.DeleteComment(ctx.User, comment); err != nil {
1376+
if err = models.DeleteComment(comment, ctx.User); err != nil {
13771377
ctx.ServerError("DeleteCommentByID", err)
13781378
return
13791379
}

services/comments/comments.go

+72
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,75 @@ func CreateCodeComment(doer *models.User, repo *models.Repository, issue *models
9898
Patch: patch,
9999
})
100100
}
101+
102+
// UpdateComment updates information of comment.
103+
func UpdateComment(c *models.Comment, doer *models.User, oldContent string) error {
104+
if err := models.UpdateComment(c, doer); err != nil {
105+
return err
106+
}
107+
108+
if err := c.LoadPoster(); err != nil {
109+
return err
110+
}
111+
if err := c.LoadIssue(); err != nil {
112+
return err
113+
}
114+
115+
if err := c.Issue.LoadAttributes(); err != nil {
116+
return err
117+
}
118+
119+
mode, _ := models.AccessLevel(doer, c.Issue.Repo)
120+
if err := models.PrepareWebhooks(c.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
121+
Action: api.HookIssueCommentEdited,
122+
Issue: c.Issue.APIFormat(),
123+
Comment: c.APIFormat(),
124+
Changes: &api.ChangesPayload{
125+
Body: &api.ChangesFromPayload{
126+
From: oldContent,
127+
},
128+
},
129+
Repository: c.Issue.Repo.APIFormat(mode),
130+
Sender: doer.APIFormat(),
131+
}); err != nil {
132+
log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
133+
} else {
134+
go models.HookQueue.Add(c.Issue.Repo.ID)
135+
}
136+
137+
return nil
138+
}
139+
140+
// DeleteComment deletes the comment
141+
func DeleteComment(comment *models.Comment, doer *models.User) error {
142+
if err := models.DeleteComment(comment, doer); err != nil {
143+
return err
144+
}
145+
146+
if err := comment.LoadPoster(); err != nil {
147+
return err
148+
}
149+
if err := comment.LoadIssue(); err != nil {
150+
return err
151+
}
152+
153+
if err := comment.Issue.LoadAttributes(); err != nil {
154+
return err
155+
}
156+
157+
mode, _ := models.AccessLevel(doer, comment.Issue.Repo)
158+
159+
if err := models.PrepareWebhooks(comment.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
160+
Action: api.HookIssueCommentDeleted,
161+
Issue: comment.Issue.APIFormat(),
162+
Comment: comment.APIFormat(),
163+
Repository: comment.Issue.Repo.APIFormat(mode),
164+
Sender: doer.APIFormat(),
165+
}); err != nil {
166+
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
167+
} else {
168+
go models.HookQueue.Add(comment.Issue.Repo.ID)
169+
}
170+
171+
return nil
172+
}

0 commit comments

Comments
 (0)