fixed circular dependencies

next: fix post call error
This commit is contained in:
Clemens 2024-03-26 14:02:30 +01:00
parent 7f0371056e
commit bbe5096307
3 changed files with 27 additions and 25 deletions

View file

@ -6,7 +6,6 @@ package forgefed
import ( import (
"time" "time"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/validation" "code.gitea.io/gitea/modules/validation"
ap "github.com/go-ap/activitypub" ap "github.com/go-ap/activitypub"
@ -20,10 +19,8 @@ type ForgeLike struct {
} }
// TODO: Use explicit values instead of ctx !! // TODO: Use explicit values instead of ctx !!
func NewForgeLike(ctx *context.APIContext) (ForgeLike, error) { func NewForgeLike(actorIRI string, objectIRI string) (ForgeLike, error) {
result := ForgeLike{} result := ForgeLike{}
actorIRI := ctx.Repo.Owner.APAPIURL()
objectIRI := ctx.Repo.Repository.APAPIURL()
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?
result.Actor = ap.ActorNew(ap.IRI(actorIRI), "ForgejoUser") // Thats us, a User result.Actor = ap.ActorNew(ap.IRI(actorIRI), "ForgejoUser") // Thats us, a User

View file

@ -7,8 +7,9 @@ import (
"context" "context"
"code.gitea.io/gitea/models/db" "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" 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/setting"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
) )
@ -25,7 +26,6 @@ func init() {
db.RegisterModel(new(Star)) 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. // StarRepo or unstar repository.
func StarRepo(ctx context.Context, userID, repoID int64, star bool) error { func StarRepo(ctx context.Context, userID, repoID int64, star bool) error {
ctx, committer, err := db.TxContext(ctx) 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 { if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID); err != nil {
return err return err
} }
if err := SendLikeActivities(ctx, userID); err != nil { if err := SendLikeActivities(ctx, userID, repoID); err != nil {
return err return err
} }
} else { } else {
@ -71,38 +71,42 @@ func StarRepo(ctx context.Context, userID, repoID int64, star bool) error {
return committer.Commit() return committer.Commit()
} }
// TODO: solve circular dependencies caused by import of forgefed! func SendLikeActivities(ctx context.Context, userID int64, repoID int64) error {
func SendLikeActivities(ctx context.Context, userID int64) error { // TODO: should this be checked somewhere else/outside?
if setting.Federation.Enabled { 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? // TODO: is user loading necessary here?
user, err := user_model.GetUserByID(ctx, userID) user, err := user_model.GetUserByID(ctx, userID)
if err != nil { if err != nil {
return err return err
} }
federatedRepos, err := FindFederatedReposByRepoID(ctx, repoID)
if err != nil {
return err
}
apclient, err := activitypub.NewClient(ctx, user, user.APAPIURL()) apclient, err := activitypub.NewClient(ctx, user, user.APAPIURL())
if err != nil { if err != nil {
return err return err
} }
// TODO: set timeouts for outgoing request in oder to mitigate DOS by slow lories for _, federatedRepo := range federatedRepos {
// ToDo: Change this to the standalone table of FederatedRepos target := federatedRepo.Uri
for _, target := range strings.Split(ctx.Repo.Repository.FederationRepos, ";") { 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) apclient.Post([]byte(json), target)
} }
*/
// Send to list of federated repos
} }
return nil return nil
} }

View file

@ -156,6 +156,7 @@ func Star(ctx *context.APIContext) {
// "404": // "404":
// "$ref": "#/responses/notFound" // "$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) err := repo_model.StarRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, true)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "StarRepo", err) ctx.Error(http.StatusInternalServerError, "StarRepo", err)