user.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. GoogleID *string `json:"google_id"`
  64. OpenIDConnectID *string `json:"openid_connect_id"`
  65. EntriesPerPage *int `json:"entries_per_page"`
  66. IsAdmin *bool `json:"is_admin"`
  67. KeyboardShortcuts *bool `json:"keyboard_shortcuts"`
  68. ShowReadingTime *bool `json:"show_reading_time"`
  69. EntrySwipe *bool `json:"entry_swipe"`
  70. GestureNav *string `json:"gesture_nav"`
  71. DisplayMode *string `json:"display_mode"`
  72. DefaultReadingSpeed *int `json:"default_reading_speed"`
  73. CJKReadingSpeed *int `json:"cjk_reading_speed"`
  74. DefaultHomePage *string `json:"default_home_page"`
  75. CategoriesSortingOrder *string `json:"categories_sorting_order"`
  76. MarkReadOnView *bool `json:"mark_read_on_view"`
  77. MarkReadOnMediaPlayerCompletion *bool `json:"mark_read_on_media_player_completion"`
  78. MediaPlaybackRate *float64 `json:"media_playback_rate"`
  79. BlockFilterEntryRules *string `json:"block_filter_entry_rules"`
  80. KeepFilterEntryRules *string `json:"keep_filter_entry_rules"`
  81. AlwaysOpenExternalLinks *bool `json:"always_open_external_links"`
  82. OpenExternalLinksInNewTab *bool `json:"open_external_links_in_new_tab"`
  83. }
  84. // Patch updates the User object with the modification request.
  85. func (u *UserModificationRequest) Patch(user *User) {
  86. if u.Username != nil {
  87. user.Username = *u.Username
  88. }
  89. if u.Password != nil {
  90. user.Password = *u.Password
  91. }
  92. if u.IsAdmin != nil {
  93. user.IsAdmin = *u.IsAdmin
  94. }
  95. if u.Theme != nil {
  96. user.Theme = *u.Theme
  97. }
  98. if u.Language != nil {
  99. user.Language = *u.Language
  100. }
  101. if u.Timezone != nil {
  102. user.Timezone = *u.Timezone
  103. }
  104. if u.EntryDirection != nil {
  105. user.EntryDirection = *u.EntryDirection
  106. }
  107. if u.EntryOrder != nil {
  108. user.EntryOrder = *u.EntryOrder
  109. }
  110. if u.Stylesheet != nil {
  111. user.Stylesheet = *u.Stylesheet
  112. }
  113. if u.CustomJS != nil {
  114. user.CustomJS = *u.CustomJS
  115. }
  116. if u.ExternalFontHosts != nil {
  117. user.ExternalFontHosts = *u.ExternalFontHosts
  118. }
  119. if u.GoogleID != nil {
  120. user.GoogleID = *u.GoogleID
  121. }
  122. if u.OpenIDConnectID != nil {
  123. user.OpenIDConnectID = *u.OpenIDConnectID
  124. }
  125. if u.EntriesPerPage != nil {
  126. user.EntriesPerPage = *u.EntriesPerPage
  127. }
  128. if u.KeyboardShortcuts != nil {
  129. user.KeyboardShortcuts = *u.KeyboardShortcuts
  130. }
  131. if u.ShowReadingTime != nil {
  132. user.ShowReadingTime = *u.ShowReadingTime
  133. }
  134. if u.EntrySwipe != nil {
  135. user.EntrySwipe = *u.EntrySwipe
  136. }
  137. if u.GestureNav != nil {
  138. user.GestureNav = *u.GestureNav
  139. }
  140. if u.DisplayMode != nil {
  141. user.DisplayMode = *u.DisplayMode
  142. }
  143. if u.DefaultReadingSpeed != nil {
  144. user.DefaultReadingSpeed = *u.DefaultReadingSpeed
  145. }
  146. if u.CJKReadingSpeed != nil {
  147. user.CJKReadingSpeed = *u.CJKReadingSpeed
  148. }
  149. if u.DefaultHomePage != nil {
  150. user.DefaultHomePage = *u.DefaultHomePage
  151. }
  152. if u.CategoriesSortingOrder != nil {
  153. user.CategoriesSortingOrder = *u.CategoriesSortingOrder
  154. }
  155. if u.MarkReadOnView != nil {
  156. user.MarkReadOnView = *u.MarkReadOnView
  157. }
  158. if u.MarkReadOnMediaPlayerCompletion != nil {
  159. user.MarkReadOnMediaPlayerCompletion = *u.MarkReadOnMediaPlayerCompletion
  160. }
  161. if u.MediaPlaybackRate != nil {
  162. user.MediaPlaybackRate = *u.MediaPlaybackRate
  163. }
  164. if u.BlockFilterEntryRules != nil {
  165. user.BlockFilterEntryRules = *u.BlockFilterEntryRules
  166. }
  167. if u.KeepFilterEntryRules != nil {
  168. user.KeepFilterEntryRules = *u.KeepFilterEntryRules
  169. }
  170. if u.AlwaysOpenExternalLinks != nil {
  171. user.AlwaysOpenExternalLinks = *u.AlwaysOpenExternalLinks
  172. }
  173. if u.OpenExternalLinksInNewTab != nil {
  174. user.OpenExternalLinksInNewTab = *u.OpenExternalLinksInNewTab
  175. }
  176. }
  177. // UseTimezone converts last login date to the given timezone.
  178. func (u *User) UseTimezone(tz string) {
  179. if u.LastLoginAt != nil {
  180. *u.LastLoginAt = timezone.Convert(tz, *u.LastLoginAt)
  181. }
  182. }
  183. // Users represents a list of users.
  184. type Users []*User
  185. // UseTimezone converts last login timestamp of all users to the given timezone.
  186. func (u Users) UseTimezone(tz string) {
  187. for _, user := range u {
  188. user.UseTimezone(tz)
  189. }
  190. }