7707d7dda7ecc73bda9d2b7ec485de0b7c224b85
[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   autoPlayNextVideo: boolean
20   autoPlayNextVideoPlaylist: boolean
21   webTorrentEnabled: boolean
22   videosHistoryEnabled: boolean
23   videoLanguages: string[]
24
25   role: UserRole
26   roleLabel: string
27
28   videoQuota: number
29   videoQuotaDaily: number
30   videoQuotaUsed?: number
31   videoQuotaUsedDaily?: number
32
33   theme: string
34
35   account: Account
36   notificationSettings?: UserNotificationSetting
37   videoChannels?: VideoChannel[]
38
39   blocked: boolean
40   blockedReason?: string
41
42   noInstanceConfigWarningModal: boolean
43   noWelcomeModal: boolean
44
45   createdAt: Date
46
47   constructor (hash: Partial<UserServerModel>) {
48     this.id = hash.id
49     this.username = hash.username
50     this.email = hash.email
51
52     this.role = hash.role
53
54     this.videoChannels = hash.videoChannels
55
56     this.videoQuota = hash.videoQuota
57     this.videoQuotaDaily = hash.videoQuotaDaily
58     this.videoQuotaUsed = hash.videoQuotaUsed
59     this.videoQuotaUsedDaily = hash.videoQuotaUsedDaily
60
61     this.nsfwPolicy = hash.nsfwPolicy
62     this.webTorrentEnabled = hash.webTorrentEnabled
63     this.videosHistoryEnabled = hash.videosHistoryEnabled
64     this.autoPlayVideo = hash.autoPlayVideo
65
66     this.theme = hash.theme
67
68     this.adminFlags = hash.adminFlags
69
70     this.blocked = hash.blocked
71     this.blockedReason = hash.blockedReason
72
73     this.noInstanceConfigWarningModal = hash.noInstanceConfigWarningModal
74     this.noWelcomeModal = hash.noWelcomeModal
75
76     this.notificationSettings = hash.notificationSettings
77
78     this.createdAt = hash.createdAt
79
80     if (hash.account !== undefined) {
81       this.account = new Account(hash.account)
82     }
83   }
84
85   get accountAvatarUrl () {
86     if (!this.account) return ''
87
88     return this.account.avatarUrl
89   }
90
91   hasRight (right: UserRight) {
92     return hasUserRight(this.role, right)
93   }
94
95   patch (obj: UserServerModel) {
96     for (const key of Object.keys(obj)) {
97       this[key] = obj[key]
98     }
99
100     if (obj.account !== undefined) {
101       this.account = new Account(obj.account)
102     }
103   }
104
105   updateAccountAvatar (newAccountAvatar: Avatar) {
106     this.account.updateAvatar(newAccountAvatar)
107   }
108 }