]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/menu/menu.component.ts
Add alert and hide upload view when no upload is possible (#2966)
[github/Chocobozzz/PeerTube.git] / client / src / app / menu / menu.component.ts
1 import { HotkeysService } from 'angular2-hotkeys'
2 import * as debug from 'debug'
3 import { switchMap } from 'rxjs/operators'
4 import { Component, OnInit, ViewChild } from '@angular/core'
5 import { AuthService, AuthStatus, AuthUser, RedirectService, ScreenService, ServerService, UserService } from '@app/core'
6 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
7 import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { ServerConfig, UserRight, VideoConstant } from '@shared/models'
10
11 const logger = debug('peertube:menu:MenuComponent')
12
13 @Component({
14 selector: 'my-menu',
15 templateUrl: './menu.component.html',
16 styleUrls: ['./menu.component.scss']
17 })
18 export class MenuComponent implements OnInit {
19 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
20 @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
21
22 user: AuthUser
23 isLoggedIn: boolean
24
25 userHasAdminAccess = false
26 helpVisible = false
27
28 videoLanguages: string[] = []
29
30 private languages: VideoConstant<string>[] = []
31 private serverConfig: ServerConfig
32 private routesPerRight: { [role in UserRight]?: string } = {
33 [UserRight.MANAGE_USERS]: '/admin/users',
34 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
35 [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses',
36 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks',
37 [UserRight.MANAGE_JOBS]: '/admin/jobs',
38 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
39 }
40
41 constructor (
42 private authService: AuthService,
43 private userService: UserService,
44 private serverService: ServerService,
45 private redirectService: RedirectService,
46 private hotkeysService: HotkeysService,
47 private screenService: ScreenService,
48 private i18n: I18n
49 ) { }
50
51 get isInMobileView () {
52 return this.screenService.isInMobileView()
53 }
54
55 get placement () {
56 if (this.isInMobileView) {
57 return 'left-top auto'
58 } else {
59 return 'right-top auto'
60 }
61 }
62
63 ngOnInit () {
64 this.serverConfig = this.serverService.getTmpConfig()
65 this.serverService.getConfig()
66 .subscribe(config => this.serverConfig = config)
67
68 this.isLoggedIn = this.authService.isLoggedIn()
69 if (this.isLoggedIn === true) {
70 this.user = this.authService.getUser()
71 this.computeVideosLink()
72 }
73
74 this.computeAdminAccess()
75
76 this.authService.loginChangedSource.subscribe(
77 status => {
78 if (status === AuthStatus.LoggedIn) {
79 this.isLoggedIn = true
80 this.user = this.authService.getUser()
81
82 this.computeAdminAccess()
83 this.computeVideosLink()
84
85 logger('Logged in.')
86 } else if (status === AuthStatus.LoggedOut) {
87 this.isLoggedIn = false
88 this.user = undefined
89
90 this.computeAdminAccess()
91
92 logger('Logged out.')
93 } else {
94 console.error('Unknown auth status: ' + status)
95 }
96 }
97 )
98
99 this.hotkeysService.cheatSheetToggle
100 .subscribe(isOpen => this.helpVisible = isOpen)
101
102 this.serverService.getVideoLanguages()
103 .subscribe(languages => {
104 this.languages = languages
105
106 this.authService.userInformationLoaded
107 .subscribe(() => this.buildUserLanguages())
108 })
109 }
110
111 get language () {
112 return this.languageChooserModal.getCurrentLanguage()
113 }
114
115 get nsfwPolicy () {
116 if (!this.user) return
117
118 switch (this.user.nsfwPolicy) {
119 case 'do_not_list':
120 return this.i18n('hide')
121
122 case 'blur':
123 return this.i18n('blur')
124
125 case 'display':
126 return this.i18n('display')
127 }
128 }
129
130 isRegistrationAllowed () {
131 return this.serverConfig.signup.allowed &&
132 this.serverConfig.signup.allowedForCurrentIP
133 }
134
135 getFirstAdminRightAvailable () {
136 const user = this.authService.getUser()
137 if (!user) return undefined
138
139 const adminRights = [
140 UserRight.MANAGE_USERS,
141 UserRight.MANAGE_SERVER_FOLLOW,
142 UserRight.MANAGE_ABUSES,
143 UserRight.MANAGE_VIDEO_BLACKLIST,
144 UserRight.MANAGE_JOBS,
145 UserRight.MANAGE_CONFIGURATION
146 ]
147
148 for (const adminRight of adminRights) {
149 if (user.hasRight(adminRight)) {
150 return adminRight
151 }
152 }
153
154 return undefined
155 }
156
157 getFirstAdminRouteAvailable () {
158 const right = this.getFirstAdminRightAvailable()
159
160 return this.routesPerRight[right]
161 }
162
163 logout (event: Event) {
164 event.preventDefault()
165
166 this.authService.logout()
167 // Redirect to home page
168 this.redirectService.redirectToHomepage()
169 }
170
171 openLanguageChooser () {
172 this.languageChooserModal.show()
173 }
174
175 openHotkeysCheatSheet () {
176 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
177 }
178
179 openQuickSettings () {
180 this.quickSettingsModal.show()
181 }
182
183 toggleUseP2P () {
184 if (!this.user) return
185 this.user.webTorrentEnabled = !this.user.webTorrentEnabled
186
187 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
188 .subscribe(() => this.authService.refreshUserInformation())
189 }
190
191 langForLocale (localeId: string) {
192 if (localeId === '_unknown') return this.i18n('Unknown')
193
194 return this.languages.find(lang => lang.id === localeId).label
195 }
196
197 private buildUserLanguages () {
198 if (!this.user) {
199 this.videoLanguages = []
200 return
201 }
202
203 if (!this.user.videoLanguages) {
204 this.videoLanguages = [this.i18n('any language')]
205 return
206 }
207
208 this.videoLanguages = this.user.videoLanguages
209 .map(locale => this.langForLocale(locale))
210 .map(value => value === undefined ? '?' : value)
211 }
212
213 private computeAdminAccess () {
214 const right = this.getFirstAdminRightAvailable()
215
216 this.userHasAdminAccess = right !== undefined
217 }
218
219 private computeVideosLink () {
220 this.authService.userInformationLoaded
221 .pipe(
222 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
223 ).subscribe(res => {
224 if (res === true) logger('User can see videos link.')
225 else logger('User cannot see videos link.')
226 })
227 }
228 }