user.go 7.1 KB

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