summaryrefslogtreecommitdiffstats
path: root/routers/web/repo/middlewares.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
committerDaniel Baumann <daniel@debian.org>2024-12-12 23:57:56 +0100
commite68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch)
tree97775d6c13b0f416af55314eb6a89ef792474615 /routers/web/repo/middlewares.go
parentInitial commit. (diff)
downloadforgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz
forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to '')
-rw-r--r--routers/web/repo/middlewares.go120
1 files changed, 120 insertions, 0 deletions
diff --git a/routers/web/repo/middlewares.go b/routers/web/repo/middlewares.go
new file mode 100644
index 0000000..ddda9f3
--- /dev/null
+++ b/routers/web/repo/middlewares.go
@@ -0,0 +1,120 @@
+// Copyright 2020 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package repo
+
+import (
+ "fmt"
+ "strconv"
+
+ system_model "code.gitea.io/gitea/models/system"
+ user_model "code.gitea.io/gitea/models/user"
+ "code.gitea.io/gitea/modules/git"
+ "code.gitea.io/gitea/modules/optional"
+ "code.gitea.io/gitea/services/context"
+ user_service "code.gitea.io/gitea/services/user"
+)
+
+// SetEditorconfigIfExists set editor config as render variable
+func SetEditorconfigIfExists(ctx *context.Context) {
+ if ctx.Repo.Repository.IsEmpty {
+ return
+ }
+
+ ec, _, err := ctx.Repo.GetEditorconfig()
+
+ if err != nil && !git.IsErrNotExist(err) {
+ description := fmt.Sprintf("Error while getting .editorconfig file: %v", err)
+ if err := system_model.CreateRepositoryNotice(description); err != nil {
+ ctx.ServerError("ErrCreatingReporitoryNotice", err)
+ }
+ return
+ }
+
+ ctx.Data["Editorconfig"] = ec
+}
+
+// SetDiffViewStyle set diff style as render variable
+func SetDiffViewStyle(ctx *context.Context) {
+ queryStyle := ctx.FormString("style")
+
+ if !ctx.IsSigned {
+ ctx.Data["IsSplitStyle"] = queryStyle == "split"
+ return
+ }
+
+ var (
+ userStyle = ctx.Doer.DiffViewStyle
+ style string
+ )
+
+ if queryStyle == "unified" || queryStyle == "split" {
+ style = queryStyle
+ } else if userStyle == "unified" || userStyle == "split" {
+ style = userStyle
+ } else {
+ style = "unified"
+ }
+
+ ctx.Data["IsSplitStyle"] = style == "split"
+
+ opts := &user_service.UpdateOptions{
+ DiffViewStyle: optional.Some(style),
+ }
+ if err := user_service.UpdateUser(ctx, ctx.Doer, opts); err != nil {
+ ctx.ServerError("UpdateUser", err)
+ }
+}
+
+// SetWhitespaceBehavior set whitespace behavior as render variable
+func SetWhitespaceBehavior(ctx *context.Context) {
+ const defaultWhitespaceBehavior = "show-all"
+ whitespaceBehavior := ctx.FormString("whitespace")
+ switch whitespaceBehavior {
+ case "", "ignore-all", "ignore-eol", "ignore-change":
+ break
+ default:
+ whitespaceBehavior = defaultWhitespaceBehavior
+ }
+ if ctx.IsSigned {
+ userWhitespaceBehavior, err := user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyDiffWhitespaceBehavior, defaultWhitespaceBehavior)
+ if err == nil {
+ if whitespaceBehavior == "" {
+ whitespaceBehavior = userWhitespaceBehavior
+ } else if whitespaceBehavior != userWhitespaceBehavior {
+ _ = user_model.SetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyDiffWhitespaceBehavior, whitespaceBehavior)
+ }
+ } // else: we can ignore the error safely
+ }
+
+ // these behaviors are for gitdiff.GetWhitespaceFlag
+ if whitespaceBehavior == "" {
+ ctx.Data["WhitespaceBehavior"] = defaultWhitespaceBehavior
+ } else {
+ ctx.Data["WhitespaceBehavior"] = whitespaceBehavior
+ }
+}
+
+// SetShowOutdatedComments set the show outdated comments option as context variable
+func SetShowOutdatedComments(ctx *context.Context) {
+ showOutdatedCommentsValue := ctx.FormString("show-outdated")
+ // var showOutdatedCommentsValue string
+
+ if showOutdatedCommentsValue != "true" && showOutdatedCommentsValue != "false" {
+ // invalid or no value for this form string -> use default or stored user setting
+ if ctx.IsSigned {
+ showOutdatedCommentsValue, _ = user_model.GetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyShowOutdatedComments, "false")
+ } else {
+ // not logged in user -> use the default value
+ showOutdatedCommentsValue = "false"
+ }
+ } else {
+ // valid value -> update user setting if user is logged in
+ if ctx.IsSigned {
+ _ = user_model.SetUserSetting(ctx, ctx.Doer.ID, user_model.SettingsKeyShowOutdatedComments, showOutdatedCommentsValue)
+ }
+ }
+
+ showOutdatedComments, _ := strconv.ParseBool(showOutdatedCommentsValue)
+ ctx.Data["ShowOutdatedComments"] = showOutdatedComments
+}