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