]>
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' | |
602eb142 C |
11 | |
12 | @Component({ | |
13 | selector: 'my-menu', | |
383bfc83 | 14 | templateUrl: './menu.component.html', |
3eeeb87f | 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 | |
b33f657c | 21 | user: User |
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 |
c199c427 | 31 | private routesPerRight: { [ role in UserRight ]?: string } = { |
954605a8 | 32 | [UserRight.MANAGE_USERS]: '/admin/users', |
4610bc5b | 33 | [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends', |
ad76628b C |
34 | [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/moderation/video-abuses', |
35 | [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blacklist', | |
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 RK |
45 | private hotkeysService: HotkeysService, |
46 | private i18n: I18n | |
602eb142 C |
47 | ) {} |
48 | ||
df98563e | 49 | ngOnInit () { |
ba430d75 C |
50 | this.serverConfig = this.serverService.getTmpConfig() |
51 | this.serverService.getConfig() | |
52 | .subscribe(config => this.serverConfig = config) | |
53 | ||
df98563e | 54 | this.isLoggedIn = this.authService.isLoggedIn() |
b33f657c | 55 | if (this.isLoggedIn === true) this.user = this.authService.getUser() |
954605a8 | 56 | this.computeIsUserHasAdminAccess() |
602eb142 C |
57 | |
58 | this.authService.loginChangedSource.subscribe( | |
59 | status => { | |
60 | if (status === AuthStatus.LoggedIn) { | |
df98563e | 61 | this.isLoggedIn = true |
b33f657c | 62 | this.user = this.authService.getUser() |
954605a8 | 63 | this.computeIsUserHasAdminAccess() |
df98563e | 64 | console.log('Logged in.') |
602eb142 | 65 | } else if (status === AuthStatus.LoggedOut) { |
df98563e | 66 | this.isLoggedIn = false |
b33f657c | 67 | this.user = undefined |
954605a8 | 68 | this.computeIsUserHasAdminAccess() |
df98563e | 69 | console.log('Logged out.') |
602eb142 | 70 | } else { |
df98563e | 71 | console.error('Unknown auth status: ' + status) |
602eb142 C |
72 | } |
73 | } | |
df98563e | 74 | ) |
4a216666 | 75 | |
111fdc26 C |
76 | this.hotkeysService.cheatSheetToggle |
77 | .subscribe(isOpen => this.helpVisible = isOpen) | |
78 | ||
79 | this.serverService.getVideoLanguages() | |
80 | .subscribe(languages => { | |
81 | this.languages = languages | |
d3217560 | 82 | |
111fdc26 C |
83 | this.authService.userInformationLoaded |
84 | .subscribe(() => this.buildUserLanguages()) | |
85 | }) | |
d3217560 RK |
86 | } |
87 | ||
88 | get language () { | |
89 | return this.languageChooserModal.getCurrentLanguage() | |
90 | } | |
91 | ||
d3217560 RK |
92 | get nsfwPolicy () { |
93 | if (!this.user) return | |
111fdc26 | 94 | |
d3217560 RK |
95 | switch (this.user.nsfwPolicy) { |
96 | case 'do_not_list': | |
97 | return this.i18n('hide') | |
111fdc26 | 98 | |
d3217560 RK |
99 | case 'blur': |
100 | return this.i18n('blur') | |
111fdc26 | 101 | |
d3217560 RK |
102 | case 'display': |
103 | return this.i18n('display') | |
104 | } | |
602eb142 C |
105 | } |
106 | ||
291e8d3e | 107 | isRegistrationAllowed () { |
ba430d75 C |
108 | return this.serverConfig.signup.allowed && |
109 | this.serverConfig.signup.allowedForCurrentIP | |
a184c71b C |
110 | } |
111 | ||
954605a8 C |
112 | getFirstAdminRightAvailable () { |
113 | const user = this.authService.getUser() | |
114 | if (!user) return undefined | |
115 | ||
116 | const adminRights = [ | |
117 | UserRight.MANAGE_USERS, | |
4610bc5b | 118 | UserRight.MANAGE_SERVER_FOLLOW, |
954605a8 | 119 | UserRight.MANAGE_VIDEO_ABUSES, |
ad76628b C |
120 | UserRight.MANAGE_VIDEO_BLACKLIST, |
121 | UserRight.MANAGE_JOBS, | |
122 | UserRight.MANAGE_CONFIGURATION | |
954605a8 C |
123 | ] |
124 | ||
125 | for (const adminRight of adminRights) { | |
126 | if (user.hasRight(adminRight)) { | |
127 | return adminRight | |
128 | } | |
129 | } | |
130 | ||
131 | return undefined | |
132 | } | |
133 | ||
134 | getFirstAdminRouteAvailable () { | |
135 | const right = this.getFirstAdminRightAvailable() | |
136 | ||
137 | return this.routesPerRight[right] | |
602eb142 C |
138 | } |
139 | ||
b33f657c C |
140 | logout (event: Event) { |
141 | event.preventDefault() | |
142 | ||
df98563e | 143 | this.authService.logout() |
602eb142 | 144 | // Redirect to home page |
b1d40cff | 145 | this.redirectService.redirectToHomepage() |
602eb142 | 146 | } |
954605a8 | 147 | |
8afc19a6 C |
148 | openLanguageChooser () { |
149 | this.languageChooserModal.show() | |
150 | } | |
151 | ||
4a216666 RK |
152 | openHotkeysCheatSheet () { |
153 | this.hotkeysService.cheatSheetToggle.next(!this.helpVisible) | |
154 | } | |
155 | ||
d3217560 RK |
156 | openQuickSettings () { |
157 | this.quickSettingsModal.show() | |
158 | } | |
159 | ||
160 | toggleUseP2P () { | |
161 | if (!this.user) return | |
162 | this.user.webTorrentEnabled = !this.user.webTorrentEnabled | |
111fdc26 C |
163 | |
164 | this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled }) | |
165 | .subscribe(() => this.authService.refreshUserInformation()) | |
d3217560 RK |
166 | } |
167 | ||
8ada87ac | 168 | langForLocale (localeId: string) { |
111fdc26 C |
169 | return this.languages.find(lang => lang.id === localeId).label |
170 | } | |
171 | ||
172 | private buildUserLanguages () { | |
173 | if (!this.user) { | |
174 | this.videoLanguages = [] | |
175 | return | |
176 | } | |
177 | ||
178 | if (!this.user.videoLanguages) { | |
179 | this.videoLanguages = [ this.i18n('any language') ] | |
180 | return | |
181 | } | |
182 | ||
183 | this.videoLanguages = this.user.videoLanguages | |
184 | .map(locale => this.langForLocale(locale)) | |
185 | .map(value => value === undefined ? '?' : value) | |
d3217560 RK |
186 | } |
187 | ||
954605a8 C |
188 | private computeIsUserHasAdminAccess () { |
189 | const right = this.getFirstAdminRightAvailable() | |
190 | ||
191 | this.userHasAdminAccess = right !== undefined | |
192 | } | |
602eb142 | 193 | } |