diff --git a/models/repo/star.go b/models/repo/star.go index 9c14e67bf0..c120eb9882 100644 --- a/models/repo/star.go +++ b/models/repo/star.go @@ -29,13 +29,13 @@ func init() { db.RegisterModel(new(Star)) } -func StarRepo(ctx context.Context, userID, repoID int64, star bool) error { - if err := starLocalRepo(ctx, userID, repoID, star); err != nil { +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, userID, repoID); err != nil { + if err := sendLikeActivities(ctx, doer, repoID); err != nil { return err } } @@ -86,15 +86,7 @@ func starLocalRepo(ctx context.Context, userID, repoID int64, star bool) error { } // ToDo: Move to federation service or simillar -func sendLikeActivities(ctx context.Context, userID int64, repoID int64) error { - - // TODO: is user loading necessary here? - imho no! - log.Info("User ID: %v, Repo ID: %v", userID, repoID) - user, err := user_model.GetUserByID(ctx, userID) - log.Info("User is: %v", user) - if err != nil { - return err - } +func sendLikeActivities(ctx context.Context, doer user_model.User, repoID int64) error { federatedRepos, err := FindFederatedReposByRepoID(ctx, repoID) log.Info("Federated Repos is: %v", federatedRepos) @@ -102,8 +94,7 @@ func sendLikeActivities(ctx context.Context, userID int64, repoID int64) error { return err } - // TODO: That's wrong - we've to send the activities to repo not to user! - apclient, err := activitypub.NewClient(ctx, user, user.APAPIURL()) + apclient, err := activitypub.NewClient(ctx, &doer, doer.APAPIURL()) if err != nil { return err } @@ -111,7 +102,7 @@ func sendLikeActivities(ctx context.Context, userID int64, repoID int64) error { 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(user.APAPIURL(), target, time.Now()) + likeActivity, err := forgefed.NewForgeLike(doer.APAPIURL(), target, time.Now()) if err != nil { return err } diff --git a/models/repo/star_test.go b/models/repo/star_test.go index 62eac4e29a..f90e8883bb 100644 --- a/models/repo/star_test.go +++ b/models/repo/star_test.go @@ -9,21 +9,22 @@ 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()) - const userID = 2 + doer := user.User{ID: 2} const repoID = 1 - 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}) + 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}) } func TestIsStaring(t *testing.T) { @@ -55,11 +56,12 @@ 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, userID, repoID, true)) + assert.NoError(t, repo_model.StarRepo(db.DefaultContext, doer, repoID, true)) unittest.AssertExistsAndLoadBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) - assert.NoError(t, repo_model.StarRepo(db.DefaultContext, userID, repoID, false)) + assert.NoError(t, repo_model.StarRepo(db.DefaultContext, doer, 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 713e4d1dbb..843a3f7cbd 100644 --- a/modules/forgefed/federation_service.go +++ b/modules/forgefed/federation_service.go @@ -82,7 +82,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.ID, repositoryID, true) + err = repo.StarRepo(ctx, user, repositoryID, true) if err != nil { return http.StatusNotAcceptable, "Error staring", err } diff --git a/routers/api/v1/user/star.go b/routers/api/v1/user/star.go index d45b1e6488..d5b36d8131 100644 --- a/routers/api/v1/user/star.go +++ b/routers/api/v1/user/star.go @@ -157,7 +157,7 @@ func Star(ctx *context.APIContext) { // "$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, ctx.Repo.Repository.ID, true) if err != nil { ctx.Error(http.StatusInternalServerError, "StarRepo", err) return