Skip to content

Commit d4f84f1

Browse files
lunnywxiaoguang
andauthored
Move reaction to models/issues/ (#19264)
* Move reaction to models/issues/ * Fix test * move the function * improve code * Update models/issues/reaction.go Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent 43332a4 commit d4f84f1

18 files changed

+279
-273
lines changed

models/action_list.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"code.gitea.io/gitea/models/db"
1111
repo_model "code.gitea.io/gitea/models/repo"
1212
user_model "code.gitea.io/gitea/models/user"
13+
"code.gitea.io/gitea/modules/container"
1314
)
1415

1516
// ActionList defines a list of actions
@@ -22,7 +23,7 @@ func (actions ActionList) getUserIDs() []int64 {
2223
userIDs[action.ActUserID] = struct{}{}
2324
}
2425
}
25-
return keysInt64(userIDs)
26+
return container.KeysInt64(userIDs)
2627
}
2728

2829
func (actions ActionList) loadUsers(e db.Engine) (map[int64]*user_model.User, error) {
@@ -52,7 +53,7 @@ func (actions ActionList) getRepoIDs() []int64 {
5253
repoIDs[action.RepoID] = struct{}{}
5354
}
5455
}
55-
return keysInt64(repoIDs)
56+
return container.KeysInt64(repoIDs)
5657
}
5758

