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