Skip to content

Commit

Permalink
extract actions on update/delete comment from models to comment service
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Sep 24, 2019
1 parent b702650 commit 4ea1357
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 65 deletions.
64 changes: 3 additions & 61 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func (c *Comment) checkInvalidation(doer *User, repo *git.Repository, branch str
}
if c.CommitSHA != "" && c.CommitSHA != commit.ID.String() {
c.Invalidated = true
return UpdateComment(doer, c, "")
return UpdateComment(c, doer)
}
return nil
}
Expand Down Expand Up @@ -899,7 +899,7 @@ func FindComments(opts FindCommentsOptions) ([]*Comment, error) {
}

// UpdateComment updates information of comment.
func UpdateComment(doer *User, c *Comment, oldContent string) error {
func UpdateComment(c *Comment, doer *User) error {
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
Expand All @@ -921,38 +921,12 @@ func UpdateComment(doer *User, c *Comment, oldContent string) error {
if err := sess.Commit(); err != nil {
return fmt.Errorf("Commit: %v", err)
}
sess.Close()

if err := c.LoadPoster(); err != nil {
return err
}
if err := c.Issue.LoadAttributes(); err != nil {
return err
}

mode, _ := AccessLevel(doer, c.Issue.Repo)
if err := PrepareWebhooks(c.Issue.Repo, HookEventIssueComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentEdited,
Issue: c.Issue.APIFormat(),
Comment: c.APIFormat(),
Changes: &api.ChangesPayload{
Body: &api.ChangesFromPayload{
From: oldContent,
},
},
Repository: c.Issue.Repo.APIFormat(mode),
Sender: doer.APIFormat(),
}); err != nil {
log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
} else {
go HookQueue.Add(c.Issue.Repo.ID)
}

return nil
}

// DeleteComment deletes the comment
func DeleteComment(doer *User, comment *Comment) error {
func DeleteComment(comment *Comment, doer *User) error {
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
Expand Down Expand Up @@ -981,38 +955,6 @@ func DeleteComment(doer *User, comment *Comment) error {
if err := sess.Commit(); err != nil {
return err
}
sess.Close()

if err := comment.LoadPoster(); err != nil {
return err
}
if err := comment.LoadIssue(); err != nil {
return err
}

if err := comment.Issue.LoadAttributes(); err != nil {
return err
}
if err := comment.loadPoster(x); err != nil {
return err
}
if err := comment.neuterCrossReferences(x); err != nil {
return err
}

mode, _ := AccessLevel(doer, comment.Issue.Repo)

if err := PrepareWebhooks(comment.Issue.Repo, HookEventIssueComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentDeleted,
Issue: comment.Issue.APIFormat(),
Comment: comment.APIFormat(),
Repository: comment.Issue.Repo.APIFormat(mode),
Sender: doer.APIFormat(),
}); err != nil {
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
} else {
go HookQueue.Add(comment.Issue.Repo.ID)
}

return nil
}
Expand Down
4 changes: 2 additions & 2 deletions routers/api/v1/repo/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)

oldContent := comment.Content
comment.Content = form.Body
if err := models.UpdateComment(ctx.User, comment, oldContent); err != nil {
if err := comment_service.UpdateComment(comment, ctx.User, oldContent); err != nil {
ctx.Error(500, "UpdateComment", err)
return
}
Expand Down Expand Up @@ -391,7 +391,7 @@ func deleteIssueComment(ctx *context.APIContext) {
return
}

if err = models.DeleteComment(ctx.User, comment); err != nil {
if err = comment_service.DeleteComment(comment, ctx.User); err != nil {
ctx.Error(500, "DeleteCommentByID", err)
return
}
Expand Down
4 changes: 2 additions & 2 deletions routers/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ func UpdateCommentContent(ctx *context.Context) {
})
return
}
if err = models.UpdateComment(ctx.User, comment, oldContent); err != nil {
if err = comment_service.UpdateComment(comment, ctx.User, oldContent); err != nil {
ctx.ServerError("UpdateComment", err)
return
}
Expand Down Expand Up @@ -1373,7 +1373,7 @@ func DeleteComment(ctx *context.Context) {
return
}

if err = models.DeleteComment(ctx.User, comment); err != nil {
if err = models.DeleteComment(comment, ctx.User); err != nil {
ctx.ServerError("DeleteCommentByID", err)
return
}
Expand Down
72 changes: 72 additions & 0 deletions services/comments/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,75 @@ func CreateCodeComment(doer *models.User, repo *models.Repository, issue *models
Patch: patch,
})
}

// UpdateComment updates information of comment.
func UpdateComment(c *models.Comment, doer *models.User, oldContent string) error {
if err := models.UpdateComment(c, doer); err != nil {
return err
}

if err := c.LoadPoster(); err != nil {
return err
}
if err := c.LoadIssue(); err != nil {
return err
}

if err := c.Issue.LoadAttributes(); err != nil {
return err
}

mode, _ := models.AccessLevel(doer, c.Issue.Repo)
if err := models.PrepareWebhooks(c.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentEdited,
Issue: c.Issue.APIFormat(),
Comment: c.APIFormat(),
Changes: &api.ChangesPayload{
Body: &api.ChangesFromPayload{
From: oldContent,
},
},
Repository: c.Issue.Repo.APIFormat(mode),
Sender: doer.APIFormat(),
}); err != nil {
log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
} else {
go models.HookQueue.Add(c.Issue.Repo.ID)
}

return nil
}

// DeleteComment deletes the comment
func DeleteComment(comment *models.Comment, doer *models.User) error {
if err := models.DeleteComment(comment, doer); err != nil {
return err
}

if err := comment.LoadPoster(); err != nil {
return err
}
if err := comment.LoadIssue(); err != nil {
return err
}

if err := comment.Issue.LoadAttributes(); err != nil {
return err
}

mode, _ := models.AccessLevel(doer, comment.Issue.Repo)

if err := models.PrepareWebhooks(comment.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
Action: api.HookIssueCommentDeleted,
Issue: comment.Issue.APIFormat(),
Comment: comment.APIFormat(),
Repository: comment.Issue.Repo.APIFormat(mode),
Sender: doer.APIFormat(),
}); err != nil {
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
} else {
go models.HookQueue.Add(comment.Issue.Repo.ID)
}

return nil
}

0 comments on commit 4ea1357

Please sign in to comment.