]>
Commit | Line | Data |
---|---|---|
1 | import { HotkeysService } from 'angular2-hotkeys' | |
2 | import * as debug from 'debug' | |
3 | import { switchMap } from 'rxjs/operators' | |
4 | import { ViewportScroller } from '@angular/common' | |
5 | import { Component, OnInit, ViewChild } from '@angular/core' | |
6 | import { Router } from '@angular/router' | |
7 | import { | |
8 | AuthService, | |
9 | AuthStatus, | |
10 | AuthUser, | |
11 | HooksService, | |
12 | MenuSection, | |
13 | MenuService, | |
14 | RedirectService, | |
15 | ScreenService, | |
16 | ServerService, | |
17 | UserService | |
18 | } from '@app/core' | |
19 | import { scrollToTop } from '@app/helpers' | |
20 | import { LanguageChooserComponent } from '@app/menu/language-chooser.component' | |
21 | import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component' | |
22 | import { PeertubeModalService } from '@app/shared/shared-main/peertube-modal/peertube-modal.service' | |
23 | import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap' | |
24 | import { HTMLServerConfig, ServerConfig, UserRight, VideoConstant } from '@shared/models' | |
25 | ||
26 | const debugLogger = debug('peertube:menu:MenuComponent') | |
27 | ||
28 | @Component({ | |
29 | selector: 'my-menu', | |
30 | templateUrl: './menu.component.html', | |
31 | styleUrls: [ './menu.component.scss' ] | |
32 | }) | |
33 | export class MenuComponent implements OnInit { | |
34 | @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent | |
35 | @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent | |
36 | @ViewChild('dropdown') dropdown: NgbDropdown | |
37 | ||
38 | user: AuthUser | |
39 | isLoggedIn: boolean | |
40 | ||
41 | userHasAdminAccess = false | |
42 | helpVisible = false | |
43 | ||
44 | videoLanguages: string[] = [] | |
45 | nsfwPolicy: string | |
46 | ||
47 | currentInterfaceLanguage: string | |
48 | ||
49 | menuSections: MenuSection[] = [] | |
50 | ||
51 | private languages: VideoConstant<string>[] = [] | |
52 | ||
53 | private htmlServerConfig: HTMLServerConfig | |
54 | private serverConfig: ServerConfig | |
55 | ||
56 | private routesPerRight: { [role in UserRight]?: string } = { | |
57 | [UserRight.MANAGE_USERS]: '/admin/users', | |
58 | [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends', | |
59 | [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses', | |
60 | [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks', | |
61 | [UserRight.MANAGE_JOBS]: '/admin/jobs', | |
62 | [UserRight.MANAGE_CONFIGURATION]: '/admin/config' | |
63 | } | |
64 | ||
65 | constructor ( | |
66 | private viewportScroller: ViewportScroller, | |
67 | private authService: AuthService, | |
68 | private userService: UserService, | |
69 | private serverService: ServerService, | |
70 | private redirectService: RedirectService, | |
71 | private hotkeysService: HotkeysService, | |
72 | private screenService: ScreenService, | |
73 | private menuService: MenuService, | |
74 | private modalService: PeertubeModalService, | |
75 | private router: Router, | |
76 | private hooks: HooksService | |
77 | ) { } | |
78 | ||
79 | get isInMobileView () { | |
80 | return this.screenService.isInMobileView() | |
81 | } | |
82 | ||
83 | get dropdownContainer () { | |
84 | if (this.isInMobileView) return null | |
85 | ||
86 | return 'body' as 'body' | |
87 | } | |
88 | ||
89 | get language () { | |
90 | return this.languageChooserModal.getCurrentLanguage() | |
91 | } | |
92 | ||
93 | get requiresApproval () { | |
94 | return this.serverConfig.signup.requiresApproval | |
95 | } | |
96 | ||
97 | ngOnInit () { | |
98 | this.htmlServerConfig = this.serverService.getHTMLConfig() | |
99 | this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage() | |
100 | ||
101 | this.isLoggedIn = this.authService.isLoggedIn() | |
102 | this.updateUserState() | |
103 | this.buildMenuSections() | |
104 | ||
105 | this.authService.loginChangedSource.subscribe( | |
106 | status => { | |
107 | if (status === AuthStatus.LoggedIn) { | |
108 | this.isLoggedIn = true | |
109 | } else if (status === AuthStatus.LoggedOut) { | |
110 | this.isLoggedIn = false | |
111 | } | |
112 | ||
113 | this.updateUserState() | |
114 | this.buildMenuSections() | |
115 | } | |
116 | ) | |
117 | ||
118 | this.hotkeysService.cheatSheetToggle | |
119 | .subscribe(isOpen => this.helpVisible = isOpen) | |
120 | ||
121 | this.serverService.getVideoLanguages() | |
122 | .subscribe(languages => { | |
123 | this.languages = languages | |
124 | ||
125 | this.authService.userInformationLoaded | |
126 | .subscribe(() => this.buildUserLanguages()) | |
127 | }) | |
128 | ||
129 | this.serverService.getConfig() | |
130 | .subscribe(config => this.serverConfig = config) | |
131 | ||
132 | this.modalService.openQuickSettingsSubject | |
133 | .subscribe(() => this.openQuickSettings()) | |
134 | } | |
135 | ||
136 | isRegistrationAllowed () { | |
137 | if (!this.serverConfig) return false | |
138 | ||
139 | return this.serverConfig.signup.allowed && | |
140 | this.serverConfig.signup.allowedForCurrentIP | |
141 | } | |
142 | ||
143 | getFirstAdminRightAvailable () { | |
144 | const user = this.authService.getUser() | |
145 | if (!user) return undefined | |
146 | ||
147 | const adminRights = [ | |
148 | UserRight.MANAGE_USERS, | |
149 | UserRight.MANAGE_SERVER_FOLLOW, | |
150 | UserRight.MANAGE_ABUSES, | |
151 | UserRight.MANAGE_VIDEO_BLACKLIST, | |
152 | UserRight.MANAGE_JOBS, | |
153 | UserRight.MANAGE_CONFIGURATION | |
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] | |
169 | } | |
170 | ||
171 | logout (event: Event) { | |
172 | event.preventDefault() | |
173 | ||
174 | this.authService.logout() | |
175 | // Redirect to home page | |
176 | this.redirectService.redirectToHomepage() | |
177 | } | |
178 | ||
179 | openLanguageChooser () { | |
180 | this.languageChooserModal.show() | |
181 | } | |
182 | ||
183 | openHotkeysCheatSheet () { | |
184 | this.hotkeysService.cheatSheetToggle.next(!this.helpVisible) | |
185 | } | |
186 | ||
187 | openQuickSettings () { | |
188 | this.quickSettingsModal.show() | |
189 | } | |
190 | ||
191 | toggleUseP2P () { | |
192 | if (!this.user) return | |
193 | this.user.p2pEnabled = !this.user.p2pEnabled | |
194 | ||
195 | this.userService.updateMyProfile({ p2pEnabled: this.user.p2pEnabled }) | |
196 | .subscribe(() => this.authService.refreshUserInformation()) | |
197 | } | |
198 | ||
199 | langForLocale (localeId: string) { | |
200 | if (localeId === '_unknown') return $localize`Unknown` | |
201 | ||
202 | return this.languages.find(lang => lang.id === localeId).label | |
203 | } | |
204 | ||
205 | onActiveLinkScrollToAnchor (link: HTMLAnchorElement) { | |
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)) { | |
211 | scrollToTop('smooth') | |
212 | } | |
213 | ||
214 | // On same url with fragment restore anchor scroll position | |
215 | if (linkHash && this.router.url === linkURL) { | |
216 | this.viewportScroller.scrollToAnchor(linkHash) | |
217 | } | |
218 | ||
219 | if (this.screenService.isInSmallView()) { | |
220 | this.menuService.toggleMenu() | |
221 | } | |
222 | } | |
223 | ||
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 = () => { | |
234 | this.dropdown?.close() | |
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 | ||
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') | |
261 | } | |
262 | ||
263 | private buildUserLanguages () { | |
264 | if (!this.user) { | |
265 | this.videoLanguages = [] | |
266 | return | |
267 | } | |
268 | ||
269 | if (!this.user.videoLanguages) { | |
270 | this.videoLanguages = [ $localize`any language` ] | |
271 | return | |
272 | } | |
273 | ||
274 | this.videoLanguages = this.user.videoLanguages | |
275 | .map(locale => this.langForLocale(locale)) | |
276 | .map(value => value === undefined ? '?' : value) | |
277 | } | |
278 | ||
279 | private computeAdminAccess () { | |
280 | const right = this.getFirstAdminRightAvailable() | |
281 | ||
282 | this.userHasAdminAccess = right !== undefined | |
283 | } | |
284 | ||
285 | private computeVideosLink () { | |
286 | if (!this.isLoggedIn) return | |
287 | ||
288 | this.authService.userInformationLoaded | |
289 | .pipe( | |
290 | switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed())) | |
291 | ).subscribe(res => { | |
292 | if (res === true) debugLogger('User can see videos link.') | |
293 | else debugLogger('User cannot see videos link.') | |
294 | }) | |
295 | } | |
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 | } | |
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 | } | |
327 | } |