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