]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
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 | } |