Skip to content

Commit 2c5d553

Browse files
committed
api: remove disabled stars check from models
also return 403 Forbidden instead of 410 Gone
1 parent c49823c commit 2c5d553

File tree

4 files changed

+31
-40
lines changed

4 files changed

+31
-40
lines changed

models/repo/star.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"code.gitea.io/gitea/models/db"
1010
user_model "code.gitea.io/gitea/models/user"
11-
"code.gitea.io/gitea/modules/setting"
1211
"code.gitea.io/gitea/modules/timeutil"
1312
)
1413

@@ -25,12 +24,7 @@ func init() {
2524
}
2625

2726
// StarRepo or unstar repository.
28-
//
29-
// Will do nothing if stars are disabled.
3027
func StarRepo(ctx context.Context, doer *user_model.User, repo *Repository, star bool) error {
31-
if setting.Repository.DisableStars {
32-
return nil
33-
}
3428
ctx, committer, err := db.TxContext(ctx)
3529
if err != nil {
3630
return err
@@ -76,23 +70,13 @@ func StarRepo(ctx context.Context, doer *user_model.User, repo *Repository, star
7670
}
7771

7872
// IsStaring checks if user has starred given repository.
79-
//
80-
// Will always return false if stars are disabled.
8173
func IsStaring(ctx context.Context, userID, repoID int64) bool {
82-
if setting.Repository.DisableStars {
83-
return false
84-
}
8574
has, _ := db.GetEngine(ctx).Get(&Star{UID: userID, RepoID: repoID})
8675
return has
8776
}
8877

8978
// GetStargazers returns the users that starred the repo.
90-
//
91-
// Will always return an empty slice if stars are disabled.
9279
func GetStargazers(ctx context.Context, repo *Repository, opts db.ListOptions) ([]*user_model.User, error) {
93-
if setting.Repository.DisableStars {
94-
return make([]*user_model.User, 0), nil
95-
}
9680
sess := db.GetEngine(ctx).Where("star.repo_id = ?", repo.ID).
9781
Join("LEFT", "star", "`user`.id = star.uid")
9882
if opts.Page > 0 {

routers/api/v1/repo/star.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ func ListStargazers(ctx *context.APIContext) {
4545
// "$ref": "#/responses/UserList"
4646
// "404":
4747
// "$ref": "#/responses/notFound"
48-
// "410":
49-
// "$ref": "#/responses/gone"
48+
// "403":
49+
// "$ref": "#/responses/forbidden"
5050

5151
if setting.Repository.DisableStars {
52-
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
52+
ctx.Error(http.StatusForbidden, "StarsDisabled", "Stars are disabled.")
5353
return
5454
}
5555

routers/api/v1/user/star.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ func GetStarredRepos(ctx *context.APIContext) {
6767
// "$ref": "#/responses/RepositoryList"
6868
// "404":
6969
// "$ref": "#/responses/notFound"
70-
// "410":
71-
// "$ref": "#/responses/gone"
70+
// "403":
71+
// "$ref": "#/responses/forbidden"
7272

7373
if setting.Repository.DisableStars {
74-
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
74+
ctx.Error(http.StatusForbidden, "StarsDisabled", "Stars are disabled.")
7575
return
7676
}
7777

@@ -105,11 +105,11 @@ func GetMyStarredRepos(ctx *context.APIContext) {
105105
// responses:
106106
// "200":
107107
// "$ref": "#/responses/RepositoryList"
108-
// "410":
109-
// "$ref": "#/responses/gone"
108+
// "403":
109+
// "$ref": "#/responses/forbidden"
110110

111111
if setting.Repository.DisableStars {
112-
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
112+
ctx.Error(http.StatusForbidden, "StarsDisabled", "Stars are disabled.")
113113
return
114114
}
115115

@@ -143,11 +143,11 @@ func IsStarring(ctx *context.APIContext) {
143143
// "$ref": "#/responses/empty"
144144
// "404":
145145
// "$ref": "#/responses/notFound"
146-
// "410":
147-
// "$ref": "#/responses/gone"
146+
// "403":
147+
// "$ref": "#/responses/forbidden"
148148

149149
if setting.Repository.DisableStars {
150-
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
150+
ctx.Error(http.StatusForbidden, "StarsDisabled", "Stars are disabled.")
151151
return
152152
}
153153

@@ -181,11 +181,9 @@ func Star(ctx *context.APIContext) {
181181
// "$ref": "#/responses/forbidden"
182182
// "404":
183183
// "$ref": "#/responses/notFound"
184-
// "410":
185-
// "$ref": "#/responses/gone"
186184

187185
if setting.Repository.DisableStars {
188-
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
186+
ctx.Error(http.StatusForbidden, "StarsDisabled", "Stars are disabled.")
189187
return
190188
}
191189

@@ -222,11 +220,11 @@ func Unstar(ctx *context.APIContext) {
222220
// "$ref": "#/responses/empty"
223221
// "404":
224222
// "$ref": "#/responses/notFound"
225-
// "410":
226-
// "$ref": "#/responses/gone"
223+
// "403":
224+
// "$ref": "#/responses/forbidden"
227225

228226
if setting.Repository.DisableStars {
229-
ctx.Error(http.StatusGone, "StarsDisabled", "Stars are disabled.")
227+
ctx.Error(http.StatusForbidden, "StarsDisabled", "Stars are disabled.")
230228
return
231229
}
232230

routers/web/repo/repo.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,13 @@ func Action(ctx *context.Context) {
317317
err = repo_model.WatchRepo(ctx, ctx.Doer, ctx.Repo.Repository, true)
318318
case "unwatch":
319319
err = repo_model.WatchRepo(ctx, ctx.Doer, ctx.Repo.Repository, false)
320-
case "star":
321-
err = repo_model.StarRepo(ctx, ctx.Doer, ctx.Repo.Repository, true)
322-
case "unstar":
323-
err = repo_model.StarRepo(ctx, ctx.Doer, ctx.Repo.Repository, false)
320+
case "star", "unstar":
321+
if setting.Repository.DisableStars {
322+
err = errors.New("stars are disabled")
323+
} else {
324+
err = repo_model.StarRepo(ctx, ctx.Doer,
325+
ctx.Repo.Repository, ctx.PathParam("action") == "star")
326+
}
324327
case "accept_transfer":
325328
err = acceptOrRejectRepoTransfer(ctx, true)
326329
case "reject_transfer":
@@ -349,7 +352,11 @@ func Action(ctx *context.Context) {
349352
case "watch", "unwatch":
350353
ctx.Data["IsWatchingRepo"] = repo_model.IsWatching(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID)
351354
case "star", "unstar":
352-
ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID)
355+
if setting.Repository.DisableStars {
356+
ctx.Data["IsStaringRepo"] = false
357+
} else {
358+
ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID)
359+
}
353360
}
354361

355362
// see the `hx-trigger="refreshUserCards ..."` comments in tmpl
@@ -370,7 +377,9 @@ func Action(ctx *context.Context) {
370377
ctx.HTML(http.StatusOK, tplWatchUnwatch)
371378
return
372379
case "star", "unstar":
373-
ctx.HTML(http.StatusOK, tplStarUnstar)
380+
if !setting.Repository.DisableStars {
381+
ctx.HTML(http.StatusOK, tplStarUnstar)
382+
}
374383
return
375384
}
376385

0 commit comments

Comments
 (0)