a37cae749c1e64d596111c4a680651e1cb09ed8e
[oweals/peertube.git] / client / src / app / shared / users / user.model.ts
1 import {
2   hasUserRight,
3   User as UserServerModel,
4   UserNotificationSetting,
5   UserRight,
6   UserRole
7 } from '../../../../../shared/models/users'
8 import { VideoChannel } from '../../../../../shared/models/videos'
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 { UserAdminFlag } from '@shared/models/users/user-flag.model'
13
14 export class User implements UserServerModel {
15   static KEYS = {
16     ID: 'id',
17     ROLE: 'role',
18     EMAIL: 'email',
19     VIDEOS_HISTORY_ENABLED: 'videos-history-enabled',
20     USERNAME: 'username',
21     NSFW_POLICY: 'nsfw_policy',
22     WEBTORRENT_ENABLED: 'peertube-videojs-' + 'webtorrent_enabled',
23     AUTO_PLAY_VIDEO: 'auto_play_video',
24     SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO: 'auto_play_next_video',
25     AUTO_PLAY_VIDEO_PLAYLIST: 'auto_play_video_playlist',
26     THEME: 'last_active_theme',
27     VIDEO_LANGUAGES: 'video_languages'
28   }
29
30   id: number
31   username: string
32   email: string
33   pendingEmail: string | null
34
35   emailVerified: boolean
36   nsfwPolicy: NSFWPolicyType
37
38   adminFlags?: UserAdminFlag
39
40   autoPlayVideo: boolean
41   autoPlayNextVideo: boolean
42   autoPlayNextVideoPlaylist: boolean
43   webTorrentEnabled: boolean
44   videosHistoryEnabled: boolean
45   videoLanguages: string[]
46
47   role: UserRole
48   roleLabel: string
49
50   videoQuota: number
51   videoQuotaDaily: number
52   videoQuotaUsed?: number
53   videoQuotaUsedDaily?: number
54
55   theme: string
56
57   account: Account
58   notificationSettings?: UserNotificationSetting
59   videoChannels?: VideoChannel[]
60
61   blocked: boolean
62   blockedReason?: string
63
64   noInstanceConfigWarningModal: boolean
65   noWelcomeModal: boolean
66
67   createdAt: Date
68
69   constructor (hash: Partial<UserServerModel>) {
70     this.id = hash.id
71     this.username = hash.username
72     this.email = hash.email
73
74     this.role = hash.role
75
76     this.videoChannels = hash.videoChannels
77
78     this.videoQuota = hash.videoQuota
79     this.videoQuotaDaily = hash.videoQuotaDaily
80     this.videoQuotaUsed = hash.videoQuotaUsed
81     this.videoQuotaUsedDaily = hash.videoQuotaUsedDaily
82
83     this.nsfwPolicy = hash.nsfwPolicy
84     this.webTorrentEnabled = hash.webTorrentEnabled
85     this.autoPlayVideo = hash.autoPlayVideo
86     this.autoPlayNextVideo = hash.autoPlayNextVideo
87     this.autoPlayNextVideoPlaylist = hash.autoPlayNextVideoPlaylist
88     this.videosHistoryEnabled = hash.videosHistoryEnabled
89     this.videoLanguages = hash.videoLanguages
90
91     this.theme = hash.theme
92
93     this.adminFlags = hash.adminFlags
94
95     this.blocked = hash.blocked
96     this.blockedReason = hash.blockedReason
97
98     this.noInstanceConfigWarningModal = hash.noInstanceConfigWarningModal
99     this.noWelcomeModal = hash.noWelcomeModal
100
101     this.notificationSettings = hash.notificationSettings
102
103     this.createdAt = hash.createdAt
104
105     if (hash.account !== undefined) {
106       this.account = new Account(hash.account)
107     }
108   }
109
110   get accountAvatarUrl () {
111     if (!this.account) return ''
112
113     return this.account.avatarUrl
114   }
115
116   hasRight (right: UserRight) {
117     return hasUserRight(this.role, right)
118   }
119
120   patch (obj: UserServerModel) {
121     for (const key of Object.keys(obj)) {
122       this[key] = obj[key]
123     }
124
125     if (obj.account !== undefined) {
126       this.account = new Account(obj.account)
127     }
128   }
129
130   updateAccountAvatar (newAccountAvatar: Avatar) {
131     this.account.updateAvatar(newAccountAvatar)
132   }
133 }