Skip to content

Commit 7f71a28

Browse files
committed
fix refactor
1 parent 2afea36 commit 7f71a28

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

models/issue.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -815,9 +815,9 @@ func (issue *Issue) ChangeContent(doer *User, content string) (err error) {
815815
return fmt.Errorf("UpdateIssueCols: %v", err)
816816
}
817817

818-
SaveIssueContentHistory(issue.PosterID, issue.ID, 0, timeutil.TimeStampNow(), issue.Content, false)
818+
SaveIssueContentHistory(ctx.Engine(), issue.PosterID, issue.ID, 0, timeutil.TimeStampNow(), issue.Content, false)
819819

820-
if err = issue.addCrossReferences(sess, doer, true); err != nil {
820+
if err = issue.addCrossReferences(ctx.Engine(), doer, true); err != nil {
821821
return err
822822
}
823823

@@ -987,7 +987,7 @@ func newIssue(e db.Engine, doer *User, opts NewIssueOptions) (err error) {
987987
return err
988988
}
989989

990-
SaveIssueContentHistory(opts.Issue.PosterID, opts.Issue.ID, 0, timeutil.TimeStampNow(), opts.Issue.Content, true)
990+
SaveIssueContentHistory(e, opts.Issue.PosterID, opts.Issue.ID, 0, timeutil.TimeStampNow(), opts.Issue.Content, true)
991991

992992
return opts.Issue.addCrossReferences(e, doer, false)
993993
}

models/issue_content_history.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package models
66

77
import (
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)

routers/web/repo/issue_content_history.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package repo
66

77
import (
88
"bytes"
9+
"code.gitea.io/gitea/models/db"
910
"fmt"
1011
"html"
1112
"net/http"
@@ -34,7 +35,7 @@ func GetContentHistoryOverview(ctx *context.Context) {
3435
"textDeleteFromHistoryConfirm": i18n.Tr(lang, "repo.issues.content_history.delete_from_history_confirm"),
3536
"textOptions": i18n.Tr(lang, "repo.issues.content_history.options"),
3637
},
37-
"editedHistoryCountMap": models.QueryIssueContentHistoryEditedCountMap(issue.ID),
38+
"editedHistoryCountMap": models.QueryIssueContentHistoryEditedCountMap(db.DefaultContext().Engine(), issue.ID),
3839
})
3940
}
4041

@@ -46,7 +47,7 @@ func GetContentHistoryList(ctx *context.Context) {
4647
return
4748
}
4849

49-
items := models.FetchIssueContentHistoryList(issue.ID, commentID)
50+
items := models.FetchIssueContentHistoryList(db.DefaultContext().Engine(), issue.ID, commentID)
5051

5152
// render history list to HTML for frontend dropdown items: (name, value)
5253
// name is HTML of "avatar + userName + userAction + timeSince"
@@ -106,7 +107,7 @@ func GetContentHistoryDetail(ctx *context.Context) {
106107
}
107108

108109
historyID := ctx.FormInt64("history_id")
109-
history, prevHistory := models.GetIssueContentHistoryAndPrev(historyID)
110+
history, prevHistory := models.GetIssueContentHistoryAndPrev(db.DefaultContext().Engine(), historyID)
110111
if history == nil {
111112
ctx.JSON(http.StatusNotFound, map[string]interface{}{
112113
"message": "Can not find the content history",
@@ -182,7 +183,7 @@ func SoftDeleteContentHistory(ctx *context.Context) {
182183
return
183184
}
184185
}
185-
if history, err = models.GetIssueContentHistoryByID(historyID); err != nil {
186+
if history, err = models.GetIssueContentHistoryByID(db.DefaultContext().Engine(), historyID); err != nil {
186187
log.Error("can not get issue content history %v. err=%v", historyID, err)
187188
return
188189
}
@@ -195,7 +196,7 @@ func SoftDeleteContentHistory(ctx *context.Context) {
195196
return
196197
}
197198

198-
models.SoftDeleteIssueContentHistory(historyID)
199+
models.SoftDeleteIssueContentHistory(db.DefaultContext().Engine(), historyID)
199200
log.Debug("soft delete issue content history. issue=%d, comment=%d, history=%d", issue.ID, commentID, historyID)
200201
ctx.JSON(http.StatusOK, map[string]interface{}{
201202
"ok": true,

services/comments/comments.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func CreateIssueComment(doer *models.User, repo *models.Repository, issue *model
2929
return nil, err
3030
}
3131

32-
models.SaveIssueContentHistory(doer.ID, issue.ID, comment.ID, timeutil.TimeStampNow(), comment.Content, true)
32+
models.SaveIssueContentHistory(db.DefaultContext().Engine(), doer.ID, issue.ID, comment.ID, timeutil.TimeStampNow(), comment.Content, true)
3333

3434
notification.NotifyCreateIssueComment(doer, repo, issue, comment, mentions)
3535

@@ -43,7 +43,7 @@ func UpdateComment(c *models.Comment, doer *models.User, oldContent string) erro
4343
}
4444

4545
if c.Type == models.CommentTypeComment && c.Content != oldContent {
46-
models.SaveIssueContentHistory(doer.ID, c.IssueID, c.ID, timeutil.TimeStampNow(), c.Content, false)
46+
models.SaveIssueContentHistory(db.DefaultContext().Engine(), doer.ID, c.IssueID, c.ID, timeutil.TimeStampNow(), c.Content, false)
4747
}
4848

4949
notification.NotifyUpdateComment(doer, c, oldContent)

0 commit comments

Comments
 (0)