]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/menu/menu.component.ts
fa104cc43676b449a4f9cf6d64ccbe6f48aaecb0
[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 { ViewportScroller } from '@angular/common'
5 import { Component, OnInit, ViewChild } from '@angular/core'
6 import { Router } from '@angular/router'
7 import {
8 AuthService,
9 AuthStatus,
10 AuthUser,
11 HooksService,
12 MenuSection,
13 MenuService,
14 RedirectService,
15 ScreenService,
16 ServerService,
17 UserService
18 } from '@app/core'
19 import { scrollToTop } from '@app/helpers'
20 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
21 import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
22 import { PeertubeModalService } from '@app/shared/shared-main/peertube-modal/peertube-modal.service'
23 import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
24 import { HTMLServerConfig, ServerConfig, UserRight, VideoConstant } from '@shared/models'
25
26 const logger = debug('peertube:menu:MenuComponent')
27
28 @Component({
29 selector: 'my-menu',
30 templateUrl: './menu.component.html',
31 styleUrls: ['./menu.component.scss']
32 })
33 export class MenuComponent implements OnInit {
34 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
35 @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
36 @ViewChild('dropdown') dropdown: NgbDropdown
37
38 user: AuthUser
39 isLoggedIn: boolean
40
41 userHasAdminAccess = false
42 helpVisible = false
43
44 videoLanguages: string[] = []
45 nsfwPolicy: string
46
47 currentInterfaceLanguage: string
48
49 menuSections: MenuSection[] = []
50
51 private languages: VideoConstant<string>[] = []
52
53 private htmlServerConfig: HTMLServerConfig
54 private serverConfig: ServerConfig
55
56 private routesPerRight: { [role in UserRight]?: string } = {
57 [UserRight.MANAGE_USERS]: '/admin/users',
58 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
59 [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses',
60 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks',
61 [UserRight.MANAGE_JOBS]: '/admin/jobs',
62 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
63 }
64
65 constructor (
66 private viewportScroller: ViewportScroller,
67 private authService: AuthService,
68 private userService: UserService,
69 private serverService: ServerService,
70 private redirectService: RedirectService,
71 private hotkeysService: HotkeysService,
72 private screenService: ScreenService,
73 private menuService: MenuService,
74 private modalService: PeertubeModalService,
75 private router: Router,
76 private hooks: HooksService
77 ) { }
78
79 get isInMobileView () {
80 return this.screenService.isInMobileView()
81 }
82
83 get dropdownContainer () {
84 if (this.isInMobileView) return null
85
86 return 'body' as 'body'
87 }
88
89 get language () {
90 return this.languageChooserModal.getCurrentLanguage()
91 }
92
93 ngOnInit () {
94 this.htmlServerConfig = this.serverService.getHTMLConfig()
95 this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage()
96
97 this.updateUserState()
98 this.buildMenuSections()
99
100 this.authService.loginChangedSource.subscribe(
101 status => {
102 if (status === AuthStatus.LoggedIn) {
103 this.isLoggedIn = true
104 } else if (status === AuthStatus.LoggedOut) {
105 this.isLoggedIn = false
106 }
107
108 this.updateUserState()
109 this.buildMenuSections()
110 }
111 )
112
113 this.hotkeysService.cheatSheetToggle
114 .subscribe(isOpen => this.helpVisible = isOpen)
115
116 this.serverService.getVideoLanguages()
117 .subscribe(languages => {
118 this.languages = languages
119
120 this.authService.userInformationLoaded
121 .subscribe(() => this.buildUserLanguages())
122 })
123
124 this.modalService.openQuickSettingsSubject
125 .subscribe(() => this.openQuickSettings())
126 }
127
128 isRegistrationAllowed () {
129 if (!this.serverConfig) return false
130
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 $localize`Unknown`
193
194 return this.languages.find(lang => lang.id === localeId).label
195 }
196
197 onActiveLinkScrollToAnchor (link: HTMLAnchorElement) {
198 const linkURL = link.getAttribute('href')
199 const linkHash = link.getAttribute('fragment')
200
201 // On same url without fragment restore top scroll position
202 if (!linkHash && this.router.url.includes(linkURL)) {
203 scrollToTop('smooth')
204 }
205
206 // On same url with fragment restore anchor scroll position
207 if (linkHash && this.router.url === linkURL) {
208 this.viewportScroller.scrollToAnchor(linkHash)
209 }
210
211 if (this.screenService.isInSmallView()) {
212 this.menuService.toggleMenu()
213 }
214 }
215
216 // Lock menu scroll when menu scroll to avoid fleeing / detached dropdown
217 onMenuScrollEvent () {
218 document.querySelector('menu').scrollTo(0, 0)
219 }
220
221 onDropdownOpenChange (opened: boolean) {
222 if (this.screenService.isInMobileView()) return
223
224 // Close dropdown when window scroll to avoid dropdown quick jump for re-position
225 const onWindowScroll = () => {
226 this.dropdown?.close()
227 window.removeEventListener('scroll', onWindowScroll)
228 }
229
230 if (opened) {
231 window.addEventListener('scroll', onWindowScroll)
232 document.querySelector('menu').scrollTo(0, 0) // Reset menu scroll to easy lock
233 document.querySelector('menu').addEventListener('scroll', this.onMenuScrollEvent)
234 } else {
235 document.querySelector('menu').removeEventListener('scroll', this.onMenuScrollEvent)
236 }
237 }
238
239 private async buildMenuSections () {
240 const menuSections = []
241
242 if (this.isLoggedIn) {
243 menuSections.push(
244 this.menuService.buildLibraryLinks(this.user?.canSeeVideosLink)
245 )
246 }
247
248 menuSections.push(
249 this.menuService.buildCommonLinks(this.htmlServerConfig)
250 )
251
252 this.menuSections = await this.hooks.wrapObject(menuSections, 'common', 'filter:left-menu.links.create.result')
253 }
254
255 private buildUserLanguages () {
256 if (!this.user) {
257 this.videoLanguages = []
258 return
259 }
260
261 if (!this.user.videoLanguages) {
262 this.videoLanguages = [ $localize`any language` ]
263 return
264 }
265
266 this.videoLanguages = this.user.videoLanguages
267 .map(locale => this.langForLocale(locale))
268 .map(value => value === undefined ? '?' : value)
269 }
270
271 private computeAdminAccess () {
272 const right = this.getFirstAdminRightAvailable()
273
274 this.userHasAdminAccess = right !== undefined
275 }
276
277 private computeVideosLink () {
278 if (!this.isLoggedIn) return
279
280 this.authService.userInformationLoaded
281 .pipe(
282 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
283 ).subscribe(res => {
284 if (res === true) logger('User can see videos link.')
285 else logger('User cannot see videos link.')
286 })
287 }
288
289 private computeNSFWPolicy () {
290 if (!this.user) {
291 this.nsfwPolicy = null
292 return
293 }
294
295 switch (this.user.nsfwPolicy) {
296 case 'do_not_list':
297 this.nsfwPolicy = $localize`hide`
298 break
299
300 case 'blur':
301 this.nsfwPolicy = $localize`blur`
302 break
303
304 case 'display':
305 this.nsfwPolicy = $localize`display`
306 break
307 }
308 }
309
310 private updateUserState () {
311 this.user = this.isLoggedIn
312 ? this.authService.getUser()
313 : undefined
314
315 this.computeAdminAccess()
316 this.computeNSFWPolicy()
317 this.computeVideosLink()
318 }
319 }