settings.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. EntriesPerPage int
  21. KeyboardShortcuts bool
  22. ShowReadingTime bool
  23. CustomCSS string
  24. EntrySwipe bool
  25. }
  26. // Merge updates the fields of the given user.
  27. func (s *SettingsForm) Merge(user *model.User) *model.User {
  28. user.Username = s.Username
  29. user.Theme = s.Theme
  30. user.Language = s.Language
  31. user.Timezone = s.Timezone
  32. user.EntryDirection = s.EntryDirection
  33. user.EntriesPerPage = s.EntriesPerPage
  34. user.KeyboardShortcuts = s.KeyboardShortcuts
  35. user.ShowReadingTime = s.ShowReadingTime
  36. user.Stylesheet = s.CustomCSS
  37. user.EntrySwipe = s.EntrySwipe
  38. if s.Password != "" {
  39. user.Password = s.Password
  40. }
  41. return user
  42. }
  43. // Validate makes sure the form values are valid.
  44. func (s *SettingsForm) Validate() error {
  45. if s.Username == "" || s.Theme == "" || s.Language == "" || s.Timezone == "" || s.EntryDirection == "" {
  46. return errors.NewLocalizedError("error.settings_mandatory_fields")
  47. }
  48. if s.Confirmation == "" {
  49. // Firefox insists on auto-completing the password field.
  50. // If the confirmation field is blank, the user probably
  51. // didn't intend to change their password.
  52. s.Password = ""
  53. } else if s.Password != "" {
  54. if s.Password != s.Confirmation {
  55. return errors.NewLocalizedError("error.different_passwords")
  56. }
  57. }
  58. return nil
  59. }
  60. // NewSettingsForm returns a new SettingsForm.
  61. func NewSettingsForm(r *http.Request) *SettingsForm {
  62. entriesPerPage, err := strconv.ParseInt(r.FormValue("entries_per_page"), 10, 64)
  63. if err != nil {
  64. entriesPerPage = 0
  65. }
  66. return &SettingsForm{
  67. Username: r.FormValue("username"),
  68. Password: r.FormValue("password"),
  69. Confirmation: r.FormValue("confirmation"),
  70. Theme: r.FormValue("theme"),
  71. Language: r.FormValue("language"),
  72. Timezone: r.FormValue("timezone"),
  73. EntryDirection: r.FormValue("entry_direction"),
  74. EntriesPerPage: int(entriesPerPage),
  75. KeyboardShortcuts: r.FormValue("keyboard_shortcuts") == "1",
  76. ShowReadingTime: r.FormValue("show_reading_time") == "1",
  77. CustomCSS: r.FormValue("custom_css"),
  78. EntrySwipe: r.FormValue("entry_swipe") == "1",
  79. }
  80. }