From 2b7a22afb31074966bc3f0dab1dcd8520f422576 Mon Sep 17 00:00:00 2001 From: Clemens Date: Wed, 3 Apr 2024 12:26:33 +0200 Subject: [PATCH] renamed FederatedRepo to FollowingRepo --- .../{federated_repo.go => following_repo.go} | 12 ++++---- ...ed_repo_test.go => following_repo_test.go} | 6 ++-- models/repo/repo_repository.go | 30 +++++++++---------- modules/context/repo.go | 8 ++--- modules/validation/helpers.go | 8 ++--- routers/web/repo/setting/setting.go | 2 +- services/federation/federation_service.go | 20 ++++++------- 7 files changed, 43 insertions(+), 43 deletions(-) rename models/repo/{federated_repo.go => following_repo.go} (78%) rename models/repo/{federated_repo_test.go => following_repo_test.go} (87%) diff --git a/models/repo/federated_repo.go b/models/repo/following_repo.go similarity index 78% rename from models/repo/federated_repo.go rename to models/repo/following_repo.go index b2af6103ff..a935433e40 100644 --- a/models/repo/federated_repo.go +++ b/models/repo/following_repo.go @@ -7,9 +7,9 @@ import ( "code.gitea.io/gitea/modules/validation" ) -// FederatedRepo represents a federated Repository Actor connected with a local Repo +// FollowingRepo represents a federated Repository Actor connected with a local Repo // ToDo: We currently get database errors if different repos on the same server want to save the same federated repos in their list -type FederatedRepo struct { +type FollowingRepo struct { ID int64 `xorm:"pk autoincr"` RepoID int64 `xorm:"NOT NULL"` ExternalID string `xorm:"TEXT UNIQUE(federation_repo_mapping) NOT NULL"` @@ -17,20 +17,20 @@ type FederatedRepo struct { Uri string } -func NewFederatedRepo(repoID int64, externalID string, federationHostID int64, uri string) (FederatedRepo, error) { - result := FederatedRepo{ +func NewFollowingRepo(repoID int64, externalID string, federationHostID int64, uri string) (FollowingRepo, error) { + result := FollowingRepo{ RepoID: repoID, ExternalID: externalID, FederationHostID: federationHostID, Uri: uri, } if valid, err := validation.IsValid(result); !valid { - return FederatedRepo{}, err + return FollowingRepo{}, err } return result, nil } -func (user FederatedRepo) Validate() []string { +func (user FollowingRepo) Validate() []string { var result []string result = append(result, validation.ValidateNotEmpty(user.RepoID, "UserID")...) result = append(result, validation.ValidateNotEmpty(user.ExternalID, "ExternalID")...) diff --git a/models/repo/federated_repo_test.go b/models/repo/following_repo_test.go similarity index 87% rename from models/repo/federated_repo_test.go rename to models/repo/following_repo_test.go index c23a74817a..d83db6aefe 100644 --- a/models/repo/federated_repo_test.go +++ b/models/repo/following_repo_test.go @@ -9,8 +9,8 @@ import ( "code.gitea.io/gitea/modules/validation" ) -func Test_FederatedRepoValidation(t *testing.T) { - sut := FederatedRepo{ +func Test_FollowingRepoValidation(t *testing.T) { + sut := FollowingRepo{ RepoID: 12, ExternalID: "12", FederationHostID: 1, @@ -20,7 +20,7 @@ func Test_FederatedRepoValidation(t *testing.T) { t.Errorf("sut should be valid but was %q", err) } - sut = FederatedRepo{ + sut = FollowingRepo{ ExternalID: "12", FederationHostID: 1, Uri: "http://localhost:3000/api/v1/activitypub/repo-id/1", diff --git a/models/repo/repo_repository.go b/models/repo/repo_repository.go index 66a1c13ab5..9707e76e36 100644 --- a/models/repo/repo_repository.go +++ b/models/repo/repo_repository.go @@ -12,29 +12,29 @@ import ( ) func init() { - db.RegisterModel(new(FederatedRepo)) + db.RegisterModel(new(FollowingRepo)) } -func FindFollowingReposByRepoID(ctx context.Context, repoId int64) ([]*FederatedRepo, error) { +func FindFollowingReposByRepoID(ctx context.Context, repoId int64) ([]*FollowingRepo, error) { maxFollowingRepos := 10 sess := db.GetEngine(ctx).Where("repo_id=?", repoId) sess = sess.Limit(maxFollowingRepos, 0) - federatedRepoList := make([]*FederatedRepo, 0, maxFollowingRepos) - err := sess.Find(&federatedRepoList) + followingRepoList := make([]*FollowingRepo, 0, maxFollowingRepos) + err := sess.Find(&followingRepoList) if err != nil { - return make([]*FederatedRepo, 0, maxFollowingRepos), err + return make([]*FollowingRepo, 0, maxFollowingRepos), err } - for _, federatedRepo := range federatedRepoList { - if res, err := validation.IsValid(*federatedRepo); !res { - return make([]*FederatedRepo, 0, maxFollowingRepos), fmt.Errorf("FederationInfo is not valid: %v", err) + for _, followingRepo := range followingRepoList { + if res, err := validation.IsValid(*followingRepo); !res { + return make([]*FollowingRepo, 0, maxFollowingRepos), fmt.Errorf("FederationInfo is not valid: %v", err) } } - return federatedRepoList, nil + return followingRepoList, nil } -func StoreFollowingRepos(ctx context.Context, localRepoId int64, federatedRepoList []*FederatedRepo) error { - for _, federatedRepo := range federatedRepoList { - if res, err := validation.IsValid(*federatedRepo); !res { +func StoreFollowingRepos(ctx context.Context, localRepoId int64, followingRepoList []*FollowingRepo) error { + for _, followingRepo := range followingRepoList { + if res, err := validation.IsValid(*followingRepo); !res { return fmt.Errorf("FederationInfo is not valid: %v", err) } } @@ -46,12 +46,12 @@ func StoreFollowingRepos(ctx context.Context, localRepoId int64, federatedRepoLi } defer committer.Close() - _, err = db.GetEngine(ctx).Where("repo_id=?", localRepoId).Delete(FederatedRepo{}) + _, err = db.GetEngine(ctx).Where("repo_id=?", localRepoId).Delete(FollowingRepo{}) if err != nil { return err } - for _, federatedRepo := range federatedRepoList { - _, err = db.GetEngine(ctx).Insert(federatedRepo) + for _, followingRepo := range followingRepoList { + _, err = db.GetEngine(ctx).Insert(followingRepo) if err != nil { return err } diff --git a/modules/context/repo.go b/modules/context/repo.go index a6ab7df3cf..695ab0f9aa 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -380,18 +380,18 @@ func repoAssignment(ctx *Context, repo *repo_model.Repository) { ctx.Data["HasAccess"] = true ctx.Data["Permission"] = &ctx.Repo.Permission - federatedRepoList, err := repo_model.FindFollowingReposByRepoID(ctx, repo.ID) + followingRepoList, err := repo_model.FindFollowingReposByRepoID(ctx, repo.ID) if err == nil { followingRepoString := "" - for idx, federatedRepo := range federatedRepoList { + for idx, followingRepo := range followingRepoList { if idx > 0 { followingRepoString += ";" } - followingRepoString += (*federatedRepo).Uri + followingRepoString += (*followingRepo).Uri } ctx.Data["FollowingRepos"] = followingRepoString } else if err != repo_model.ErrMirrorNotExist { - ctx.ServerError("FindFederatedRepoByRepoID", err) + ctx.ServerError("FindFollowingRepoByRepoID", err) return } diff --git a/modules/validation/helpers.go b/modules/validation/helpers.go index 9780399747..6f2fc6ca6e 100644 --- a/modules/validation/helpers.go +++ b/modules/validation/helpers.go @@ -127,7 +127,7 @@ func IsValidForgejoActivityPubURL(url string) bool { return true } -func IsValidFederatedRepoURL(url string) bool { +func IsValidFollowingRepoURL(url string) bool { if !IsValidForgejoActivityPubURL(url) { return false } @@ -143,13 +143,13 @@ func IsValidFederatedRepoURL(url string) bool { return true } -func IsValidFederatedRepoURLList(urls string) bool { +func IsValidFollowingRepoURLList(urls string) bool { switch { case len(strings.Split(urls, ";")) == 1: - return IsValidFederatedRepoURL(urls) + return IsValidFollowingRepoURL(urls) default: for _, url := range strings.Split(urls, ";") { - if !IsValidFederatedRepoURLList(url) { + if !IsValidFollowingRepoURLList(url) { return false } } diff --git a/routers/web/repo/setting/setting.go b/routers/web/repo/setting/setting.go index 1b8d439edf..5eda0a0ff0 100644 --- a/routers/web/repo/setting/setting.go +++ b/routers/web/repo/setting/setting.go @@ -214,7 +214,7 @@ func SettingsPost(ctx *context.Context) { federationRepoSplit[idx] = strings.TrimSpace(repo) } - if _, _, err := federation.StoreFederatedRepoList(ctx, ctx.Repo.Repository.ID, federationRepoSplit); err != nil { + if _, _, err := federation.StoreFollowingRepoList(ctx, ctx.Repo.Repository.ID, federationRepoSplit); err != nil { ctx.ServerError("UpdateRepository", err) return } diff --git a/services/federation/federation_service.go b/services/federation/federation_service.go index b68de15400..f0d6996bef 100644 --- a/services/federation/federation_service.go +++ b/services/federation/federation_service.go @@ -213,23 +213,23 @@ func CreateUserFromAP(ctx context.Context, personID forgefed.PersonID, federatio return &newUser, &federatedUser, nil } -// Create or update a list of FederatedRepo structs -func StoreFederatedRepoList(ctx context.Context, localRepoId int64, federatedRepoList []string) (int, string, error) { - followingRepos := make([]*repo.FederatedRepo, 0, len(federatedRepoList)) - for _, uri := range federatedRepoList { +// Create or update a list of FollowingRepo structs +func StoreFollowingRepoList(ctx context.Context, localRepoId int64, followingRepoList []string) (int, string, error) { + followingRepos := make([]*repo.FollowingRepo, 0, len(followingRepoList)) + for _, uri := range followingRepoList { federationHost, err := GetFederationHostForUri(ctx, uri) if err != nil { return http.StatusInternalServerError, "Wrong FederationHost", err } - federatedRepoID, err := forgefed.NewRepositoryID(uri, string(federationHost.NodeInfo.Source)) + followingRepoID, err := forgefed.NewRepositoryID(uri, string(federationHost.NodeInfo.Source)) if err != nil { return http.StatusNotAcceptable, "Invalid federated repo", err } - federatedRepo, err := repo.NewFederatedRepo(localRepoId, federatedRepoID.ID, federationHost.ID, uri) + followingRepo, err := repo.NewFollowingRepo(localRepoId, followingRepoID.ID, federationHost.ID, uri) if err != nil { return http.StatusNotAcceptable, "Invalid federated repo", err } - followingRepos = append(followingRepos, &federatedRepo) + followingRepos = append(followingRepos, &followingRepo) } repo.StoreFollowingRepos(ctx, localRepoId, followingRepos) @@ -238,7 +238,7 @@ func StoreFederatedRepoList(ctx context.Context, localRepoId int64, federatedRep } func DeleteFollowingRepos(ctx context.Context, localRepoId int64) error { - return repo.StoreFollowingRepos(ctx, localRepoId, []*repo.FederatedRepo{}) + return repo.StoreFollowingRepos(ctx, localRepoId, []*repo.FollowingRepo{}) } func SendLikeActivities(ctx context.Context, doer user.User, repoID int64) error { @@ -249,8 +249,8 @@ func SendLikeActivities(ctx context.Context, doer user.User, repoID int64) error } likeActivityList := make([]forgefed.ForgeLike, 0) - for _, federatedRepo := range followingRepos { - target := federatedRepo.Uri + for _, followingRepo := range followingRepos { + target := followingRepo.Uri likeActivity, err := forgefed.NewForgeLike(doer.APAPIURL(), target, time.Now()) if err != nil { return err