55package models
66
77import (
8+ "code.gitea.io/gitea/models/db"
89 "fmt"
910
1011 "code.gitea.io/gitea/modules/log"
@@ -25,8 +26,12 @@ type IssueContentHistory struct {
2526 IsDeleted bool `xorm:""`
2627}
2728
29+ func init () {
30+ db .RegisterModel (new (IssueContentHistory ))
31+ }
32+
2833// SaveIssueContentHistory save history
29- func SaveIssueContentHistory (posterID , issueID , commentID int64 , editTime timeutil.TimeStamp , contentText string , isFirstCreated bool ) {
34+ func SaveIssueContentHistory (e db. Engine , posterID , issueID , commentID int64 , editTime timeutil.TimeStamp , contentText string , isFirstCreated bool ) {
3035 ch := & IssueContentHistory {
3136 PosterID : posterID ,
3237 IssueID : issueID ,
@@ -35,25 +40,25 @@ func SaveIssueContentHistory(posterID, issueID, commentID int64, editTime timeut
3540 EditedUnix : editTime ,
3641 IsFirstCreated : isFirstCreated ,
3742 }
38- _ , err := x .Insert (ch )
43+ _ , err := e .Insert (ch )
3944 if err != nil {
4045 log .Error ("can not save issue content history. err=%v" , err )
4146 }
4247}
4348
4449// QueryIssueContentHistoryEditedCountMap query related history count of each comment (comment_id = 0 means the main issue)
4550// only return the count map for "edited" (history revision count > 1) issues or comments.
46- func QueryIssueContentHistoryEditedCountMap (issueID int64 ) map [int64 ]int {
51+ func QueryIssueContentHistoryEditedCountMap (e db. Engine , issueID int64 ) map [int64 ]int {
4752 type HistoryCountRecord struct {
4853 CommentID int64
4954 HistoryCount int
5055 }
5156 records := make ([]* HistoryCountRecord , 0 )
5257
53- err := x .GroupBy ("comment_id" ).
54- Select ("comment_id, COUNT(1) as history_count" ).
58+ err := e .Select ("comment_id, COUNT(1) as history_count" ).
5559 Table ("issue_content_history" ).
5660 Where (builder.Eq {"issue_id" : issueID }).
61+ GroupBy ("comment_id" ).
5762 Having ("history_count > 1" ).
5863 Find (& records )
5964 if err != nil {
@@ -83,9 +88,9 @@ type IssueContentListItem struct {
8388}
8489
8590// FetchIssueContentHistoryList fetch list
86- func FetchIssueContentHistoryList (issueID int64 , commentID int64 ) []* IssueContentListItem {
91+ func FetchIssueContentHistoryList (e db. Engine , issueID int64 , commentID int64 ) []* IssueContentListItem {
8792 res := make ([]* IssueContentListItem , 0 )
88- err := x .Select ("u.id as user_id, u.name as user_name," +
93+ err := e .Select ("u.id as user_id, u.name as user_name," +
8994 "u.avatar as user_avatar, u.avatar_email as user_avatar_email, u.use_custom_avatar," +
9095 "h.id as history_id, h.edited_unix, h.is_first_created, h.is_deleted" ).
9196 Table ([]string {"issue_content_history" , "h" }).
@@ -112,8 +117,8 @@ func FetchIssueContentHistoryList(issueID int64, commentID int64) []*IssueConten
112117}
113118
114119//SoftDeleteIssueContentHistory soft delete
115- func SoftDeleteIssueContentHistory (historyID int64 ) {
116- if _ , err := x .ID (historyID ).Cols ("is_deleted" , "content_text" ).Update (& IssueContentHistory {
120+ func SoftDeleteIssueContentHistory (e db. Engine , historyID int64 ) {
121+ if _ , err := e .ID (historyID ).Cols ("is_deleted" , "content_text" ).Update (& IssueContentHistory {
117122 IsDeleted : true ,
118123 ContentText : "" ,
119124 }); err != nil {
@@ -132,9 +137,9 @@ func (err ErrIssueContentHistoryNotExist) Error() string {
132137}
133138
134139// GetIssueContentHistoryByID get issue content history
135- func GetIssueContentHistoryByID (id int64 ) (* IssueContentHistory , error ) {
140+ func GetIssueContentHistoryByID (e db. Engine , id int64 ) (* IssueContentHistory , error ) {
136141 h := & IssueContentHistory {}
137- has , err := x .ID (id ).Get (h )
142+ has , err := e .ID (id ).Get (h )
138143 if err != nil {
139144 return nil , err
140145 } else if ! has {
@@ -144,9 +149,9 @@ func GetIssueContentHistoryByID(id int64) (*IssueContentHistory, error) {
144149}
145150
146151// GetIssueContentHistoryAndPrev get a history and the previous non-deleted history (to compare)
147- func GetIssueContentHistoryAndPrev (id int64 ) (history , prevHistory * IssueContentHistory ) {
152+ func GetIssueContentHistoryAndPrev (e db. Engine , id int64 ) (history , prevHistory * IssueContentHistory ) {
148153 history = & IssueContentHistory {}
149- has , err := x .ID (id ).Get (history )
154+ has , err := e .ID (id ).Get (history )
150155 if err != nil {
151156 log .Error ("failed to get issue content history %v. err=%v" , id , err )
152157 return nil , nil
@@ -156,7 +161,7 @@ func GetIssueContentHistoryAndPrev(id int64) (history, prevHistory *IssueContent
156161 }
157162
158163 prevHistory = & IssueContentHistory {}
159- has , err = x .Where (builder.Eq {"issue_id" : history .IssueID , "comment_id" : history .CommentID , "is_deleted" : false }).
164+ has , err = e .Where (builder.Eq {"issue_id" : history .IssueID , "comment_id" : history .CommentID , "is_deleted" : false }).
160165 And (builder.Lt {"edited_unix" : history .EditedUnix }).
161166 OrderBy ("edited_unix DESC" ).Limit (1 ).
162167 Get (prevHistory )
0 commit comments