From 436466dcade0f919ff60f12abb41fee0f250abbc Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 26 Mar 2024 16:21:28 +0100 Subject: [PATCH] Check if federation enabled in StarRepo --- models/repo/star.go | 77 ++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/models/repo/star.go b/models/repo/star.go index 4394bb6804..4f3cd05631 100644 --- a/models/repo/star.go +++ b/models/repo/star.go @@ -32,7 +32,7 @@ func StarRepo(ctx context.Context, userID, repoID int64, star bool) error { return err } - if star { + if star && setting.Federation.Enabled { if err := sendLikeActivities(ctx, userID, repoID); err != nil { return err } @@ -85,46 +85,45 @@ 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: should this be checked somewhere else/outside? - if setting.Federation.Enabled { - // TODO: is user loading necessary here? - 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 - } - federatedRepos, err := FindFederatedReposByRepoID(ctx, repoID) - log.Info("Federated Repos is: %v", federatedRepos) - if err != nil { - return err - } - - apclient, err := activitypub.NewClient(ctx, user, user.APAPIURL()) - if err != nil { - return err - } - - for _, federatedRepo := range federatedRepos { - target := federatedRepo.Uri - log.Info("Federated Repo URI is: %v", target) - likeActivity, err := forgefed.NewForgeLike(user.APAPIURL(), target) - if err != nil { - return err - } - log.Info("Like Activity: %v", likeActivity) - 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) - } + // TODO: is user loading necessary here? + 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 } + + federatedRepos, err := FindFederatedReposByRepoID(ctx, repoID) + log.Info("Federated Repos is: %v", federatedRepos) + if err != nil { + return err + } + + apclient, err := activitypub.NewClient(ctx, user, user.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(user.APAPIURL(), target) + if err != nil { + return err + } + log.Info("Like Activity: %v", likeActivity) + 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) + } + return nil }