You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

middlewares.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "fmt"
  6. "strconv"
  7. system_model "code.gitea.io/gitea/models/system"
  8. user_model "code.gitea.io/gitea/models/user"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/git"
  11. )
  12. // SetEditorconfigIfExists set editor config as render variable
  13. func SetEditorconfigIfExists(ctx *context.Context) {
  14. if ctx.Repo.Repository.IsEmpty {
  15. ctx.Data["Editorconfig"] = nil
  16. return
  17. }
  18. ec, _, err := ctx.Repo.GetEditorconfig()
  19. if err != nil && !git.IsErrNotExist(err) {
  20. description := fmt.Sprintf("Error while getting .editorconfig file: %v", err)
  21. if err := system_model.CreateRepositoryNotice(description); err != nil {
  22. ctx.ServerError("ErrCreatingReporitoryNotice", err)
  23. }
  24. return
  25. }
  26. ctx.Data["Editorconfig"] = ec
  27. }
  28. // SetDiffViewStyle set diff style as render variable
  29. func SetDiffViewStyle(ctx *context.Context) {
  30. queryStyle := ctx.FormString("style")
  31. if !ctx.IsSigned {
  32. ctx.Data["IsSplitStyle"] = queryStyle == "split"
  33. return
  34. }
  35. var (
  36. userStyle = ctx.Doer.DiffViewStyle
  37. style string
  38. )
  39. if queryStyle == "unified" || queryStyle == "split" {
  40. style = queryStyle
  41. } else if userStyle == "unified" || userStyle == "split" {
  42. style = userStyle
  43. } else {
  44. style = "unified"
  45. }
  46. ctx.Data["IsSplitStyle"] = style == "split"
  47. if err := user_model.UpdateUserDiffViewStyle(ctx.Doer, style); err != nil {
  48. ctx.ServerError("ErrUpdateDiffViewStyle", err)
  49. }
  50. }
  51. // SetWhitespaceBehavior set whitespace behavior as render variable
  52. func SetWhitespaceBehavior(ctx *context.Context) {
  53. const defaultWhitespaceBehavior = "show-all"
  54. whitespaceBehavior := ctx.FormString("whitespace")
  55. switch whitespaceBehavior {
  56. case "", "ignore-all", "ignore-eol", "ignore-change":
  57. break
  58. default:
  59. whitespaceBehavior = defaultWhitespaceBehavior
  60. }
  61. if ctx.IsSigned {
  62. userWhitespaceBehavior, err := user_model.GetUserSetting(ctx.Doer.ID, user_model.SettingsKeyDiffWhitespaceBehavior, defaultWhitespaceBehavior)
  63. if err == nil {
  64. if whitespaceBehavior == "" {
  65. whitespaceBehavior = userWhitespaceBehavior
  66. } else if whitespaceBehavior != userWhitespaceBehavior {
  67. _ = user_model.SetUserSetting(ctx.Doer.ID, user_model.SettingsKeyDiffWhitespaceBehavior, whitespaceBehavior)
  68. }
  69. } // else: we can ignore the error safely
  70. }
  71. // these behaviors are for gitdiff.GetWhitespaceFlag
  72. if whitespaceBehavior == "" {
  73. ctx.Data["WhitespaceBehavior"] = defaultWhitespaceBehavior
  74. } else {
  75. ctx.Data["WhitespaceBehavior"] = whitespaceBehavior
  76. }
  77. }
  78. // SetShowOutdatedComments set the show outdated comments option as context variable
  79. func SetShowOutdatedComments(ctx *context.Context) {
  80. showOutdatedCommentsValue := ctx.FormString("show-outdated")
  81. // var showOutdatedCommentsValue string
  82. if showOutdatedCommentsValue != "true" && showOutdatedCommentsValue != "false" {
  83. // invalid or no value for this form string -> use default or stored user setting
  84. if ctx.IsSigned {
  85. showOutdatedCommentsValue, _ = user_model.GetUserSetting(ctx.Doer.ID, user_model.SettingsKeyShowOutdatedComments, "false")
  86. } else {
  87. // not logged in user -> use the default value
  88. showOutdatedCommentsValue = "false"
  89. }
  90. } else {
  91. // valid value -> update user setting if user is logged in
  92. if ctx.IsSigned {
  93. _ = user_model.SetUserSetting(ctx.Doer.ID, user_model.SettingsKeyShowOutdatedComments, showOutdatedCommentsValue)
  94. }
  95. }
  96. showOutdatedComments, _ := strconv.ParseBool(showOutdatedCommentsValue)
  97. ctx.Data["ShowOutdatedComments"] = showOutdatedComments
  98. }