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