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