]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/menu/menu.component.ts
Fix subtitle download
[github/Chocobozzz/PeerTube.git] / client / src / app / menu / menu.component.ts
CommitLineData
67ed6552 1import { HotkeysService } from 'angular2-hotkeys'
dfe3f7b7
K
2import * as debug from 'debug'
3import { switchMap } from 'rxjs/operators'
27f4a1ec 4import { ViewportScroller } from '@angular/common'
8afc19a6 5import { Component, OnInit, ViewChild } from '@angular/core'
30d55e75 6import { Router } from '@angular/router'
2539932e
C
7import {
8 AuthService,
9 AuthStatus,
10 AuthUser,
8beea2d3
C
11 HooksService,
12 MenuSection,
2539932e
C
13 MenuService,
14 RedirectService,
15 ScreenService,
16 ServerService,
17 UserService
18} from '@app/core'
27f4a1ec 19import { scrollToTop } from '@app/helpers'
8afc19a6 20import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
d3217560 21import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
43a3d281 22import { PeertubeModalService } from '@app/shared/shared-main/peertube-modal/peertube-modal.service'
27f4a1ec 23import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
2989628b 24import { HTMLServerConfig, ServerConfig, UserRight, VideoConstant } from '@shared/models'
602eb142 25
dfe3f7b7
K
26const logger = debug('peertube:menu:MenuComponent')
27
602eb142
C
28@Component({
29 selector: 'my-menu',
383bfc83 30 templateUrl: './menu.component.html',
dfe3f7b7 31 styleUrls: ['./menu.component.scss']
602eb142
C
32})
33export class MenuComponent implements OnInit {
f36da21e 34 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
d3217560 35 @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
51a83970 36 @ViewChild('dropdown') dropdown: NgbDropdown
8afc19a6 37
dfe3f7b7 38 user: AuthUser
df98563e 39 isLoggedIn: boolean
d3217560 40
954605a8 41 userHasAdminAccess = false
4a216666 42 helpVisible = false
954605a8 43
111fdc26 44 videoLanguages: string[] = []
68f6c87a
C
45 nsfwPolicy: string
46
68f6c87a 47 currentInterfaceLanguage: string
111fdc26 48
8beea2d3 49 menuSections: MenuSection[] = []
2539932e 50
111fdc26 51 private languages: VideoConstant<string>[] = []
2989628b
C
52
53 private htmlServerConfig: HTMLServerConfig
ba430d75 54 private serverConfig: ServerConfig
2989628b 55
dfe3f7b7 56 private routesPerRight: { [role in UserRight]?: string } = {
954605a8 57 [UserRight.MANAGE_USERS]: '/admin/users',
4610bc5b 58 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
d95d1559 59 [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses',
3487330d 60 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks',
ad76628b
C
61 [UserRight.MANAGE_JOBS]: '/admin/jobs',
62 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
954605a8 63 }
602eb142
C
64
65 constructor (
f3081d64 66 private viewportScroller: ViewportScroller,
602eb142 67 private authService: AuthService,
d3217560 68 private userService: UserService,
db7af09b 69 private serverService: ServerService,
8c985ef5 70 private redirectService: RedirectService,
d3217560 71 private hotkeysService: HotkeysService,
30d55e75
K
72 private screenService: ScreenService,
73 private menuService: MenuService,
43a3d281 74 private modalService: PeertubeModalService,
8beea2d3
C
75 private router: Router,
76 private hooks: HooksService
27f4a1ec 77 ) { }
51a83970
K
78
79 get isInMobileView () {
80 return this.screenService.isInMobileView()
81 }
82
83 get dropdownContainer () {
27f4a1ec
C
84 if (this.isInMobileView) return null
85
86 return 'body' as 'body'
51a83970
K
87 }
88
89 get language () {
90 return this.languageChooserModal.getCurrentLanguage()
91 }
ca4b1594 92
df98563e 93 ngOnInit () {
2989628b 94 this.htmlServerConfig = this.serverService.getHTMLConfig()
68f6c87a
C
95 this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage()
96
27bc9586 97 this.isLoggedIn = this.authService.isLoggedIn()
8beea2d3
C
98 this.updateUserState()
99 this.buildMenuSections()
100
602eb142
C
101 this.authService.loginChangedSource.subscribe(
102 status => {
103 if (status === AuthStatus.LoggedIn) {
df98563e 104 this.isLoggedIn = true
602eb142 105 } else if (status === AuthStatus.LoggedOut) {
df98563e 106 this.isLoggedIn = false
602eb142 107 }
8beea2d3
C
108
109 this.updateUserState()
110 this.buildMenuSections()
602eb142 111 }
df98563e 112 )
4a216666 113
111fdc26 114 this.hotkeysService.cheatSheetToggle
dfe3f7b7 115 .subscribe(isOpen => this.helpVisible = isOpen)
111fdc26
C
116
117 this.serverService.getVideoLanguages()
dfe3f7b7
K
118 .subscribe(languages => {
119 this.languages = languages
d3217560 120
dfe3f7b7
K
121 this.authService.userInformationLoaded
122 .subscribe(() => this.buildUserLanguages())
123 })
43a3d281 124
125 this.modalService.openQuickSettingsSubject
126 .subscribe(() => this.openQuickSettings())
d3217560
RK
127 }
128
291e8d3e 129 isRegistrationAllowed () {
2989628b
C
130 if (!this.serverConfig) return false
131
ba430d75 132 return this.serverConfig.signup.allowed &&
dfe3f7b7 133 this.serverConfig.signup.allowedForCurrentIP
a184c71b
C
134 }
135
954605a8
C
136 getFirstAdminRightAvailable () {
137 const user = this.authService.getUser()
138 if (!user) return undefined
139
140 const adminRights = [
141 UserRight.MANAGE_USERS,
4610bc5b 142 UserRight.MANAGE_SERVER_FOLLOW,
d95d1559 143 UserRight.MANAGE_ABUSES,
3487330d 144 UserRight.MANAGE_VIDEO_BLACKLIST,
ad76628b
C
145 UserRight.MANAGE_JOBS,
146 UserRight.MANAGE_CONFIGURATION
954605a8
C
147 ]
148
149 for (const adminRight of adminRights) {
150 if (user.hasRight(adminRight)) {
151 return adminRight
152 }
153 }
154
155 return undefined
156 }
157
158 getFirstAdminRouteAvailable () {
159 const right = this.getFirstAdminRightAvailable()
160
161 return this.routesPerRight[right]
602eb142
C
162 }
163
b33f657c
C
164 logout (event: Event) {
165 event.preventDefault()
166
df98563e 167 this.authService.logout()
602eb142 168 // Redirect to home page
b1d40cff 169 this.redirectService.redirectToHomepage()
602eb142 170 }
954605a8 171
8afc19a6
C
172 openLanguageChooser () {
173 this.languageChooserModal.show()
174 }
175
4a216666
RK
176 openHotkeysCheatSheet () {
177 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
178 }
179
d3217560
RK
180 openQuickSettings () {
181 this.quickSettingsModal.show()
182 }
183
184 toggleUseP2P () {
185 if (!this.user) return
186 this.user.webTorrentEnabled = !this.user.webTorrentEnabled
111fdc26
C
187
188 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
dfe3f7b7 189 .subscribe(() => this.authService.refreshUserInformation())
d3217560
RK
190 }
191
8ada87ac 192 langForLocale (localeId: string) {
66357162 193 if (localeId === '_unknown') return $localize`Unknown`
bb3933ef 194
111fdc26
C
195 return this.languages.find(lang => lang.id === localeId).label
196 }
197
f3081d64 198 onActiveLinkScrollToAnchor (link: HTMLAnchorElement) {
30d55e75
K
199 const linkURL = link.getAttribute('href')
200 const linkHash = link.getAttribute('fragment')
201
202 // On same url without fragment restore top scroll position
203 if (!linkHash && this.router.url.includes(linkURL)) {
f3081d64 204 scrollToTop('smooth')
30d55e75
K
205 }
206
207 // On same url with fragment restore anchor scroll position
208 if (linkHash && this.router.url === linkURL) {
f3081d64 209 this.viewportScroller.scrollToAnchor(linkHash)
30d55e75
K
210 }
211
212 if (this.screenService.isInSmallView()) {
213 this.menuService.toggleMenu()
214 }
215 }
216
51a83970
K
217 // Lock menu scroll when menu scroll to avoid fleeing / detached dropdown
218 onMenuScrollEvent () {
219 document.querySelector('menu').scrollTo(0, 0)
220 }
221
222 onDropdownOpenChange (opened: boolean) {
223 if (this.screenService.isInMobileView()) return
224
225 // Close dropdown when window scroll to avoid dropdown quick jump for re-position
226 const onWindowScroll = () => {
56f08761 227 this.dropdown?.close()
51a83970
K
228 window.removeEventListener('scroll', onWindowScroll)
229 }
230
231 if (opened) {
232 window.addEventListener('scroll', onWindowScroll)
233 document.querySelector('menu').scrollTo(0, 0) // Reset menu scroll to easy lock
234 document.querySelector('menu').addEventListener('scroll', this.onMenuScrollEvent)
235 } else {
236 document.querySelector('menu').removeEventListener('scroll', this.onMenuScrollEvent)
237 }
238 }
239
8beea2d3
C
240 private async buildMenuSections () {
241 const menuSections = []
242
243 if (this.isLoggedIn) {
244 menuSections.push(
245 this.menuService.buildLibraryLinks(this.user?.canSeeVideosLink)
246 )
247 }
248
249 menuSections.push(
250 this.menuService.buildCommonLinks(this.htmlServerConfig)
251 )
252
253 this.menuSections = await this.hooks.wrapObject(menuSections, 'common', 'filter:left-menu.links.create.result')
2539932e
C
254 }
255
111fdc26
C
256 private buildUserLanguages () {
257 if (!this.user) {
258 this.videoLanguages = []
259 return
260 }
261
262 if (!this.user.videoLanguages) {
8beea2d3 263 this.videoLanguages = [ $localize`any language` ]
111fdc26
C
264 return
265 }
266
267 this.videoLanguages = this.user.videoLanguages
dfe3f7b7
K
268 .map(locale => this.langForLocale(locale))
269 .map(value => value === undefined ? '?' : value)
d3217560
RK
270 }
271
dfe3f7b7 272 private computeAdminAccess () {
954605a8
C
273 const right = this.getFirstAdminRightAvailable()
274
275 this.userHasAdminAccess = right !== undefined
276 }
dfe3f7b7
K
277
278 private computeVideosLink () {
8beea2d3
C
279 if (!this.isLoggedIn) return
280
dfe3f7b7
K
281 this.authService.userInformationLoaded
282 .pipe(
283 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
284 ).subscribe(res => {
285 if (res === true) logger('User can see videos link.')
286 else logger('User cannot see videos link.')
287 })
288 }
68f6c87a
C
289
290 private computeNSFWPolicy () {
291 if (!this.user) {
292 this.nsfwPolicy = null
293 return
294 }
295
296 switch (this.user.nsfwPolicy) {
297 case 'do_not_list':
298 this.nsfwPolicy = $localize`hide`
299 break
300
301 case 'blur':
302 this.nsfwPolicy = $localize`blur`
303 break
304
305 case 'display':
306 this.nsfwPolicy = $localize`display`
307 break
308 }
309 }
8beea2d3
C
310
311 private updateUserState () {
312 this.user = this.isLoggedIn
313 ? this.authService.getUser()
314 : undefined
315
316 this.computeAdminAccess()
317 this.computeNSFWPolicy()
318 this.computeVideosLink()
319 }
602eb142 320}