fix some linting issues

This commit is contained in:
Clemens 2024-04-04 15:08:02 +02:00
parent 976d79044f
commit 92d011f1a5
8 changed files with 28 additions and 25 deletions

View file

@ -18,7 +18,7 @@ type ForgeLike struct {
ap.Activity ap.Activity
} }
func NewForgeLike(actorIRI string, objectIRI string, startTime time.Time) (ForgeLike, error) { func NewForgeLike(actorIRI, objectIRI string, startTime time.Time) (ForgeLike, error) {
result := ForgeLike{} result := ForgeLike{}
result.Type = ap.LikeType result.Type = ap.LikeType
// ToDo: Would validating the source by Actor.Type field make sense? // ToDo: Would validating the source by Actor.Type field make sense?

View file

@ -14,7 +14,7 @@ type FollowingRepo struct {
RepoID int64 `xorm:"NOT NULL"` RepoID int64 `xorm:"NOT NULL"`
ExternalID string `xorm:"TEXT UNIQUE(federation_repo_mapping) NOT NULL"` ExternalID string `xorm:"TEXT UNIQUE(federation_repo_mapping) NOT NULL"`
FederationHostID int64 `xorm:"UNIQUE(federation_repo_mapping) NOT NULL"` FederationHostID int64 `xorm:"UNIQUE(federation_repo_mapping) NOT NULL"`
Uri string URI string
} }
func NewFollowingRepo(repoID int64, externalID string, federationHostID int64, uri string) (FollowingRepo, error) { func NewFollowingRepo(repoID int64, externalID string, federationHostID int64, uri string) (FollowingRepo, error) {
@ -22,7 +22,7 @@ func NewFollowingRepo(repoID int64, externalID string, federationHostID int64, u
RepoID: repoID, RepoID: repoID,
ExternalID: externalID, ExternalID: externalID,
FederationHostID: federationHostID, FederationHostID: federationHostID,
Uri: uri, URI: uri,
} }
if valid, err := validation.IsValid(result); !valid { if valid, err := validation.IsValid(result); !valid {
return FollowingRepo{}, err return FollowingRepo{}, err
@ -35,6 +35,6 @@ func (user FollowingRepo) Validate() []string {
result = append(result, validation.ValidateNotEmpty(user.RepoID, "UserID")...) result = append(result, validation.ValidateNotEmpty(user.RepoID, "UserID")...)
result = append(result, validation.ValidateNotEmpty(user.ExternalID, "ExternalID")...) result = append(result, validation.ValidateNotEmpty(user.ExternalID, "ExternalID")...)
result = append(result, validation.ValidateNotEmpty(user.FederationHostID, "FederationHostID")...) result = append(result, validation.ValidateNotEmpty(user.FederationHostID, "FederationHostID")...)
result = append(result, validation.ValidateNotEmpty(user.Uri, "Uri")...) result = append(result, validation.ValidateNotEmpty(user.URI, "Uri")...)
return result return result
} }

View file

@ -14,7 +14,7 @@ func Test_FollowingRepoValidation(t *testing.T) {
RepoID: 12, RepoID: 12,
ExternalID: "12", ExternalID: "12",
FederationHostID: 1, FederationHostID: 1,
Uri: "http://localhost:3000/api/v1/activitypub/repo-id/1", URI: "http://localhost:3000/api/v1/activitypub/repo-id/1",
} }
if res, err := validation.IsValid(sut); !res { if res, err := validation.IsValid(sut); !res {
t.Errorf("sut should be valid but was %q", err) t.Errorf("sut should be valid but was %q", err)
@ -23,7 +23,7 @@ func Test_FollowingRepoValidation(t *testing.T) {
sut = FollowingRepo{ sut = FollowingRepo{
ExternalID: "12", ExternalID: "12",
FederationHostID: 1, FederationHostID: 1,
Uri: "http://localhost:3000/api/v1/activitypub/repo-id/1", URI: "http://localhost:3000/api/v1/activitypub/repo-id/1",
} }
if res, _ := validation.IsValid(sut); res { if res, _ := validation.IsValid(sut); res {
t.Errorf("sut should be invalid") t.Errorf("sut should be invalid")

View file

@ -15,9 +15,9 @@ func init() {
db.RegisterModel(new(FollowingRepo)) db.RegisterModel(new(FollowingRepo))
} }
func FindFollowingReposByRepoID(ctx context.Context, repoId int64) ([]*FollowingRepo, error) { func FindFollowingReposByRepoID(ctx context.Context, repoID int64) ([]*FollowingRepo, error) {
maxFollowingRepos := 10 maxFollowingRepos := 10
sess := db.GetEngine(ctx).Where("repo_id=?", repoId) sess := db.GetEngine(ctx).Where("repo_id=?", repoID)
sess = sess.Limit(maxFollowingRepos, 0) sess = sess.Limit(maxFollowingRepos, 0)
followingRepoList := make([]*FollowingRepo, 0, maxFollowingRepos) followingRepoList := make([]*FollowingRepo, 0, maxFollowingRepos)
err := sess.Find(&followingRepoList) err := sess.Find(&followingRepoList)
@ -32,7 +32,7 @@ func FindFollowingReposByRepoID(ctx context.Context, repoId int64) ([]*Following
return followingRepoList, nil return followingRepoList, nil
} }
func StoreFollowingRepos(ctx context.Context, localRepoId int64, followingRepoList []*FollowingRepo) error { func StoreFollowingRepos(ctx context.Context, localRepoID int64, followingRepoList []*FollowingRepo) error {
for _, followingRepo := range followingRepoList { for _, followingRepo := range followingRepoList {
if res, err := validation.IsValid(*followingRepo); !res { if res, err := validation.IsValid(*followingRepo); !res {
return fmt.Errorf("FederationInfo is not valid: %v", err) return fmt.Errorf("FederationInfo is not valid: %v", err)
@ -46,7 +46,7 @@ func StoreFollowingRepos(ctx context.Context, localRepoId int64, followingRepoLi
} }
defer committer.Close() defer committer.Close()
_, err = db.GetEngine(ctx).Where("repo_id=?", localRepoId).Delete(FollowingRepo{}) _, err = db.GetEngine(ctx).Where("repo_id=?", localRepoID).Delete(FollowingRepo{})
if err != nil { if err != nil {
return err return err
} }

View file

@ -387,7 +387,7 @@ func repoAssignment(ctx *Context, repo *repo_model.Repository) {
if idx > 0 { if idx > 0 {
followingRepoString += ";" followingRepoString += ";"
} }
followingRepoString += (*followingRepo).Uri followingRepoString += followingRepo.URI
} }
ctx.Data["FollowingRepos"] = followingRepoString ctx.Data["FollowingRepos"] = followingRepoString
} else if err != repo_model.ErrMirrorNotExist { } else if err != repo_model.ErrMirrorNotExist {

View file

@ -33,7 +33,6 @@ import (
"code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/services/convert" "code.gitea.io/gitea/services/convert"
"code.gitea.io/gitea/services/forms" "code.gitea.io/gitea/services/forms"
"code.gitea.io/gitea/services/repository"
repo_service "code.gitea.io/gitea/services/repository" repo_service "code.gitea.io/gitea/services/repository"
archiver_service "code.gitea.io/gitea/services/repository/archiver" archiver_service "code.gitea.io/gitea/services/repository/archiver"
) )
@ -318,9 +317,9 @@ func Action(ctx *context.Context) {
case "unwatch": case "unwatch":
err = repo_model.WatchRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, false) err = repo_model.WatchRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, false)
case "star": case "star":
err = repository.StarRepoAndFederate(ctx, *ctx.Doer, ctx.Repo.Repository.ID, true) err = repo_service.StarRepoAndFederate(ctx, *ctx.Doer, ctx.Repo.Repository.ID, true)
case "unstar": case "unstar":
err = repository.StarRepoAndFederate(ctx, *ctx.Doer, ctx.Repo.Repository.ID, false) err = repo_service.StarRepoAndFederate(ctx, *ctx.Doer, ctx.Repo.Repository.ID, false)
case "accept_transfer": case "accept_transfer":
err = acceptOrRejectRepoTransfer(ctx, true) err = acceptOrRejectRepoTransfer(ctx, true)
case "reject_transfer": case "reject_transfer":

View file

@ -40,7 +40,7 @@ func ProcessLikeActivity(ctx context.Context, form any, repositoryID int64) (int
// parse actorID (person) // parse actorID (person)
actorURI := activity.Actor.GetID().String() actorURI := activity.Actor.GetID().String()
log.Info("actorURI was: %v", actorURI) log.Info("actorURI was: %v", actorURI)
federationHost, err := GetFederationHostForUri(ctx, actorURI) federationHost, err := GetFederationHostForURI(ctx, actorURI)
if err != nil { if err != nil {
return http.StatusInternalServerError, "Wrong FederationHost", err return http.StatusInternalServerError, "Wrong FederationHost", err
} }
@ -129,7 +129,7 @@ func CreateFederationHostFromAP(ctx context.Context, actorID forgefed.ActorID) (
return &result, nil return &result, nil
} }
func GetFederationHostForUri(ctx context.Context, actorURI string) (*forgefed.FederationHost, error) { func GetFederationHostForURI(ctx context.Context, actorURI string) (*forgefed.FederationHost, error) {
// parse actorID (person) // parse actorID (person)
log.Info("Input was: %v", actorURI) log.Info("Input was: %v", actorURI)
rawActorID, err := forgefed.NewActorID(actorURI) rawActorID, err := forgefed.NewActorID(actorURI)
@ -214,10 +214,10 @@ func CreateUserFromAP(ctx context.Context, personID forgefed.PersonID, federatio
} }
// Create or update a list of FollowingRepo structs // Create or update a list of FollowingRepo structs
func StoreFollowingRepoList(ctx context.Context, localRepoId int64, followingRepoList []string) (int, string, error) { func StoreFollowingRepoList(ctx context.Context, localRepoID int64, followingRepoList []string) (int, string, error) {
followingRepos := make([]*repo.FollowingRepo, 0, len(followingRepoList)) followingRepos := make([]*repo.FollowingRepo, 0, len(followingRepoList))
for _, uri := range followingRepoList { for _, uri := range followingRepoList {
federationHost, err := GetFederationHostForUri(ctx, uri) federationHost, err := GetFederationHostForURI(ctx, uri)
if err != nil { if err != nil {
return http.StatusInternalServerError, "Wrong FederationHost", err return http.StatusInternalServerError, "Wrong FederationHost", err
} }
@ -225,20 +225,22 @@ func StoreFollowingRepoList(ctx context.Context, localRepoId int64, followingRep
if err != nil { if err != nil {
return http.StatusNotAcceptable, "Invalid federated repo", err return http.StatusNotAcceptable, "Invalid federated repo", err
} }
followingRepo, err := repo.NewFollowingRepo(localRepoId, followingRepoID.ID, federationHost.ID, uri) followingRepo, err := repo.NewFollowingRepo(localRepoID, followingRepoID.ID, federationHost.ID, uri)
if err != nil { if err != nil {
return http.StatusNotAcceptable, "Invalid federated repo", err return http.StatusNotAcceptable, "Invalid federated repo", err
} }
followingRepos = append(followingRepos, &followingRepo) followingRepos = append(followingRepos, &followingRepo)
} }
repo.StoreFollowingRepos(ctx, localRepoId, followingRepos) if err := repo.StoreFollowingRepos(ctx, localRepoID, followingRepos); err != nil {
return 0, "", err
}
return 0, "", nil return 0, "", nil
} }
func DeleteFollowingRepos(ctx context.Context, localRepoId int64) error { func DeleteFollowingRepos(ctx context.Context, localRepoID int64) error {
return repo.StoreFollowingRepos(ctx, localRepoId, []*repo.FollowingRepo{}) return repo.StoreFollowingRepos(ctx, localRepoID, []*repo.FollowingRepo{})
} }
func SendLikeActivities(ctx context.Context, doer user.User, repoID int64) error { func SendLikeActivities(ctx context.Context, doer user.User, repoID int64) error {
@ -250,7 +252,7 @@ func SendLikeActivities(ctx context.Context, doer user.User, repoID int64) error
likeActivityList := make([]forgefed.ForgeLike, 0) likeActivityList := make([]forgefed.ForgeLike, 0)
for _, followingRepo := range followingRepos { for _, followingRepo := range followingRepos {
target := followingRepo.Uri target := followingRepo.URI
likeActivity, err := forgefed.NewForgeLike(doer.APAPIURL(), target, time.Now()) likeActivity, err := forgefed.NewForgeLike(doer.APAPIURL(), target, time.Now())
if err != nil { if err != nil {
return err return err
@ -268,7 +270,7 @@ func SendLikeActivities(ctx context.Context, doer user.User, repoID int64) error
return err return err
} }
_, err = apclient.Post([]byte(json), fmt.Sprintf("%v/inbox/", activity.Object)) _, err = apclient.Post(json, fmt.Sprintf("%v/inbox/", activity.Object))
if err != nil { if err != nil {
log.Error("error %v while sending activity: %q", err, activity) log.Error("error %v while sending activity: %q", err, activity)
} }

View file

@ -67,7 +67,9 @@ func DeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_mod
return err return err
} }
federation_service.DeleteFollowingRepos(ctx, repo.ID) if err := federation_service.DeleteFollowingRepos(ctx, repo.ID); err != nil {
return err
}
return packages_model.UnlinkRepositoryFromAllPackages(ctx, repo.ID) return packages_model.UnlinkRepositoryFromAllPackages(ctx, repo.ID)
} }