]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/menu/menu.component.ts
Add links from user-edit
[github/Chocobozzz/PeerTube.git] / client / src / app / menu / menu.component.ts
CommitLineData
8afc19a6 1import { Component, OnInit, ViewChild } from '@angular/core'
b33f657c 2import { UserRight } from '../../../../shared/models/users/user-right.enum'
d3217560
RK
3import { AuthService, AuthStatus, RedirectService, ServerService } from '../core'
4import { User } from '@app/shared/users/user.model'
5import { UserService } from '@app/shared/users/user.service'
8afc19a6 6import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
4a216666 7import { HotkeysService } from 'angular2-hotkeys'
d3217560
RK
8import { ServerConfig, VideoConstant } from '@shared/models'
9import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
10import { 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})
17export 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
954605a8 26
111fdc26
C
27 videoLanguages: string[] = []
28
29 private languages: VideoConstant<string>[] = []
ba430d75 30 private serverConfig: ServerConfig
c199c427 31 private routesPerRight: { [ role in UserRight ]?: string } = {
954605a8 32 [UserRight.MANAGE_USERS]: '/admin/users',
4610bc5b 33 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
ad76628b
C
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'
954605a8 38 }
602eb142
C
39
40 constructor (
41 private authService: AuthService,
d3217560 42 private userService: UserService,
db7af09b 43 private serverService: ServerService,
8c985ef5 44 private redirectService: RedirectService,
d3217560
RK
45 private hotkeysService: HotkeysService,
46 private i18n: I18n
602eb142
C
47 ) {}
48
df98563e 49 ngOnInit () {
ba430d75
C
50 this.serverConfig = this.serverService.getTmpConfig()
51 this.serverService.getConfig()
52 .subscribe(config => this.serverConfig = config)
53
df98563e 54 this.isLoggedIn = this.authService.isLoggedIn()
b33f657c 55 if (this.isLoggedIn === true) this.user = this.authService.getUser()
954605a8 56 this.computeIsUserHasAdminAccess()
602eb142
C
57
58 this.authService.loginChangedSource.subscribe(
59 status => {
60 if (status === AuthStatus.LoggedIn) {
df98563e 61 this.isLoggedIn = true
b33f657c 62 this.user = this.authService.getUser()
954605a8 63 this.computeIsUserHasAdminAccess()
df98563e 64 console.log('Logged in.')
602eb142 65 } else if (status === AuthStatus.LoggedOut) {
df98563e 66 this.isLoggedIn = false
b33f657c 67 this.user = undefined
954605a8 68 this.computeIsUserHasAdminAccess()
df98563e 69 console.log('Logged out.')
602eb142 70 } else {
df98563e 71 console.error('Unknown auth status: ' + status)
602eb142
C
72 }
73 }
df98563e 74 )
4a216666 75
111fdc26
C
76 this.hotkeysService.cheatSheetToggle
77 .subscribe(isOpen => this.helpVisible = isOpen)
78
79 this.serverService.getVideoLanguages()
80 .subscribe(languages => {
81 this.languages = languages
d3217560 82
111fdc26
C
83 this.authService.userInformationLoaded
84 .subscribe(() => this.buildUserLanguages())
85 })
d3217560
RK
86 }
87
88 get language () {
89 return this.languageChooserModal.getCurrentLanguage()
90 }
91
d3217560
RK
92 get nsfwPolicy () {
93 if (!this.user) return
111fdc26 94
d3217560
RK
95 switch (this.user.nsfwPolicy) {
96 case 'do_not_list':
97 return this.i18n('hide')
111fdc26 98
d3217560
RK
99 case 'blur':
100 return this.i18n('blur')
111fdc26 101
d3217560
RK
102 case 'display':
103 return this.i18n('display')
104 }
602eb142
C
105 }
106
291e8d3e 107 isRegistrationAllowed () {
ba430d75
C
108 return this.serverConfig.signup.allowed &&
109 this.serverConfig.signup.allowedForCurrentIP
a184c71b
C
110 }
111
954605a8
C
112 getFirstAdminRightAvailable () {
113 const user = this.authService.getUser()
114 if (!user) return undefined
115
116 const adminRights = [
117 UserRight.MANAGE_USERS,
4610bc5b 118 UserRight.MANAGE_SERVER_FOLLOW,
954605a8 119 UserRight.MANAGE_VIDEO_ABUSES,
ad76628b
C
120 UserRight.MANAGE_VIDEO_BLACKLIST,
121 UserRight.MANAGE_JOBS,
122 UserRight.MANAGE_CONFIGURATION
954605a8
C
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]
602eb142
C
138 }
139
b33f657c
C
140 logout (event: Event) {
141 event.preventDefault()
142
df98563e 143 this.authService.logout()
602eb142 144 // Redirect to home page
b1d40cff 145 this.redirectService.redirectToHomepage()
602eb142 146 }
954605a8 147
8afc19a6
C
148 openLanguageChooser () {
149 this.languageChooserModal.show()
150 }
151
4a216666
RK
152 openHotkeysCheatSheet () {
153 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
154 }
155
d3217560
RK
156 openQuickSettings () {
157 this.quickSettingsModal.show()
158 }
159
160 toggleUseP2P () {
161 if (!this.user) return
162 this.user.webTorrentEnabled = !this.user.webTorrentEnabled
111fdc26
C
163
164 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
165 .subscribe(() => this.authService.refreshUserInformation())
d3217560
RK
166 }
167
8ada87ac 168 langForLocale (localeId: string) {
bb3933ef
C
169 if (localeId === '_unknown') return this.i18n('Unknown')
170
111fdc26
C
171 return this.languages.find(lang => lang.id === localeId).label
172 }
173
174 private buildUserLanguages () {
175 if (!this.user) {
176 this.videoLanguages = []
177 return
178 }
179
180 if (!this.user.videoLanguages) {
181 this.videoLanguages = [ this.i18n('any language') ]
182 return
183 }
184
185 this.videoLanguages = this.user.videoLanguages
186 .map(locale => this.langForLocale(locale))
187 .map(value => value === undefined ? '?' : value)
d3217560
RK
188 }
189
954605a8
C
190 private computeIsUserHasAdminAccess () {
191 const right = this.getFirstAdminRightAvailable()
192
193 this.userHasAdminAccess = right !== undefined
194 }
602eb142 195}