api: remove disabled stars check from models

also return 403 Forbidden instead of 410 Gone
pull/33208/head
HeCorr 2 weeks ago
parent c49823c2b6
commit 2c5d55397e
No known key found for this signature in database
GPG Key ID: 9333B03459C34098

@ -8,7 +8,6 @@ import (
"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
)
@ -25,12 +24,7 @@ func init() {
}
// StarRepo or unstar repository.
//
// Will do nothing if stars are disabled.
func StarRepo(ctx context.Context, doer *user_model.User, repo *Repository, star bool) error {
if setting.Repository.DisableStars {
return nil
}
ctx, committer, err := db.TxContext(ctx)
if err != nil {
return err
@ -76,23 +70,13 @@ func StarRepo(ctx context.Context, doer *user_model.User, repo *Repository, star
}
// IsStaring checks if user has starred given repository.
//
// Will always return false if stars are disabled.
func IsStaring(ctx context.Context, userID, repoID int64) bool {
if setting.Repository.DisableStars {
return false
}
has, _ := db.GetEngine(ctx).Get(&Star{UID: userID, RepoID: repoID})
return has
}
// GetStargazers returns the users that starred the repo.
//
// Will always return an empty slice if stars are disabled.
func GetStargazers(ctx context.Context, repo *Repository, opts db.ListOptions) ([]*user_model.User, error) {
if setting.Repository.DisableStars {
return make([]*user_model.User, 0), nil
}
sess := db.GetEngine(ctx).Where("star.repo_id = ?", repo.ID).
Join("LEFT", "star", "`user`.id = star.uid")
if opts.Page > 0 {

@ -45,11 +45,11 @@ func ListStargazers(ctx *context.APIContext) {
// "$ref": "#/responses/UserList"
// "404":
// "$ref": "#/responses/notFound"
// "410":
// "$ref": "#/responses/gone"
// "403":
// "$ref": "#/responses/forbidden"
if setting.Repository.DisableStars {
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
ctx.Error(http.StatusForbidden, "StarsDisabled", "Stars are disabled.")
return
}

@ -67,11 +67,11 @@ func GetStarredRepos(ctx *context.APIContext) {
// "$ref": "#/responses/RepositoryList"
// "404":
// "$ref": "#/responses/notFound"
// "410":
// "$ref": "#/responses/gone"
// "403":
// "$ref": "#/responses/forbidden"
if setting.Repository.DisableStars {
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
ctx.Error(http.StatusForbidden, "StarsDisabled", "Stars are disabled.")
return
}
@ -105,11 +105,11 @@ func GetMyStarredRepos(ctx *context.APIContext) {
// responses:
// "200":
// "$ref": "#/responses/RepositoryList"
// "410":
// "$ref": "#/responses/gone"
// "403":
// "$ref": "#/responses/forbidden"
if setting.Repository.DisableStars {
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
ctx.Error(http.StatusForbidden, "StarsDisabled", "Stars are disabled.")
return
}
@ -143,11 +143,11 @@ func IsStarring(ctx *context.APIContext) {
// "$ref": "#/responses/empty"
// "404":
// "$ref": "#/responses/notFound"
// "410":
// "$ref": "#/responses/gone"
// "403":
// "$ref": "#/responses/forbidden"
if setting.Repository.DisableStars {
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
ctx.Error(http.StatusForbidden, "StarsDisabled", "Stars are disabled.")
return
}
@ -181,11 +181,9 @@ func Star(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
// "410":
// "$ref": "#/responses/gone"
if setting.Repository.DisableStars {
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
ctx.Error(http.StatusForbidden, "StarsDisabled", "Stars are disabled.")
return
}
@ -222,11 +220,11 @@ func Unstar(ctx *context.APIContext) {
// "$ref": "#/responses/empty"
// "404":
// "$ref": "#/responses/notFound"
// "410":
// "$ref": "#/responses/gone"
// "403":
// "$ref": "#/responses/forbidden"
if setting.Repository.DisableStars {
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
ctx.Error(http.StatusForbidden, "StarsDisabled", "Stars are disabled.")
return
}

@ -317,10 +317,13 @@ func Action(ctx *context.Context) {
err = repo_model.WatchRepo(ctx, ctx.Doer, ctx.Repo.Repository, true)
case "unwatch":
err = repo_model.WatchRepo(ctx, ctx.Doer, ctx.Repo.Repository, false)
case "star":
err = repo_model.StarRepo(ctx, ctx.Doer, ctx.Repo.Repository, true)
case "unstar":
err = repo_model.StarRepo(ctx, ctx.Doer, ctx.Repo.Repository, false)
case "star", "unstar":
if setting.Repository.DisableStars {
err = errors.New("stars are disabled")
} else {
err = repo_model.StarRepo(ctx, ctx.Doer,
ctx.Repo.Repository, ctx.PathParam("action") == "star")
}
case "accept_transfer":
err = acceptOrRejectRepoTransfer(ctx, true)
case "reject_transfer":
@ -349,7 +352,11 @@ func Action(ctx *context.Context) {
case "watch", "unwatch":
ctx.Data["IsWatchingRepo"] = repo_model.IsWatching(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID)
case "star", "unstar":
ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID)
if setting.Repository.DisableStars {
ctx.Data["IsStaringRepo"] = false
} else {
ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID)
}
}
// see the `hx-trigger="refreshUserCards ..."` comments in tmpl
@ -370,7 +377,9 @@ func Action(ctx *context.Context) {
ctx.HTML(http.StatusOK, tplWatchUnwatch)
return
case "star", "unstar":
ctx.HTML(http.StatusOK, tplStarUnstar)
if !setting.Repository.DisableStars {
ctx.HTML(http.StatusOK, tplStarUnstar)
}
return
}

Loading…
Cancel
Save