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