From e64c66bb2344e903bf1bc0a7c9d7851236e9a1cc Mon Sep 17 00:00:00 2001 From: pikomonde <32364823+pikomonde@users.noreply.github.com> Date: Wed, 3 Apr 2024 22:06:59 +0700 Subject: [PATCH 1/2] add backend changes --- internal/database/comment.go | 34 +++++++++++++-------- internal/database/issue.go | 2 +- internal/route/api/v1/repo/issue_comment.go | 14 +++++++-- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/internal/database/comment.go b/internal/database/comment.go index 54780cb5127..639f7551ffc 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,23 @@ 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) +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) +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) +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..f728acebaaa 100644 --- a/internal/route/api/v1/repo/issue_comment.go +++ b/internal/route/api/v1/repo/issue_comment.go @@ -31,7 +31,12 @@ func ListIssueComments(c *context.APIContext) { return } - comments, err := database.GetCommentsByIssueIDSince(issue.ID, since.Unix()) + var isAsc bool = true + if c.Query("is_asc") == "false" { + isAsc = false + } + + comments, err := database.GetCommentsByIssueIDSince(issue.ID, since.Unix(), isAsc) if err != nil { c.Error(err, "get comments by issue ID") return @@ -55,7 +60,12 @@ func ListRepoIssueComments(c *context.APIContext) { } } - comments, err := database.GetCommentsByRepoIDSince(c.Repo.Repository.ID, since.Unix()) + var isAsc bool = true + if c.Query("is_asc") == "false" { + isAsc = false + } + + comments, err := database.GetCommentsByRepoIDSince(c.Repo.Repository.ID, since.Unix(), isAsc) if err != nil { c.Error(err, "get comments by repository ID") return From 2ecbca04517dd06dac2d6ee596f77e433162a624 Mon Sep 17 00:00:00 2001 From: pikomonde <32364823+pikomonde@users.noreply.github.com> Date: Thu, 4 Apr 2024 20:05:58 +0700 Subject: [PATCH 2/2] add docs --- internal/database/comment.go | 8 +++++--- internal/route/api/v1/repo/issue_comment.go | 22 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/internal/database/comment.go b/internal/database/comment.go index 639f7551ffc..9ebd2d1de8e 100644 --- a/internal/database/comment.go +++ b/internal/database/comment.go @@ -473,17 +473,19 @@ func getCommentsByIssueID(e Engine, issueID int64, isAsc bool) ([]*Comment, erro return getCommentsByIssueIDSince(e, issueID, -1, isAsc) } -// GetCommentsByIssueID returns all comments of an issue. +// 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. +// 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. +// 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) } diff --git a/internal/route/api/v1/repo/issue_comment.go b/internal/route/api/v1/repo/issue_comment.go index f728acebaaa..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,34 +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 } + // 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 { @@ -60,11 +76,15 @@ func ListRepoIssueComments(c *context.APIContext) { } } + // 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")