1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-22 08:42:32 -05:00

Moved sendLikeActivities to federation_service

This commit is contained in:
Clemens 2024-03-28 14:27:50 +01:00
parent 1ad7bdf983
commit 7f19247b0a
6 changed files with 72 additions and 76 deletions

View file

@ -6,15 +6,10 @@ package repo
import ( import (
"context" "context"
"time"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/forgefed"
user_model "code.gitea.io/gitea/models/user" 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" "code.gitea.io/gitea/modules/timeutil"
) )
@ -30,22 +25,7 @@ func init() {
db.RegisterModel(new(Star)) db.RegisterModel(new(Star))
} }
func StarRepo(ctx context.Context, doer user_model.User, repoID int64, star bool) error { func StarRepo(ctx context.Context, userID, 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 {
ctx, committer, err := db.TxContext(ctx) ctx, committer, err := db.TxContext(ctx)
if err != nil { if err != nil {
return err return err
@ -86,43 +66,6 @@ func starLocalRepo(ctx context.Context, userID, repoID int64, star bool) error {
return committer.Commit() 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. // IsStaring checks if user has starred given repository.
func IsStaring(ctx context.Context, userID, repoID int64) bool { func IsStaring(ctx context.Context, userID, repoID int64) bool {
has, _ := db.GetEngine(ctx).Get(&Star{UID: userID, RepoID: repoID}) has, _ := db.GetEngine(ctx).Get(&Star{UID: userID, RepoID: repoID})

View file

@ -9,22 +9,21 @@ import (
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/models/user"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestStarRepo(t *testing.T) { func TestStarRepo(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, unittest.PrepareTestDatabase())
doer := user.User{ID: 2} const userID = 2
const repoID = 1 const repoID = 1
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, doer, repoID, true)) assert.NoError(t, repo_model.StarRepo(db.DefaultContext, userID, repoID, true))
unittest.AssertExistsAndLoadBean(t, &repo_model.Star{UID: doer.ID, RepoID: repoID}) unittest.AssertExistsAndLoadBean(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: doer.ID, RepoID: repoID}) 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: doer.ID, RepoID: repoID}) unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID})
} }
func TestIsStaring(t *testing.T) { func TestIsStaring(t *testing.T) {
@ -56,12 +55,11 @@ func TestRepository_GetStargazers2(t *testing.T) {
func TestClearRepoStars(t *testing.T) { func TestClearRepoStars(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase()) assert.NoError(t, unittest.PrepareTestDatabase())
const userID = 2 const userID = 2
doer := user.User{ID: userID}
const repoID = 1 const repoID = 1
unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) 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}) 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}) unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID})
assert.NoError(t, repo_model.ClearRepoStars(db.DefaultContext, repoID)) assert.NoError(t, repo_model.ClearRepoStars(db.DefaultContext, repoID))
unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID}) unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID})

View file

@ -9,6 +9,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"time"
"code.gitea.io/gitea/models/forgefed" "code.gitea.io/gitea/models/forgefed"
"code.gitea.io/gitea/models/repo" "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 // execute the activity if the repo was not stared already
alreadyStared := repo.IsStaring(ctx, user.ID, repositoryID) alreadyStared := repo.IsStaring(ctx, user.ID, repositoryID)
if !alreadyStared { if !alreadyStared {
err = repo.StarRepo(ctx, *user, repositoryID, true) err = StarRepoAndFederate(ctx, *user, repositoryID, true)
if err != nil { if err != nil {
return http.StatusNotAcceptable, "Error staring", err return http.StatusNotAcceptable, "Error staring", err
} }
@ -236,3 +237,52 @@ func StoreFederatedRepoList(ctx context.Context, localRepoId int64, federatedRep
return 0, "", nil 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
}

View file

@ -0,0 +1,4 @@
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
// SPDX-License-Identifier: MIT
package repository

View file

@ -14,6 +14,7 @@ import (
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/forgefed"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils" "code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/services/convert" "code.gitea.io/gitea/services/convert"
@ -156,8 +157,7 @@ func Star(ctx *context.APIContext) {
// "404": // "404":
// "$ref": "#/responses/notFound" // "$ref": "#/responses/notFound"
// TODO: why is this *context.APIContext passed, where a context.Context is expected? err := forgefed.StarRepoAndFederate(ctx, *ctx.Doer, ctx.Repo.Repository.ID, true)
err := repo_model.StarRepo(ctx, *ctx.Doer, ctx.Repo.Repository.ID, true)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "StarRepo", err) ctx.Error(http.StatusInternalServerError, "StarRepo", err)
return return
@ -188,7 +188,7 @@ func Unstar(ctx *context.APIContext) {
// "404": // "404":
// "$ref": "#/responses/notFound" // "$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 { if err != nil {
ctx.Error(http.StatusInternalServerError, "StarRepo", err) ctx.Error(http.StatusInternalServerError, "StarRepo", err)
return return

View file

@ -22,6 +22,7 @@ import (
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/forgefed"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
repo_module "code.gitea.io/gitea/modules/repository" repo_module "code.gitea.io/gitea/modules/repository"
@ -316,9 +317,9 @@ func Action(ctx *context.Context) {
case "unwatch": case "unwatch":
err = repo_model.WatchRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, false) err = repo_model.WatchRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, false)
case "star": 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": 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": case "accept_transfer":
err = acceptOrRejectRepoTransfer(ctx, true) err = acceptOrRejectRepoTransfer(ctx, true)
case "reject_transfer": case "reject_transfer":