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