5859
func (actions ActionList) loadRepositories(e db.Engine) error {

models/error.go

-30
Original file line numberDiff line numberDiff line change
@@ -765,36 +765,6 @@ func (err ErrPullWasClosed) Error() string {
765765
return fmt.Sprintf("Pull request [%d] %d was already closed", err.ID, err.Index)
766766
}
767767

768-
// ErrForbiddenIssueReaction is used when a forbidden reaction was try to created
769-
type ErrForbiddenIssueReaction struct {
770-
Reaction string
771-
}
772-
773-
// IsErrForbiddenIssueReaction checks if an error is a ErrForbiddenIssueReaction.
774-
func IsErrForbiddenIssueReaction(err error) bool {
775-
_, ok := err.(ErrForbiddenIssueReaction)
776-
return ok
777-
}
778-
779-
func (err ErrForbiddenIssueReaction) Error() string {
780-
return fmt.Sprintf("'%s' is not an allowed reaction", err.Reaction)
781-
}
782-
783-
// ErrReactionAlreadyExist is used when a existing reaction was try to created
784-
type ErrReactionAlreadyExist struct {
785-
Reaction string
786-
}
787-
788-
// IsErrReactionAlreadyExist checks if an error is a ErrReactionAlreadyExist.
789-
func IsErrReactionAlreadyExist(err error) bool {
790-
_, ok := err.(ErrReactionAlreadyExist)
791-
return ok
792-
}
793-
794-
func (err ErrReactionAlreadyExist) Error() string {
795-
return fmt.Sprintf("reaction '%s' already exists", err.Reaction)
796-
}
797-
798768
// __________ .__ .__ __________ __
799769
// \______ \__ __| | | |\______ \ ____ ________ __ ____ _______/ |_
800770
// | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\

models/helper.go

-17
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,12 @@ package models
66

77
import (
88
repo_model "code.gitea.io/gitea/models/repo"
9-
user_model "code.gitea.io/gitea/models/user"
109
)
1110

12-
func keysInt64(m map[int64]struct{}) []int64 {
13-
keys := make([]int64, 0, len(m))
14-
for k := range m {
15-
keys = append(keys, k)
16-
}
17-
return keys
18-
}
19-
2011
func valuesRepository(m map[int64]*repo_model.Repository) []*repo_model.Repository {
2112
values := make([]*repo_model.Repository, 0, len(m))
2213
for _, v := range m {
2314
values = append(values, v)
2415
}
2516
return values
2617
}
27-
28-
func valuesUser(m map[int64]*user_model.User) []*user_model.User {
29-
values := make([]*user_model.User, 0, len(m))
30-
for _, v := range m {
31-
values = append(values, v)
32-
}
33-
return values
34-
}

models/issue.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type Issue struct {
7272

7373
Attachments []*repo_model.Attachment `xorm:"-"`
7474
Comments []*Comment `xorm:"-"`
75-
Reactions ReactionList `xorm:"-"`
75+
Reactions issues.ReactionList `xorm:"-"`
7676
TotalTrackedTime int64 `xorm:"-"`
7777
Assignees []*user_model.User `xorm:"-"`
7878
ForeignReference *foreignreference.ForeignReference `xorm:"-"`
@@ -244,8 +244,7 @@ func (issue *Issue) loadReactions(ctx context.Context) (err error) {
244244
if issue.Reactions != nil {
245245
return nil
246246
}
247-
e := db.GetEngine(ctx)
248-
reactions, _, err := findReactions(e, FindReactionsOptions{
247+
reactions, _, err := issues.FindReactions(ctx, issues.FindReactionsOptions{
249248
IssueID: issue.ID,
250249
})
251250
if err != nil {
@@ -255,7 +254,7 @@ func (issue *Issue) loadReactions(ctx context.Context) (err error) {
255254
return err
256255
}
257256
// Load reaction user data
258-
if _, err := ReactionList(reactions).loadUsers(e, issue.Repo); err != nil {
257+
if _, err := issues.ReactionList(reactions).LoadUsers(ctx, issue.Repo); err != nil {
259258
return err
260259
}
261260

@@ -2111,7 +2110,7 @@ func deleteIssue(ctx context.Context, issue *Issue) error {
21112110
&IssueAssignees{},
21122111
&IssueUser{},
21132112
&Notification{},
2114-
&Reaction{},
2113+
&issues.Reaction{},
21152114
&IssueWatch{},
21162115
&Stopwatch{},
21172116
&TrackedTime{},
@@ -2429,7 +2428,7 @@ func deleteIssuesByRepoID(sess db.Engine, repoID int64) (attachmentPaths []strin
24292428
}
24302429

24312430
if _, err = sess.In("issue_id", deleteCond).
2432-
Delete(&Reaction{}); err != nil {
2431+
Delete(&issues.Reaction{}); err != nil {
24332432
return
24342433
}
24352434

models/issue_comment.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ type Comment struct {
244244
CommitSHA string `xorm:"VARCHAR(40)"`
245245

246246
Attachments []*repo_model.Attachment `xorm:"-"`
247-
Reactions ReactionList `xorm:"-"`
247+
Reactions issues.ReactionList `xorm:"-"`
248248

249249
// For view issue page.
250250
ShowRole RoleDescriptor `xorm:"-"`
@@ -631,27 +631,27 @@ func (c *Comment) LoadTime() error {
631631
return err
632632
}
633633

634-
func (c *Comment) loadReactions(e db.Engine, repo *repo_model.Repository) (err error) {
634+
func (c *Comment) loadReactions(ctx context.Context, repo *repo_model.Repository) (err error) {
635635
if c.Reactions != nil {
636636
return nil
637637
}
638-
c.Reactions, _, err = findReactions(e, FindReactionsOptions{
638+
c.Reactions, _, err = issues.FindReactions(ctx, issues.FindReactionsOptions{
639639
IssueID: c.IssueID,
640640
CommentID: c.ID,
641641
})
642642
if err != nil {
643643
return err
644644
}
645645
// Load reaction user data
646-
if _, err := c.Reactions.loadUsers(e, repo); err != nil {
646+
if _, err := c.Reactions.LoadUsers(ctx, repo); err != nil {
647647
return err
648648
}
649649
return nil
650650
}
651651

652652
// LoadReactions loads comment reactions
653653
func (c *Comment) LoadReactions(repo *repo_model.Repository) error {
654-
return c.loadReactions(db.GetEngine(db.DefaultContext), repo)
654+
return c.loadReactions(db.DefaultContext, repo)
655655
}
656656

657657
func (c *Comment) loadReview(e db.Engine) (err error) {
@@ -1146,14 +1146,15 @@ func DeleteComment(comment *Comment) error {
11461146
}
11471147
defer committer.Close()
11481148

1149-
if err := deleteComment(db.GetEngine(ctx), comment); err != nil {
1149+
if err := deleteComment(ctx, comment); err != nil {
11501150
return err
11511151
}
11521152

11531153
return committer.Commit()
11541154
}
11551155

1156-
func deleteComment(e db.Engine, comment *Comment) error {
1156+
func deleteComment(ctx context.Context, comment *Comment) error {
1157+
e := db.GetEngine(ctx)
11571158
if _, err := e.ID(comment.ID).NoAutoCondition().Delete(comment); err != nil {
11581159
return err
11591160
}
@@ -1177,7 +1178,7 @@ func deleteComment(e db.Engine, comment *Comment) error {
11771178
return err
11781179
}
11791180

1180-
return deleteReaction(e, &ReactionOptions{Comment: comment})
1181+
return issues.DeleteReaction(ctx, &issues.ReactionOptions{CommentID: comment.ID})
11811182
}
11821183

11831184
// CodeComments represents comments on code by using this structure: FILENAME -> LINE (+ == proposed; - == previous) -> COMMENTS

models/issue_comment_list.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"code.gitea.io/gitea/models/db"
1111
repo_model "code.gitea.io/gitea/models/repo"
1212
user_model "code.gitea.io/gitea/models/user"
13+
"code.gitea.io/gitea/modules/container"
1314
)
1415

1516
// CommentList defines a list of comments
@@ -22,7 +23,7 @@ func (comments CommentList) getPosterIDs() []int64 {
2223
posterIDs[comment.PosterID] = struct{}{}
2324
}
2425
}
25-
return keysInt64(posterIDs)
26+
return container.KeysInt64(posterIDs)
2627
}
2728

2829
func (comments CommentList) loadPosters(e db.Engine) error {
@@ -75,7 +76,7 @@ func (comments CommentList) getLabelIDs() []int64 {
7576
ids[comment.LabelID] = struct{}{}
7677
}
7778
}
78-
return keysInt64(ids)
79+
return container.KeysInt64(ids)
7980
}
8081

8182
func (comments CommentList) loadLabels(e db.Engine) error {
@@ -125,7 +126,7 @@ func (comments CommentList) getMilestoneIDs() []int64 {
125126
ids[comment.MilestoneID] = struct{}{}
126127
}
127128
}
128-
return keysInt64(ids)
129+
return container.KeysInt64(ids)
129130
}
130131

131132
func (comments CommentList) loadMilestones(e db.Engine) error {
@@ -168,7 +169,7 @@ func (comments CommentList) getOldMilestoneIDs() []int64 {
168169
ids[comment.OldMilestoneID] = struct{}{}
169170
}
170171
}
171-
return keysInt64(ids)
172+
return container.KeysInt64(ids)
172173
}
173174

174175
func (comments CommentList) loadOldMilestones(e db.Engine) error {
@@ -211,7 +212,7 @@ func (comments CommentList) getAssigneeIDs() []int64 {
211212
ids[comment.AssigneeID] = struct{}{}
212213
}
213214
}
214-
return keysInt64(ids)
215+
return container.KeysInt64(ids)
215216
}
216217

217218
func (comments CommentList) loadAssignees(e db.Engine) error {
@@ -267,7 +268,7 @@ func (comments CommentList) getIssueIDs() []int64 {
267268
ids[comment.IssueID] = struct{}{}
268269
}
269270
}
270-
return keysInt64(ids)
271+
return container.KeysInt64(ids)
271272
}
272273

273274
// Issues returns all the issues of comments
@@ -342,7 +343,7 @@ func (comments CommentList) getDependentIssueIDs() []int64 {
342343
ids[comment.DependentIssueID] = struct{}{}
343344
}
344345
}
345-
return keysInt64(ids)
346+
return container.KeysInt64(ids)
346347
}
347348

348349
func (comments CommentList) loadDependentIssues(ctx context.Context) error {
@@ -444,7 +445,7 @@ func (comments CommentList) getReviewIDs() []int64 {
444445
ids[comment.ReviewID] = struct{}{}
445446
}
446447
}
447-
return keysInt64(ids)
448+
return container.KeysInt64(ids)
448449
}
449450

450451
func (comments CommentList) loadReviews(e db.Engine) error {

models/issue_list.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"code.gitea.io/gitea/models/db"
1111
repo_model "code.gitea.io/gitea/models/repo"
1212
user_model "code.gitea.io/gitea/models/user"
13+
"code.gitea.io/gitea/modules/container"
1314

1415
"xorm.io/builder"
1516
)
@@ -32,7 +33,7 @@ func (issues IssueList) getRepoIDs() []int64 {
3233
repoIDs[issue.RepoID] = struct{}{}
3334
}
3435
}
35-
return keysInt64(repoIDs)
36+
return container.KeysInt64(repoIDs)
3637
}
3738

3839
func (issues IssueList) loadRepositories(e db.Engine) ([]*repo_model.Repository, error) {
@@ -83,7 +84,7 @@ func (issues IssueList) getPosterIDs() []int64 {
8384
posterIDs[issue.PosterID] = struct{}{}
8485
}
8586
}
86-
return keysInt64(posterIDs)
87+
return container.KeysInt64(posterIDs)
8788
}
8889

8990
func (issues IssueList) loadPosters(e db.Engine) error {
@@ -189,7 +190,7 @@ func (issues IssueList) getMilestoneIDs() []int64 {
189190
ids[issue.MilestoneID] = struct{}{}
190191
}
191192
}
192-
return keysInt64(ids)
193+
return container.KeysInt64(ids)
193194
}
194195

195196
func (issues IssueList) loadMilestones(e db.Engine) error {

models/issues/main_test.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,18 @@ import (
99
"testing"
1010

1111
"code.gitea.io/gitea/models/unittest"
12+
"code.gitea.io/gitea/modules/setting"
1213
)
1314

15+
func init() {
16+
setting.SetCustomPathAndConf("", "", "")
17+
setting.LoadForTest()
18+
}
19+
1420
func TestMain(m *testing.M) {
15-
unittest.MainTest(m, filepath.Join("..", ".."), "")
21+
unittest.MainTest(m, filepath.Join("..", ".."),
22+
"reaction.yml",
23+
"user.yml",
24+
"repository.yml",
25+
)
1626
}

0 commit comments

Comments
 (0)