settings.go 3.8 KB

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