user.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package model // import "miniflux.app/v2/internal/model"
  4. import (
  5. "time"
  6. "miniflux.app/v2/internal/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. MarkReadOnView bool `json:"mark_read_on_view"`
  34. MarkReadOnMediaPlayerCompletion bool `json:"mark_read_on_media_player_completion"`
  35. MediaPlaybackRate float64 `json:"media_playback_rate"`
  36. BlockFilterEntryRules string `json:"block_filter_entry_rules"`
  37. KeepFilterEntryRules string `json:"keep_filter_entry_rules"`
  38. }
  39. // UserCreationRequest represents the request to create a user.
  40. type UserCreationRequest struct {
  41. Username string `json:"username"`
  42. Password string `json:"password"`
  43. IsAdmin bool `json:"is_admin"`
  44. GoogleID string `json:"google_id"`
  45. OpenIDConnectID string `json:"openid_connect_id"`
  46. }
  47. // UserModificationRequest represents the request to update a user.
  48. type UserModificationRequest struct {
  49. Username *string `json:"username"`
  50. Password *string `json:"password"`
  51. Theme *string `json:"theme"`
  52. Language *string `json:"language"`
  53. Timezone *string `json:"timezone"`
  54. EntryDirection *string `json:"entry_sorting_direction"`
  55. EntryOrder *string `json:"entry_sorting_order"`
  56. Stylesheet *string `json:"stylesheet"`
  57. GoogleID *string `json:"google_id"`
  58. OpenIDConnectID *string `json:"openid_connect_id"`
  59. EntriesPerPage *int `json:"entries_per_page"`
  60. IsAdmin *bool `json:"is_admin"`
  61. KeyboardShortcuts *bool `json:"keyboard_shortcuts"`
  62. ShowReadingTime *bool `json:"show_reading_time"`
  63. EntrySwipe *bool `json:"entry_swipe"`
  64. GestureNav *string `json:"gesture_nav"`
  65. DisplayMode *string `json:"display_mode"`
  66. DefaultReadingSpeed *int `json:"default_reading_speed"`
  67. CJKReadingSpeed *int `json:"cjk_reading_speed"`
  68. DefaultHomePage *string `json:"default_home_page"`
  69. CategoriesSortingOrder *string `json:"categories_sorting_order"`
  70. MarkReadOnView *bool `json:"mark_read_on_view"`
  71. MarkReadOnMediaPlayerCompletion *bool `json:"mark_read_on_media_player_completion"`
  72. MediaPlaybackRate *float64 `json:"media_playback_rate"`
  73. BlockFilterEntryRules *string `json:"block_filter_entry_rules"`
  74. KeepFilterEntryRules *string `json:"keep_filter_entry_rules"`
  75. }
  76. // Patch updates the User object with the modification request.
  77. func (u *UserModificationRequest) Patch(user *User) {
  78. if u.Username != nil {
  79. user.Username = *u.Username
  80. }
  81. if u.Password != nil {
  82. user.Password = *u.Password
  83. }
  84. if u.IsAdmin != nil {
  85. user.IsAdmin = *u.IsAdmin
  86. }
  87. if u.Theme != nil {
  88. user.Theme = *u.Theme
  89. }
  90. if u.Language != nil {
  91. user.Language = *u.Language
  92. }
  93. if u.Timezone != nil {
  94. user.Timezone = *u.Timezone
  95. }
  96. if u.EntryDirection != nil {
  97. user.EntryDirection = *u.EntryDirection
  98. }
  99. if u.EntryOrder != nil {
  100. user.EntryOrder = *u.EntryOrder
  101. }
  102. if u.Stylesheet != nil {
  103. user.Stylesheet = *u.Stylesheet
  104. }
  105. if u.GoogleID != nil {
  106. user.GoogleID = *u.GoogleID
  107. }
  108. if u.OpenIDConnectID != nil {
  109. user.OpenIDConnectID = *u.OpenIDConnectID
  110. }
  111. if u.EntriesPerPage != nil {
  112. user.EntriesPerPage = *u.EntriesPerPage
  113. }
  114. if u.KeyboardShortcuts != nil {
  115. user.KeyboardShortcuts = *u.KeyboardShortcuts
  116. }
  117. if u.ShowReadingTime != nil {
  118. user.ShowReadingTime = *u.ShowReadingTime
  119. }
  120. if u.EntrySwipe != nil {
  121. user.EntrySwipe = *u.EntrySwipe
  122. }
  123. if u.GestureNav != nil {
  124. user.GestureNav = *u.GestureNav
  125. }
  126. if u.DisplayMode != nil {
  127. user.DisplayMode = *u.DisplayMode
  128. }
  129. if u.DefaultReadingSpeed != nil {
  130. user.DefaultReadingSpeed = *u.DefaultReadingSpeed
  131. }
  132. if u.CJKReadingSpeed != nil {
  133. user.CJKReadingSpeed = *u.CJKReadingSpeed
  134. }
  135. if u.DefaultHomePage != nil {
  136. user.DefaultHomePage = *u.DefaultHomePage
  137. }
  138. if u.CategoriesSortingOrder != nil {
  139. user.CategoriesSortingOrder = *u.CategoriesSortingOrder
  140. }
  141. if u.MarkReadOnView != nil {
  142. user.MarkReadOnView = *u.MarkReadOnView
  143. }
  144. if u.MarkReadOnMediaPlayerCompletion != nil {
  145. user.MarkReadOnMediaPlayerCompletion = *u.MarkReadOnMediaPlayerCompletion
  146. }
  147. if u.MediaPlaybackRate != nil {
  148. user.MediaPlaybackRate = *u.MediaPlaybackRate
  149. }
  150. if u.BlockFilterEntryRules != nil {
  151. user.BlockFilterEntryRules = *u.BlockFilterEntryRules
  152. }
  153. if u.KeepFilterEntryRules != nil {
  154. user.KeepFilterEntryRules = *u.KeepFilterEntryRules
  155. }
  156. }
  157. // UseTimezone converts last login date to the given timezone.
  158. func (u *User) UseTimezone(tz string) {
  159. if u.LastLoginAt != nil {
  160. *u.LastLoginAt = timezone.Convert(tz, *u.LastLoginAt)
  161. }
  162. }
  163. // Users represents a list of users.
  164. type Users []*User
  165. // UseTimezone converts last login timestamp of all users to the given timezone.
  166. func (u Users) UseTimezone(tz string) {
  167. for _, user := range u {
  168. user.UseTimezone(tz)
  169. }
  170. }