Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-22 09:54:24 +01:00

use doer coming from api

This commit is contained in:
Michael Jerger 2024-03-28 08:13:00 +01:00
parent 992fbf6898
commit d624ebb712
4 changed files with 20 additions and 27 deletions

View file

@ -29,13 +29,13 @@ func init() {
db.RegisterModel(new(Star)) db.RegisterModel(new(Star))
} }
func StarRepo(ctx context.Context, userID, repoID int64, star bool) error { func StarRepo(ctx context.Context, doer user_model.User, repoID int64, star bool) error {
if err := starLocalRepo(ctx, userID, repoID, star); err != nil { if err := starLocalRepo(ctx, doer.ID, repoID, star); err != nil {
return err return err
} }
if star && setting.Federation.Enabled { if star && setting.Federation.Enabled {
if err := sendLikeActivities(ctx, userID, repoID); err != nil { if err := sendLikeActivities(ctx, doer, repoID); err != nil {
return err return err
} }
} }
@ -86,15 +86,7 @@ func starLocalRepo(ctx context.Context, userID, repoID int64, star bool) error {
} }
// ToDo: Move to federation service or simillar // ToDo: Move to federation service or simillar
func sendLikeActivities(ctx context.Context, userID int64, repoID int64) error { func sendLikeActivities(ctx context.Context, doer user_model.User, repoID int64) error {
// TODO: is user loading necessary here? - imho no!
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) federatedRepos, err := FindFederatedReposByRepoID(ctx, repoID)
log.Info("Federated Repos is: %v", federatedRepos) log.Info("Federated Repos is: %v", federatedRepos)
@ -102,8 +94,7 @@ func sendLikeActivities(ctx context.Context, userID int64, repoID int64) error {
return err return err
} }
// TODO: That's wrong - we've to send the activities to repo not to user! apclient, err := activitypub.NewClient(ctx, &doer, doer.APAPIURL())
apclient, err := activitypub.NewClient(ctx, user, user.APAPIURL())
if err != nil { if err != nil {
return err return err
} }
@ -111,7 +102,7 @@ func sendLikeActivities(ctx context.Context, userID int64, repoID int64) error {
for _, federatedRepo := range federatedRepos { for _, federatedRepo := range federatedRepos {
target := federatedRepo.Uri + "/inbox/" // A like goes to the inbox of the federated repo target := federatedRepo.Uri + "/inbox/" // A like goes to the inbox of the federated repo
log.Info("Federated Repo URI is: %v", target) log.Info("Federated Repo URI is: %v", target)
likeActivity, err := forgefed.NewForgeLike(user.APAPIURL(), target, time.Now()) likeActivity, err := forgefed.NewForgeLike(doer.APAPIURL(), target, time.Now())
if err != nil { if err != nil {
return err return err
} }

View file

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

@ -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 // 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.ID, repositoryID, true) err = repo.StarRepo(ctx, user, repositoryID, true)
if err != nil { if err != nil {
return http.StatusNotAcceptable, "Error staring", err return http.StatusNotAcceptable, "Error staring", err
} }

View file

@ -157,7 +157,7 @@ func Star(ctx *context.APIContext) {
// "$ref": "#/responses/notFound" // "$ref": "#/responses/notFound"
// TODO: why is this *context.APIContext passed, where a context.Context is expected? // TODO: why is this *context.APIContext passed, where a context.Context is expected?
err := repo_model.StarRepo(ctx, ctx.Doer.ID, 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