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