]>
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 RK |
7 | import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component' |
8 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
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, |
ca4b1594 | 47 | private screenService: ScreenService, |
d3217560 | 48 | private i18n: I18n |
ca4b1594 K |
49 | ) { } |
50 | ||
51 | get isInMobileView () { | |
52 | return this.screenService.isInMobileView() | |
53 | } | |
54 | ||
55 | get placement () { | |
56 | if (this.isInMobileView) { | |
57 | return 'left-top auto' | |
58 | } else { | |
59 | return 'right-top auto' | |
60 | } | |
61 | } | |
602eb142 | 62 | |
df98563e | 63 | ngOnInit () { |
ba430d75 C |
64 | this.serverConfig = this.serverService.getTmpConfig() |
65 | this.serverService.getConfig() | |
66 | .subscribe(config => this.serverConfig = config) | |
67 | ||
df98563e | 68 | this.isLoggedIn = this.authService.isLoggedIn() |
dfe3f7b7 K |
69 | if (this.isLoggedIn === true) { |
70 | this.user = this.authService.getUser() | |
71 | this.computeVideosLink() | |
72 | } | |
73 | ||
74 | this.computeAdminAccess() | |
602eb142 C |
75 | |
76 | this.authService.loginChangedSource.subscribe( | |
77 | status => { | |
78 | if (status === AuthStatus.LoggedIn) { | |
df98563e | 79 | this.isLoggedIn = true |
b33f657c | 80 | this.user = this.authService.getUser() |
dfe3f7b7 K |
81 | |
82 | this.computeAdminAccess() | |
83 | this.computeVideosLink() | |
84 | ||
85 | logger('Logged in.') | |
602eb142 | 86 | } else if (status === AuthStatus.LoggedOut) { |
df98563e | 87 | this.isLoggedIn = false |
b33f657c | 88 | this.user = undefined |
dfe3f7b7 K |
89 | |
90 | this.computeAdminAccess() | |
91 | ||
92 | logger('Logged out.') | |
602eb142 | 93 | } else { |
df98563e | 94 | console.error('Unknown auth status: ' + status) |
602eb142 C |
95 | } |
96 | } | |
df98563e | 97 | ) |
4a216666 | 98 | |
111fdc26 | 99 | this.hotkeysService.cheatSheetToggle |
dfe3f7b7 | 100 | .subscribe(isOpen => this.helpVisible = isOpen) |
111fdc26 C |
101 | |
102 | this.serverService.getVideoLanguages() | |
dfe3f7b7 K |
103 | .subscribe(languages => { |
104 | this.languages = languages | |
d3217560 | 105 | |
dfe3f7b7 K |
106 | this.authService.userInformationLoaded |
107 | .subscribe(() => this.buildUserLanguages()) | |
108 | }) | |
d3217560 RK |
109 | } |
110 | ||
111 | get language () { | |
112 | return this.languageChooserModal.getCurrentLanguage() | |
113 | } | |
114 | ||
d3217560 RK |
115 | get nsfwPolicy () { |
116 | if (!this.user) return | |
111fdc26 | 117 | |
d3217560 RK |
118 | switch (this.user.nsfwPolicy) { |
119 | case 'do_not_list': | |
120 | return this.i18n('hide') | |
111fdc26 | 121 | |
d3217560 RK |
122 | case 'blur': |
123 | return this.i18n('blur') | |
111fdc26 | 124 | |
d3217560 RK |
125 | case 'display': |
126 | return this.i18n('display') | |
127 | } | |
602eb142 C |
128 | } |
129 | ||
291e8d3e | 130 | isRegistrationAllowed () { |
ba430d75 | 131 | return this.serverConfig.signup.allowed && |
dfe3f7b7 | 132 | this.serverConfig.signup.allowedForCurrentIP |
a184c71b C |
133 | } |
134 | ||
954605a8 C |
135 | getFirstAdminRightAvailable () { |
136 | const user = this.authService.getUser() | |
137 | if (!user) return undefined | |
138 | ||
139 | const adminRights = [ | |
140 | UserRight.MANAGE_USERS, | |
4610bc5b | 141 | UserRight.MANAGE_SERVER_FOLLOW, |
d95d1559 | 142 | UserRight.MANAGE_ABUSES, |
3487330d | 143 | UserRight.MANAGE_VIDEO_BLACKLIST, |
ad76628b C |
144 | UserRight.MANAGE_JOBS, |
145 | UserRight.MANAGE_CONFIGURATION | |
954605a8 C |
146 | ] |
147 | ||
148 | for (const adminRight of adminRights) { | |
149 | if (user.hasRight(adminRight)) { | |
150 | return adminRight | |
151 | } | |
152 | } | |
153 | ||
154 | return undefined | |
155 | } | |
156 | ||
157 | getFirstAdminRouteAvailable () { | |
158 | const right = this.getFirstAdminRightAvailable() | |
159 | ||
160 | return this.routesPerRight[right] | |
602eb142 C |
161 | } |
162 | ||
b33f657c C |
163 | logout (event: Event) { |
164 | event.preventDefault() | |
165 | ||
df98563e | 166 | this.authService.logout() |
602eb142 | 167 | // Redirect to home page |
b1d40cff | 168 | this.redirectService.redirectToHomepage() |
602eb142 | 169 | } |
954605a8 | 170 | |
8afc19a6 C |
171 | openLanguageChooser () { |
172 | this.languageChooserModal.show() | |
173 | } | |
174 | ||
4a216666 RK |
175 | openHotkeysCheatSheet () { |
176 | this.hotkeysService.cheatSheetToggle.next(!this.helpVisible) | |
177 | } | |
178 | ||
d3217560 RK |
179 | openQuickSettings () { |
180 | this.quickSettingsModal.show() | |
181 | } | |
182 | ||
183 | toggleUseP2P () { | |
184 | if (!this.user) return | |
185 | this.user.webTorrentEnabled = !this.user.webTorrentEnabled | |
111fdc26 C |
186 | |
187 | this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled }) | |
dfe3f7b7 | 188 | .subscribe(() => this.authService.refreshUserInformation()) |
d3217560 RK |
189 | } |
190 | ||
8ada87ac | 191 | langForLocale (localeId: string) { |
bb3933ef C |
192 | if (localeId === '_unknown') return this.i18n('Unknown') |
193 | ||
111fdc26 C |
194 | return this.languages.find(lang => lang.id === localeId).label |
195 | } | |
196 | ||
197 | private buildUserLanguages () { | |
198 | if (!this.user) { | |
199 | this.videoLanguages = [] | |
200 | return | |
201 | } | |
202 | ||
203 | if (!this.user.videoLanguages) { | |
dfe3f7b7 | 204 | this.videoLanguages = [this.i18n('any language')] |
111fdc26 C |
205 | return |
206 | } | |
207 | ||
208 | this.videoLanguages = this.user.videoLanguages | |
dfe3f7b7 K |
209 | .map(locale => this.langForLocale(locale)) |
210 | .map(value => value === undefined ? '?' : value) | |
d3217560 RK |
211 | } |
212 | ||
dfe3f7b7 | 213 | private computeAdminAccess () { |
954605a8 C |
214 | const right = this.getFirstAdminRightAvailable() |
215 | ||
216 | this.userHasAdminAccess = right !== undefined | |
217 | } | |
dfe3f7b7 K |
218 | |
219 | private computeVideosLink () { | |
220 | this.authService.userInformationLoaded | |
221 | .pipe( | |
222 | switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed())) | |
223 | ).subscribe(res => { | |
224 | if (res === true) logger('User can see videos link.') | |
225 | else logger('User cannot see videos link.') | |
226 | }) | |
227 | } | |
602eb142 | 228 | } |