]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/users/user.model.ts
240c7aacfb03606ff3d2a9f79105ed3b59436ec6
[github/Chocobozzz/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 import { WebTorrentPolicyType } from '../../../../../shared/models/users/user-webtorrent-policy.type'
13
14 export type UserConstructorHash = {
15 id: number,
16 username: string,
17 email: string,
18 role: UserRole,
19 videoQuota?: number,
20 videoQuotaDaily?: number,
21 nsfwPolicy?: NSFWPolicyType,
22 webTorrentPolicy?: WebTorrentPolicyType,
23 autoPlayVideo?: boolean,
24 createdAt?: Date,
25 account?: AccountServerModel,
26 videoChannels?: VideoChannel[]
27
28 blocked?: boolean
29 blockedReason?: string
30 }
31 export class User implements UserServerModel {
32 id: number
33 username: string
34 email: string
35 role: UserRole
36 nsfwPolicy: NSFWPolicyType
37 webTorrentPolicy: WebTorrentPolicyType
38 autoPlayVideo: boolean
39 videoQuota: number
40 videoQuotaDaily: number
41 account: Account
42 videoChannels: VideoChannel[]
43 createdAt: Date
44
45 blocked: boolean
46 blockedReason?: string
47
48 constructor (hash: UserConstructorHash) {
49 this.id = hash.id
50 this.username = hash.username
51 this.email = hash.email
52 this.role = hash.role
53
54 this.videoChannels = hash.videoChannels
55 this.videoQuota = hash.videoQuota
56 this.videoQuotaDaily = hash.videoQuotaDaily
57 this.nsfwPolicy = hash.nsfwPolicy
58 this.webTorrentPolicy = hash.webTorrentPolicy
59 this.autoPlayVideo = hash.autoPlayVideo
60 this.createdAt = hash.createdAt
61 this.blocked = hash.blocked
62 this.blockedReason = hash.blockedReason
63
64 if (hash.account !== undefined) {
65 this.account = new Account(hash.account)
66 }
67 }
68
69 get accountAvatarUrl () {
70 if (!this.account) return ''
71
72 return this.account.avatarUrl
73 }
74
75 hasRight (right: UserRight) {
76 return hasUserRight(this.role, right)
77 }
78
79 patch (obj: UserServerModel) {
80 for (const key of Object.keys(obj)) {
81 this[key] = obj[key]
82 }
83
84 if (obj.account !== undefined) {
85 this.account = new Account(obj.account)
86 }
87 }
88
89 updateAccountAvatar (newAccountAvatar: Avatar) {
90 this.account.updateAvatar(newAccountAvatar)
91 }
92 }