settings.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package form // import "miniflux.app/v2/internal/ui/form"
  4. import (
  5. "net/http"
  6. "strconv"
  7. "miniflux.app/v2/internal/locale"
  8. "miniflux.app/v2/internal/model"
  9. )
  10. // SettingsForm represents the settings form.
  11. type SettingsForm struct {
  12. Username string
  13. Password string
  14. Confirmation string
  15. Theme string
  16. Language string
  17. Timezone string
  18. EntryDirection string
  19. EntryOrder string
  20. EntriesPerPage int
  21. KeyboardShortcuts bool
  22. ShowReadingTime bool
  23. CustomCSS string
  24. EntrySwipe bool
  25. GestureNav string
  26. DisplayMode string
  27. DefaultReadingSpeed int
  28. CJKReadingSpeed int
  29. DefaultHomePage string
  30. CategoriesSortingOrder string
  31. MarkReadOnView bool
  32. }
  33. // Merge updates the fields of the given user.
  34. func (s *SettingsForm) Merge(user *model.User) *model.User {
  35. user.Username = s.Username
  36. user.Theme = s.Theme
  37. user.Language = s.Language
  38. user.Timezone = s.Timezone
  39. user.EntryDirection = s.EntryDirection
  40. user.EntryOrder = s.EntryOrder
  41. user.EntriesPerPage = s.EntriesPerPage
  42. user.KeyboardShortcuts = s.KeyboardShortcuts
  43. user.ShowReadingTime = s.ShowReadingTime
  44. user.Stylesheet = s.CustomCSS
  45. user.EntrySwipe = s.EntrySwipe
  46. user.GestureNav = s.GestureNav
  47. user.DisplayMode = s.DisplayMode
  48. user.CJKReadingSpeed = s.CJKReadingSpeed
  49. user.DefaultReadingSpeed = s.DefaultReadingSpeed
  50. user.DefaultHomePage = s.DefaultHomePage
  51. user.CategoriesSortingOrder = s.CategoriesSortingOrder
  52. user.MarkReadOnView = s.MarkReadOnView
  53. if s.Password != "" {
  54. user.Password = s.Password
  55. }
  56. return user
  57. }
  58. // Validate makes sure the form values are valid.
  59. func (s *SettingsForm) Validate() *locale.LocalizedError {
  60. if s.Username == "" || s.Theme == "" || s.Language == "" || s.Timezone == "" || s.EntryDirection == "" || s.DisplayMode == "" || s.DefaultHomePage == "" {
  61. return locale.NewLocalizedError("error.settings_mandatory_fields")
  62. }
  63. if s.CJKReadingSpeed <= 0 || s.DefaultReadingSpeed <= 0 {
  64. return locale.NewLocalizedError("error.settings_reading_speed_is_positive")
  65. }
  66. if s.Confirmation == "" {
  67. // Firefox insists on auto-completing the password field.
  68. // If the confirmation field is blank, the user probably
  69. // didn't intend to change their password.
  70. s.Password = ""
  71. } else if s.Password != "" {
  72. if s.Password != s.Confirmation {
  73. return locale.NewLocalizedError("error.different_passwords")
  74. }
  75. }
  76. return nil
  77. }
  78. // NewSettingsForm returns a new SettingsForm.
  79. func NewSettingsForm(r *http.Request) *SettingsForm {
  80. entriesPerPage, err := strconv.ParseInt(r.FormValue("entries_per_page"), 10, 0)
  81. if err != nil {
  82. entriesPerPage = 0
  83. }
  84. defaultReadingSpeed, err := strconv.ParseInt(r.FormValue("default_reading_speed"), 10, 0)
  85. if err != nil {
  86. defaultReadingSpeed = 0
  87. }
  88. cjkReadingSpeed, err := strconv.ParseInt(r.FormValue("cjk_reading_speed"), 10, 0)
  89. if err != nil {
  90. cjkReadingSpeed = 0
  91. }
  92. return &SettingsForm{
  93. Username: r.FormValue("username"),
  94. Password: r.FormValue("password"),
  95. Confirmation: r.FormValue("confirmation"),
  96. Theme: r.FormValue("theme"),
  97. Language: r.FormValue("language"),
  98. Timezone: r.FormValue("timezone"),
  99. EntryDirection: r.FormValue("entry_direction"),
  100. EntryOrder: r.FormValue("entry_order"),
  101. EntriesPerPage: int(entriesPerPage),
  102. KeyboardShortcuts: r.FormValue("keyboard_shortcuts") == "1",
  103. ShowReadingTime: r.FormValue("show_reading_time") == "1",
  104. CustomCSS: r.FormValue("custom_css"),
  105. EntrySwipe: r.FormValue("entry_swipe") == "1",
  106. GestureNav: r.FormValue("gesture_nav"),
  107. DisplayMode: r.FormValue("display_mode"),
  108. DefaultReadingSpeed: int(defaultReadingSpeed),
  109. CJKReadingSpeed: int(cjkReadingSpeed),
  110. DefaultHomePage: r.FormValue("default_home_page"),
  111. CategoriesSortingOrder: r.FormValue("categories_sorting_order"),
  112. MarkReadOnView: r.FormValue("mark_read_on_view") == "1",
  113. }
  114. }