]>
Commit | Line | Data |
---|---|---|
f3081d64 | 1 | import { ViewportScroller } from '@angular/common' |
67ed6552 | 2 | import { HotkeysService } from 'angular2-hotkeys' |
dfe3f7b7 K |
3 | import * as debug from 'debug' |
4 | import { switchMap } from 'rxjs/operators' | |
8afc19a6 | 5 | import { Component, OnInit, ViewChild } from '@angular/core' |
30d55e75 | 6 | import { Router } from '@angular/router' |
f3081d64 | 7 | import { scrollToTop } from '@app/helpers' |
30d55e75 | 8 | import { AuthService, AuthStatus, AuthUser, MenuService, RedirectService, ScreenService, ServerService, UserService } from '@app/core' |
8afc19a6 | 9 | import { LanguageChooserComponent } from '@app/menu/language-chooser.component' |
d3217560 | 10 | import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component' |
67ed6552 | 11 | import { ServerConfig, UserRight, VideoConstant } from '@shared/models' |
602eb142 | 12 | |
dfe3f7b7 K |
13 | const logger = debug('peertube:menu:MenuComponent') |
14 | ||
602eb142 C |
15 | @Component({ |
16 | selector: 'my-menu', | |
383bfc83 | 17 | templateUrl: './menu.component.html', |
dfe3f7b7 | 18 | styleUrls: ['./menu.component.scss'] |
602eb142 C |
19 | }) |
20 | export class MenuComponent implements OnInit { | |
f36da21e | 21 | @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent |
d3217560 | 22 | @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent |
8afc19a6 | 23 | |
dfe3f7b7 | 24 | user: AuthUser |
df98563e | 25 | isLoggedIn: boolean |
d3217560 | 26 | |
954605a8 | 27 | userHasAdminAccess = false |
4a216666 | 28 | helpVisible = false |
954605a8 | 29 | |
111fdc26 C |
30 | videoLanguages: string[] = [] |
31 | ||
32 | private languages: VideoConstant<string>[] = [] | |
ba430d75 | 33 | private serverConfig: ServerConfig |
dfe3f7b7 | 34 | private routesPerRight: { [role in UserRight]?: string } = { |
954605a8 | 35 | [UserRight.MANAGE_USERS]: '/admin/users', |
4610bc5b | 36 | [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends', |
d95d1559 | 37 | [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses', |
3487330d | 38 | [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks', |
ad76628b C |
39 | [UserRight.MANAGE_JOBS]: '/admin/jobs', |
40 | [UserRight.MANAGE_CONFIGURATION]: '/admin/config' | |
954605a8 | 41 | } |
602eb142 C |
42 | |
43 | constructor ( | |
f3081d64 | 44 | private viewportScroller: ViewportScroller, |
602eb142 | 45 | private authService: AuthService, |
d3217560 | 46 | private userService: UserService, |
db7af09b | 47 | private serverService: ServerService, |
8c985ef5 | 48 | private redirectService: RedirectService, |
d3217560 | 49 | private hotkeysService: HotkeysService, |
30d55e75 K |
50 | private screenService: ScreenService, |
51 | private menuService: MenuService, | |
52 | private router: Router | |
53 | ) { } | |
ca4b1594 K |
54 | |
55 | get isInMobileView () { | |
56 | return this.screenService.isInMobileView() | |
57 | } | |
58 | ||
59 | get placement () { | |
60 | if (this.isInMobileView) { | |
61 | return 'left-top auto' | |
62 | } else { | |
63 | return 'right-top auto' | |
64 | } | |
65 | } | |
602eb142 | 66 | |
17119e4a C |
67 | get language () { |
68 | return this.languageChooserModal.getCurrentLanguage() | |
69 | } | |
70 | ||
71 | get nsfwPolicy () { | |
72 | if (!this.user) return | |
73 | ||
74 | switch (this.user.nsfwPolicy) { | |
75 | case 'do_not_list': | |
76 | return $localize`hide` | |
77 | ||
78 | case 'blur': | |
79 | return $localize`blur` | |
80 | ||
81 | case 'display': | |
82 | return $localize`display` | |
83 | } | |
84 | } | |
85 | ||
86 | get instanceName () { | |
87 | return this.serverConfig.instance.name | |
88 | } | |
89 | ||
df98563e | 90 | ngOnInit () { |
ba430d75 C |
91 | this.serverConfig = this.serverService.getTmpConfig() |
92 | this.serverService.getConfig() | |
93 | .subscribe(config => this.serverConfig = config) | |
94 | ||
df98563e | 95 | this.isLoggedIn = this.authService.isLoggedIn() |
dfe3f7b7 K |
96 | if (this.isLoggedIn === true) { |
97 | this.user = this.authService.getUser() | |
98 | this.computeVideosLink() | |
99 | } | |
100 | ||
101 | this.computeAdminAccess() | |
602eb142 C |
102 | |
103 | this.authService.loginChangedSource.subscribe( | |
104 | status => { | |
105 | if (status === AuthStatus.LoggedIn) { | |
df98563e | 106 | this.isLoggedIn = true |
b33f657c | 107 | this.user = this.authService.getUser() |
dfe3f7b7 K |
108 | |
109 | this.computeAdminAccess() | |
110 | this.computeVideosLink() | |
111 | ||
112 | logger('Logged in.') | |
602eb142 | 113 | } else if (status === AuthStatus.LoggedOut) { |
df98563e | 114 | this.isLoggedIn = false |
b33f657c | 115 | this.user = undefined |
dfe3f7b7 K |
116 | |
117 | this.computeAdminAccess() | |
118 | ||
119 | logger('Logged out.') | |
602eb142 | 120 | } else { |
df98563e | 121 | console.error('Unknown auth status: ' + status) |
602eb142 C |
122 | } |
123 | } | |
df98563e | 124 | ) |
4a216666 | 125 | |
111fdc26 | 126 | this.hotkeysService.cheatSheetToggle |
dfe3f7b7 | 127 | .subscribe(isOpen => this.helpVisible = isOpen) |
111fdc26 C |
128 | |
129 | this.serverService.getVideoLanguages() | |
dfe3f7b7 K |
130 | .subscribe(languages => { |
131 | this.languages = languages | |
d3217560 | 132 | |
dfe3f7b7 K |
133 | this.authService.userInformationLoaded |
134 | .subscribe(() => this.buildUserLanguages()) | |
135 | }) | |
d3217560 RK |
136 | } |
137 | ||
291e8d3e | 138 | isRegistrationAllowed () { |
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 | |
193 | this.user.webTorrentEnabled = !this.user.webTorrentEnabled | |
111fdc26 C |
194 | |
195 | this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled }) | |
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 | ||
111fdc26 C |
224 | private buildUserLanguages () { |
225 | if (!this.user) { | |
226 | this.videoLanguages = [] | |
227 | return | |
228 | } | |
229 | ||
230 | if (!this.user.videoLanguages) { | |
66357162 | 231 | this.videoLanguages = [$localize`any language`] |
111fdc26 C |
232 | return |
233 | } | |
234 | ||
235 | this.videoLanguages = this.user.videoLanguages | |
dfe3f7b7 K |
236 | .map(locale => this.langForLocale(locale)) |
237 | .map(value => value === undefined ? '?' : value) | |
d3217560 RK |
238 | } |
239 | ||
dfe3f7b7 | 240 | private computeAdminAccess () { |
954605a8 C |
241 | const right = this.getFirstAdminRightAvailable() |
242 | ||
243 | this.userHasAdminAccess = right !== undefined | |
244 | } | |
dfe3f7b7 K |
245 | |
246 | private computeVideosLink () { | |
247 | this.authService.userInformationLoaded | |
248 | .pipe( | |
249 | switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed())) | |
250 | ).subscribe(res => { | |
251 | if (res === true) logger('User can see videos link.') | |
252 | else logger('User cannot see videos link.') | |
253 | }) | |
254 | } | |
602eb142 | 255 | } |