From 15ff9cd4b9815d9cfca8b4cb43608b452c889fe4 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 16 Nov 2024 12:58:42 -0800 Subject: [PATCH] Move more functions to service layer --- models/org_team.go | 43 ---------------------------- services/org/team.go | 5 ++-- services/repository/collaboration.go | 32 +++++++++++++++++++-- 3 files changed, 31 insertions(+), 49 deletions(-) delete mode 100644 models/org_team.go diff --git a/models/org_team.go b/models/org_team.go deleted file mode 100644 index e808369d29..0000000000 --- a/models/org_team.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2018 The Gitea Authors. All rights reserved. -// Copyright 2016 The Gogs Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package models - -import ( - "context" - "fmt" - - "code.gitea.io/gitea/models/db" - issues_model "code.gitea.io/gitea/models/issues" - access_model "code.gitea.io/gitea/models/perm/access" - repo_model "code.gitea.io/gitea/models/repo" - user_model "code.gitea.io/gitea/models/user" - - "xorm.io/builder" -) - -func ReconsiderRepoIssuesAssignee(ctx context.Context, repo *repo_model.Repository, user *user_model.User) error { - if canAssigned, err := access_model.CanBeAssigned(ctx, user, repo, true); err != nil || canAssigned { - return err - } - - if _, err := db.GetEngine(ctx).Where(builder.Eq{"assignee_id": user.ID}). - In("issue_id", builder.Select("id").From("issue").Where(builder.Eq{"repo_id": repo.ID})). - Delete(&issues_model.IssueAssignees{}); err != nil { - return fmt.Errorf("Could not delete assignee[%d] %w", user.ID, err) - } - return nil -} - -func ReconsiderWatches(ctx context.Context, repo *repo_model.Repository, user *user_model.User) error { - if has, err := access_model.HasAnyUnitAccess(ctx, user.ID, repo); err != nil || has { - return err - } - if err := repo_model.WatchRepo(ctx, user, repo, false); err != nil { - return err - } - - // Remove all IssueWatches a user has subscribed to in the repository - return issues_model.RemoveIssueWatchersByRepoID(ctx, user.ID, repo.ID) -} diff --git a/services/org/team.go b/services/org/team.go index 16675593a7..dc0a7bbd94 100644 --- a/services/org/team.go +++ b/services/org/team.go @@ -8,7 +8,6 @@ import ( "fmt" "strings" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" git_model "code.gitea.io/gitea/models/git" issues_model "code.gitea.io/gitea/models/issues" @@ -370,12 +369,12 @@ func removeTeamMember(ctx context.Context, team *organization.Team, user *user_m } // Remove watches from now unaccessible - if err := models.ReconsiderWatches(ctx, repo, user); err != nil { + if err := repo_service.ReconsiderWatches(ctx, repo, user); err != nil { return err } // Remove issue assignments from now unaccessible - if err := models.ReconsiderRepoIssuesAssignee(ctx, repo, user); err != nil { + if err := repo_service.ReconsiderRepoIssuesAssignee(ctx, repo, user); err != nil { return err } } diff --git a/services/repository/collaboration.go b/services/repository/collaboration.go index abe0489fc5..b5fc523623 100644 --- a/services/repository/collaboration.go +++ b/services/repository/collaboration.go @@ -6,9 +6,10 @@ package repository import ( "context" + "fmt" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" + issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/perm" access_model "code.gitea.io/gitea/models/perm/access" repo_model "code.gitea.io/gitea/models/repo" @@ -94,14 +95,39 @@ func DeleteCollaboration(ctx context.Context, repo *repo_model.Repository, colla return err } - if err = models.ReconsiderWatches(ctx, repo, collaborator); err != nil { + if err = ReconsiderWatches(ctx, repo, collaborator); err != nil { return err } // Unassign a user from any issue (s)he has been assigned to in the repository - if err := models.ReconsiderRepoIssuesAssignee(ctx, repo, collaborator); err != nil { + if err := ReconsiderRepoIssuesAssignee(ctx, repo, collaborator); err != nil { return err } return committer.Commit() } + +func ReconsiderRepoIssuesAssignee(ctx context.Context, repo *repo_model.Repository, user *user_model.User) error { + if canAssigned, err := access_model.CanBeAssigned(ctx, user, repo, true); err != nil || canAssigned { + return err + } + + if _, err := db.GetEngine(ctx).Where(builder.Eq{"assignee_id": user.ID}). + In("issue_id", builder.Select("id").From("issue").Where(builder.Eq{"repo_id": repo.ID})). + Delete(&issues_model.IssueAssignees{}); err != nil { + return fmt.Errorf("Could not delete assignee[%d] %w", user.ID, err) + } + return nil +} + +func ReconsiderWatches(ctx context.Context, repo *repo_model.Repository, user *user_model.User) error { + if has, err := access_model.HasAnyUnitAccess(ctx, user.ID, repo); err != nil || has { + return err + } + if err := repo_model.WatchRepo(ctx, user, repo, false); err != nil { + return err + } + + // Remove all IssueWatches a user has subscribed to in the repository + return issues_model.RemoveIssueWatchersByRepoID(ctx, user.ID, repo.ID) +}