forgejo/models/repo/repo_repository.go

63 lines
1.7 KiB
Go
Raw Normal View History

2024-04-12 11:52:26 +00:00
// Copyright 2024 The Forgejo Authors. All rights reserved.
2024-03-21 07:25:40 +00:00
// 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 (
"context"
"fmt"
2024-03-21 07:25:40 +00:00
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/validation"
2024-03-21 07:25:40 +00:00
)
func init() {
2024-04-03 10:26:33 +00:00
db.RegisterModel(new(FollowingRepo))
2024-03-21 07:25:40 +00:00
}
2024-04-04 13:08:02 +00:00
func FindFollowingReposByRepoID(ctx context.Context, repoID int64) ([]*FollowingRepo, error) {
2024-04-03 10:08:44 +00:00
maxFollowingRepos := 10
2024-04-04 13:08:02 +00:00
sess := db.GetEngine(ctx).Where("repo_id=?", repoID)
2024-04-03 10:08:44 +00:00
sess = sess.Limit(maxFollowingRepos, 0)
2024-04-03 10:26:33 +00:00
followingRepoList := make([]*FollowingRepo, 0, maxFollowingRepos)
err := sess.Find(&followingRepoList)
2024-03-25 15:47:18 +00:00
if err != nil {
2024-04-03 10:26:33 +00:00
return make([]*FollowingRepo, 0, maxFollowingRepos), err
2024-03-25 15:47:18 +00:00
}
2024-04-03 10:26:33 +00:00
for _, followingRepo := range followingRepoList {
if res, err := validation.IsValid(*followingRepo); !res {
return make([]*FollowingRepo, 0, maxFollowingRepos), fmt.Errorf("FederationInfo is not valid: %v", err)
2024-03-25 15:47:18 +00:00
}
}
2024-04-03 10:26:33 +00:00
return followingRepoList, nil
}
2024-04-04 13:08:02 +00:00
func StoreFollowingRepos(ctx context.Context, localRepoID int64, followingRepoList []*FollowingRepo) error {
2024-04-03 10:26:33 +00:00
for _, followingRepo := range followingRepoList {
if res, err := validation.IsValid(*followingRepo); !res {
2024-03-22 19:14:20 +00:00
return fmt.Errorf("FederationInfo is not valid: %v", err)
}
}
2024-03-22 19:14:20 +00:00
// Begin transaction
ctx, committer, err := db.TxContext((ctx))
if err != nil {
return err
}
defer committer.Close()
2024-04-04 13:08:02 +00:00
_, err = db.GetEngine(ctx).Where("repo_id=?", localRepoID).Delete(FollowingRepo{})
2024-03-22 19:14:20 +00:00
if err != nil {
return err
}
2024-04-03 10:26:33 +00:00
for _, followingRepo := range followingRepoList {
_, err = db.GetEngine(ctx).Insert(followingRepo)
2024-03-22 19:14:20 +00:00
if err != nil {
return err
}
}
// Commit transaction
return committer.Commit()
}