@@ -16,11 +16,17 @@ import (
16
16
17
17
"code.gitea.io/gitea/modules/log"
18
18
"code.gitea.io/gitea/modules/markdown"
19
+ "github.com/go-xorm/builder"
19
20
)
20
21
21
22
// CommentType defines whether a comment is just a simple comment, an action (like close) or a reference.
22
23
type CommentType int
23
24
25
+ // define unknown comment type
26
+ const (
27
+ CommentTypeUnknown CommentType = - 1
28
+ )
29
+
24
30
// Enumerate all the comment types
25
31
const (
26
32
// Plain comment, can be associated with a commit (CommitID > 0) and a line (LineNum > 0)
@@ -568,45 +574,68 @@ func GetCommentByID(id int64) (*Comment, error) {
568
574
return c , nil
569
575
}
570
576
571
- func getCommentsByIssueIDSince (e Engine , issueID , since int64 ) ([]* Comment , error ) {
572
- comments := make ([]* Comment , 0 , 10 )
573
- sess := e .
574
- Where ("issue_id = ?" , issueID ).
575
- Asc ("created_unix" )
576
- if since > 0 {
577
- sess .And ("updated_unix >= ?" , since )
577
+ // FindCommentsOptions describes the condtions to Find comments
578
+ type FindCommentsOptions struct {
579
+ RepoID int64
580
+ IssueID int64
581
+ Since int64
582
+ Type CommentType
583
+ }
584
+
585
+ func (opts * FindCommentsOptions ) toConds () builder.Cond {
586
+ var cond = builder .NewCond ()
587
+ if opts .RepoID > 0 {
588
+ cond = cond .And (builder.Eq {"issue.repo_id" : opts .RepoID })
589
+ }
590
+ if opts .IssueID > 0 {
591
+ cond = cond .And (builder.Eq {"issue.id" : opts .IssueID })
578
592
}
579
- return comments , sess .Find (& comments )
593
+ if opts .Since > 0 {
594
+ cond = cond .And (builder.Gte {"comment.updated_unix" : opts .Since })
595
+ }
596
+ if opts .Type > - 1 {
597
+ cond = cond .And (builder.Eq {"comment.type" : opts .Type })
598
+ }
599
+ return cond
580
600
}
581
601
582
- func getCommentsByRepoIDSince (e Engine , repoID , since int64 ) ([]* Comment , error ) {
602
+ func findComments (e Engine , opts FindCommentsOptions ) ([]* Comment , error ) {
583
603
comments := make ([]* Comment , 0 , 10 )
584
- sess := e .Where ("issue.repo_id = ?" , repoID ).
585
- Join ("INNER" , "issue" , "issue.id = comment.issue_id" ).
586
- Asc ("comment.created_unix" )
587
- if since > 0 {
588
- sess .And ("comment.updated_unix >= ?" , since )
589
- }
590
- return comments , sess .Find (& comments )
604
+ return comments , e .Join ("INNER" , "issue" , "issue.id = comment.issue_id" ).
605
+ Where (opts .toConds ()).
606
+ Asc ("comment.created_unix" ).
607
+ Find (& comments )
591
608
}
592
609
593
- func getCommentsByIssueID (e Engine , issueID int64 ) ([]* Comment , error ) {
594
- return getCommentsByIssueIDSince (e , issueID , - 1 )
610
+ // FindComments returns all comments according options
611
+ func FindComments (opts FindCommentsOptions ) ([]* Comment , error ) {
612
+ return findComments (x , opts )
595
613
}
596
614
597
615
// GetCommentsByIssueID returns all comments of an issue.
598
616
func GetCommentsByIssueID (issueID int64 ) ([]* Comment , error ) {
599
- return getCommentsByIssueID (x , issueID )
617
+ return findComments (x , FindCommentsOptions {
618
+ IssueID : issueID ,
619
+ Type : CommentTypeUnknown ,
620
+ })
600
621
}
601
622
602
623
// GetCommentsByIssueIDSince returns a list of comments of an issue since a given time point.
603
624
func GetCommentsByIssueIDSince (issueID , since int64 ) ([]* Comment , error ) {
604
- return getCommentsByIssueIDSince (x , issueID , since )
625
+ return findComments (x , FindCommentsOptions {
626
+ IssueID : issueID ,
627
+ Type : CommentTypeUnknown ,
628
+ Since : since ,
629
+ })
605
630
}
606
631
607
632
// GetCommentsByRepoIDSince returns a list of comments for all issues in a repo since a given time point.
608
633
func GetCommentsByRepoIDSince (repoID , since int64 ) ([]* Comment , error ) {
609
- return getCommentsByRepoIDSince (x , repoID , since )
634
+ return findComments (x , FindCommentsOptions {
635
+ RepoID : repoID ,
636
+ Type : CommentTypeUnknown ,
637
+ Since : since ,
638
+ })
610
639
}
611
640
612
641
// UpdateComment updates information of comment.
0 commit comments