user.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. }
  35. // UserCreationRequest represents the request to create a user.
  36. type UserCreationRequest struct {
  37. Username string `json:"username"`
  38. Password string `json:"password"`
  39. IsAdmin bool `json:"is_admin"`
  40. GoogleID string `json:"google_id"`
  41. OpenIDConnectID string `json:"openid_connect_id"`
  42. }
  43. // UserModificationRequest represents the request to update a user.
  44. type UserModificationRequest struct {
  45. Username *string `json:"username"`
  46. Password *string `json:"password"`
  47. Theme *string `json:"theme"`
  48. Language *string `json:"language"`
  49. Timezone *string `json:"timezone"`
  50. EntryDirection *string `json:"entry_sorting_direction"`
  51. EntryOrder *string `json:"entry_sorting_order"`
  52. Stylesheet *string `json:"stylesheet"`
  53. GoogleID *string `json:"google_id"`
  54. OpenIDConnectID *string `json:"openid_connect_id"`
  55. EntriesPerPage *int `json:"entries_per_page"`
  56. IsAdmin *bool `json:"is_admin"`
  57. KeyboardShortcuts *bool `json:"keyboard_shortcuts"`
  58. ShowReadingTime *bool `json:"show_reading_time"`
  59. EntrySwipe *bool `json:"entry_swipe"`
  60. GestureNav *string `json:"gesture_nav"`
  61. DisplayMode *string `json:"display_mode"`
  62. DefaultReadingSpeed *int `json:"default_reading_speed"`
  63. CJKReadingSpeed *int `json:"cjk_reading_speed"`
  64. DefaultHomePage *string `json:"default_home_page"`
  65. CategoriesSortingOrder *string `json:"categories_sorting_order"`
  66. MarkReadOnView *bool `json:"mark_read_on_view"`
  67. }
  68. // Patch updates the User object with the modification request.
  69. func (u *UserModificationRequest) Patch(user *User) {
  70. if u.Username != nil {
  71. user.Username = *u.Username
  72. }
  73. if u.Password != nil {
  74. user.Password = *u.Password
  75. }
  76. if u.IsAdmin != nil {
  77. user.IsAdmin = *u.IsAdmin
  78. }
  79. if u.Theme != nil {
  80. user.Theme = *u.Theme
  81. }
  82. if u.Language != nil {
  83. user.Language = *u.Language
  84. }
  85. if u.Timezone != nil {
  86. user.Timezone = *u.Timezone
  87. }
  88. if u.EntryDirection != nil {
  89. user.EntryDirection = *u.EntryDirection
  90. }
  91. if u.EntryOrder != nil {
  92. user.EntryOrder = *u.EntryOrder
  93. }
  94. if u.Stylesheet != nil {
  95. user.Stylesheet = *u.Stylesheet
  96. }
  97. if u.GoogleID != nil {
  98. user.GoogleID = *u.GoogleID
  99. }
  100. if u.OpenIDConnectID != nil {
  101. user.OpenIDConnectID = *u.OpenIDConnectID
  102. }
  103. if u.EntriesPerPage != nil {
  104. user.EntriesPerPage = *u.EntriesPerPage
  105. }
  106. if u.KeyboardShortcuts != nil {
  107. user.KeyboardShortcuts = *u.KeyboardShortcuts
  108. }
  109. if u.ShowReadingTime != nil {
  110. user.ShowReadingTime = *u.ShowReadingTime
  111. }
  112. if u.EntrySwipe != nil {
  113. user.EntrySwipe = *u.EntrySwipe
  114. }
  115. if u.GestureNav != nil {
  116. user.GestureNav = *u.GestureNav
  117. }
  118. if u.DisplayMode != nil {
  119. user.DisplayMode = *u.DisplayMode
  120. }
  121. if u.DefaultReadingSpeed != nil {
  122. user.DefaultReadingSpeed = *u.DefaultReadingSpeed
  123. }
  124. if u.CJKReadingSpeed != nil {
  125. user.CJKReadingSpeed = *u.CJKReadingSpeed
  126. }
  127. if u.DefaultHomePage != nil {
  128. user.DefaultHomePage = *u.DefaultHomePage
  129. }
  130. if u.CategoriesSortingOrder != nil {
  131. user.CategoriesSortingOrder = *u.CategoriesSortingOrder
  132. }
  133. if u.MarkReadOnView != nil {
  134. user.MarkReadOnView = *u.MarkReadOnView
  135. }
  136. }
  137. // UseTimezone converts last login date to the given timezone.
  138. func (u *User) UseTimezone(tz string) {
  139. if u.LastLoginAt != nil {
  140. *u.LastLoginAt = timezone.Convert(tz, *u.LastLoginAt)
  141. }
  142. }
  143. // Users represents a list of users.
  144. type Users []*User
  145. // UseTimezone converts last login timestamp of all users to the given timezone.
  146. func (u Users) UseTimezone(tz string) {
  147. for _, user := range u {
  148. user.UseTimezone(tz)
  149. }
  150. }