refactor services

This commit is contained in:
Michael Jerger 2024-03-28 15:00:55 +01:00
parent 1ad7bdf983
commit 4d5c8b5563
8 changed files with 91 additions and 81 deletions

View file

@ -6,15 +6,12 @@ package repo
import (
"context"
"time"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/forgefed"
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"
)
@ -30,22 +27,8 @@ func init() {
db.RegisterModel(new(Star))
}
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, doer, repoID); err != nil {
return err
}
}
return nil
}
// StarRepo or unstar repository.
func starLocalRepo(ctx context.Context, userID, repoID int64, star bool) error {
func StarLocalRepo(ctx context.Context, userID, repoID int64, star bool) error {
ctx, committer, err := db.TxContext(ctx)
if err != nil {
return err
@ -86,43 +69,6 @@ func starLocalRepo(ctx context.Context, userID, repoID int64, star bool) error {
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.
func IsStaring(ctx context.Context, userID, repoID int64) bool {
has, _ := db.GetEngine(ctx).Get(&Star{UID: userID, RepoID: repoID})

View file

@ -9,22 +9,21 @@ 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())
doer := user.User{ID: 2}
const doerID = 2
const repoID = 1
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})
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})
}
func TestIsStaring(t *testing.T) {
@ -56,12 +55,11 @@ 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, doer, repoID, true))
assert.NoError(t, repo_model.StarLocalRepo(db.DefaultContext, userID, repoID, true))
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.StarLocalRepo(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})

View file

@ -10,10 +10,10 @@ import (
forgefed_model "code.gitea.io/gitea/models/forgefed"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/forgefed"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/services/federation"
ap "github.com/go-ap/activitypub"
)
@ -72,7 +72,7 @@ func RepositoryInbox(ctx *context.APIContext) {
log.Info("RepositoryInbox: repo: %v", repository)
form := web.GetForm(ctx)
httpStatus, title, err := forgefed.LikeActivity(ctx, form, repository.ID)
httpStatus, title, err := federation.ProcessLikeActivity(ctx, form, repository.ID)
if err != nil {
ctx.Error(httpStatus, title, err)
}

View file

@ -17,6 +17,7 @@ import (
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,7 +158,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, ctx.Repo.Repository.ID, true)
err := repository.StarRepo(ctx, *ctx.Doer, ctx.Repo.Repository.ID, true)
if err != nil {
ctx.Error(http.StatusInternalServerError, "StarRepo", err)
return
@ -188,7 +189,7 @@ func Unstar(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
err := repo_model.StarRepo(ctx, *ctx.Doer, ctx.Repo.Repository.ID, false)
err := repository.StarRepo(ctx, *ctx.Doer, ctx.Repo.Repository.ID, false)
if err != nil {
ctx.Error(http.StatusInternalServerError, "StarRepo", err)
return

View file

@ -1,5 +1,6 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2020 The Gitea Authors. All rights reserved.
// Copyright 2024 The forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
@ -316,9 +317,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_model.StarRepo(ctx, *ctx.Doer, ctx.Repo.Repository.ID, true)
err = repo_service.StarRepo(ctx, *ctx.Doer, ctx.Repo.Repository.ID, true)
case "unstar":
err = repo_model.StarRepo(ctx, *ctx.Doer, ctx.Repo.Repository.ID, false)
err = repo_service.StarRepo(ctx, *ctx.Doer, ctx.Repo.Repository.ID, false)
case "accept_transfer":
err = acceptOrRejectRepoTransfer(ctx, true)
case "reject_transfer":

View file

@ -1,5 +1,6 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2018 The Gitea Authors. All rights reserved.
// Copyright 2024 The forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package setting
@ -20,7 +21,6 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/forgefed"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/indexer/code"
"code.gitea.io/gitea/modules/indexer/stats"
@ -33,6 +33,7 @@ import (
"code.gitea.io/gitea/modules/validation"
"code.gitea.io/gitea/modules/web"
asymkey_service "code.gitea.io/gitea/services/asymkey"
"code.gitea.io/gitea/services/federation"
"code.gitea.io/gitea/services/forms"
"code.gitea.io/gitea/services/migrations"
mirror_service "code.gitea.io/gitea/services/mirror"
@ -204,7 +205,7 @@ func SettingsPost(ctx *context.Context) {
return
}
if _, _, err := forgefed.StoreFederatedRepoList(ctx, ctx.Repo.Repository.ID, strings.Split(federationRepos, ";")); err != nil {
if _, _, err := federation.StoreFederatedRepoList(ctx, ctx.Repo.Repository.ID, strings.Split(federationRepos, ";")); err != nil {
ctx.ServerError("UpdateRepository", err)
return
}

View file

@ -1,7 +1,7 @@
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package forgefed
package federation
import (
"context"
@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"strings"
"time"
"code.gitea.io/gitea/models/forgefed"
"code.gitea.io/gitea/models/repo"
@ -22,15 +23,14 @@ import (
"github.com/google/uuid"
)
// ToDo: May need to change the name to reflect workings of function better
// LikeActivity receives a ForgeLike activity and does the following:
// ProcessLikeActivity receives a ForgeLike activity and does the following:
// Validation of the activity
// Creation of a (remote) federationHost if not existing
// Creation of a forgefed Person if not existing
// Validation of incoming RepositoryID against Local RepositoryID
// Star the repo if it wasn't already stared
// Do some mitigation against out of order attacks
func LikeActivity(ctx context.Context, form any, repositoryID int64) (int, string, error) {
func ProcessLikeActivity(ctx context.Context, form any, repositoryID int64) (int, string, error) {
activity := form.(*forgefed.ForgeLike)
if res, err := validation.IsValid(activity); !res {
return http.StatusNotAcceptable, "Invalid activity", err
@ -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, repositoryID, true)
err = repo.StarLocalRepo(ctx, user.ID, repositoryID, true)
if err != nil {
return http.StatusNotAcceptable, "Error staring", err
}
@ -236,3 +236,39 @@ func StoreFederatedRepoList(ctx context.Context, localRepoId int64, federatedRep
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
}

View file

@ -0,0 +1,27 @@
// Copyright 2024 The forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repository
import (
"context"
"code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/federation"
)
func StarRepo(ctx context.Context, doer user.User, repoID int64, star bool) error {
if err := repo.StarLocalRepo(ctx, doer.ID, repoID, star); err != nil {
return err
}
if star && setting.Federation.Enabled {
if err := federation.SendLikeActivities(ctx, doer, repoID); err != nil {
return err
}
}
return nil
}