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