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