Merge branch 'release/v1.3.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   emailVerified: boolean
13   nsfwPolicy: NSFWPolicyType
14
15   role: UserRole
16   roleLabel: string
17
18   webTorrentEnabled: boolean
19   autoPlayVideo: boolean
20   videosHistoryEnabled: boolean
21
22   videoQuota: number
23   videoQuotaDaily: number
24   account: Account
25   videoChannels: VideoChannel[]
26   createdAt: Date
27
28   adminFlags?: UserAdminFlag
29
30   blocked: boolean
31   blockedReason?: string
32
33   notificationSettings?: UserNotificationSetting
34
35   constructor (hash: Partial<UserServerModel>) {
36     this.id = hash.id
37     this.username = hash.username
38     this.email = hash.email
39
40     this.role = hash.role
41
42     this.videoChannels = hash.videoChannels
43     this.videoQuota = hash.videoQuota
44     this.videoQuotaDaily = hash.videoQuotaDaily
45     this.nsfwPolicy = hash.nsfwPolicy
46     this.webTorrentEnabled = hash.webTorrentEnabled
47     this.videosHistoryEnabled = hash.videosHistoryEnabled
48     this.autoPlayVideo = hash.autoPlayVideo
49     this.createdAt = hash.createdAt
50
51     this.adminFlags = hash.adminFlags
52
53     this.blocked = hash.blocked
54     this.blockedReason = hash.blockedReason
55
56     this.notificationSettings = hash.notificationSettings
57
58     if (hash.account !== undefined) {
59       this.account = new Account(hash.account)
60     }
61   }
62
63   get accountAvatarUrl () {
64     if (!this.account) return ''
65
66     return this.account.avatarUrl
67   }
68
69   hasRight (right: UserRight) {
70     return hasUserRight(this.role, right)
71   }
72
73   patch (obj: UserServerModel) {
74     for (const key of Object.keys(obj)) {
75       this[key] = obj[key]
76     }
77
78     if (obj.account !== undefined) {
79       this.account = new Account(obj.account)
80     }
81   }
82
83   updateAccountAvatar (newAccountAvatar: Avatar) {
84     this.account.updateAvatar(newAccountAvatar)
85   }
86 }