🌐 AI搜索 & 代理 主页
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions internal/database/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion internal/database/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
36 changes: 33 additions & 3 deletions internal/route/api/v1/repo/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down