user.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package model // import "miniflux.app/model"
  4. import (
  5. "time"
  6. "miniflux.app/timezone"
  7. )
  8. // User represents a user in the system.
  9. type User struct {
  10. ID int64 `json:"id"`
  11. Username string `json:"username"`
  12. Password string `json:"-"`
  13. IsAdmin bool `json:"is_admin"`
  14. Theme string `json:"theme"`
  15. Language string `json:"language"`
  16. Timezone string `json:"timezone"`
  17. EntryDirection string `json:"entry_sorting_direction"`
  18. EntryOrder string `json:"entry_sorting_order"`
  19. Stylesheet string `json:"stylesheet"`
  20. GoogleID string `json:"google_id"`
  21. OpenIDConnectID string `json:"openid_connect_id"`
  22. EntriesPerPage int `json:"entries_per_page"`
  23. KeyboardShortcuts bool `json:"keyboard_shortcuts"`
  24. ShowReadingTime bool `json:"show_reading_time"`
  25. EntrySwipe bool `json:"entry_swipe"`
  26. GestureNav string `json:"gesture_nav"`
  27. LastLoginAt *time.Time `json:"last_login_at"`
  28. DisplayMode string `json:"display_mode"`
  29. DefaultReadingSpeed int `json:"default_reading_speed"`
  30. CJKReadingSpeed int `json:"cjk_reading_speed"`
  31. DefaultHomePage string `json:"default_home_page"`
  32. CategoriesSortingOrder string `json:"categories_sorting_order"`
  33. }
  34. // UserCreationRequest represents the request to create a user.
  35. type UserCreationRequest struct {
  36. Username string `json:"username"`
  37. Password string `json:"password"`
  38. IsAdmin bool `json:"is_admin"`
  39. GoogleID string `json:"google_id"`
  40. OpenIDConnectID string `json:"openid_connect_id"`
  41. }
  42. // UserModificationRequest represents the request to update a user.
  43. type UserModificationRequest struct {
  44. Username *string `json:"username"`
  45. Password *string `json:"password"`
  46. Theme *string `json:"theme"`
  47. Language *string `json:"language"`
  48. Timezone *string `json:"timezone"`
  49. EntryDirection *string `json:"entry_sorting_direction"`
  50. EntryOrder *string `json:"entry_sorting_order"`
  51. Stylesheet *string `json:"stylesheet"`
  52. GoogleID *string `json:"google_id"`
  53. OpenIDConnectID *string `json:"openid_connect_id"`
  54. EntriesPerPage *int `json:"entries_per_page"`
  55. IsAdmin *bool `json:"is_admin"`
  56. KeyboardShortcuts *bool `json:"keyboard_shortcuts"`
  57. ShowReadingTime *bool `json:"show_reading_time"`
  58. EntrySwipe *bool `json:"entry_swipe"`
  59. GestureNav *string `json:"gesture_nav"`
  60. DisplayMode *string `json:"display_mode"`
  61. DefaultReadingSpeed *int `json:"default_reading_speed"`
  62. CJKReadingSpeed *int `json:"cjk_reading_speed"`
  63. DefaultHomePage *string `json:"default_home_page"`
  64. CategoriesSortingOrder *string `json:"categories_sorting_order"`
  65. }
  66. // Patch updates the User object with the modification request.
  67. func (u *UserModificationRequest) Patch(user *User) {
  68. if u.Username != nil {
  69. user.Username = *u.Username
  70. }
  71. if u.Password != nil {
  72. user.Password = *u.Password
  73. }
  74. if u.IsAdmin != nil {
  75. user.IsAdmin = *u.IsAdmin
  76. }
  77. if u.Theme != nil {
  78. user.Theme = *u.Theme
  79. }
  80. if u.Language != nil {
  81. user.Language = *u.Language
  82. }
  83. if u.Timezone != nil {
  84. user.Timezone = *u.Timezone
  85. }
  86. if u.EntryDirection != nil {
  87. user.EntryDirection = *u.EntryDirection
  88. }
  89. if u.EntryOrder != nil {
  90. user.EntryOrder = *u.EntryOrder
  91. }
  92. if u.Stylesheet != nil {
  93. user.Stylesheet = *u.Stylesheet
  94. }
  95. if u.GoogleID != nil {
  96. user.GoogleID = *u.GoogleID
  97. }
  98. if u.OpenIDConnectID != nil {
  99. user.OpenIDConnectID = *u.OpenIDConnectID
  100. }
  101. if u.EntriesPerPage != nil {
  102. user.EntriesPerPage = *u.EntriesPerPage
  103. }
  104. if u.KeyboardShortcuts != nil {
  105. user.KeyboardShortcuts = *u.KeyboardShortcuts
  106. }
  107. if u.ShowReadingTime != nil {
  108. user.ShowReadingTime = *u.ShowReadingTime
  109. }
  110. if u.EntrySwipe != nil {
  111. user.EntrySwipe = *u.EntrySwipe
  112. }
  113. if u.GestureNav != nil {
  114. user.GestureNav = *u.GestureNav
  115. }
  116. if u.DisplayMode != nil {
  117. user.DisplayMode = *u.DisplayMode
  118. }
  119. if u.DefaultReadingSpeed != nil {
  120. user.DefaultReadingSpeed = *u.DefaultReadingSpeed
  121. }
  122. if u.CJKReadingSpeed != nil {
  123. user.CJKReadingSpeed = *u.CJKReadingSpeed
  124. }
  125. if u.DefaultHomePage != nil {
  126. user.DefaultHomePage = *u.DefaultHomePage
  127. }
  128. if u.CategoriesSortingOrder != nil {
  129. user.CategoriesSortingOrder = *u.CategoriesSortingOrder
  130. }
  131. }
  132. // UseTimezone converts last login date to the given timezone.
  133. func (u *User) UseTimezone(tz string) {
  134. if u.LastLoginAt != nil {
  135. *u.LastLoginAt = timezone.Convert(tz, *u.LastLoginAt)
  136. }
  137. }
  138. // Users represents a list of users.
  139. type Users []*User
  140. // UseTimezone converts last login timestamp of all users to the given timezone.
  141. func (u Users) UseTimezone(tz string) {
  142. for _, user := range u {
  143. user.UseTimezone(tz)
  144. }
  145. }