provide specific engine boundaries for nodejs and yarn
[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   videosCount?: number
55   videoAbusesCount?: number
56   videoAbusesAcceptedCount?: number
57   videoAbusesCreatedCount?: number
58   videoCommentsCount?: number
59
60   theme: string
61
62   account: Account
63   notificationSettings?: UserNotificationSetting
64   videoChannels?: VideoChannel[]
65
66   blocked: boolean
67   blockedReason?: string
68
69   noInstanceConfigWarningModal: boolean
70   noWelcomeModal: boolean
71
72   pluginAuth: string | null
73
74   lastLoginDate: Date | null
75
76   createdAt: Date
77
78   constructor (hash: Partial<UserServerModel>) {
79     this.id = hash.id
80     this.username = hash.username
81     this.email = hash.email
82
83     this.role = hash.role
84
85     this.videoChannels = hash.videoChannels
86
87     this.videoQuota = hash.videoQuota
88     this.videoQuotaDaily = hash.videoQuotaDaily
89     this.videoQuotaUsed = hash.videoQuotaUsed
90     this.videoQuotaUsedDaily = hash.videoQuotaUsedDaily
91     this.videosCount = hash.videosCount
92     this.videoAbusesCount = hash.videoAbusesCount
93     this.videoAbusesAcceptedCount = hash.videoAbusesAcceptedCount
94     this.videoAbusesCreatedCount = hash.videoAbusesCreatedCount
95     this.videoCommentsCount = hash.videoCommentsCount
96
97     this.nsfwPolicy = hash.nsfwPolicy
98     this.webTorrentEnabled = hash.webTorrentEnabled
99     this.autoPlayVideo = hash.autoPlayVideo
100     this.autoPlayNextVideo = hash.autoPlayNextVideo
101     this.autoPlayNextVideoPlaylist = hash.autoPlayNextVideoPlaylist
102     this.videosHistoryEnabled = hash.videosHistoryEnabled
103     this.videoLanguages = hash.videoLanguages
104
105     this.theme = hash.theme
106
107     this.adminFlags = hash.adminFlags
108
109     this.blocked = hash.blocked
110     this.blockedReason = hash.blockedReason
111
112     this.noInstanceConfigWarningModal = hash.noInstanceConfigWarningModal
113     this.noWelcomeModal = hash.noWelcomeModal
114
115     this.notificationSettings = hash.notificationSettings
116
117     this.createdAt = hash.createdAt
118
119     this.pluginAuth = hash.pluginAuth
120     this.lastLoginDate = hash.lastLoginDate
121
122     if (hash.account !== undefined) {
123       this.account = new Account(hash.account)
124     }
125   }
126
127   get accountAvatarUrl () {
128     if (!this.account) return ''
129
130     return this.account.avatarUrl
131   }
132
133   hasRight (right: UserRight) {
134     return hasUserRight(this.role, right)
135   }
136
137   patch (obj: UserServerModel) {
138     for (const key of Object.keys(obj)) {
139       this[key] = obj[key]
140     }
141
142     if (obj.account !== undefined) {
143       this.account = new Account(obj.account)
144     }
145   }
146
147   updateAccountAvatar (newAccountAvatar: Avatar) {
148     this.account.updateAvatar(newAccountAvatar)
149   }
150 }