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