]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/menu/menu.component.ts
Fix tokens loading
[github/Chocobozzz/PeerTube.git] / client / src / app / menu / menu.component.ts
CommitLineData
67ed6552 1import { HotkeysService } from 'angular2-hotkeys'
dfe3f7b7
K
2import * as debug from 'debug'
3import { switchMap } from 'rxjs/operators'
8afc19a6 4import { Component, OnInit, ViewChild } from '@angular/core'
30d55e75
K
5import { Router } from '@angular/router'
6import { AuthService, AuthStatus, AuthUser, MenuService, RedirectService, ScreenService, ServerService, UserService } from '@app/core'
8afc19a6 7import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
d3217560 8import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
67ed6552 9import { ServerConfig, UserRight, VideoConstant } from '@shared/models'
602eb142 10
dfe3f7b7
K
11const logger = debug('peertube:menu:MenuComponent')
12
602eb142
C
13@Component({
14 selector: 'my-menu',
383bfc83 15 templateUrl: './menu.component.html',
dfe3f7b7 16 styleUrls: ['./menu.component.scss']
602eb142
C
17})
18export class MenuComponent implements OnInit {
f36da21e 19 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
d3217560 20 @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
8afc19a6 21
dfe3f7b7 22 user: AuthUser
df98563e 23 isLoggedIn: boolean
d3217560 24
954605a8 25 userHasAdminAccess = false
4a216666 26 helpVisible = false
954605a8 27
111fdc26
C
28 videoLanguages: string[] = []
29
30 private languages: VideoConstant<string>[] = []
ba430d75 31 private serverConfig: ServerConfig
dfe3f7b7 32 private routesPerRight: { [role in UserRight]?: string } = {
954605a8 33 [UserRight.MANAGE_USERS]: '/admin/users',
4610bc5b 34 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
d95d1559 35 [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses',
3487330d 36 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks',
ad76628b
C
37 [UserRight.MANAGE_JOBS]: '/admin/jobs',
38 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
954605a8 39 }
602eb142
C
40
41 constructor (
42 private authService: AuthService,
d3217560 43 private userService: UserService,
db7af09b 44 private serverService: ServerService,
8c985ef5 45 private redirectService: RedirectService,
d3217560 46 private hotkeysService: HotkeysService,
30d55e75
K
47 private screenService: ScreenService,
48 private menuService: MenuService,
49 private router: Router
50 ) { }
ca4b1594
K
51
52 get isInMobileView () {
53 return this.screenService.isInMobileView()
54 }
55
56 get placement () {
57 if (this.isInMobileView) {
58 return 'left-top auto'
59 } else {
60 return 'right-top auto'
61 }
62 }
602eb142 63
df98563e 64 ngOnInit () {
ba430d75
C
65 this.serverConfig = this.serverService.getTmpConfig()
66 this.serverService.getConfig()
67 .subscribe(config => this.serverConfig = config)
68
df98563e 69 this.isLoggedIn = this.authService.isLoggedIn()
dfe3f7b7
K
70 if (this.isLoggedIn === true) {
71 this.user = this.authService.getUser()
72 this.computeVideosLink()
73 }
74
75 this.computeAdminAccess()
602eb142
C
76
77 this.authService.loginChangedSource.subscribe(
78 status => {
79 if (status === AuthStatus.LoggedIn) {
df98563e 80 this.isLoggedIn = true
b33f657c 81 this.user = this.authService.getUser()
dfe3f7b7
K
82
83 this.computeAdminAccess()
84 this.computeVideosLink()
85
86 logger('Logged in.')
602eb142 87 } else if (status === AuthStatus.LoggedOut) {
df98563e 88 this.isLoggedIn = false
b33f657c 89 this.user = undefined
dfe3f7b7
K
90
91 this.computeAdminAccess()
92
93 logger('Logged out.')
602eb142 94 } else {
df98563e 95 console.error('Unknown auth status: ' + status)
602eb142
C
96 }
97 }
df98563e 98 )
4a216666 99
111fdc26 100 this.hotkeysService.cheatSheetToggle
dfe3f7b7 101 .subscribe(isOpen => this.helpVisible = isOpen)
111fdc26
C
102
103 this.serverService.getVideoLanguages()
dfe3f7b7
K
104 .subscribe(languages => {
105 this.languages = languages
d3217560 106
dfe3f7b7
K
107 this.authService.userInformationLoaded
108 .subscribe(() => this.buildUserLanguages())
109 })
d3217560
RK
110 }
111
112 get language () {
113 return this.languageChooserModal.getCurrentLanguage()
114 }
115
d3217560
RK
116 get nsfwPolicy () {
117 if (!this.user) return
111fdc26 118
d3217560
RK
119 switch (this.user.nsfwPolicy) {
120 case 'do_not_list':
66357162 121 return $localize`hide`
111fdc26 122
d3217560 123 case 'blur':
66357162 124 return $localize`blur`
111fdc26 125
d3217560 126 case 'display':
66357162 127 return $localize`display`
d3217560 128 }
602eb142
C
129 }
130
291e8d3e 131 isRegistrationAllowed () {
ba430d75 132 return this.serverConfig.signup.allowed &&
dfe3f7b7 133 this.serverConfig.signup.allowedForCurrentIP
a184c71b
C
134 }
135
954605a8
C
136 getFirstAdminRightAvailable () {
137 const user = this.authService.getUser()
138 if (!user) return undefined
139
140 const adminRights = [
141 UserRight.MANAGE_USERS,
4610bc5b 142 UserRight.MANAGE_SERVER_FOLLOW,
d95d1559 143 UserRight.MANAGE_ABUSES,
3487330d 144 UserRight.MANAGE_VIDEO_BLACKLIST,
ad76628b
C
145 UserRight.MANAGE_JOBS,
146 UserRight.MANAGE_CONFIGURATION
954605a8
C
147 ]
148
149 for (const adminRight of adminRights) {
150 if (user.hasRight(adminRight)) {
151 return adminRight
152 }
153 }
154
155 return undefined
156 }
157
158 getFirstAdminRouteAvailable () {
159 const right = this.getFirstAdminRightAvailable()
160
161 return this.routesPerRight[right]
602eb142
C
162 }
163
b33f657c
C
164 logout (event: Event) {
165 event.preventDefault()
166
df98563e 167 this.authService.logout()
602eb142 168 // Redirect to home page
b1d40cff 169 this.redirectService.redirectToHomepage()
602eb142 170 }
954605a8 171
8afc19a6
C
172 openLanguageChooser () {
173 this.languageChooserModal.show()
174 }
175
4a216666
RK
176 openHotkeysCheatSheet () {
177 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
178 }
179
d3217560
RK
180 openQuickSettings () {
181 this.quickSettingsModal.show()
182 }
183
184 toggleUseP2P () {
185 if (!this.user) return
186 this.user.webTorrentEnabled = !this.user.webTorrentEnabled
111fdc26
C
187
188 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
dfe3f7b7 189 .subscribe(() => this.authService.refreshUserInformation())
d3217560
RK
190 }
191
8ada87ac 192 langForLocale (localeId: string) {
66357162 193 if (localeId === '_unknown') return $localize`Unknown`
bb3933ef 194
111fdc26
C
195 return this.languages.find(lang => lang.id === localeId).label
196 }
197
30d55e75
K
198 onSameUrlRestoreScrollPosition (link: HTMLAnchorElement) {
199 const linkURL = link.getAttribute('href')
200 const linkHash = link.getAttribute('fragment')
201
202 // On same url without fragment restore top scroll position
203 if (!linkHash && this.router.url.includes(linkURL)) {
204 window.scrollTo({
205 left: 0,
206 top: 0,
207 behavior: 'smooth'
208 })
209 }
210
211 // On same url with fragment restore anchor scroll position
212 if (linkHash && this.router.url === linkURL) {
213 const anchor = document.getElementById(link.getAttribute('fragment'))
214 anchor.scrollIntoView({ behavior: 'smooth', inline: 'nearest' })
215 }
216
217 if (this.screenService.isInSmallView()) {
218 this.menuService.toggleMenu()
219 }
220 }
221
111fdc26
C
222 private buildUserLanguages () {
223 if (!this.user) {
224 this.videoLanguages = []
225 return
226 }
227
228 if (!this.user.videoLanguages) {
66357162 229 this.videoLanguages = [$localize`any language`]
111fdc26
C
230 return
231 }
232
233 this.videoLanguages = this.user.videoLanguages
dfe3f7b7
K
234 .map(locale => this.langForLocale(locale))
235 .map(value => value === undefined ? '?' : value)
d3217560
RK
236 }
237
dfe3f7b7 238 private computeAdminAccess () {
954605a8
C
239 const right = this.getFirstAdminRightAvailable()
240
241 this.userHasAdminAccess = right !== undefined
242 }
dfe3f7b7
K
243
244 private computeVideosLink () {
245 this.authService.userInformationLoaded
246 .pipe(
247 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
248 ).subscribe(res => {
249 if (res === true) logger('User can see videos link.')
250 else logger('User cannot see videos link.')
251 })
252 }
602eb142 253}