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