2024-01-19 16:26:16 +01:00
|
|
|
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package forgefed
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-02-07 14:30:17 +01:00
|
|
|
"strings"
|
2024-01-19 16:26:16 +01:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
"code.gitea.io/gitea/modules/validation"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(FederationHost))
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetFederationHost(ctx context.Context, ID int64) (*FederationHost, error) {
|
|
|
|
host := new(FederationHost)
|
|
|
|
has, err := db.GetEngine(ctx).Where("id=?", ID).Get(host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, fmt.Errorf("FederationInfo record %v does not exist", ID)
|
|
|
|
}
|
|
|
|
if res, err := validation.IsValid(host); !res {
|
2024-05-13 11:14:04 +02:00
|
|
|
return nil, err
|
2024-01-19 16:26:16 +01:00
|
|
|
}
|
|
|
|
return host, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func FindFederationHostByFqdn(ctx context.Context, fqdn string) (*FederationHost, error) {
|
|
|
|
host := new(FederationHost)
|
2024-02-07 14:30:17 +01:00
|
|
|
has, err := db.GetEngine(ctx).Where("host_fqdn=?", strings.ToLower(fqdn)).Get(host)
|
2024-01-19 16:26:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if res, err := validation.IsValid(host); !res {
|
2024-05-13 11:14:04 +02:00
|
|
|
return nil, err
|
2024-01-19 16:26:16 +01:00
|
|
|
}
|
|
|
|
return host, nil
|
|
|
|
}
|
|
|
|
|
2024-02-08 15:16:37 +01:00
|
|
|
func CreateFederationHost(ctx context.Context, host *FederationHost) error {
|
2024-01-19 16:26:16 +01:00
|
|
|
if res, err := validation.IsValid(host); !res {
|
2024-05-13 11:14:04 +02:00
|
|
|
return err
|
2024-01-19 16:26:16 +01:00
|
|
|
}
|
|
|
|
_, err := db.GetEngine(ctx).Insert(host)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-08 15:16:37 +01:00
|
|
|
func UpdateFederationHost(ctx context.Context, host *FederationHost) error {
|
2024-01-19 16:26:16 +01:00
|
|
|
if res, err := validation.IsValid(host); !res {
|
2024-05-13 11:14:04 +02:00
|
|
|
return err
|
2024-01-19 16:26:16 +01:00
|
|
|
}
|
|
|
|
_, err := db.GetEngine(ctx).ID(host.ID).Update(host)
|
|
|
|
return err
|
|
|
|
}
|