Skip to content

Commit 1482291

Browse files
committed
Move reaction to models/issues/
1 parent 43332a4 commit 1482291

File tree

17 files changed

+257
-267
lines changed

17 files changed

+257
-267
lines changed

models/action_list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (actions ActionList) getUserIDs() []int64 {
2222
userIDs[action.ActUserID] = struct{}{}
2323
}
2424
}
25-
return keysInt64(userIDs)
25+
return db.KeysInt64(userIDs)
2626
}
2727

2828
func (actions ActionList) loadUsers(e db.Engine) (map[int64]*user_model.User, error) {
@@ -52,7 +52,7 @@ func (actions ActionList) getRepoIDs() []int64 {
5252
repoIDs[action.RepoID] = struct{}{}
5353
}
5454
}
55-
return keysInt64(repoIDs)
55+
return db.KeysInt64(repoIDs)
5656
}
5757

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

models/db/map.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package db
6+
7+
// KeysInt64 returns keys slice for a map with int64 key
8+
func KeysInt64(m map[int64]struct{}) []int64 {
9+
keys := make([]int64, 0, len(m))
10+
for k := range m {
11+
keys = append(keys, k)
12+
}
13+
return keys
14+
}

models/error.go

Lines changed: 0 additions & 30 deletions
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

Lines changed: 0 additions & 17 deletions
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

Lines changed: 5 additions & 6 deletions
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

Lines changed: 9 additions & 8 deletions
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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (comments CommentList) getPosterIDs() []int64 {
2222
posterIDs[comment.PosterID] = struct{}{}
2323
}
2424
}
25-
return keysInt64(posterIDs)
25+
return db.KeysInt64(posterIDs)
2626
}
2727

2828
func (comments CommentList) loadPosters(e db.Engine) error {
@@ -75,7 +75,7 @@ func (comments CommentList) getLabelIDs() []int64 {
7575
ids[comment.LabelID] = struct{}{}
7676
}
7777
}
78-
return keysInt64(ids)
78+
return db.KeysInt64(ids)
7979
}
8080

8181
func (comments CommentList) loadLabels(e db.Engine) error {
@@ -125,7 +125,7 @@ func (comments CommentList) getMilestoneIDs() []int64 {
125125
ids[comment.MilestoneID] = struct{}{}
126126
}
127127
}
128-
return keysInt64(ids)
128+
return db.KeysInt64(ids)
129129
}
130130

131131
func (comments CommentList) loadMilestones(e db.Engine) error {
@@ -168,7 +168,7 @@ func (comments CommentList) getOldMilestoneIDs() []int64 {
168168
ids[comment.OldMilestoneID] = struct{}{}
169169
}
170170
}
171-
return keysInt64(ids)
171+
return db.KeysInt64(ids)
172172
}
173173

174174
func (comments CommentList) loadOldMilestones(e db.Engine) error {
@@ -211,7 +211,7 @@ func (comments CommentList) getAssigneeIDs() []int64 {
211211
ids[comment.AssigneeID] = struct{}{}
212212
}
213213
}
214-
return keysInt64(ids)
214+
return db.KeysInt64(ids)
215215
}
216216

217217
func (comments CommentList) loadAssignees(e db.Engine) error {
@@ -267,7 +267,7 @@ func (comments CommentList) getIssueIDs() []int64 {
267267
ids[comment.IssueID] = struct{}{}
268268
}
269269
}
270-
return keysInt64(ids)
270+
return db.KeysInt64(ids)
271271
}
272272

273273
// Issues returns all the issues of comments
@@ -342,7 +342,7 @@ func (comments CommentList) getDependentIssueIDs() []int64 {
342342
ids[comment.DependentIssueID] = struct{}{}
343343
}
344344
}
345-
return keysInt64(ids)
345+
return db.KeysInt64(ids)
346346
}
347347

348348
func (comments CommentList) loadDependentIssues(ctx context.Context) error {
@@ -444,7 +444,7 @@ func (comments CommentList) getReviewIDs() []int64 {
444444
ids[comment.ReviewID] = struct{}{}
445445
}
446446
}
447-
return keysInt64(ids)
447+
return db.KeysInt64(ids)
448448
}
449449

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

models/issue_list.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (issues IssueList) getRepoIDs() []int64 {
3232
repoIDs[issue.RepoID] = struct{}{}
3333
}
3434
}
35-
return keysInt64(repoIDs)
35+
return db.KeysInt64(repoIDs)
3636
}
3737

3838
func (issues IssueList) loadRepositories(e db.Engine) ([]*repo_model.Repository, error) {
@@ -83,7 +83,7 @@ func (issues IssueList) getPosterIDs() []int64 {
8383
posterIDs[issue.PosterID] = struct{}{}
8484
}
8585
}
86-
return keysInt64(posterIDs)
86+
return db.KeysInt64(posterIDs)
8787
}
8888

8989
func (issues IssueList) loadPosters(e db.Engine) error {
@@ -189,7 +189,7 @@ func (issues IssueList) getMilestoneIDs() []int64 {
189189
ids[issue.MilestoneID] = struct{}{}
190190
}
191191
}
192-
return keysInt64(ids)
192+
return db.KeysInt64(ids)
193193
}
194194

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

models/issues/main_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ import (
1212
)
1313

1414
func TestMain(m *testing.M) {
15-
unittest.MainTest(m, filepath.Join("..", ".."), "")
15+
unittest.MainTest(m, filepath.Join("..", ".."),
16+
"reaction.yml",
17+
"user.yml",
18+
)
1619
}

0 commit comments

Comments
 (0)