Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-29 13:16:10 +01:00
forgejo/models/repo/repo_repository.go

63 lines
1.7 KiB
Go
Raw Normal View History

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