]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/users/user.model.ts
Upgrade Angular first step
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / users / user.model.ts
1 import { Account, hasUserRight, User as UserServerModel, UserRight, UserRole, VideoChannel } from '../../../../../shared'
2 import { NSFWPolicyType } from '../../../../../shared/models/videos/nsfw-policy.type'
3 import { Actor } from '@app/shared/actor/actor.model'
4
5 export type UserConstructorHash = {
6 id: number,
7 username: string,
8 email: string,
9 role: UserRole,
10 videoQuota?: number,
11 nsfwPolicy?: NSFWPolicyType,
12 autoPlayVideo?: boolean,
13 createdAt?: Date,
14 account?: Account,
15 videoChannels?: VideoChannel[]
16 }
17 export class User implements UserServerModel {
18 id: number
19 username: string
20 email: string
21 role: UserRole
22 nsfwPolicy: NSFWPolicyType
23 autoPlayVideo: boolean
24 videoQuota: number
25 account: Account
26 videoChannels: VideoChannel[]
27 createdAt: Date
28 accountAvatarUrl: string
29
30 constructor (hash: UserConstructorHash) {
31 this.id = hash.id
32 this.username = hash.username
33 this.email = hash.email
34 this.role = hash.role
35 this.account = hash.account
36
37 if (hash.videoChannels !== undefined) {
38 this.videoChannels = hash.videoChannels
39 }
40
41 if (hash.videoQuota !== undefined) {
42 this.videoQuota = hash.videoQuota
43 }
44
45 if (hash.nsfwPolicy !== undefined) {
46 this.nsfwPolicy = hash.nsfwPolicy
47 }
48
49 if (hash.autoPlayVideo !== undefined) {
50 this.autoPlayVideo = hash.autoPlayVideo
51 }
52
53 if (hash.createdAt !== undefined) {
54 this.createdAt = hash.createdAt
55 }
56
57 this.updateComputedAttributes()
58 }
59
60 hasRight (right: UserRight) {
61 return hasUserRight(this.role, right)
62 }
63
64 patch (obj: UserServerModel) {
65 for (const key of Object.keys(obj)) {
66 this[key] = obj[key]
67 }
68
69 this.updateComputedAttributes()
70 }
71
72 private updateComputedAttributes () {
73 this.accountAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.account)
74 }
75 }