]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/menu/menu.component.ts
Migrate to $localize
[github/Chocobozzz/PeerTube.git] / client / src / app / menu / menu.component.ts
1 import { HotkeysService } from 'angular2-hotkeys'
2 import * as debug from 'debug'
3 import { switchMap } from 'rxjs/operators'
4 import { Component, OnInit, ViewChild } from '@angular/core'
5 import { AuthService, AuthStatus, AuthUser, RedirectService, ScreenService, ServerService, UserService } from '@app/core'
6 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
7 import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
8 import { ServerConfig, UserRight, VideoConstant } from '@shared/models'
9
10 const logger = debug('peertube:menu:MenuComponent')
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: AuthUser
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_ABUSES]: '/admin/moderation/abuses',
35 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks',
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 screenService: ScreenService
47 ) { }
48
49 get isInMobileView () {
50 return this.screenService.isInMobileView()
51 }
52
53 get placement () {
54 if (this.isInMobileView) {
55 return 'left-top auto'
56 } else {
57 return 'right-top auto'
58 }
59 }
60
61 ngOnInit () {
62 this.serverConfig = this.serverService.getTmpConfig()
63 this.serverService.getConfig()
64 .subscribe(config => this.serverConfig = config)
65
66 this.isLoggedIn = this.authService.isLoggedIn()
67 if (this.isLoggedIn === true) {
68 this.user = this.authService.getUser()
69 this.computeVideosLink()
70 }
71
72 this.computeAdminAccess()
73
74 this.authService.loginChangedSource.subscribe(
75 status => {
76 if (status === AuthStatus.LoggedIn) {
77 this.isLoggedIn = true
78 this.user = this.authService.getUser()
79
80 this.computeAdminAccess()
81 this.computeVideosLink()
82
83 logger('Logged in.')
84 } else if (status === AuthStatus.LoggedOut) {
85 this.isLoggedIn = false
86 this.user = undefined
87
88 this.computeAdminAccess()
89
90 logger('Logged out.')
91 } else {
92 console.error('Unknown auth status: ' + status)
93 }
94 }
95 )
96
97 this.hotkeysService.cheatSheetToggle
98 .subscribe(isOpen => this.helpVisible = isOpen)
99
100 this.serverService.getVideoLanguages()
101 .subscribe(languages => {
102 this.languages = languages
103
104 this.authService.userInformationLoaded
105 .subscribe(() => this.buildUserLanguages())
106 })
107 }
108
109 get language () {
110 return this.languageChooserModal.getCurrentLanguage()
111 }
112
113 get nsfwPolicy () {
114 if (!this.user) return
115
116 switch (this.user.nsfwPolicy) {
117 case 'do_not_list':
118 return $localize`hide`
119
120 case 'blur':
121 return $localize`blur`
122
123 case 'display':
124 return $localize`display`
125 }
126 }
127
128 isRegistrationAllowed () {
129 return this.serverConfig.signup.allowed &&
130 this.serverConfig.signup.allowedForCurrentIP
131 }
132
133 getFirstAdminRightAvailable () {
134 const user = this.authService.getUser()
135 if (!user) return undefined
136
137 const adminRights = [
138 UserRight.MANAGE_USERS,
139 UserRight.MANAGE_SERVER_FOLLOW,
140 UserRight.MANAGE_ABUSES,
141 UserRight.MANAGE_VIDEO_BLACKLIST,
142 UserRight.MANAGE_JOBS,
143 UserRight.MANAGE_CONFIGURATION
144 ]
145
146 for (const adminRight of adminRights) {
147 if (user.hasRight(adminRight)) {
148 return adminRight
149 }
150 }
151
152 return undefined
153 }
154
155 getFirstAdminRouteAvailable () {
156 const right = this.getFirstAdminRightAvailable()
157
158 return this.routesPerRight[right]
159 }
160
161 logout (event: Event) {
162 event.preventDefault()
163
164 this.authService.logout()
165 // Redirect to home page
166 this.redirectService.redirectToHomepage()
167 }
168
169 openLanguageChooser () {
170 this.languageChooserModal.show()
171 }
172
173 openHotkeysCheatSheet () {
174 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
175 }
176
177 openQuickSettings () {
178 this.quickSettingsModal.show()
179 }
180
181 toggleUseP2P () {
182 if (!this.user) return
183 this.user.webTorrentEnabled = !this.user.webTorrentEnabled
184
185 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
186 .subscribe(() => this.authService.refreshUserInformation())
187 }
188
189 langForLocale (localeId: string) {
190 if (localeId === '_unknown') return $localize`Unknown`
191
192 return this.languages.find(lang => lang.id === localeId).label
193 }
194
195 private buildUserLanguages () {
196 if (!this.user) {
197 this.videoLanguages = []
198 return
199 }
200
201 if (!this.user.videoLanguages) {
202 this.videoLanguages = [$localize`any language`]
203 return
204 }
205
206 this.videoLanguages = this.user.videoLanguages
207 .map(locale => this.langForLocale(locale))
208 .map(value => value === undefined ? '?' : value)
209 }
210
211 private computeAdminAccess () {
212 const right = this.getFirstAdminRightAvailable()
213
214 this.userHasAdminAccess = right !== undefined
215 }
216
217 private computeVideosLink () {
218 this.authService.userInformationLoaded
219 .pipe(
220 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
221 ).subscribe(res => {
222 if (res === true) logger('User can see videos link.')
223 else logger('User cannot see videos link.')
224 })
225 }
226 }