]>
Commit | Line | Data |
---|---|---|
1 | import { HotkeysService } from 'angular2-hotkeys' | |
2 | import { Component, OnInit, ViewChild } from '@angular/core' | |
3 | import { AuthService, AuthStatus, RedirectService, ScreenService, ServerService, User, UserService } from '@app/core' | |
4 | import { LanguageChooserComponent } from '@app/menu/language-chooser.component' | |
5 | import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component' | |
6 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
7 | import { ServerConfig, UserRight, VideoConstant } from '@shared/models' | |
8 | ||
9 | @Component({ | |
10 | selector: 'my-menu', | |
11 | templateUrl: './menu.component.html', | |
12 | styleUrls: [ './menu.component.scss' ] | |
13 | }) | |
14 | export class MenuComponent implements OnInit { | |
15 | @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent | |
16 | @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent | |
17 | ||
18 | user: User | |
19 | isLoggedIn: boolean | |
20 | ||
21 | userHasAdminAccess = false | |
22 | helpVisible = false | |
23 | ||
24 | videoLanguages: string[] = [] | |
25 | ||
26 | private languages: VideoConstant<string>[] = [] | |
27 | private serverConfig: ServerConfig | |
28 | private routesPerRight: { [ role in UserRight ]?: string } = { | |
29 | [UserRight.MANAGE_USERS]: '/admin/users', | |
30 | [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends', | |
31 | [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses', | |
32 | [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks', | |
33 | [UserRight.MANAGE_JOBS]: '/admin/jobs', | |
34 | [UserRight.MANAGE_CONFIGURATION]: '/admin/config' | |
35 | } | |
36 | ||
37 | constructor ( | |
38 | private authService: AuthService, | |
39 | private userService: UserService, | |
40 | private serverService: ServerService, | |
41 | private redirectService: RedirectService, | |
42 | private hotkeysService: HotkeysService, | |
43 | private screenService: ScreenService, | |
44 | private i18n: I18n | |
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 | } | |
58 | ||
59 | ngOnInit () { | |
60 | this.serverConfig = this.serverService.getTmpConfig() | |
61 | this.serverService.getConfig() | |
62 | .subscribe(config => this.serverConfig = config) | |
63 | ||
64 | this.isLoggedIn = this.authService.isLoggedIn() | |
65 | if (this.isLoggedIn === true) this.user = this.authService.getUser() | |
66 | this.computeIsUserHasAdminAccess() | |
67 | ||
68 | this.authService.loginChangedSource.subscribe( | |
69 | status => { | |
70 | if (status === AuthStatus.LoggedIn) { | |
71 | this.isLoggedIn = true | |
72 | this.user = this.authService.getUser() | |
73 | this.computeIsUserHasAdminAccess() | |
74 | console.log('Logged in.') | |
75 | } else if (status === AuthStatus.LoggedOut) { | |
76 | this.isLoggedIn = false | |
77 | this.user = undefined | |
78 | this.computeIsUserHasAdminAccess() | |
79 | console.log('Logged out.') | |
80 | } else { | |
81 | console.error('Unknown auth status: ' + status) | |
82 | } | |
83 | } | |
84 | ) | |
85 | ||
86 | this.hotkeysService.cheatSheetToggle | |
87 | .subscribe(isOpen => this.helpVisible = isOpen) | |
88 | ||
89 | this.serverService.getVideoLanguages() | |
90 | .subscribe(languages => { | |
91 | this.languages = languages | |
92 | ||
93 | this.authService.userInformationLoaded | |
94 | .subscribe(() => this.buildUserLanguages()) | |
95 | }) | |
96 | } | |
97 | ||
98 | get language () { | |
99 | return this.languageChooserModal.getCurrentLanguage() | |
100 | } | |
101 | ||
102 | get nsfwPolicy () { | |
103 | if (!this.user) return | |
104 | ||
105 | switch (this.user.nsfwPolicy) { | |
106 | case 'do_not_list': | |
107 | return this.i18n('hide') | |
108 | ||
109 | case 'blur': | |
110 | return this.i18n('blur') | |
111 | ||
112 | case 'display': | |
113 | return this.i18n('display') | |
114 | } | |
115 | } | |
116 | ||
117 | isRegistrationAllowed () { | |
118 | return this.serverConfig.signup.allowed && | |
119 | this.serverConfig.signup.allowedForCurrentIP | |
120 | } | |
121 | ||
122 | getFirstAdminRightAvailable () { | |
123 | const user = this.authService.getUser() | |
124 | if (!user) return undefined | |
125 | ||
126 | const adminRights = [ | |
127 | UserRight.MANAGE_USERS, | |
128 | UserRight.MANAGE_SERVER_FOLLOW, | |
129 | UserRight.MANAGE_ABUSES, | |
130 | UserRight.MANAGE_VIDEO_BLACKLIST, | |
131 | UserRight.MANAGE_JOBS, | |
132 | UserRight.MANAGE_CONFIGURATION | |
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] | |
148 | } | |
149 | ||
150 | logout (event: Event) { | |
151 | event.preventDefault() | |
152 | ||
153 | this.authService.logout() | |
154 | // Redirect to home page | |
155 | this.redirectService.redirectToHomepage() | |
156 | } | |
157 | ||
158 | openLanguageChooser () { | |
159 | this.languageChooserModal.show() | |
160 | } | |
161 | ||
162 | openHotkeysCheatSheet () { | |
163 | this.hotkeysService.cheatSheetToggle.next(!this.helpVisible) | |
164 | } | |
165 | ||
166 | openQuickSettings () { | |
167 | this.quickSettingsModal.show() | |
168 | } | |
169 | ||
170 | toggleUseP2P () { | |
171 | if (!this.user) return | |
172 | this.user.webTorrentEnabled = !this.user.webTorrentEnabled | |
173 | ||
174 | this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled }) | |
175 | .subscribe(() => this.authService.refreshUserInformation()) | |
176 | } | |
177 | ||
178 | langForLocale (localeId: string) { | |
179 | if (localeId === '_unknown') return this.i18n('Unknown') | |
180 | ||
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) | |
198 | } | |
199 | ||
200 | private computeIsUserHasAdminAccess () { | |
201 | const right = this.getFirstAdminRightAvailable() | |
202 | ||
203 | this.userHasAdminAccess = right !== undefined | |
204 | } | |
205 | } |