user.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. AlwaysOpenExternalLinks bool `json:"always_open_external_links"`
  41. OpenExternalLinksInNewTab bool `json:"open_external_links_in_new_tab"`
  42. }
  43. // UserCreationRequest represents the request to create a user.
  44. type UserCreationRequest struct {
  45. Username string `json:"username"`
  46. Password string `json:"password"`
  47. IsAdmin bool `json:"is_admin"`
  48. GoogleID string `json:"google_id"`
  49. OpenIDConnectID string `json:"openid_connect_id"`
  50. }
  51. // UserModificationRequest represents the request to update a user.
  52. type UserModificationRequest struct {
  53. Username *string `json:"username"`
  54. Password *string `json:"password"`
  55. Theme *string `json:"theme"`
  56. Language *string `json:"language"`
  57. Timezone *string `json:"timezone"`
  58. EntryDirection *string `json:"entry_sorting_direction"`
  59. EntryOrder *string `json:"entry_sorting_order"`
  60. Stylesheet *string `json:"stylesheet"`
  61. CustomJS *string `json:"custom_js"`
  62. ExternalFontHosts *string `json:"external_font_hosts"`
  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. AlwaysOpenExternalLinks *bool `json:"always_open_external_links"`
  80. OpenExternalLinksInNewTab *bool `json:"open_external_links_in_new_tab"`
  81. }
  82. // Patch updates the User object with the modification request.
  83. func (u *UserModificationRequest) Patch(user *User) {
  84. if u.Username != nil {
  85. user.Username = *u.Username
  86. }
  87. if u.Password != nil {
  88. user.Password = *u.Password
  89. }
  90. if u.IsAdmin != nil {
  91. user.IsAdmin = *u.IsAdmin
  92. }
  93. if u.Theme != nil {
  94. user.Theme = *u.Theme
  95. }
  96. if u.Language != nil {
  97. user.Language = *u.Language
  98. }
  99. if u.Timezone != nil {
  100. user.Timezone = *u.Timezone
  101. }
  102. if u.EntryDirection != nil {
  103. user.EntryDirection = *u.EntryDirection
  104. }
  105. if u.EntryOrder != nil {
  106. user.EntryOrder = *u.EntryOrder
  107. }
  108. if u.Stylesheet != nil {
  109. user.Stylesheet = *u.Stylesheet
  110. }
  111. if u.CustomJS != nil {
  112. user.CustomJS = *u.CustomJS
  113. }
  114. if u.ExternalFontHosts != nil {
  115. user.ExternalFontHosts = *u.ExternalFontHosts
  116. }
  117. if u.EntriesPerPage != nil {
  118. user.EntriesPerPage = *u.EntriesPerPage
  119. }
  120. if u.KeyboardShortcuts != nil {
  121. user.KeyboardShortcuts = *u.KeyboardShortcuts
  122. }
  123. if u.ShowReadingTime != nil {
  124. user.ShowReadingTime = *u.ShowReadingTime
  125. }
  126. if u.EntrySwipe != nil {
  127. user.EntrySwipe = *u.EntrySwipe
  128. }
  129. if u.GestureNav != nil {
  130. user.GestureNav = *u.GestureNav
  131. }
  132. if u.DisplayMode != nil {
  133. user.DisplayMode = *u.DisplayMode
  134. }
  135. if u.DefaultReadingSpeed != nil {
  136. user.DefaultReadingSpeed = *u.DefaultReadingSpeed
  137. }
  138. if u.CJKReadingSpeed != nil {
  139. user.CJKReadingSpeed = *u.CJKReadingSpeed
  140. }
  141. if u.DefaultHomePage != nil {
  142. user.DefaultHomePage = *u.DefaultHomePage
  143. }
  144. if u.CategoriesSortingOrder != nil {
  145. user.CategoriesSortingOrder = *u.CategoriesSortingOrder
  146. }
  147. if u.MarkReadOnView != nil {
  148. user.MarkReadOnView = *u.MarkReadOnView
  149. }
  150. if u.MarkReadOnMediaPlayerCompletion != nil {
  151. user.MarkReadOnMediaPlayerCompletion = *u.MarkReadOnMediaPlayerCompletion
  152. }
  153. if u.MediaPlaybackRate != nil {
  154. user.MediaPlaybackRate = *u.MediaPlaybackRate
  155. }
  156. if u.BlockFilterEntryRules != nil {
  157. user.BlockFilterEntryRules = *u.BlockFilterEntryRules
  158. }
  159. if u.KeepFilterEntryRules != nil {
  160. user.KeepFilterEntryRules = *u.KeepFilterEntryRules
  161. }
  162. if u.AlwaysOpenExternalLinks != nil {
  163. user.AlwaysOpenExternalLinks = *u.AlwaysOpenExternalLinks
  164. }
  165. if u.OpenExternalLinksInNewTab != nil {
  166. user.OpenExternalLinksInNewTab = *u.OpenExternalLinksInNewTab
  167. }
  168. }
  169. // UseTimezone converts last login date to the given timezone.
  170. func (u *User) UseTimezone(tz string) {
  171. if u.LastLoginAt != nil {
  172. *u.LastLoginAt = timezone.Convert(tz, *u.LastLoginAt)
  173. }
  174. }
  175. // Users represents a list of users.
  176. type Users []*User
  177. // UseTimezone converts last login timestamp of all users to the given timezone.
  178. func (u Users) UseTimezone(tz string) {
  179. for _, user := range u {
  180. user.UseTimezone(tz)
  181. }
  182. }