2024-03-21 07:25:40 +00:00
|
|
|
// Copyright 2024 The forgejo Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
2024-03-22 16:51:20 +00:00
|
|
|
// ToDo: Is this package the right place for federated repo? May need to diskuss this.
|
2024-03-21 07:25:40 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2024-03-22 16:41:59 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2024-03-21 07:25:40 +00:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2024-03-22 16:41:59 +00:00
|
|
|
"code.gitea.io/gitea/modules/validation"
|
2024-03-21 07:25:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(FederatedRepo))
|
|
|
|
}
|
2024-03-22 16:41:59 +00:00
|
|
|
|
2024-04-03 10:08:44 +00:00
|
|
|
func FindFollowingReposByRepoID(ctx context.Context, repoId int64) ([]*FederatedRepo, error) {
|
|
|
|
maxFollowingRepos := 10
|
2024-03-25 08:15:32 +00:00
|
|
|
sess := db.GetEngine(ctx).Where("repo_id=?", repoId)
|
2024-04-03 10:08:44 +00:00
|
|
|
sess = sess.Limit(maxFollowingRepos, 0)
|
|
|
|
federatedRepoList := make([]*FederatedRepo, 0, maxFollowingRepos)
|
2024-03-25 15:47:18 +00:00
|
|
|
err := sess.Find(&federatedRepoList)
|
|
|
|
if err != nil {
|
2024-04-03 10:08:44 +00:00
|
|
|
return make([]*FederatedRepo, 0, maxFollowingRepos), err
|
2024-03-25 15:47:18 +00:00
|
|
|
}
|
|
|
|
for _, federatedRepo := range federatedRepoList {
|
|
|
|
if res, err := validation.IsValid(*federatedRepo); !res {
|
2024-04-03 10:08:44 +00:00
|
|
|
return make([]*FederatedRepo, 0, maxFollowingRepos), fmt.Errorf("FederationInfo is not valid: %v", err)
|
2024-03-25 15:47:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return federatedRepoList, nil
|
2024-03-22 16:41:59 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 10:08:44 +00:00
|
|
|
func StoreFollowingRepos(ctx context.Context, localRepoId int64, federatedRepoList []*FederatedRepo) error {
|
2024-03-22 19:14:20 +00:00
|
|
|
for _, federatedRepo := range federatedRepoList {
|
2024-03-23 16:01:25 +00:00
|
|
|
if res, err := validation.IsValid(*federatedRepo); !res {
|
2024-03-22 19:14:20 +00:00
|
|
|
return fmt.Errorf("FederationInfo is not valid: %v", err)
|
|
|
|
}
|
2024-03-22 16:41:59 +00:00
|
|
|
}
|
2024-03-22 19:14:20 +00:00
|
|
|
|
|
|
|
// Begin transaction
|
|
|
|
ctx, committer, err := db.TxContext((ctx))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer committer.Close()
|
|
|
|
|
|
|
|
_, err = db.GetEngine(ctx).Where("repo_id=?", localRepoId).Delete(FederatedRepo{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, federatedRepo := range federatedRepoList {
|
|
|
|
_, err = db.GetEngine(ctx).Insert(federatedRepo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commit transaction
|
|
|
|
return committer.Commit()
|
2024-03-22 16:41:59 +00:00
|
|
|
}
|