From bbe50963079493a5ff7f8e5fba815c3cec756428 Mon Sep 17 00:00:00 2001 From: Clemens Date: Tue, 26 Mar 2024 14:02:30 +0100 Subject: [PATCH] fixed circular dependencies next: fix post call error --- models/forgefed/activity.go | 5 +--- models/repo/star.go | 46 ++++++++++++++++++++----------------- routers/api/v1/user/star.go | 1 + 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/models/forgefed/activity.go b/models/forgefed/activity.go index 409a074142..4fb0fac148 100644 --- a/models/forgefed/activity.go +++ b/models/forgefed/activity.go @@ -6,7 +6,6 @@ package forgefed import ( "time" - "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/validation" ap "github.com/go-ap/activitypub" @@ -20,10 +19,8 @@ type ForgeLike struct { } // TODO: Use explicit values instead of ctx !! -func NewForgeLike(ctx *context.APIContext) (ForgeLike, error) { +func NewForgeLike(actorIRI string, objectIRI string) (ForgeLike, error) { result := ForgeLike{} - actorIRI := ctx.Repo.Owner.APAPIURL() - objectIRI := ctx.Repo.Repository.APAPIURL() result.Type = ap.LikeType // ToDo: Would validating the source by Actor.Type field make sense? result.Actor = ap.ActorNew(ap.IRI(actorIRI), "ForgejoUser") // Thats us, a User diff --git a/models/repo/star.go b/models/repo/star.go index f8566725c2..7164a245e2 100644 --- a/models/repo/star.go +++ b/models/repo/star.go @@ -7,8 +7,9 @@ import ( "context" "code.gitea.io/gitea/models/db" - //"code.gitea.io/gitea/models/forgefed" + "code.gitea.io/gitea/models/forgefed" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/activitypub" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" ) @@ -25,7 +26,6 @@ func init() { db.RegisterModel(new(Star)) } -// TODO: why is this ctx not type *context.APIContext, as in routers/api/v1/user/star.go? // StarRepo or unstar repository. func StarRepo(ctx context.Context, userID, repoID int64, star bool) error { ctx, committer, err := db.TxContext(ctx) @@ -49,7 +49,7 @@ func StarRepo(ctx context.Context, userID, repoID int64, star bool) error { if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID); err != nil { return err } - if err := SendLikeActivities(ctx, userID); err != nil { + if err := SendLikeActivities(ctx, userID, repoID); err != nil { return err } } else { @@ -71,38 +71,42 @@ func StarRepo(ctx context.Context, userID, repoID int64, star bool) error { return committer.Commit() } -// TODO: solve circular dependencies caused by import of forgefed! -func SendLikeActivities(ctx context.Context, userID int64) error { +func SendLikeActivities(ctx context.Context, userID int64, repoID int64) error { + // TODO: should this be checked somewhere else/outside? if setting.Federation.Enabled { - - /*likeActivity, err := forgefed.NewForgeLike(ctx) - if err != nil { - return err - } - - json, err := likeActivity.MarshalJSON() - if err != nil { - return err - } - // TODO: is user loading necessary here? user, err := user_model.GetUserByID(ctx, userID) if err != nil { return err } + federatedRepos, err := FindFederatedReposByRepoID(ctx, repoID) + if err != nil { + return err + } + apclient, err := activitypub.NewClient(ctx, user, user.APAPIURL()) if err != nil { return err } - // TODO: set timeouts for outgoing request in oder to mitigate DOS by slow lories - // ToDo: Change this to the standalone table of FederatedRepos - for _, target := range strings.Split(ctx.Repo.Repository.FederationRepos, ";") { + for _, federatedRepo := range federatedRepos { + target := federatedRepo.Uri + likeActivity, err := forgefed.NewForgeLike(user.APAPIURL(), target) + if err != nil { + return err + } + + json, err := likeActivity.MarshalJSON() + if err != nil { + return err + } + + // TODO: set timeouts for outgoing request in oder to mitigate DOS by slow lories + // TODO: Check if we need to respect rate limits + // ToDo: Change this to the standalone table of FederatedRepos apclient.Post([]byte(json), target) } - */ - // Send to list of federated repos } return nil } diff --git a/routers/api/v1/user/star.go b/routers/api/v1/user/star.go index 2ea059479e..d45b1e6488 100644 --- a/routers/api/v1/user/star.go +++ b/routers/api/v1/user/star.go @@ -156,6 +156,7 @@ func Star(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" + // TODO: why is this *context.APIContext passed, where a context.Context is expected? err := repo_model.StarRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, true) if err != nil { ctx.Error(http.StatusInternalServerError, "StarRepo", err)