summaryrefslogtreecommitdiffstats
path: root/models/issue_comment.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2019-09-25 01:39:50 +0800
committertechknowlogick <techknowlogick@gitea.io>2019-09-24 13:39:50 -0400
commit061388379aae8fa5edc2b41a53512f17adc7a672 (patch)
tree8f866f552d99374a5c9bb54da0b4f9aa18b59254 /models/issue_comment.go
parent3dd1cee331b8d0beb84e2f92481183520939213a (diff)
downloadgitea-061388379aae8fa5edc2b41a53512f17adc7a672.tar.gz
gitea-061388379aae8fa5edc2b41a53512f17adc7a672.zip
Move create issue comment to comments package (#8212)
* move create issue comment to comments package * extract actions on update/delete comment from models to comment service * fix lint * fix lint
Diffstat (limited to 'models/issue_comment.go')
-rw-r--r--models/issue_comment.go117
1 files changed, 6 insertions, 111 deletions
diff --git a/models/issue_comment.go b/models/issue_comment.go
index 6786032f41..e8043c1ec7 100644
--- a/models/issue_comment.go
+++ b/models/issue_comment.go
@@ -169,7 +169,7 @@ func (c *Comment) loadIssue(e Engine) (err error) {
}
func (c *Comment) loadPoster(e Engine) (err error) {
- if c.Poster != nil {
+ if c.PosterID <= 0 || c.Poster != nil {
return nil
}
@@ -338,21 +338,7 @@ func (c *Comment) LoadMilestone() error {
// LoadPoster loads comment poster
func (c *Comment) LoadPoster() error {
- if c.PosterID <= 0 || c.Poster != nil {
- return nil
- }
-
- var err error
- c.Poster, err = getUserByID(x, c.PosterID)
- if err != nil {
- if IsErrUserNotExist(err) {
- c.PosterID = -1
- c.Poster = NewGhostUser()
- } else {
- log.Error("getUserByID[%d]: %v", c.ID, err)
- }
- }
- return nil
+ return c.loadPoster(x)
}
// LoadAttachments loads attachments
@@ -440,7 +426,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
}
@@ -811,35 +797,6 @@ func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
return comment, nil
}
-// CreateIssueComment creates a plain issue comment.
-func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content string, attachments []string) (*Comment, error) {
- comment, err := CreateComment(&CreateCommentOptions{
- Type: CommentTypeComment,
- Doer: doer,
- Repo: repo,
- Issue: issue,
- Content: content,
- Attachments: attachments,
- })
- if err != nil {
- return nil, fmt.Errorf("CreateComment: %v", err)
- }
-
- mode, _ := AccessLevel(doer, repo)
- if err = PrepareWebhooks(repo, HookEventIssueComment, &api.IssueCommentPayload{
- Action: api.HookIssueCommentCreated,
- Issue: issue.APIFormat(),
- Comment: comment.APIFormat(),
- Repository: repo.APIFormat(mode),
- Sender: doer.APIFormat(),
- }); err != nil {
- log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
- } else {
- go HookQueue.Add(repo.ID)
- }
- return comment, nil
-}
-
// CreateRefComment creates a commit reference comment to issue.
func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error {
if len(commitSHA) == 0 {
@@ -928,7 +885,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 {
@@ -950,38 +907,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 {
@@ -1007,43 +938,7 @@ func DeleteComment(doer *User, comment *Comment) error {
return err
}
- 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
+ return sess.Commit()
}
// CodeComments represents comments on code by using this structure: FILENAME -> LINE (+ == proposed; - == previous) -> COMMENTS