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