Refactor routers/web/repo.Action
Split up `repo.Action` in `routers/web` into smaller functions. While some of the functionality was very similar (starring / watching), they are ultimately separate actions. Rather than collecting all of them under a single handler (`repo.Action`), split them up into smaller, independent functions. This does result in a little bit of code duplication, but the independent functions should be easier to follow and understand. Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
This commit is contained in:
parent
02eab930c9
commit
38e30f2a8f
2 changed files with 55 additions and 39 deletions
|
@ -307,57 +307,66 @@ const (
|
||||||
tplStarUnstar base.TplName = "repo/star_unstar"
|
tplStarUnstar base.TplName = "repo/star_unstar"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Action response for actions to a repository
|
func ActionWatch(watch bool) func(ctx *context.Context) {
|
||||||
func Action(ctx *context.Context) {
|
return func(ctx *context.Context) {
|
||||||
var err error
|
err := repo_model.WatchRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, watch)
|
||||||
switch ctx.Params(":action") {
|
|
||||||
case "watch":
|
|
||||||
err = repo_model.WatchRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, true)
|
|
||||||
case "unwatch":
|
|
||||||
err = repo_model.WatchRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, false)
|
|
||||||
case "star":
|
|
||||||
err = repo_model.StarRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, true)
|
|
||||||
case "unstar":
|
|
||||||
err = repo_model.StarRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, false)
|
|
||||||
case "accept_transfer":
|
|
||||||
err = acceptOrRejectRepoTransfer(ctx, true)
|
|
||||||
case "reject_transfer":
|
|
||||||
err = acceptOrRejectRepoTransfer(ctx, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.ServerError(fmt.Sprintf("Action (%s)", ctx.Params(":action")), err)
|
ctx.ServerError(fmt.Sprintf("Action (watch, %t)", watch), err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ctx.Params(":action") {
|
|
||||||
case "watch", "unwatch":
|
|
||||||
ctx.Data["IsWatchingRepo"] = repo_model.IsWatching(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID)
|
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ctx.Params(":action") {
|
|
||||||
case "watch", "unwatch", "star", "unstar":
|
|
||||||
// we have to reload the repository because NumStars or NumWatching (used in the templates) has just changed
|
// we have to reload the repository because NumStars or NumWatching (used in the templates) has just changed
|
||||||
ctx.Data["Repository"], err = repo_model.GetRepositoryByName(ctx, ctx.Repo.Repository.OwnerID, ctx.Repo.Repository.Name)
|
ctx.Data["Repository"], err = repo_model.GetRepositoryByName(ctx, ctx.Repo.Repository.OwnerID, ctx.Repo.Repository.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.ServerError(fmt.Sprintf("Action (%s)", ctx.Params(":action")), err)
|
ctx.ServerError(fmt.Sprintf("Action (watch, %t)", watch), err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx.HTML(http.StatusOK, tplWatchUnwatch)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ctx.Params(":action") {
|
func ActionStar(star bool) func(ctx *context.Context) {
|
||||||
case "watch", "unwatch":
|
return func(ctx *context.Context) {
|
||||||
ctx.HTML(http.StatusOK, tplWatchUnwatch)
|
err := repo_model.StarRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID, star)
|
||||||
|
if err != nil {
|
||||||
|
ctx.ServerError(fmt.Sprintf("Action (star, %t)", star), err)
|
||||||
return
|
return
|
||||||
case "star", "unstar":
|
}
|
||||||
|
|
||||||
|
ctx.Data["IsStaringRepo"] = repo_model.IsStaring(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID)
|
||||||
|
|
||||||
|
// we have to reload the repository because NumStars or NumWatching (used in the templates) has just changed
|
||||||
|
ctx.Data["Repository"], err = repo_model.GetRepositoryByName(ctx, ctx.Repo.Repository.OwnerID, ctx.Repo.Repository.Name)
|
||||||
|
if err != nil {
|
||||||
|
ctx.ServerError(fmt.Sprintf("Action (star, %t)", star), err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
ctx.HTML(http.StatusOK, tplStarUnstar)
|
ctx.HTML(http.StatusOK, tplStarUnstar)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ActionTransfer(accept bool) func(ctx *context.Context) {
|
||||||
|
return func(ctx *context.Context) {
|
||||||
|
var action string
|
||||||
|
if accept {
|
||||||
|
action = "accept_transfer"
|
||||||
|
} else {
|
||||||
|
action = "reject_transfer"
|
||||||
|
}
|
||||||
|
|
||||||
|
err := acceptOrRejectRepoTransfer(ctx, accept)
|
||||||
|
if err != nil {
|
||||||
|
ctx.ServerError(fmt.Sprintf("Action (%s)", action), err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.RedirectToFirst(ctx.FormString("redirect_to"), ctx.Repo.RepoLink)
|
ctx.RedirectToFirst(ctx.FormString("redirect_to"), ctx.Repo.RepoLink)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func acceptOrRejectRepoTransfer(ctx *context.Context, accept bool) error {
|
func acceptOrRejectRepoTransfer(ctx *context.Context, accept bool) error {
|
||||||
repoTransfer, err := models.GetPendingRepositoryTransfer(ctx, ctx.Repo.Repository)
|
repoTransfer, err := models.GetPendingRepositoryTransfer(ctx, ctx.Repo.Repository)
|
||||||
|
|
|
@ -1124,7 +1124,14 @@ func registerRoutes(m *web.Route) {
|
||||||
}, ctxDataSet("PageIsRepoSettings", true, "LFSStartServer", setting.LFS.StartServer))
|
}, ctxDataSet("PageIsRepoSettings", true, "LFSStartServer", setting.LFS.StartServer))
|
||||||
}, reqSignIn, context.RepoAssignment, context.UnitTypes(), reqRepoAdmin, context.RepoRef())
|
}, reqSignIn, context.RepoAssignment, context.UnitTypes(), reqRepoAdmin, context.RepoRef())
|
||||||
|
|
||||||
m.Post("/{username}/{reponame}/action/{action}", reqSignIn, context.RepoAssignment, context.UnitTypes(), repo.Action)
|
m.Group("/{username}/{reponame}/action", func() {
|
||||||
|
m.Post("/watch", repo.ActionWatch(true))
|
||||||
|
m.Post("/unwatch", repo.ActionWatch(false))
|
||||||
|
m.Post("/accept_transfer", repo.ActionTransfer(true))
|
||||||
|
m.Post("/reject_transfer", repo.ActionTransfer(false))
|
||||||
|
m.Post("/star", repo.ActionStar(true))
|
||||||
|
m.Post("/unstar", repo.ActionStar(false))
|
||||||
|
}, reqSignIn, context.RepoAssignment, context.UnitTypes())
|
||||||
|
|
||||||
// Grouping for those endpoints not requiring authentication (but should respect ignSignIn)
|
// Grouping for those endpoints not requiring authentication (but should respect ignSignIn)
|
||||||
m.Group("/{username}/{reponame}", func() {
|
m.Group("/{username}/{reponame}", func() {
|
||||||
|
|
Loading…
Reference in a new issue