mirror of https://github.com/go-gitea/gitea
Support updating comments
This commit is contained in:
parent
8127c25de8
commit
07ba08000b
|
@ -274,6 +274,84 @@ func InsertIssueComments(comments []*issues_model.Comment) error {
|
|||
return committer.Commit()
|
||||
}
|
||||
|
||||
// UpsertIssueComments inserts many comments of issues.
|
||||
func UpsertIssueComments(comments []*issues_model.Comment) error {
|
||||
if len(comments) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
issueIDs := make(map[int64]bool)
|
||||
for _, comment := range comments {
|
||||
issueIDs[comment.IssueID] = true
|
||||
}
|
||||
|
||||
ctx, committer, err := db.TxContext()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer committer.Close()
|
||||
|
||||
sess := db.GetEngine(ctx)
|
||||
for _, comment := range comments {
|
||||
exists, err := sess.Exist(&issues_model.Comment{
|
||||
IssueID: comment.IssueID,
|
||||
CreatedUnix: comment.CreatedUnix,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
if _, err := sess.NoAutoTime().Insert(comment); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if _, err := sess.NoAutoTime().Where(
|
||||
"issue_id = ? AND created_unix = ?", comment.IssueID, comment.CreatedUnix,
|
||||
).Update(comment); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, reaction := range comment.Reactions {
|
||||
reaction.IssueID = comment.IssueID
|
||||
reaction.CommentID = comment.ID
|
||||
}
|
||||
if len(comment.Reactions) > 0 {
|
||||
for _, reaction := range comment.Reactions {
|
||||
// issue is uniquely identified by issue_id, comment_id and type
|
||||
exists, err := sess.Exist(&issues_model.Reaction{
|
||||
IssueID: reaction.IssueID,
|
||||
CommentID: reaction.CommentID,
|
||||
Type: reaction.Type,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
if _, err := sess.Where(
|
||||
"issue_id = ? AND comment_id = ? AND type = ?",
|
||||
reaction.IssueID, reaction.CommentID, reaction.Type,
|
||||
).Update(&reaction); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if _, err := sess.Insert(&reaction); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for issueID := range issueIDs {
|
||||
if _, err := db.Exec(ctx, "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ? AND `type`=?) WHERE id = ?",
|
||||
issueID, issues_model.CommentTypeComment, issueID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return committer.Commit()
|
||||
}
|
||||
|
||||
// InsertPullRequests inserted pull requests
|
||||
func InsertPullRequests(prs ...*issues_model.PullRequest) error {
|
||||
ctx, committer, err := db.TxContext()
|
||||
|
|
|
@ -449,14 +449,13 @@ func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// CreateComments creates comments of issues
|
||||
func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
|
||||
func (g *GiteaLocalUploader) prepareComments(comments ...*base.Comment) ([]*issues_model.Comment, error) {
|
||||
cms := make([]*issues_model.Comment, 0, len(comments))
|
||||
for _, comment := range comments {
|
||||
var issue *issues_model.Issue
|
||||
issue, ok := g.issues[comment.IssueIndex]
|
||||
if !ok {
|
||||
return fmt.Errorf("comment references non existent IssueIndex %d", comment.IssueIndex)
|
||||
return nil, fmt.Errorf("comment references non existent IssueIndex %d", comment.IssueIndex)
|
||||
}
|
||||
|
||||
if comment.Created.IsZero() {
|
||||
|
@ -475,7 +474,7 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
|
|||
}
|
||||
|
||||
if err := g.remapUser(comment, &cm); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// add reactions
|
||||
|
@ -485,13 +484,22 @@ func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
|
|||
CreatedUnix: timeutil.TimeStampNow(),
|
||||
}
|
||||
if err := g.remapUser(reaction, &res); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
cm.Reactions = append(cm.Reactions, &res)
|
||||
}
|
||||
|
||||
cms = append(cms, &cm)
|
||||
}
|
||||
return cms, nil
|
||||
}
|
||||
|
||||
// CreateComments creates comments of issues
|
||||
func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
|
||||
cms, err := g.prepareComments(comments...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(cms) == 0 {
|
||||
return nil
|
||||
|
@ -897,7 +905,15 @@ func (g *GiteaLocalUploader) PatchIssues(issues ...*base.Issue) error {
|
|||
}
|
||||
|
||||
func (g *GiteaLocalUploader) PatchComments(comments ...*base.Comment) error {
|
||||
return nil
|
||||
cms, err := g.prepareComments(comments...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(cms) == 0 {
|
||||
return nil
|
||||
}
|
||||
return models.UpsertIssueComments(cms)
|
||||
}
|
||||
|
||||
func (g *GiteaLocalUploader) PatchPullRequests(prs ...*base.PullRequest) error {
|
||||
|
|
Loading…
Reference in New Issue