Skip to content

Commit

Permalink
Move reaction to models/issues/ (#19264)
Browse files Browse the repository at this point in the history
* Move reaction to models/issues/

* Fix test

* move the function

* improve code

* Update models/issues/reaction.go

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
  • Loading branch information
lunny and wxiaoguang authored Mar 31, 2022
1 parent 43332a4 commit d4f84f1
Show file tree
Hide file tree
Showing 18 changed files with 279 additions and 273 deletions.
5 changes: 3 additions & 2 deletions models/action_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/container"
)

// ActionList defines a list of actions
Expand All @@ -22,7 +23,7 @@ func (actions ActionList) getUserIDs() []int64 {
userIDs[action.ActUserID] = struct{}{}
}
}
return keysInt64(userIDs)
return container.KeysInt64(userIDs)
}

func (actions ActionList) loadUsers(e db.Engine) (map[int64]*user_model.User, error) {
Expand Down Expand Up @@ -52,7 +53,7 @@ func (actions ActionList) getRepoIDs() []int64 {
repoIDs[action.RepoID] = struct{}{}
}
}
return keysInt64(repoIDs)
return container.KeysInt64(repoIDs)
}

func (actions ActionList) loadRepositories(e db.Engine) error {
Expand Down
30 changes: 0 additions & 30 deletions models/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,36 +765,6 @@ func (err ErrPullWasClosed) Error() string {
return fmt.Sprintf("Pull request [%d] %d was already closed", err.ID, err.Index)
}

// ErrForbiddenIssueReaction is used when a forbidden reaction was try to created
type ErrForbiddenIssueReaction struct {
Reaction string
}

// IsErrForbiddenIssueReaction checks if an error is a ErrForbiddenIssueReaction.
func IsErrForbiddenIssueReaction(err error) bool {
_, ok := err.(ErrForbiddenIssueReaction)
return ok
}

func (err ErrForbiddenIssueReaction) Error() string {
return fmt.Sprintf("'%s' is not an allowed reaction", err.Reaction)
}

// ErrReactionAlreadyExist is used when a existing reaction was try to created
type ErrReactionAlreadyExist struct {
Reaction string
}

// IsErrReactionAlreadyExist checks if an error is a ErrReactionAlreadyExist.
func IsErrReactionAlreadyExist(err error) bool {
_, ok := err.(ErrReactionAlreadyExist)
return ok
}

func (err ErrReactionAlreadyExist) Error() string {
return fmt.Sprintf("reaction '%s' already exists", err.Reaction)
}

// __________ .__ .__ __________ __
// \______ \__ __| | | |\______ \ ____ ________ __ ____ _______/ |_
// | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
Expand Down
17 changes: 0 additions & 17 deletions models/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,12 @@ package models

import (
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
)

func keysInt64(m map[int64]struct{}) []int64 {
keys := make([]int64, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}

func valuesRepository(m map[int64]*repo_model.Repository) []*repo_model.Repository {
values := make([]*repo_model.Repository, 0, len(m))
for _, v := range m {
values = append(values, v)
}
return values
}

func valuesUser(m map[int64]*user_model.User) []*user_model.User {
values := make([]*user_model.User, 0, len(m))
for _, v := range m {
values = append(values, v)
}
return values
}
11 changes: 5 additions & 6 deletions models/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type Issue struct {

Attachments []*repo_model.Attachment `xorm:"-"`
Comments []*Comment `xorm:"-"`
Reactions ReactionList `xorm:"-"`
Reactions issues.ReactionList `xorm:"-"`
TotalTrackedTime int64 `xorm:"-"`
Assignees []*user_model.User `xorm:"-"`
ForeignReference *foreignreference.ForeignReference `xorm:"-"`
Expand Down Expand Up @@ -244,8 +244,7 @@ func (issue *Issue) loadReactions(ctx context.Context) (err error) {
if issue.Reactions != nil {
return nil
}
e := db.GetEngine(ctx)
reactions, _, err := findReactions(e, FindReactionsOptions{
reactions, _, err := issues.FindReactions(ctx, issues.FindReactionsOptions{
IssueID: issue.ID,
})
if err != nil {
Expand All @@ -255,7 +254,7 @@ func (issue *Issue) loadReactions(ctx context.Context) (err error) {
return err
}
// Load reaction user data
if _, err := ReactionList(reactions).loadUsers(e, issue.Repo); err != nil {
if _, err := issues.ReactionList(reactions).LoadUsers(ctx, issue.Repo); err != nil {
return err
}

Expand Down Expand Up @@ -2111,7 +2110,7 @@ func deleteIssue(ctx context.Context, issue *Issue) error {
&IssueAssignees{},
&IssueUser{},
&Notification{},
&Reaction{},
&issues.Reaction{},
&IssueWatch{},
&Stopwatch{},
&TrackedTime{},
Expand Down Expand Up @@ -2429,7 +2428,7 @@ func deleteIssuesByRepoID(sess db.Engine, repoID int64) (attachmentPaths []strin
}

if _, err = sess.In("issue_id", deleteCond).
Delete(&Reaction{}); err != nil {
Delete(&issues.Reaction{}); err != nil {
return
}

Expand Down
17 changes: 9 additions & 8 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ type Comment struct {
CommitSHA string `xorm:"VARCHAR(40)"`

Attachments []*repo_model.Attachment `xorm:"-"`
Reactions ReactionList `xorm:"-"`
Reactions issues.ReactionList `xorm:"-"`

// For view issue page.
ShowRole RoleDescriptor `xorm:"-"`
Expand Down Expand Up @@ -631,27 +631,27 @@ func (c *Comment) LoadTime() error {
return err
}

func (c *Comment) loadReactions(e db.Engine, repo *repo_model.Repository) (err error) {
func (c *Comment) loadReactions(ctx context.Context, repo *repo_model.Repository) (err error) {
if c.Reactions != nil {
return nil
}
c.Reactions, _, err = findReactions(e, FindReactionsOptions{
c.Reactions, _, err = issues.FindReactions(ctx, issues.FindReactionsOptions{
IssueID: c.IssueID,
CommentID: c.ID,
})
if err != nil {
return err
}
// Load reaction user data
if _, err := c.Reactions.loadUsers(e, repo); err != nil {
if _, err := c.Reactions.LoadUsers(ctx, repo); err != nil {
return err
}
return nil
}

// LoadReactions loads comment reactions
func (c *Comment) LoadReactions(repo *repo_model.Repository) error {
return c.loadReactions(db.GetEngine(db.DefaultContext), repo)
return c.loadReactions(db.DefaultContext, repo)
}

func (c *Comment) loadReview(e db.Engine) (err error) {
Expand Down Expand Up @@ -1146,14 +1146,15 @@ func DeleteComment(comment *Comment) error {
}
defer committer.Close()

if err := deleteComment(db.GetEngine(ctx), comment); err != nil {
if err := deleteComment(ctx, comment); err != nil {
return err
}

return committer.Commit()
}

func deleteComment(e db.Engine, comment *Comment) error {
func deleteComment(ctx context.Context, comment *Comment) error {
e := db.GetEngine(ctx)
if _, err := e.ID(comment.ID).NoAutoCondition().Delete(comment); err != nil {
return err
}
Expand All @@ -1177,7 +1178,7 @@ func deleteComment(e db.Engine, comment *Comment) error {
return err
}

return deleteReaction(e, &ReactionOptions{Comment: comment})
return issues.DeleteReaction(ctx, &issues.ReactionOptions{CommentID: comment.ID})
}

// CodeComments represents comments on code by using this structure: FILENAME -> LINE (+ == proposed; - == previous) -> COMMENTS
Expand Down
17 changes: 9 additions & 8 deletions models/issue_comment_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/container"
)

// CommentList defines a list of comments
Expand All @@ -22,7 +23,7 @@ func (comments CommentList) getPosterIDs() []int64 {
posterIDs[comment.PosterID] = struct{}{}
}
}
return keysInt64(posterIDs)
return container.KeysInt64(posterIDs)
}

func (comments CommentList) loadPosters(e db.Engine) error {
Expand Down Expand Up @@ -75,7 +76,7 @@ func (comments CommentList) getLabelIDs() []int64 {
ids[comment.LabelID] = struct{}{}
}
}
return keysInt64(ids)
return container.KeysInt64(ids)
}

func (comments CommentList) loadLabels(e db.Engine) error {
Expand Down Expand Up @@ -125,7 +126,7 @@ func (comments CommentList) getMilestoneIDs() []int64 {
ids[comment.MilestoneID] = struct{}{}
}
}
return keysInt64(ids)
return container.KeysInt64(ids)
}

func (comments CommentList) loadMilestones(e db.Engine) error {
Expand Down Expand Up @@ -168,7 +169,7 @@ func (comments CommentList) getOldMilestoneIDs() []int64 {
ids[comment.OldMilestoneID] = struct{}{}
}
}
return keysInt64(ids)
return container.KeysInt64(ids)
}

func (comments CommentList) loadOldMilestones(e db.Engine) error {
Expand Down Expand Up @@ -211,7 +212,7 @@ func (comments CommentList) getAssigneeIDs() []int64 {
ids[comment.AssigneeID] = struct{}{}
}
}
return keysInt64(ids)
return container.KeysInt64(ids)
}

func (comments CommentList) loadAssignees(e db.Engine) error {
Expand Down Expand Up @@ -267,7 +268,7 @@ func (comments CommentList) getIssueIDs() []int64 {
ids[comment.IssueID] = struct{}{}
}
}
return keysInt64(ids)
return container.KeysInt64(ids)
}

// Issues returns all the issues of comments
Expand Down Expand Up @@ -342,7 +343,7 @@ func (comments CommentList) getDependentIssueIDs() []int64 {
ids[comment.DependentIssueID] = struct{}{}
}
}
return keysInt64(ids)
return container.KeysInt64(ids)
}

func (comments CommentList) loadDependentIssues(ctx context.Context) error {
Expand Down Expand Up @@ -444,7 +445,7 @@ func (comments CommentList) getReviewIDs() []int64 {
ids[comment.ReviewID] = struct{}{}
}
}
return keysInt64(ids)
return container.KeysInt64(ids)
}

func (comments CommentList) loadReviews(e db.Engine) error {
Expand Down
7 changes: 4 additions & 3 deletions models/issue_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/container"

"xorm.io/builder"
)
Expand All @@ -32,7 +33,7 @@ func (issues IssueList) getRepoIDs() []int64 {
repoIDs[issue.RepoID] = struct{}{}
}
}
return keysInt64(repoIDs)
return container.KeysInt64(repoIDs)
}

func (issues IssueList) loadRepositories(e db.Engine) ([]*repo_model.Repository, error) {
Expand Down Expand Up @@ -83,7 +84,7 @@ func (issues IssueList) getPosterIDs() []int64 {
posterIDs[issue.PosterID] = struct{}{}
}
}
return keysInt64(posterIDs)
return container.KeysInt64(posterIDs)
}

func (issues IssueList) loadPosters(e db.Engine) error {
Expand Down Expand Up @@ -189,7 +190,7 @@ func (issues IssueList) getMilestoneIDs() []int64 {
ids[issue.MilestoneID] = struct{}{}
}
}
return keysInt64(ids)
return container.KeysInt64(ids)
}

func (issues IssueList) loadMilestones(e db.Engine) error {
Expand Down
12 changes: 11 additions & 1 deletion models/issues/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ import (
"testing"

"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/setting"
)

func init() {
setting.SetCustomPathAndConf("", "", "")
setting.LoadForTest()
}

func TestMain(m *testing.M) {
unittest.MainTest(m, filepath.Join("..", ".."), "")
unittest.MainTest(m, filepath.Join("..", ".."),
"reaction.yml",
"user.yml",
"repository.yml",
)
}
Loading

0 comments on commit d4f84f1

Please sign in to comment.