mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-29 04:36:18 +01:00
renamed FederatedRepo to FollowingRepo
This commit is contained in:
parent
daccaed157
commit
2b7a22afb3
7 changed files with 43 additions and 43 deletions
|
@ -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")...)
|
|
@ -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",
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue