mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-23 11:01:49 +01:00
Merge branch 'forgejo-federated-star' of codeberg.org:meissa/forgejo into forgejo-federated-star
This commit is contained in:
commit
fc8e2b3f8a
6 changed files with 41 additions and 27 deletions
|
@ -10,8 +10,6 @@ import (
|
|||
"code.gitea.io/gitea/models/db"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
|
||||
//"code.gitea.io/gitea/modules/activitypub"
|
||||
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
)
|
||||
|
||||
|
@ -27,8 +25,7 @@ func init() {
|
|||
db.RegisterModel(new(Star))
|
||||
}
|
||||
|
||||
// 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
|
||||
|
|
|
@ -15,15 +15,15 @@ import (
|
|||
|
||||
func TestStarRepo(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
const doerID = 2
|
||||
const userID = 2
|
||||
const repoID = 1
|
||||
unittest.AssertNotExistsBean(t, &repo_model.Star{UID: doerID, RepoID: repoID})
|
||||
assert.NoError(t, repo_model.StarLocalRepo(db.DefaultContext, doerID, repoID, true))
|
||||
unittest.AssertExistsAndLoadBean(t, &repo_model.Star{UID: doerID, RepoID: repoID})
|
||||
assert.NoError(t, repo_model.StarLocalRepo(db.DefaultContext, doerID, repoID, true))
|
||||
unittest.AssertExistsAndLoadBean(t, &repo_model.Star{UID: doerID, RepoID: repoID})
|
||||
assert.NoError(t, repo_model.StarLocalRepo(db.DefaultContext, doerID, repoID, false))
|
||||
unittest.AssertNotExistsBean(t, &repo_model.Star{UID: doerID, 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) {
|
||||
|
@ -57,9 +57,9 @@ func TestClearRepoStars(t *testing.T) {
|
|||
const userID = 2
|
||||
const repoID = 1
|
||||
unittest.AssertNotExistsBean(t, &repo_model.Star{UID: userID, RepoID: repoID})
|
||||
assert.NoError(t, repo_model.StarLocalRepo(db.DefaultContext, userID, 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.StarLocalRepo(db.DefaultContext, userID, 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})
|
||||
|
|
4
modules/repository/star_service.go
Normal file
4
modules/repository/star_service.go
Normal file
|
@ -0,0 +1,4 @@
|
|||
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package repository
|
|
@ -14,10 +14,10 @@ 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"
|
||||
"code.gitea.io/gitea/services/repository"
|
||||
)
|
||||
|
||||
// getStarredRepos returns the repos that the user with the specified userID has
|
||||
|
@ -157,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 := repository.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
|
||||
|
@ -189,7 +188,7 @@ func Unstar(ctx *context.APIContext) {
|
|||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
|
||||
err := repository.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
|
||||
|
|
|
@ -23,6 +23,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"
|
||||
|
@ -317,9 +318,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_service.StarRepo(ctx, *ctx.Doer, ctx.Repo.Repository.ID, true)
|
||||
err = forgefed.StarRepoAndFederate(ctx, *ctx.Doer, ctx.Repo.Repository.ID, true)
|
||||
case "unstar":
|
||||
err = repo_service.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":
|
||||
|
|
|
@ -82,7 +82,7 @@ func ProcessLikeActivity(ctx context.Context, form any, repositoryID int64) (int
|
|||
// execute the activity if the repo was not stared already
|
||||
alreadyStared := repo.IsStaring(ctx, user.ID, repositoryID)
|
||||
if !alreadyStared {
|
||||
err = repo.StarLocalRepo(ctx, user.ID, repositoryID, true)
|
||||
err = StarRepoAndFederate(ctx, *user, repositoryID, true)
|
||||
if err != nil {
|
||||
return http.StatusNotAcceptable, "Error staring", err
|
||||
}
|
||||
|
@ -238,17 +238,16 @@ func StoreFederatedRepoList(ctx context.Context, localRepoId int64, federatedRep
|
|||
}
|
||||
|
||||
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())
|
||||
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
|
||||
|
@ -258,16 +257,30 @@ func SendLikeActivities(ctx context.Context, doer user.User, repoID int64) error
|
|||
return err
|
||||
}
|
||||
log.Info("Like Activity: %v", likeActivity)
|
||||
/*json, err := likeActivity.MarshalJSON()
|
||||
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)
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue