1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-21 08:31:27 -05:00

moved functionality

import of forgefed causes circular dependencies. This has to be solved!
This commit is contained in:
Clemens 2024-03-26 10:23:14 +01:00
parent d251fc2611
commit 7f0371056e
2 changed files with 42 additions and 31 deletions

View file

@ -7,7 +7,9 @@ import (
"context" "context"
"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/setting"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
) )
@ -23,6 +25,7 @@ func init() {
db.RegisterModel(new(Star)) db.RegisterModel(new(Star))
} }
// TODO: why is this ctx not type *context.APIContext, as in routers/api/v1/user/star.go?
// StarRepo or unstar repository. // StarRepo or unstar repository.
func StarRepo(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) ctx, committer, err := db.TxContext(ctx)
@ -46,6 +49,9 @@ func StarRepo(ctx context.Context, userID, repoID int64, star bool) error {
if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID); err != nil { if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID); err != nil {
return err return err
} }
if err := SendLikeActivities(ctx, userID); err != nil {
return err
}
} else { } else {
if !staring { if !staring {
return nil return nil
@ -65,6 +71,42 @@ func StarRepo(ctx context.Context, userID, repoID int64, star bool) error {
return committer.Commit() return committer.Commit()
} }
// TODO: solve circular dependencies caused by import of forgefed!
func SendLikeActivities(ctx context.Context, userID int64) error {
if setting.Federation.Enabled {
/*likeActivity, err := forgefed.NewForgeLike(ctx)
if err != nil {
return err
}
json, err := likeActivity.MarshalJSON()
if err != nil {
return err
}
// TODO: is user loading necessary here?
user, err := user_model.GetUserByID(ctx, userID)
if err != nil {
return err
}
apclient, err := activitypub.NewClient(ctx, user, user.APAPIURL())
if err != nil {
return err
}
// TODO: set timeouts for outgoing request in oder to mitigate DOS by slow lories
// ToDo: Change this to the standalone table of FederatedRepos
for _, target := range strings.Split(ctx.Repo.Repository.FederationRepos, ";") {
apclient.Post([]byte(json), target)
}
*/
// Send to list of federated repos
}
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

@ -8,16 +8,12 @@ package user
import ( import (
std_context "context" std_context "context"
"net/http" "net/http"
"strings"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/forgefed"
access_model "code.gitea.io/gitea/models/perm/access" access_model "code.gitea.io/gitea/models/perm/access"
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/activitypub"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting"
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"
@ -165,34 +161,7 @@ func Star(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "StarRepo", err) ctx.Error(http.StatusInternalServerError, "StarRepo", err)
return return
} }
// TODO: move this code to repo.federated_repository
if setting.Federation.Enabled {
likeActivity, err := forgefed.NewForgeLike(ctx)
if err != nil {
ctx.Error(http.StatusInternalServerError, "StarRepo", err)
return
}
json, err := likeActivity.MarshalJSON()
if err != nil {
ctx.Error(http.StatusInternalServerError, "StarRepo", err)
return
}
apclient, err := activitypub.NewClient(ctx, ctx.Doer, ctx.Doer.APAPIURL())
if err != nil {
ctx.Error(http.StatusInternalServerError, "StarRepo", err)
return
}
// TODO: set timeouts for outgoing request in oder to mitigate DOS by slow lories
// ToDo: Change this to the standalone table of FederatedRepos
for _, target := range strings.Split(ctx.Repo.Repository.FederationRepos, ";") {
apclient.Post([]byte(json), target)
}
// Send to list of federated repos
}
ctx.Status(http.StatusNoContent) ctx.Status(http.StatusNoContent)
} }