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