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