From 7f0371056e637fb64f8c115f5aac2aa81a67959b Mon Sep 17 00:00:00 2001 From: Clemens Date: Tue, 26 Mar 2024 10:23:14 +0100 Subject: [PATCH] moved functionality import of forgefed causes circular dependencies. This has to be solved! --- models/repo/star.go | 42 +++++++++++++++++++++++++++++++++++++ routers/api/v1/user/star.go | 31 --------------------------- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/models/repo/star.go b/models/repo/star.go index 60737149da..f8566725c2 100644 --- a/models/repo/star.go +++ b/models/repo/star.go @@ -7,7 +7,9 @@ import ( "context" "code.gitea.io/gitea/models/db" + //"code.gitea.io/gitea/models/forgefed" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" ) @@ -23,6 +25,7 @@ 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) @@ -46,6 +49,9 @@ 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 { + return err + } } else { if !staring { return nil @@ -65,6 +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 { + 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 + } + + 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, ";") { + apclient.Post([]byte(json), target) + } + */ + // Send to list of federated repos + } + return nil +} + // IsStaring checks if user has starred given repository. func IsStaring(ctx context.Context, userID, repoID int64) bool { has, _ := db.GetEngine(ctx).Get(&Star{UID: userID, RepoID: repoID}) diff --git a/routers/api/v1/user/star.go b/routers/api/v1/user/star.go index 42df6d9a90..2ea059479e 100644 --- a/routers/api/v1/user/star.go +++ b/routers/api/v1/user/star.go @@ -8,16 +8,12 @@ package user import ( std_context "context" "net/http" - "strings" "code.gitea.io/gitea/models/db" - "code.gitea.io/gitea/models/forgefed" access_model "code.gitea.io/gitea/models/perm/access" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" - "code.gitea.io/gitea/modules/activitypub" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/routers/api/v1/utils" "code.gitea.io/gitea/services/convert" @@ -165,34 +161,7 @@ func Star(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "StarRepo", err) return } - // TODO: move this code to repo.federated_repository - if setting.Federation.Enabled { - likeActivity, err := forgefed.NewForgeLike(ctx) - if err != nil { - ctx.Error(http.StatusInternalServerError, "StarRepo", err) - return - } - - json, err := likeActivity.MarshalJSON() - if err != nil { - ctx.Error(http.StatusInternalServerError, "StarRepo", err) - return - } - - apclient, err := activitypub.NewClient(ctx, ctx.Doer, ctx.Doer.APAPIURL()) - if err != nil { - ctx.Error(http.StatusInternalServerError, "StarRepo", err) - return - } - // 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, ";") { - apclient.Post([]byte(json), target) - } - - // Send to list of federated repos - } ctx.Status(http.StatusNoContent) }