mirror of
1
Fork 0

renamed FederatedRepo to FollowingRepo

This commit is contained in:
Clemens 2024-04-03 12:26:33 +02:00
parent daccaed157
commit 2b7a22afb3
7 changed files with 43 additions and 43 deletions

View File

@ -7,9 +7,9 @@ import (
"code.gitea.io/gitea/modules/validation" "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 // 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"` ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"NOT NULL"` RepoID int64 `xorm:"NOT NULL"`
ExternalID string `xorm:"TEXT UNIQUE(federation_repo_mapping) NOT NULL"` ExternalID string `xorm:"TEXT UNIQUE(federation_repo_mapping) NOT NULL"`
@ -17,20 +17,20 @@ type FederatedRepo struct {
Uri string Uri string
} }
func NewFederatedRepo(repoID int64, externalID string, federationHostID int64, uri string) (FederatedRepo, error) { func NewFollowingRepo(repoID int64, externalID string, federationHostID int64, uri string) (FollowingRepo, error) {
result := FederatedRepo{ result := FollowingRepo{
RepoID: repoID, RepoID: repoID,
ExternalID: externalID, ExternalID: externalID,
FederationHostID: federationHostID, FederationHostID: federationHostID,
Uri: uri, Uri: uri,
} }
if valid, err := validation.IsValid(result); !valid { if valid, err := validation.IsValid(result); !valid {
return FederatedRepo{}, err return FollowingRepo{}, err
} }
return result, nil return result, nil
} }
func (user FederatedRepo) Validate() []string { func (user FollowingRepo) Validate() []string {
var result []string var result []string
result = append(result, validation.ValidateNotEmpty(user.RepoID, "UserID")...) result = append(result, validation.ValidateNotEmpty(user.RepoID, "UserID")...)
result = append(result, validation.ValidateNotEmpty(user.ExternalID, "ExternalID")...) result = append(result, validation.ValidateNotEmpty(user.ExternalID, "ExternalID")...)

View File

@ -9,8 +9,8 @@ import (
"code.gitea.io/gitea/modules/validation" "code.gitea.io/gitea/modules/validation"
) )
func Test_FederatedRepoValidation(t *testing.T) { func Test_FollowingRepoValidation(t *testing.T) {
sut := FederatedRepo{ sut := FollowingRepo{
RepoID: 12, RepoID: 12,
ExternalID: "12", ExternalID: "12",
FederationHostID: 1, FederationHostID: 1,
@ -20,7 +20,7 @@ func Test_FederatedRepoValidation(t *testing.T) {
t.Errorf("sut should be valid but was %q", err) t.Errorf("sut should be valid but was %q", err)
} }
sut = FederatedRepo{ sut = FollowingRepo{
ExternalID: "12", ExternalID: "12",
FederationHostID: 1, FederationHostID: 1,
Uri: "http://localhost:3000/api/v1/activitypub/repo-id/1", Uri: "http://localhost:3000/api/v1/activitypub/repo-id/1",

View File

@ -12,29 +12,29 @@ import (
) )
func init() { 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 maxFollowingRepos := 10
sess := db.GetEngine(ctx).Where("repo_id=?", repoId) sess := db.GetEngine(ctx).Where("repo_id=?", repoId)
sess = sess.Limit(maxFollowingRepos, 0) sess = sess.Limit(maxFollowingRepos, 0)
federatedRepoList := make([]*FederatedRepo, 0, maxFollowingRepos) followingRepoList := make([]*FollowingRepo, 0, maxFollowingRepos)
err := sess.Find(&federatedRepoList) err := sess.Find(&followingRepoList)
if err != nil { if err != nil {
return make([]*FederatedRepo, 0, maxFollowingRepos), err return make([]*FollowingRepo, 0, maxFollowingRepos), err
} }
for _, federatedRepo := range federatedRepoList { for _, followingRepo := range followingRepoList {
if res, err := validation.IsValid(*federatedRepo); !res { if res, err := validation.IsValid(*followingRepo); !res {
return make([]*FederatedRepo, 0, maxFollowingRepos), fmt.Errorf("FederationInfo is not valid: %v", err) 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 { func StoreFollowingRepos(ctx context.Context, localRepoId int64, followingRepoList []*FollowingRepo) error {
for _, federatedRepo := range federatedRepoList { for _, followingRepo := range followingRepoList {
if res, err := validation.IsValid(*federatedRepo); !res { if res, err := validation.IsValid(*followingRepo); !res {
return fmt.Errorf("FederationInfo is not valid: %v", err) return fmt.Errorf("FederationInfo is not valid: %v", err)
} }
} }
@ -46,12 +46,12 @@ func StoreFollowingRepos(ctx context.Context, localRepoId int64, federatedRepoLi
} }
defer committer.Close() 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 { if err != nil {
return err return err
} }
for _, federatedRepo := range federatedRepoList { for _, followingRepo := range followingRepoList {
_, err = db.GetEngine(ctx).Insert(federatedRepo) _, err = db.GetEngine(ctx).Insert(followingRepo)
if err != nil { if err != nil {
return err return err
} }

View File

@ -380,18 +380,18 @@ func repoAssignment(ctx *Context, repo *repo_model.Repository) {
ctx.Data["HasAccess"] = true ctx.Data["HasAccess"] = true
ctx.Data["Permission"] = &ctx.Repo.Permission 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 { if err == nil {
followingRepoString := "" followingRepoString := ""
for idx, federatedRepo := range federatedRepoList { for idx, followingRepo := range followingRepoList {
if idx > 0 { if idx > 0 {
followingRepoString += ";" followingRepoString += ";"
} }
followingRepoString += (*federatedRepo).Uri followingRepoString += (*followingRepo).Uri
} }
ctx.Data["FollowingRepos"] = followingRepoString ctx.Data["FollowingRepos"] = followingRepoString
} else if err != repo_model.ErrMirrorNotExist { } else if err != repo_model.ErrMirrorNotExist {
ctx.ServerError("FindFederatedRepoByRepoID", err) ctx.ServerError("FindFollowingRepoByRepoID", err)
return return
} }

View File

@ -127,7 +127,7 @@ func IsValidForgejoActivityPubURL(url string) bool {
return true return true
} }
func IsValidFederatedRepoURL(url string) bool { func IsValidFollowingRepoURL(url string) bool {
if !IsValidForgejoActivityPubURL(url) { if !IsValidForgejoActivityPubURL(url) {
return false return false
} }
@ -143,13 +143,13 @@ func IsValidFederatedRepoURL(url string) bool {
return true return true
} }
func IsValidFederatedRepoURLList(urls string) bool { func IsValidFollowingRepoURLList(urls string) bool {
switch { switch {
case len(strings.Split(urls, ";")) == 1: case len(strings.Split(urls, ";")) == 1:
return IsValidFederatedRepoURL(urls) return IsValidFollowingRepoURL(urls)
default: default:
for _, url := range strings.Split(urls, ";") { for _, url := range strings.Split(urls, ";") {
if !IsValidFederatedRepoURLList(url) { if !IsValidFollowingRepoURLList(url) {
return false return false
} }
} }

View File

@ -214,7 +214,7 @@ func SettingsPost(ctx *context.Context) {
federationRepoSplit[idx] = strings.TrimSpace(repo) 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) ctx.ServerError("UpdateRepository", err)
return return
} }

View File

@ -213,23 +213,23 @@ func CreateUserFromAP(ctx context.Context, personID forgefed.PersonID, federatio
return &newUser, &federatedUser, nil return &newUser, &federatedUser, nil
} }
// Create or update a list of FederatedRepo structs // Create or update a list of FollowingRepo structs
func StoreFederatedRepoList(ctx context.Context, localRepoId int64, federatedRepoList []string) (int, string, error) { func StoreFollowingRepoList(ctx context.Context, localRepoId int64, followingRepoList []string) (int, string, error) {
followingRepos := make([]*repo.FederatedRepo, 0, len(federatedRepoList)) followingRepos := make([]*repo.FollowingRepo, 0, len(followingRepoList))
for _, uri := range federatedRepoList { for _, uri := range followingRepoList {
federationHost, err := GetFederationHostForUri(ctx, uri) federationHost, err := GetFederationHostForUri(ctx, uri)
if err != nil { if err != nil {
return http.StatusInternalServerError, "Wrong FederationHost", err 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 { if err != nil {
return http.StatusNotAcceptable, "Invalid federated repo", err 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 { if err != nil {
return http.StatusNotAcceptable, "Invalid federated repo", err return http.StatusNotAcceptable, "Invalid federated repo", err
} }
followingRepos = append(followingRepos, &federatedRepo) followingRepos = append(followingRepos, &followingRepo)
} }
repo.StoreFollowingRepos(ctx, localRepoId, followingRepos) 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 { 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 { 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) likeActivityList := make([]forgefed.ForgeLike, 0)
for _, federatedRepo := range followingRepos { for _, followingRepo := range followingRepos {
target := federatedRepo.Uri target := followingRepo.Uri
likeActivity, err := forgefed.NewForgeLike(doer.APAPIURL(), target, time.Now()) likeActivity, err := forgefed.NewForgeLike(doer.APAPIURL(), target, time.Now())
if err != nil { if err != nil {
return err return err