Remove uneccessary details to link titles
[oweals/peertube.git] / client / src / app / menu / menu.component.ts
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 import { ScreenService } from '@app/shared/misc/screen.service'
12
13 @Component({
14   selector: 'my-menu',
15   templateUrl: './menu.component.html',
16   styleUrls: [ './menu.component.scss' ]
17 })
18 export class MenuComponent implements OnInit {
19   @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
20   @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
21
22   user: User
23   isLoggedIn: boolean
24
25   userHasAdminAccess = false
26   helpVisible = false
27
28   videoLanguages: string[] = []
29
30   private languages: VideoConstant<string>[] = []
31   private serverConfig: ServerConfig
32   private routesPerRight: { [ role in UserRight ]?: string } = {
33     [UserRight.MANAGE_USERS]: '/admin/users',
34     [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
35     [UserRight.MANAGE_VIDEO_ABUSES]: '/admin/moderation/video-abuses',
36     [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks',
37     [UserRight.MANAGE_JOBS]: '/admin/jobs',
38     [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
39   }
40
41   constructor (
42     private authService: AuthService,
43     private userService: UserService,
44     private serverService: ServerService,
45     private redirectService: RedirectService,
46     private hotkeysService: HotkeysService,
47     private screenService: ScreenService,
48     private i18n: I18n
49   ) { }
50
51   get isInMobileView () {
52     return this.screenService.isInMobileView()
53   }
54
55   get placement () {
56     if (this.isInMobileView) {
57       return 'left-top auto'
58     } else {
59       return 'right-top auto'
60     }
61   }
62
63   ngOnInit () {
64     this.serverConfig = this.serverService.getTmpConfig()
65     this.serverService.getConfig()
66       .subscribe(config => this.serverConfig = config)
67
68     this.isLoggedIn = this.authService.isLoggedIn()
69     if (this.isLoggedIn === true) this.user = this.authService.getUser()
70     this.computeIsUserHasAdminAccess()
71
72     this.authService.loginChangedSource.subscribe(
73       status => {
74         if (status === AuthStatus.LoggedIn) {
75           this.isLoggedIn = true
76           this.user = this.authService.getUser()
77           this.computeIsUserHasAdminAccess()
78           console.log('Logged in.')
79         } else if (status === AuthStatus.LoggedOut) {
80           this.isLoggedIn = false
81           this.user = undefined
82           this.computeIsUserHasAdminAccess()
83           console.log('Logged out.')
84         } else {
85           console.error('Unknown auth status: ' + status)
86         }
87       }
88     )
89
90     this.hotkeysService.cheatSheetToggle
91         .subscribe(isOpen => this.helpVisible = isOpen)
92
93     this.serverService.getVideoLanguages()
94         .subscribe(languages => {
95           this.languages = languages
96
97           this.authService.userInformationLoaded
98               .subscribe(() => this.buildUserLanguages())
99         })
100   }
101
102   get language () {
103     return this.languageChooserModal.getCurrentLanguage()
104   }
105
106   get nsfwPolicy () {
107     if (!this.user) return
108
109     switch (this.user.nsfwPolicy) {
110       case 'do_not_list':
111         return this.i18n('hide')
112
113       case 'blur':
114         return this.i18n('blur')
115
116       case 'display':
117         return this.i18n('display')
118     }
119   }
120
121   isRegistrationAllowed () {
122     return this.serverConfig.signup.allowed &&
123            this.serverConfig.signup.allowedForCurrentIP
124   }
125
126   getFirstAdminRightAvailable () {
127     const user = this.authService.getUser()
128     if (!user) return undefined
129
130     const adminRights = [
131       UserRight.MANAGE_USERS,
132       UserRight.MANAGE_SERVER_FOLLOW,
133       UserRight.MANAGE_VIDEO_ABUSES,
134       UserRight.MANAGE_VIDEO_BLACKLIST,
135       UserRight.MANAGE_JOBS,
136       UserRight.MANAGE_CONFIGURATION
137     ]
138
139     for (const adminRight of adminRights) {
140       if (user.hasRight(adminRight)) {
141         return adminRight
142       }
143     }
144
145     return undefined
146   }
147
148   getFirstAdminRouteAvailable () {
149     const right = this.getFirstAdminRightAvailable()
150
151     return this.routesPerRight[right]
152   }
153
154   logout (event: Event) {
155     event.preventDefault()
156
157     this.authService.logout()
158     // Redirect to home page
159     this.redirectService.redirectToHomepage()
160   }
161
162   openLanguageChooser () {
163     this.languageChooserModal.show()
164   }
165
166   openHotkeysCheatSheet () {
167     this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
168   }
169
170   openQuickSettings () {
171     this.quickSettingsModal.show()
172   }
173
174   toggleUseP2P () {
175     if (!this.user) return
176     this.user.webTorrentEnabled = !this.user.webTorrentEnabled
177
178     this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
179         .subscribe(() => this.authService.refreshUserInformation())
180   }
181
182   langForLocale (localeId: string) {
183     if (localeId === '_unknown') return this.i18n('Unknown')
184
185     return this.languages.find(lang => lang.id === localeId).label
186   }
187
188   private buildUserLanguages () {
189     if (!this.user) {
190       this.videoLanguages = []
191       return
192     }
193
194     if (!this.user.videoLanguages) {
195       this.videoLanguages = [ this.i18n('any language') ]
196       return
197     }
198
199     this.videoLanguages = this.user.videoLanguages
200                               .map(locale => this.langForLocale(locale))
201                               .map(value => value === undefined ? '?' : value)
202   }
203
204   private computeIsUserHasAdminAccess () {
205     const right = this.getFirstAdminRightAvailable()
206
207     this.userHasAdminAccess = right !== undefined
208   }
209 }