]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit, ViewChild } from '@angular/core' | |
2 | import { UserRight } from '../../../../shared/models/users/user-right.enum' | |
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' | |
6 | import { LanguageChooserComponent } from '@app/menu/language-chooser.component' | |
7 | import { HotkeysService } from 'angular2-hotkeys' | |
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' | |
11 | ||
12 | @Component({ | |
13 | selector: 'my-menu', | |
14 | templateUrl: './menu.component.html', | |
15 | styleUrls: [ './menu.component.scss' ] | |
16 | }) | |
17 | export class MenuComponent implements OnInit { | |
18 | @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent | |
19 | @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent | |
20 | ||
21 | user: User | |
22 | isLoggedIn: boolean | |
23 | ||
24 | userHasAdminAccess = false | |
25 | helpVisible = false | |
26 | ||
27 | videoLanguages: string[] = [] | |
28 | ||
29 | private languages: VideoConstant<string>[] = [] | |
30 | private serverConfig: ServerConfig | |
31 | private routesPerRight: { [ role in UserRight ]?: string } = { | |
32 | [UserRight.MANAGE_USERS]: '/admin/users', | |
33 | [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends', | |
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' | |
38 | } | |
39 | ||
40 | constructor ( | |
41 | private authService: AuthService, | |
42 | private userService: UserService, | |
43 | private serverService: ServerService, | |
44 | private redirectService: RedirectService, | |
45 | private hotkeysService: HotkeysService, | |
46 | private i18n: I18n | |
47 | ) {} | |
48 | ||
49 | ngOnInit () { | |
50 | this.serverConfig = this.serverService.getTmpConfig() | |
51 | this.serverService.getConfig() | |
52 | .subscribe(config => this.serverConfig = config) | |
53 | ||
54 | this.isLoggedIn = this.authService.isLoggedIn() | |
55 | if (this.isLoggedIn === true) this.user = this.authService.getUser() | |
56 | this.computeIsUserHasAdminAccess() | |
57 | ||
58 | this.authService.loginChangedSource.subscribe( | |
59 | status => { | |
60 | if (status === AuthStatus.LoggedIn) { | |
61 | this.isLoggedIn = true | |
62 | this.user = this.authService.getUser() | |
63 | this.computeIsUserHasAdminAccess() | |
64 | console.log('Logged in.') | |
65 | } else if (status === AuthStatus.LoggedOut) { | |
66 | this.isLoggedIn = false | |
67 | this.user = undefined | |
68 | this.computeIsUserHasAdminAccess() | |
69 | console.log('Logged out.') | |
70 | } else { | |
71 | console.error('Unknown auth status: ' + status) | |
72 | } | |
73 | } | |
74 | ) | |
75 | ||
76 | this.hotkeysService.cheatSheetToggle | |
77 | .subscribe(isOpen => this.helpVisible = isOpen) | |
78 | ||
79 | this.serverService.getVideoLanguages() | |
80 | .subscribe(languages => { | |
81 | this.languages = languages | |
82 | ||
83 | this.authService.userInformationLoaded | |
84 | .subscribe(() => this.buildUserLanguages()) | |
85 | }) | |
86 | } | |
87 | ||
88 | get language () { | |
89 | return this.languageChooserModal.getCurrentLanguage() | |
90 | } | |
91 | ||
92 | get nsfwPolicy () { | |
93 | if (!this.user) return | |
94 | ||
95 | switch (this.user.nsfwPolicy) { | |
96 | case 'do_not_list': | |
97 | return this.i18n('hide') | |
98 | ||
99 | case 'blur': | |
100 | return this.i18n('blur') | |
101 | ||
102 | case 'display': | |
103 | return this.i18n('display') | |
104 | } | |
105 | } | |
106 | ||
107 | isRegistrationAllowed () { | |
108 | return this.serverConfig.signup.allowed && | |
109 | this.serverConfig.signup.allowedForCurrentIP | |
110 | } | |
111 | ||
112 | getFirstAdminRightAvailable () { | |
113 | const user = this.authService.getUser() | |
114 | if (!user) return undefined | |
115 | ||
116 | const adminRights = [ | |
117 | UserRight.MANAGE_USERS, | |
118 | UserRight.MANAGE_SERVER_FOLLOW, | |
119 | UserRight.MANAGE_VIDEO_ABUSES, | |
120 | UserRight.MANAGE_VIDEO_BLACKLIST, | |
121 | UserRight.MANAGE_JOBS, | |
122 | UserRight.MANAGE_CONFIGURATION | |
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] | |
138 | } | |
139 | ||
140 | logout (event: Event) { | |
141 | event.preventDefault() | |
142 | ||
143 | this.authService.logout() | |
144 | // Redirect to home page | |
145 | this.redirectService.redirectToHomepage() | |
146 | } | |
147 | ||
148 | openLanguageChooser () { | |
149 | this.languageChooserModal.show() | |
150 | } | |
151 | ||
152 | openHotkeysCheatSheet () { | |
153 | this.hotkeysService.cheatSheetToggle.next(!this.helpVisible) | |
154 | } | |
155 | ||
156 | openQuickSettings () { | |
157 | this.quickSettingsModal.show() | |
158 | } | |
159 | ||
160 | toggleUseP2P () { | |
161 | if (!this.user) return | |
162 | this.user.webTorrentEnabled = !this.user.webTorrentEnabled | |
163 | ||
164 | this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled }) | |
165 | .subscribe(() => this.authService.refreshUserInformation()) | |
166 | } | |
167 | ||
168 | langForLocale (localeId: string) { | |
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) | |
186 | } | |
187 | ||
188 | private computeIsUserHasAdminAccess () { | |
189 | const right = this.getFirstAdminRightAvailable() | |
190 | ||
191 | this.userHasAdminAccess = right !== undefined | |
192 | } | |
193 | } |