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