5
5
package models
6
6
7
7
import (
8
+ "code.gitea.io/gitea/models/db"
8
9
"fmt"
9
10
10
11
"code.gitea.io/gitea/modules/log"
@@ -25,8 +26,12 @@ type IssueContentHistory struct {
25
26
IsDeleted bool `xorm:""`
26
27
}
27
28
29
+ func init () {
30
+ db .RegisterModel (new (IssueContentHistory ))
31
+ }
32
+
28
33
// 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 ) {
30
35
ch := & IssueContentHistory {
31
36
PosterID : posterID ,
32
37
IssueID : issueID ,
@@ -35,25 +40,25 @@ func SaveIssueContentHistory(posterID, issueID, commentID int64, editTime timeut
35
40
EditedUnix : editTime ,
36
41
IsFirstCreated : isFirstCreated ,
37
42
}
38
- _ , err := x .Insert (ch )
43
+ _ , err := e .Insert (ch )
39
44
if err != nil {
40
45
log .Error ("can not save issue content history. err=%v" , err )
41
46
}
42
47
}
43
48
44
49
// QueryIssueContentHistoryEditedCountMap query related history count of each comment (comment_id = 0 means the main issue)
45
50
// 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 {
47
52
type HistoryCountRecord struct {
48
53
CommentID int64
49
54
HistoryCount int
50
55
}
51
56
records := make ([]* HistoryCountRecord , 0 )
52
57
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" ).
55
59
Table ("issue_content_history" ).
56
60
Where (builder.Eq {"issue_id" : issueID }).
61
+ GroupBy ("comment_id" ).
57
62
Having ("history_count > 1" ).
58
63
Find (& records )
59
64
if err != nil {
@@ -83,9 +88,9 @@ type IssueContentListItem struct {
83
88
}
84
89
85
90
// FetchIssueContentHistoryList fetch list
86
- func FetchIssueContentHistoryList (issueID int64 , commentID int64 ) []* IssueContentListItem {
91
+ func FetchIssueContentHistoryList (e db. Engine , issueID int64 , commentID int64 ) []* IssueContentListItem {
87
92
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," +
89
94
"u.avatar as user_avatar, u.avatar_email as user_avatar_email, u.use_custom_avatar," +
90
95
"h.id as history_id, h.edited_unix, h.is_first_created, h.is_deleted" ).
91
96
Table ([]string {"issue_content_history" , "h" }).
@@ -112,8 +117,8 @@ func FetchIssueContentHistoryList(issueID int64, commentID int64) []*IssueConten
112
117
}
113
118
114
119
//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 {
117
122
IsDeleted : true ,
118
123
ContentText : "" ,
119
124
}); err != nil {
@@ -132,9 +137,9 @@ func (err ErrIssueContentHistoryNotExist) Error() string {
132
137
}
133
138
134
139
// GetIssueContentHistoryByID get issue content history
135
- func GetIssueContentHistoryByID (id int64 ) (* IssueContentHistory , error ) {
140
+ func GetIssueContentHistoryByID (e db. Engine , id int64 ) (* IssueContentHistory , error ) {
136
141
h := & IssueContentHistory {}
137
- has , err := x .ID (id ).Get (h )
142
+ has , err := e .ID (id ).Get (h )
138
143
if err != nil {
139
144
return nil , err
140
145
} else if ! has {
@@ -144,9 +149,9 @@ func GetIssueContentHistoryByID(id int64) (*IssueContentHistory, error) {
144
149
}
145
150
146
151
// 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 ) {
148
153
history = & IssueContentHistory {}
149
- has , err := x .ID (id ).Get (history )
154
+ has , err := e .ID (id ).Get (history )
150
155
if err != nil {
151
156
log .Error ("failed to get issue content history %v. err=%v" , id , err )
152
157
return nil , nil
@@ -156,7 +161,7 @@ func GetIssueContentHistoryAndPrev(id int64) (history, prevHistory *IssueContent
156
161
}
157
162
158
163
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 }).
160
165
And (builder.Lt {"edited_unix" : history .EditedUnix }).
161
166
OrderBy ("edited_unix DESC" ).Limit (1 ).
162
167
Get (prevHistory )
0 commit comments