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