From b01f3bb2a1659b5a5ebde6909cc5f7934ef98a21 Mon Sep 17 00:00:00 2001 From: Michael Jerger Date: Fri, 24 May 2024 00:25:57 +0000 Subject: [PATCH] federated-star: finalize receive activity (#3871) PR will finalize the ability to receive a federated star from a remote instance. This is part of: https://codeberg.org/forgejo/forgejo/pulls/1680 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3871 Reviewed-by: Earl Warren Co-authored-by: Michael Jerger Co-committed-by: Michael Jerger --- .deadcode-out | 1 - services/federation/federation_service.go | 15 +++++++++++++++ .../api_activitypub_repository_test.go | 5 +++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/.deadcode-out b/.deadcode-out index 600f348859..51429164ae 100644 --- a/.deadcode-out +++ b/.deadcode-out @@ -45,7 +45,6 @@ package "code.gitea.io/gitea/models/dbfs" package "code.gitea.io/gitea/models/forgefed" func GetFederationHost - func UpdateFederationHost package "code.gitea.io/gitea/models/forgejo/semver" func GetVersion diff --git a/services/federation/federation_service.go b/services/federation/federation_service.go index 0e7efa133a..be2dc2eb6a 100644 --- a/services/federation/federation_service.go +++ b/services/federation/federation_service.go @@ -11,6 +11,7 @@ import ( "strings" "code.gitea.io/gitea/models/forgefed" + "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/activitypub" "code.gitea.io/gitea/modules/auth/password" @@ -78,6 +79,20 @@ func ProcessLikeActivity(ctx context.Context, form any, repositoryID int64) (int } log.Info("Got user:%v", user.Name) + // 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) + if err != nil { + return http.StatusNotAcceptable, "Error staring", err + } + } + federationHost.LatestActivity = activity.StartTime + err = forgefed.UpdateFederationHost(ctx, federationHost) + if err != nil { + return http.StatusNotAcceptable, "Error updating federatedHost", err + } + return 0, "", nil } diff --git a/tests/integration/api_activitypub_repository_test.go b/tests/integration/api_activitypub_repository_test.go index 203307a369..bccc594d07 100644 --- a/tests/integration/api_activitypub_repository_test.go +++ b/tests/integration/api_activitypub_repository_test.go @@ -203,6 +203,11 @@ func TestActivityPubRepositoryInboxValid(t *testing.T) { federatedUser = unittest.AssertExistsAndLoadBean(t, &user.FederatedUser{ExternalID: "30", FederationHostID: federationHost.ID}) unittest.AssertExistsAndLoadBean(t, &user.User{ID: federatedUser.UserID}) + + // Replay activity2. + resp, err = c.Post(activity2, repoInboxURL) + assert.NoError(t, err) + assert.Equal(t, http.StatusNotAcceptable, resp.StatusCode) }) }