]>
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 | 30 | videoLanguages: string[] = [] |
68f6c87a C |
31 | nsfwPolicy: string |
32 | ||
33 | loggedInMorePlacement: string | |
34 | ||
35 | currentInterfaceLanguage: string | |
111fdc26 C |
36 | |
37 | private languages: VideoConstant<string>[] = [] | |
ba430d75 | 38 | private serverConfig: ServerConfig |
dfe3f7b7 | 39 | private routesPerRight: { [role in UserRight]?: string } = { |
954605a8 | 40 | [UserRight.MANAGE_USERS]: '/admin/users', |
4610bc5b | 41 | [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends', |
d95d1559 | 42 | [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses', |
3487330d | 43 | [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks', |
ad76628b C |
44 | [UserRight.MANAGE_JOBS]: '/admin/jobs', |
45 | [UserRight.MANAGE_CONFIGURATION]: '/admin/config' | |
954605a8 | 46 | } |
602eb142 C |
47 | |
48 | constructor ( | |
f3081d64 | 49 | private viewportScroller: ViewportScroller, |
602eb142 | 50 | private authService: AuthService, |
d3217560 | 51 | private userService: UserService, |
db7af09b | 52 | private serverService: ServerService, |
8c985ef5 | 53 | private redirectService: RedirectService, |
d3217560 | 54 | private hotkeysService: HotkeysService, |
30d55e75 K |
55 | private screenService: ScreenService, |
56 | private menuService: MenuService, | |
57 | private router: Router | |
58 | ) { } | |
ca4b1594 | 59 | |
17119e4a C |
60 | get instanceName () { |
61 | return this.serverConfig.instance.name | |
62 | } | |
63 | ||
df98563e | 64 | ngOnInit () { |
ba430d75 C |
65 | this.serverConfig = this.serverService.getTmpConfig() |
66 | this.serverService.getConfig() | |
67 | .subscribe(config => this.serverConfig = config) | |
68 | ||
df98563e | 69 | this.isLoggedIn = this.authService.isLoggedIn() |
dfe3f7b7 K |
70 | if (this.isLoggedIn === true) { |
71 | this.user = this.authService.getUser() | |
68f6c87a C |
72 | |
73 | this.computeNSFWPolicy() | |
dfe3f7b7 K |
74 | this.computeVideosLink() |
75 | } | |
76 | ||
77 | this.computeAdminAccess() | |
602eb142 | 78 | |
68f6c87a C |
79 | this.loggedInMorePlacement = this.screenService.isInMobileView() |
80 | ? 'left-top auto' | |
81 | : 'right-top auto' | |
82 | ||
83 | this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage() | |
84 | ||
602eb142 C |
85 | this.authService.loginChangedSource.subscribe( |
86 | status => { | |
87 | if (status === AuthStatus.LoggedIn) { | |
df98563e | 88 | this.isLoggedIn = true |
b33f657c | 89 | this.user = this.authService.getUser() |
dfe3f7b7 K |
90 | |
91 | this.computeAdminAccess() | |
92 | this.computeVideosLink() | |
93 | ||
94 | logger('Logged in.') | |
602eb142 | 95 | } else if (status === AuthStatus.LoggedOut) { |
df98563e | 96 | this.isLoggedIn = false |
b33f657c | 97 | this.user = undefined |
dfe3f7b7 K |
98 | |
99 | this.computeAdminAccess() | |
100 | ||
101 | logger('Logged out.') | |
602eb142 | 102 | } else { |
df98563e | 103 | console.error('Unknown auth status: ' + status) |
602eb142 C |
104 | } |
105 | } | |
df98563e | 106 | ) |
4a216666 | 107 | |
111fdc26 | 108 | this.hotkeysService.cheatSheetToggle |
dfe3f7b7 | 109 | .subscribe(isOpen => this.helpVisible = isOpen) |
111fdc26 C |
110 | |
111 | this.serverService.getVideoLanguages() | |
dfe3f7b7 K |
112 | .subscribe(languages => { |
113 | this.languages = languages | |
d3217560 | 114 | |
dfe3f7b7 K |
115 | this.authService.userInformationLoaded |
116 | .subscribe(() => this.buildUserLanguages()) | |
117 | }) | |
d3217560 RK |
118 | } |
119 | ||
291e8d3e | 120 | isRegistrationAllowed () { |
ba430d75 | 121 | return this.serverConfig.signup.allowed && |
dfe3f7b7 | 122 | this.serverConfig.signup.allowedForCurrentIP |
a184c71b C |
123 | } |
124 | ||
954605a8 C |
125 | getFirstAdminRightAvailable () { |
126 | const user = this.authService.getUser() | |
127 | if (!user) return undefined | |
128 | ||
129 | const adminRights = [ | |
130 | UserRight.MANAGE_USERS, | |
4610bc5b | 131 | UserRight.MANAGE_SERVER_FOLLOW, |
d95d1559 | 132 | UserRight.MANAGE_ABUSES, |
3487330d | 133 | UserRight.MANAGE_VIDEO_BLACKLIST, |
ad76628b C |
134 | UserRight.MANAGE_JOBS, |
135 | UserRight.MANAGE_CONFIGURATION | |
954605a8 C |
136 | ] |
137 | ||
138 | for (const adminRight of adminRights) { | |
139 | if (user.hasRight(adminRight)) { | |
140 | return adminRight | |
141 | } | |
142 | } | |
143 | ||
144 | return undefined | |
145 | } | |
146 | ||
147 | getFirstAdminRouteAvailable () { | |
148 | const right = this.getFirstAdminRightAvailable() | |
149 | ||
150 | return this.routesPerRight[right] | |
602eb142 C |
151 | } |
152 | ||
b33f657c C |
153 | logout (event: Event) { |
154 | event.preventDefault() | |
155 | ||
df98563e | 156 | this.authService.logout() |
602eb142 | 157 | // Redirect to home page |
b1d40cff | 158 | this.redirectService.redirectToHomepage() |
602eb142 | 159 | } |
954605a8 | 160 | |
8afc19a6 C |
161 | openLanguageChooser () { |
162 | this.languageChooserModal.show() | |
163 | } | |
164 | ||
4a216666 RK |
165 | openHotkeysCheatSheet () { |
166 | this.hotkeysService.cheatSheetToggle.next(!this.helpVisible) | |
167 | } | |
168 | ||
d3217560 RK |
169 | openQuickSettings () { |
170 | this.quickSettingsModal.show() | |
171 | } | |
172 | ||
173 | toggleUseP2P () { | |
174 | if (!this.user) return | |
175 | this.user.webTorrentEnabled = !this.user.webTorrentEnabled | |
111fdc26 C |
176 | |
177 | this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled }) | |
dfe3f7b7 | 178 | .subscribe(() => this.authService.refreshUserInformation()) |
d3217560 RK |
179 | } |
180 | ||
8ada87ac | 181 | langForLocale (localeId: string) { |
66357162 | 182 | if (localeId === '_unknown') return $localize`Unknown` |
bb3933ef | 183 | |
111fdc26 C |
184 | return this.languages.find(lang => lang.id === localeId).label |
185 | } | |
186 | ||
f3081d64 | 187 | onActiveLinkScrollToAnchor (link: HTMLAnchorElement) { |
30d55e75 K |
188 | const linkURL = link.getAttribute('href') |
189 | const linkHash = link.getAttribute('fragment') | |
190 | ||
191 | // On same url without fragment restore top scroll position | |
192 | if (!linkHash && this.router.url.includes(linkURL)) { | |
f3081d64 | 193 | scrollToTop('smooth') |
30d55e75 K |
194 | } |
195 | ||
196 | // On same url with fragment restore anchor scroll position | |
197 | if (linkHash && this.router.url === linkURL) { | |
f3081d64 | 198 | this.viewportScroller.scrollToAnchor(linkHash) |
30d55e75 K |
199 | } |
200 | ||
201 | if (this.screenService.isInSmallView()) { | |
202 | this.menuService.toggleMenu() | |
203 | } | |
204 | } | |
205 | ||
111fdc26 C |
206 | private buildUserLanguages () { |
207 | if (!this.user) { | |
208 | this.videoLanguages = [] | |
209 | return | |
210 | } | |
211 | ||
212 | if (!this.user.videoLanguages) { | |
66357162 | 213 | this.videoLanguages = [$localize`any language`] |
111fdc26 C |
214 | return |
215 | } | |
216 | ||
217 | this.videoLanguages = this.user.videoLanguages | |
dfe3f7b7 K |
218 | .map(locale => this.langForLocale(locale)) |
219 | .map(value => value === undefined ? '?' : value) | |
d3217560 RK |
220 | } |
221 | ||
dfe3f7b7 | 222 | private computeAdminAccess () { |
954605a8 C |
223 | const right = this.getFirstAdminRightAvailable() |
224 | ||
225 | this.userHasAdminAccess = right !== undefined | |
226 | } | |
dfe3f7b7 K |
227 | |
228 | private computeVideosLink () { | |
229 | this.authService.userInformationLoaded | |
230 | .pipe( | |
231 | switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed())) | |
232 | ).subscribe(res => { | |
233 | if (res === true) logger('User can see videos link.') | |
234 | else logger('User cannot see videos link.') | |
235 | }) | |
236 | } | |
68f6c87a C |
237 | |
238 | private computeNSFWPolicy () { | |
239 | if (!this.user) { | |
240 | this.nsfwPolicy = null | |
241 | return | |
242 | } | |
243 | ||
244 | switch (this.user.nsfwPolicy) { | |
245 | case 'do_not_list': | |
246 | this.nsfwPolicy = $localize`hide` | |
247 | break | |
248 | ||
249 | case 'blur': | |
250 | this.nsfwPolicy = $localize`blur` | |
251 | break | |
252 | ||
253 | case 'display': | |
254 | this.nsfwPolicy = $localize`display` | |
255 | break | |
256 | } | |
257 | } | |
602eb142 | 258 | } |