From 7f19247b0ae6ef37ed57b4686d8f8a67ad57728b Mon Sep 17 00:00:00 2001 From: Clemens Date: Thu, 28 Mar 2024 14:27:50 +0100 Subject: [PATCH] Moved sendLikeActivities to federation_service --- models/repo/star.go | 59 +------------------------- models/repo/star_test.go | 22 +++++----- modules/forgefed/federation_service.go | 52 ++++++++++++++++++++++- modules/repository/star_service.go | 4 ++ routers/api/v1/user/star.go | 6 +-- routers/web/repo/repo.go | 5 ++- 6 files changed, 72 insertions(+), 76 deletions(-) create mode 100644 modules/repository/star_service.go diff --git a/models/repo/star.go b/models/repo/star.go index 61df584173..b2e729f5de 100644 --- a/models/repo/star.go +++ b/models/repo/star.go @@ -6,15 +6,10 @@ package repo import ( "context" - "time" "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/activitypub" - "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" ) @@ -30,22 +25,7 @@ func init() { db.RegisterModel(new(Star)) } -func StarRepo(ctx context.Context, doer user_model.User, repoID int64, star bool) error { - if err := starLocalRepo(ctx, doer.ID, repoID, star); err != nil { - return err - } - - if star && setting.Federation.Enabled { - if err := sendLikeActivities(ctx, doer, repoID); err != nil { - return err - } - } - - return nil -} - -// StarRepo or unstar repository. -func starLocalRepo(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) if err != nil { return err @@ -86,43 +66,6 @@ func starLocalRepo(ctx context.Context, userID, repoID int64, star bool) error { return committer.Commit() } -// ToDo: Move to federation service or simillar -func sendLikeActivities(ctx context.Context, doer user_model.User, repoID int64) error { - - federatedRepos, err := FindFederatedReposByRepoID(ctx, repoID) - log.Info("Federated Repos is: %v", federatedRepos) - if err != nil { - return err - } - - /*apclient, err := activitypub.NewClient(ctx, &doer, doer.APAPIURL()) - if err != nil { - return err - }*/ - - for _, federatedRepo := range federatedRepos { - target := federatedRepo.Uri + "/inbox/" // A like goes to the inbox of the federated repo - log.Info("Federated Repo URI is: %v", target) - likeActivity, err := forgefed.NewForgeLike(doer.APAPIURL(), target, time.Now()) - if err != nil { - return err - } - log.Info("Like Activity: %v", likeActivity) - /*json, err := likeActivity.MarshalJSON() - if err != nil { - return err - }*/ - - // TODO: decouple loading & creating activities from sending them - use two loops. - // 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) - } - - 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/models/repo/star_test.go b/models/repo/star_test.go index f90e8883bb..62eac4e29a 100644 --- a/models/repo/star_test.go +++ b/models/repo/star_test.go @@ -9,22 +9,21 @@ import ( "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" - "code.gitea.io/gitea/models/user" "github.com/stretchr/testify/assert" ) func TestStarRepo(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) - doer := user.User{ID: 2} + const userID = 2 const repoID = 1 - unittest.AssertNotExistsBean(t, &repo_model.Star{UID: doer.ID, RepoID: repoID}) - assert.NoError(t, repo_model.StarRepo(db.DefaultContext, doer, repoID, true)) - unittest.AssertExistsAndLoadBean(t, &repo_model.Star{UID: doer.ID, RepoID: repoID}) - assert.NoError(t, repo_model.StarRepo(db.DefaultContext, doer, repoID, true)) - unittest.AssertExistsAndLoadBean(t, &repo_model.Star{UID: doer.ID, RepoID: repoID}) - assert.NoError(t, repo_model.StarRepo(db.DefaultContext, doer, repoID, false)) - unittest.AssertNotExistsBean(t, &repo_model.Star{UID: doer.ID, RepoID: repoID}) + unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) + assert.NoError(t, repo_model.StarRepo(db.DefaultContext, userID, repoID, true)) + unittest.AssertExistsAndLoadBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) + assert.NoError(t, repo_model.StarRepo(db.DefaultContext, userID, repoID, true)) + unittest.AssertExistsAndLoadBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) + assert.NoError(t, repo_model.StarRepo(db.DefaultContext, userID, repoID, false)) + unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) } func TestIsStaring(t *testing.T) { @@ -56,12 +55,11 @@ func TestRepository_GetStargazers2(t *testing.T) { func TestClearRepoStars(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) const userID = 2 - doer := user.User{ID: userID} const repoID = 1 unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) - assert.NoError(t, repo_model.StarRepo(db.DefaultContext, doer, repoID, true)) + assert.NoError(t, repo_model.StarRepo(db.DefaultContext, userID, repoID, true)) unittest.AssertExistsAndLoadBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) - assert.NoError(t, repo_model.StarRepo(db.DefaultContext, doer, repoID, false)) + assert.NoError(t, repo_model.StarRepo(db.DefaultContext, userID, repoID, false)) unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) assert.NoError(t, repo_model.ClearRepoStars(db.DefaultContext, repoID)) unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) diff --git a/modules/forgefed/federation_service.go b/modules/forgefed/federation_service.go index 1751d6958e..9fe07b8b47 100644 --- a/modules/forgefed/federation_service.go +++ b/modules/forgefed/federation_service.go @@ -9,6 +9,7 @@ import ( "net/http" "net/url" "strings" + "time" "code.gitea.io/gitea/models/forgefed" "code.gitea.io/gitea/models/repo" @@ -82,7 +83,7 @@ func LikeActivity(ctx context.Context, form any, repositoryID int64) (int, strin // execute the activity if the repo was not stared already alreadyStared := repo.IsStaring(ctx, user.ID, repositoryID) if !alreadyStared { - err = repo.StarRepo(ctx, *user, repositoryID, true) + err = StarRepoAndFederate(ctx, *user, repositoryID, true) if err != nil { return http.StatusNotAcceptable, "Error staring", err } @@ -236,3 +237,52 @@ func StoreFederatedRepoList(ctx context.Context, localRepoId int64, federatedRep return 0, "", nil } + +func SendLikeActivities(ctx context.Context, doer user.User, repoID int64) error { + federatedRepos, err := repo.FindFederatedReposByRepoID(ctx, repoID) + log.Info("Federated Repos is: %v", federatedRepos) + if err != nil { + return err + } + + apclient, err := activitypub.NewClient(ctx, &doer, doer.APAPIURL()) + if err != nil { + return err + } + + for _, federatedRepo := range federatedRepos { + target := federatedRepo.Uri + "/inbox/" // A like goes to the inbox of the federated repo + log.Info("Federated Repo URI is: %v", target) + likeActivity, err := forgefed.NewForgeLike(doer.APAPIURL(), target, time.Now()) + if err != nil { + return err + } + log.Info("Like Activity: %v", likeActivity) + json, err := likeActivity.MarshalJSON() + if err != nil { + return err + } + + // TODO: decouple loading & creating activities from sending them - use two loops. + // 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) + } + + return nil +} + +func StarRepoAndFederate(ctx context.Context, doer user.User, repoID int64, star bool) error { + if err := repo.StarRepo(ctx, doer.ID, repoID, star); err != nil { + return err + } + + if star && setting.Federation.Enabled { + if err := SendLikeActivities(ctx, doer, repoID); err != nil { + return err + } + } + + return nil +} diff --git a/modules/repository/star_service.go b/modules/repository/star_service.go new file mode 100644 index 0000000000..1dc74804ae --- /dev/null +++ b/modules/repository/star_service.go @@ -0,0 +1,4 @@ +// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved. +// SPDX-License-Identifier: MIT + +package repository diff --git a/routers/api/v1/user/star.go b/routers/api/v1/user/star.go index e0131dfe28..2e37499c59 100644 --- a/routers/api/v1/user/star.go +++ b/routers/api/v1/user/star.go @@ -14,6 +14,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/forgefed" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/routers/api/v1/utils" "code.gitea.io/gitea/services/convert" @@ -156,8 +157,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, ctx.Repo.Repository.ID, true) + err := forgefed.StarRepoAndFederate(ctx, *ctx.Doer, ctx.Repo.Repository.ID, true) if err != nil { ctx.Error(http.StatusInternalServerError, "StarRepo", err) return @@ -188,7 +188,7 @@ func Unstar(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" - err := repo_model.StarRepo(ctx, *ctx.Doer, ctx.Repo.Repository.ID, false) + err := forgefed.StarRepoAndFederate(ctx, *ctx.Doer, ctx.Repo.Repository.ID, false) if err != nil { ctx.Error(http.StatusInternalServerError, "StarRepo", err) return diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index fbc267a3cb..06adc26254 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -22,6 +22,7 @@ import ( "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/forgefed" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" repo_module "code.gitea.io/gitea/modules/repository" @@ -316,9 +317,9 @@ func Action(ctx *context.Context) { case "unwatch": err = repo_model.WatchRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, false) case "star": - err = repo_model.StarRepo(ctx, *ctx.Doer, ctx.Repo.Repository.ID, true) + err = forgefed.StarRepoAndFederate(ctx, *ctx.Doer, ctx.Repo.Repository.ID, true) case "unstar": - err = repo_model.StarRepo(ctx, *ctx.Doer, ctx.Repo.Repository.ID, false) + err = forgefed.StarRepoAndFederate(ctx, *ctx.Doer, ctx.Repo.Repository.ID, false) case "accept_transfer": err = acceptOrRejectRepoTransfer(ctx, true) case "reject_transfer":