Only use account name in routes
[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 { Actor } from '@app/shared/actor/actor.model'
11 import { Account } from '@app/shared/account/account.model'
12
13 export type UserConstructorHash = {
14   id: number,
15   username: string,
16   email: string,
17   role: UserRole,
18   videoQuota?: number,
19   nsfwPolicy?: NSFWPolicyType,
20   autoPlayVideo?: boolean,
21   createdAt?: Date,
22   account?: AccountServerModel,
23   videoChannels?: VideoChannel[]
24 }
25 export class User implements UserServerModel {
26   id: number
27   username: string
28   email: string
29   role: UserRole
30   nsfwPolicy: NSFWPolicyType
31   autoPlayVideo: boolean
32   videoQuota: number
33   account: Account
34   videoChannels: VideoChannel[]
35   createdAt: Date
36   accountAvatarUrl: string
37
38   constructor (hash: UserConstructorHash) {
39     this.id = hash.id
40     this.username = hash.username
41     this.email = hash.email
42     this.role = hash.role
43
44     if (hash.account !== undefined) {
45       this.account = new Account(hash.account)
46     }
47
48     if (hash.videoChannels !== undefined) {
49       this.videoChannels = hash.videoChannels
50     }
51
52     if (hash.videoQuota !== undefined) {
53       this.videoQuota = hash.videoQuota
54     }
55
56     if (hash.nsfwPolicy !== undefined) {
57       this.nsfwPolicy = hash.nsfwPolicy
58     }
59
60     if (hash.autoPlayVideo !== undefined) {
61       this.autoPlayVideo = hash.autoPlayVideo
62     }
63
64     if (hash.createdAt !== undefined) {
65       this.createdAt = hash.createdAt
66     }
67
68     this.updateComputedAttributes()
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     this.updateComputedAttributes()
85   }
86
87   private updateComputedAttributes () {
88     this.accountAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.account)
89   }
90 }