diff --git a/internal/database/comment.go b/internal/database/comment.go index 54780cb5127..9ebd2d1de8e 100644 --- a/internal/database/comment.go +++ b/internal/database/comment.go @@ -434,9 +434,14 @@ func loadCommentsAttributes(e Engine, comments []*Comment) (err error) { return nil } -func getCommentsByIssueIDSince(e Engine, issueID, since int64) ([]*Comment, error) { +func getCommentsByIssueIDSince(e Engine, issueID, since int64, isAsc bool) ([]*Comment, error) { comments := make([]*Comment, 0, 10) - sess := e.Where("issue_id = ?", issueID).Asc("created_unix") + sess := e.Where("issue_id = ?", issueID) + if isAsc { + sess.Asc("created_unix") + } else { + sess.Desc("created_unix") + } if since > 0 { sess.And("updated_unix >= ?", since) } @@ -447,9 +452,14 @@ func getCommentsByIssueIDSince(e Engine, issueID, since int64) ([]*Comment, erro return comments, loadCommentsAttributes(e, comments) } -func getCommentsByRepoIDSince(e Engine, repoID, since int64) ([]*Comment, error) { +func getCommentsByRepoIDSince(e Engine, repoID, since int64, isAsc bool) ([]*Comment, error) { comments := make([]*Comment, 0, 10) - sess := e.Where("issue.repo_id = ?", repoID).Join("INNER", "issue", "issue.id = comment.issue_id").Asc("comment.created_unix") + sess := e.Where("issue.repo_id = ?", repoID).Join("INNER", "issue", "issue.id = comment.issue_id") + if isAsc { + sess.Asc("comment.created_unix") + } else { + sess.Desc("comment.created_unix") + } if since > 0 { sess.And("comment.updated_unix >= ?", since) } @@ -459,23 +469,25 @@ func getCommentsByRepoIDSince(e Engine, repoID, since int64) ([]*Comment, error) return comments, loadCommentsAttributes(e, comments) } -func getCommentsByIssueID(e Engine, issueID int64) ([]*Comment, error) { - return getCommentsByIssueIDSince(e, issueID, -1) +func getCommentsByIssueID(e Engine, issueID int64, isAsc bool) ([]*Comment, error) { + return getCommentsByIssueIDSince(e, issueID, -1, isAsc) } -// GetCommentsByIssueID returns all comments of an issue. -func GetCommentsByIssueID(issueID int64) ([]*Comment, error) { - return getCommentsByIssueID(x, issueID) +// GetCommentsByIssueID returns all comments of an issue, sorted by created time. +func GetCommentsByIssueID(issueID int64, isAsc bool) ([]*Comment, error) { + return getCommentsByIssueID(x, issueID, isAsc) } -// GetCommentsByIssueIDSince returns a list of comments of an issue since a given time point. -func GetCommentsByIssueIDSince(issueID, since int64) ([]*Comment, error) { - return getCommentsByIssueIDSince(x, issueID, since) +// GetCommentsByIssueIDSince returns a list of comments of an issue since a given time point, sorted +// by created time. +func GetCommentsByIssueIDSince(issueID, since int64, isAsc bool) ([]*Comment, error) { + return getCommentsByIssueIDSince(x, issueID, since, isAsc) } -// GetCommentsByRepoIDSince returns a list of comments for all issues in a repo since a given time point. -func GetCommentsByRepoIDSince(repoID, since int64) ([]*Comment, error) { - return getCommentsByRepoIDSince(x, repoID, since) +// GetCommentsByRepoIDSince returns a list of comments for all issues in a repo since a given time +// point, sorted by created time. +func GetCommentsByRepoIDSince(repoID, since int64, isAsc bool) ([]*Comment, error) { + return getCommentsByRepoIDSince(x, repoID, since, isAsc) } // UpdateComment updates information of comment. diff --git a/internal/database/issue.go b/internal/database/issue.go index 18cdef1531a..800a820dda0 100644 --- a/internal/database/issue.go +++ b/internal/database/issue.go @@ -152,7 +152,7 @@ func (issue *Issue) loadAttributes(e Engine) (err error) { } if issue.Comments == nil { - issue.Comments, err = getCommentsByIssueID(e, issue.ID) + issue.Comments, err = getCommentsByIssueID(e, issue.ID, true) if err != nil { return fmt.Errorf("getCommentsByIssueID [%d]: %v", issue.ID, err) } diff --git a/internal/route/api/v1/repo/issue_comment.go b/internal/route/api/v1/repo/issue_comment.go index a816daad641..bfd5813c618 100644 --- a/internal/route/api/v1/repo/issue_comment.go +++ b/internal/route/api/v1/repo/issue_comment.go @@ -13,9 +13,13 @@ import ( "gogs.io/gogs/internal/database" ) +// ListIssueComments list comments on an issue. func ListIssueComments(c *context.APIContext) { + // Initialize a variable to hold the "since" time var since time.Time + // Check if the "since" query parameter is provided if len(c.Query("since")) > 0 { + // Attempt to parse the "since" value as a time in RFC3339 format var err error since, err = time.Parse(time.RFC3339, c.Query("since")) if err != nil { @@ -24,29 +28,46 @@ func ListIssueComments(c *context.APIContext) { } } - // comments,err:=db.GetCommentsByIssueIDSince(, since) + // Retrieve the raw issue by its index issue, err := database.GetRawIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index")) + // If an error occurs, return an error response if err != nil { c.Error(err, "get raw issue by index") return } - comments, err := database.GetCommentsByIssueIDSince(issue.ID, since.Unix()) + // Initialize a boolean variable to determine the sort order + var isAsc bool = true + // Check if the "is_asc" query parameter is set to "false" + if c.Query("is_asc") == "false" { + // If so, set the sort order to descending + isAsc = false + } + + // Retrieve comments for the issue since a given time + comments, err := database.GetCommentsByIssueIDSince(issue.ID, since.Unix(), isAsc) + // If an error occurs, return an error response if err != nil { c.Error(err, "get comments by issue ID") return } + // Create a slice of API comments to hold the formatted comments apiComments := make([]*api.Comment, len(comments)) + // Iterate over the comments and format them for the API for i := range comments { apiComments[i] = comments[i].APIFormat() } + // Return the formatted comments as a JSON response c.JSONSuccess(&apiComments) } +// ListRepoIssueComments list comments for a given repo. func ListRepoIssueComments(c *context.APIContext) { var since time.Time + // Check if the "since" query parameter is provided if len(c.Query("since")) > 0 { + // Attempt to parse the "since" value as a time in RFC3339 format var err error since, err = time.Parse(time.RFC3339, c.Query("since")) if err != nil { @@ -55,7 +76,16 @@ func ListRepoIssueComments(c *context.APIContext) { } } - comments, err := database.GetCommentsByRepoIDSince(c.Repo.Repository.ID, since.Unix()) + // Initialize a boolean variable to determine the sort order + var isAsc bool = true + // Check if the "is_asc" query parameter is set to "false" + if c.Query("is_asc") == "false" { + // If so, set the sort order to descending + isAsc = false + } + + // Retrieve comments for the repository since a given time + comments, err := database.GetCommentsByRepoIDSince(c.Repo.Repository.ID, since.Unix(), isAsc) if err != nil { c.Error(err, "get comments by repository ID") return