user.model.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { Account } from '@app/shared/shared-main/account/account.model'
  2. import {
  3. Avatar,
  4. hasUserRight,
  5. NSFWPolicyType,
  6. User as UserServerModel,
  7. UserAdminFlag,
  8. UserNotificationSetting,
  9. UserRight,
  10. UserRole,
  11. VideoChannel
  12. } from '@shared/models'
  13. import { UserKeys } from '@root-helpers/user-keys'
  14. export class User implements UserServerModel {
  15. static KEYS = UserKeys
  16. id: number
  17. username: string
  18. email: string
  19. pendingEmail: string | null
  20. emailVerified: boolean
  21. nsfwPolicy: NSFWPolicyType
  22. adminFlags?: UserAdminFlag
  23. autoPlayVideo: boolean
  24. autoPlayNextVideo: boolean
  25. autoPlayNextVideoPlaylist: boolean
  26. webTorrentEnabled: boolean
  27. videosHistoryEnabled: boolean
  28. videoLanguages: string[]
  29. role: UserRole
  30. roleLabel: string
  31. videoQuota: number
  32. videoQuotaDaily: number
  33. videoQuotaUsed?: number
  34. videoQuotaUsedDaily?: number
  35. videosCount?: number
  36. videoCommentsCount?: number
  37. abusesCount?: number
  38. abusesAcceptedCount?: number
  39. abusesCreatedCount?: number
  40. theme: string
  41. account: Account
  42. notificationSettings?: UserNotificationSetting
  43. videoChannels?: VideoChannel[]
  44. blocked: boolean
  45. blockedReason?: string
  46. noInstanceConfigWarningModal: boolean
  47. noWelcomeModal: boolean
  48. pluginAuth: string | null
  49. lastLoginDate: Date | null
  50. createdAt: Date
  51. constructor (hash: Partial<UserServerModel>) {
  52. this.id = hash.id
  53. this.username = hash.username
  54. this.email = hash.email
  55. this.role = hash.role
  56. this.videoChannels = hash.videoChannels
  57. this.videoQuota = hash.videoQuota
  58. this.videoQuotaDaily = hash.videoQuotaDaily
  59. this.videoQuotaUsed = hash.videoQuotaUsed
  60. this.videoQuotaUsedDaily = hash.videoQuotaUsedDaily
  61. this.videosCount = hash.videosCount
  62. this.abusesCount = hash.abusesCount
  63. this.abusesAcceptedCount = hash.abusesAcceptedCount
  64. this.abusesCreatedCount = hash.abusesCreatedCount
  65. this.videoCommentsCount = hash.videoCommentsCount
  66. this.nsfwPolicy = hash.nsfwPolicy
  67. this.webTorrentEnabled = hash.webTorrentEnabled
  68. this.autoPlayVideo = hash.autoPlayVideo
  69. this.autoPlayNextVideo = hash.autoPlayNextVideo
  70. this.autoPlayNextVideoPlaylist = hash.autoPlayNextVideoPlaylist
  71. this.videosHistoryEnabled = hash.videosHistoryEnabled
  72. this.videoLanguages = hash.videoLanguages
  73. this.theme = hash.theme
  74. this.adminFlags = hash.adminFlags
  75. this.blocked = hash.blocked
  76. this.blockedReason = hash.blockedReason
  77. this.noInstanceConfigWarningModal = hash.noInstanceConfigWarningModal
  78. this.noWelcomeModal = hash.noWelcomeModal
  79. this.notificationSettings = hash.notificationSettings
  80. this.createdAt = hash.createdAt
  81. this.pluginAuth = hash.pluginAuth
  82. this.lastLoginDate = hash.lastLoginDate
  83. if (hash.account !== undefined) {
  84. this.account = new Account(hash.account)
  85. }
  86. }
  87. get accountAvatarUrl () {
  88. if (!this.account) return ''
  89. return this.account.avatarUrl
  90. }
  91. hasRight (right: UserRight) {
  92. return hasUserRight(this.role, right)
  93. }
  94. patch (obj: UserServerModel) {
  95. for (const key of Object.keys(obj)) {
  96. this[key] = obj[key]
  97. }
  98. if (obj.account !== undefined) {
  99. this.account = new Account(obj.account)
  100. }
  101. }
  102. updateAccountAvatar (newAccountAvatar: Avatar) {
  103. this.account.updateAvatar(newAccountAvatar)
  104. }
  105. isUploadDisabled () {
  106. return this.videoQuota === 0 || this.videoQuotaDaily === 0
  107. }
  108. }