]>
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 | MenuLink, | |
12 | MenuService, | |
13 | RedirectService, | |
14 | ScreenService, | |
15 | ServerService, | |
16 | UserService | |
17 | } from '@app/core' | |
18 | import { scrollToTop } from '@app/helpers' | |
19 | import { LanguageChooserComponent } from '@app/menu/language-chooser.component' | |
20 | import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component' | |
21 | import { PeertubeModalService } from '@app/shared/shared-main/peertube-modal/peertube-modal.service' | |
22 | import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap' | |
23 | import { HTMLServerConfig, ServerConfig, UserRight, VideoConstant } from '@shared/models' | |
24 | ||
25 | const logger = debug('peertube:menu:MenuComponent') | |
26 | ||
27 | @Component({ | |
28 | selector: 'my-menu', | |
29 | templateUrl: './menu.component.html', | |
30 | styleUrls: ['./menu.component.scss'] | |
31 | }) | |
32 | export class MenuComponent implements OnInit { | |
33 | @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent | |
34 | @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent | |
35 | @ViewChild('dropdown') dropdown: NgbDropdown | |
36 | ||
37 | user: AuthUser | |
38 | isLoggedIn: boolean | |
39 | ||
40 | userHasAdminAccess = false | |
41 | helpVisible = false | |
42 | ||
43 | videoLanguages: string[] = [] | |
44 | nsfwPolicy: string | |
45 | ||
46 | currentInterfaceLanguage: string | |
47 | ||
48 | commonMenuLinks: MenuLink[] = [] | |
49 | ||
50 | private languages: VideoConstant<string>[] = [] | |
51 | ||
52 | private htmlServerConfig: HTMLServerConfig | |
53 | private serverConfig: ServerConfig | |
54 | ||
55 | private routesPerRight: { [role in UserRight]?: string } = { | |
56 | [UserRight.MANAGE_USERS]: '/admin/users', | |
57 | [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends', | |
58 | [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses', | |
59 | [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks', | |
60 | [UserRight.MANAGE_JOBS]: '/admin/jobs', | |
61 | [UserRight.MANAGE_CONFIGURATION]: '/admin/config' | |
62 | } | |
63 | ||
64 | constructor ( | |
65 | private viewportScroller: ViewportScroller, | |
66 | private authService: AuthService, | |
67 | private userService: UserService, | |
68 | private serverService: ServerService, | |
69 | private redirectService: RedirectService, | |
70 | private hotkeysService: HotkeysService, | |
71 | private screenService: ScreenService, | |
72 | private menuService: MenuService, | |
73 | private modalService: PeertubeModalService, | |
74 | private router: Router | |
75 | ) { } | |
76 | ||
77 | get isInMobileView () { | |
78 | return this.screenService.isInMobileView() | |
79 | } | |
80 | ||
81 | get dropdownContainer () { | |
82 | if (this.isInMobileView) return null | |
83 | ||
84 | return 'body' as 'body' | |
85 | } | |
86 | ||
87 | get language () { | |
88 | return this.languageChooserModal.getCurrentLanguage() | |
89 | } | |
90 | ||
91 | get instanceName () { | |
92 | return this.htmlServerConfig.instance.name | |
93 | } | |
94 | ||
95 | ngOnInit () { | |
96 | this.htmlServerConfig = this.serverService.getHTMLConfig() | |
97 | this.buildMenuLinks() | |
98 | ||
99 | this.isLoggedIn = this.authService.isLoggedIn() | |
100 | if (this.isLoggedIn === true) { | |
101 | this.user = this.authService.getUser() | |
102 | ||
103 | this.computeNSFWPolicy() | |
104 | this.computeVideosLink() | |
105 | } | |
106 | ||
107 | this.computeAdminAccess() | |
108 | ||
109 | this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage() | |
110 | ||
111 | this.authService.loginChangedSource.subscribe( | |
112 | status => { | |
113 | if (status === AuthStatus.LoggedIn) { | |
114 | this.isLoggedIn = true | |
115 | this.user = this.authService.getUser() | |
116 | ||
117 | this.computeAdminAccess() | |
118 | this.computeVideosLink() | |
119 | ||
120 | logger('Logged in.') | |
121 | } else if (status === AuthStatus.LoggedOut) { | |
122 | this.isLoggedIn = false | |
123 | this.user = undefined | |
124 | ||
125 | this.computeAdminAccess() | |
126 | ||
127 | logger('Logged out.') | |
128 | } else { | |
129 | console.error('Unknown auth status: ' + status) | |
130 | } | |
131 | } | |
132 | ) | |
133 | ||
134 | this.hotkeysService.cheatSheetToggle | |
135 | .subscribe(isOpen => this.helpVisible = isOpen) | |
136 | ||
137 | this.serverService.getVideoLanguages() | |
138 | .subscribe(languages => { | |
139 | this.languages = languages | |
140 | ||
141 | this.authService.userInformationLoaded | |
142 | .subscribe(() => this.buildUserLanguages()) | |
143 | }) | |
144 | ||
145 | this.modalService.openQuickSettingsSubject | |
146 | .subscribe(() => this.openQuickSettings()) | |
147 | } | |
148 | ||
149 | isRegistrationAllowed () { | |
150 | if (!this.serverConfig) return false | |
151 | ||
152 | return this.serverConfig.signup.allowed && | |
153 | this.serverConfig.signup.allowedForCurrentIP | |
154 | } | |
155 | ||
156 | getFirstAdminRightAvailable () { | |
157 | const user = this.authService.getUser() | |
158 | if (!user) return undefined | |
159 | ||
160 | const adminRights = [ | |
161 | UserRight.MANAGE_USERS, | |
162 | UserRight.MANAGE_SERVER_FOLLOW, | |
163 | UserRight.MANAGE_ABUSES, | |
164 | UserRight.MANAGE_VIDEO_BLACKLIST, | |
165 | UserRight.MANAGE_JOBS, | |
166 | UserRight.MANAGE_CONFIGURATION | |
167 | ] | |
168 | ||
169 | for (const adminRight of adminRights) { | |
170 | if (user.hasRight(adminRight)) { | |
171 | return adminRight | |
172 | } | |
173 | } | |
174 | ||
175 | return undefined | |
176 | } | |
177 | ||
178 | getFirstAdminRouteAvailable () { | |
179 | const right = this.getFirstAdminRightAvailable() | |
180 | ||
181 | return this.routesPerRight[right] | |
182 | } | |
183 | ||
184 | logout (event: Event) { | |
185 | event.preventDefault() | |
186 | ||
187 | this.authService.logout() | |
188 | // Redirect to home page | |
189 | this.redirectService.redirectToHomepage() | |
190 | } | |
191 | ||
192 | openLanguageChooser () { | |
193 | this.languageChooserModal.show() | |
194 | } | |
195 | ||
196 | openHotkeysCheatSheet () { | |
197 | this.hotkeysService.cheatSheetToggle.next(!this.helpVisible) | |
198 | } | |
199 | ||
200 | openQuickSettings () { | |
201 | this.quickSettingsModal.show() | |
202 | } | |
203 | ||
204 | toggleUseP2P () { | |
205 | if (!this.user) return | |
206 | this.user.webTorrentEnabled = !this.user.webTorrentEnabled | |
207 | ||
208 | this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled }) | |
209 | .subscribe(() => this.authService.refreshUserInformation()) | |
210 | } | |
211 | ||
212 | langForLocale (localeId: string) { | |
213 | if (localeId === '_unknown') return $localize`Unknown` | |
214 | ||
215 | return this.languages.find(lang => lang.id === localeId).label | |
216 | } | |
217 | ||
218 | onActiveLinkScrollToAnchor (link: HTMLAnchorElement) { | |
219 | const linkURL = link.getAttribute('href') | |
220 | const linkHash = link.getAttribute('fragment') | |
221 | ||
222 | // On same url without fragment restore top scroll position | |
223 | if (!linkHash && this.router.url.includes(linkURL)) { | |
224 | scrollToTop('smooth') | |
225 | } | |
226 | ||
227 | // On same url with fragment restore anchor scroll position | |
228 | if (linkHash && this.router.url === linkURL) { | |
229 | this.viewportScroller.scrollToAnchor(linkHash) | |
230 | } | |
231 | ||
232 | if (this.screenService.isInSmallView()) { | |
233 | this.menuService.toggleMenu() | |
234 | } | |
235 | } | |
236 | ||
237 | // Lock menu scroll when menu scroll to avoid fleeing / detached dropdown | |
238 | onMenuScrollEvent () { | |
239 | document.querySelector('menu').scrollTo(0, 0) | |
240 | } | |
241 | ||
242 | onDropdownOpenChange (opened: boolean) { | |
243 | if (this.screenService.isInMobileView()) return | |
244 | ||
245 | // Close dropdown when window scroll to avoid dropdown quick jump for re-position | |
246 | const onWindowScroll = () => { | |
247 | this.dropdown?.close() | |
248 | window.removeEventListener('scroll', onWindowScroll) | |
249 | } | |
250 | ||
251 | if (opened) { | |
252 | window.addEventListener('scroll', onWindowScroll) | |
253 | document.querySelector('menu').scrollTo(0, 0) // Reset menu scroll to easy lock | |
254 | document.querySelector('menu').addEventListener('scroll', this.onMenuScrollEvent) | |
255 | } else { | |
256 | document.querySelector('menu').removeEventListener('scroll', this.onMenuScrollEvent) | |
257 | } | |
258 | } | |
259 | ||
260 | private buildMenuLinks () { | |
261 | this.commonMenuLinks = this.menuService.buildCommonLinks(this.htmlServerConfig) | |
262 | } | |
263 | ||
264 | private buildUserLanguages () { | |
265 | if (!this.user) { | |
266 | this.videoLanguages = [] | |
267 | return | |
268 | } | |
269 | ||
270 | if (!this.user.videoLanguages) { | |
271 | this.videoLanguages = [$localize`any language`] | |
272 | return | |
273 | } | |
274 | ||
275 | this.videoLanguages = this.user.videoLanguages | |
276 | .map(locale => this.langForLocale(locale)) | |
277 | .map(value => value === undefined ? '?' : value) | |
278 | } | |
279 | ||
280 | private computeAdminAccess () { | |
281 | const right = this.getFirstAdminRightAvailable() | |
282 | ||
283 | this.userHasAdminAccess = right !== undefined | |
284 | } | |
285 | ||
286 | private computeVideosLink () { | |
287 | this.authService.userInformationLoaded | |
288 | .pipe( | |
289 | switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed())) | |
290 | ).subscribe(res => { | |
291 | if (res === true) logger('User can see videos link.') | |
292 | else logger('User cannot see videos link.') | |
293 | }) | |
294 | } | |
295 | ||
296 | private computeNSFWPolicy () { | |
297 | if (!this.user) { | |
298 | this.nsfwPolicy = null | |
299 | return | |
300 | } | |
301 | ||
302 | switch (this.user.nsfwPolicy) { | |
303 | case 'do_not_list': | |
304 | this.nsfwPolicy = $localize`hide` | |
305 | break | |
306 | ||
307 | case 'blur': | |
308 | this.nsfwPolicy = $localize`blur` | |
309 | break | |
310 | ||
311 | case 'display': | |
312 | this.nsfwPolicy = $localize`display` | |
313 | break | |
314 | } | |
315 | } | |
316 | } |