]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/menu/menu.component.ts
Refactor actor avatar display
[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 { ViewportScroller } from '@angular/common'
5 import { Component, OnInit, ViewChild } from '@angular/core'
6 import { Router } from '@angular/router'
7 import { AuthService, AuthStatus, AuthUser, MenuService, RedirectService, ScreenService, ServerService, UserService } from '@app/core'
8 import { scrollToTop } from '@app/helpers'
9 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
10 import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
11 import { PeertubeModalService } from '@app/shared/shared-main/peertube-modal/peertube-modal.service'
12 import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
13 import { ServerConfig, UserRight, VideoConstant } from '@shared/models'
14
15 const logger = debug('peertube:menu:MenuComponent')
16
17 @Component({
18 selector: 'my-menu',
19 templateUrl: './menu.component.html',
20 styleUrls: ['./menu.component.scss']
21 })
22 export class MenuComponent implements OnInit {
23 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
24 @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
25 @ViewChild('dropdown') dropdown: NgbDropdown
26
27 user: AuthUser
28 isLoggedIn: boolean
29
30 userHasAdminAccess = false
31 helpVisible = false
32
33 videoLanguages: string[] = []
34 nsfwPolicy: string
35
36 currentInterfaceLanguage: string
37
38 private languages: VideoConstant<string>[] = []
39 private serverConfig: ServerConfig
40 private routesPerRight: { [role in UserRight]?: string } = {
41 [UserRight.MANAGE_USERS]: '/admin/users',
42 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
43 [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses',
44 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks',
45 [UserRight.MANAGE_JOBS]: '/admin/jobs',
46 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
47 }
48
49 constructor (
50 private viewportScroller: ViewportScroller,
51 private authService: AuthService,
52 private userService: UserService,
53 private serverService: ServerService,
54 private redirectService: RedirectService,
55 private hotkeysService: HotkeysService,
56 private screenService: ScreenService,
57 private menuService: MenuService,
58 private modalService: PeertubeModalService,
59 private router: Router
60 ) { }
61
62 get isInMobileView () {
63 return this.screenService.isInMobileView()
64 }
65
66 get dropdownContainer () {
67 if (this.isInMobileView) return null
68
69 return 'body' as 'body'
70 }
71
72 get language () {
73 return this.languageChooserModal.getCurrentLanguage()
74 }
75
76 get instanceName () {
77 return this.serverConfig.instance.name
78 }
79
80 ngOnInit () {
81 this.serverConfig = this.serverService.getTmpConfig()
82 this.serverService.getConfig()
83 .subscribe(config => this.serverConfig = config)
84
85 this.isLoggedIn = this.authService.isLoggedIn()
86 if (this.isLoggedIn === true) {
87 this.user = this.authService.getUser()
88
89 this.computeNSFWPolicy()
90 this.computeVideosLink()
91 }
92
93 this.computeAdminAccess()
94
95 this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage()
96
97 this.authService.loginChangedSource.subscribe(
98 status => {
99 if (status === AuthStatus.LoggedIn) {
100 this.isLoggedIn = true
101 this.user = this.authService.getUser()
102
103 this.computeAdminAccess()
104 this.computeVideosLink()
105
106 logger('Logged in.')
107 } else if (status === AuthStatus.LoggedOut) {
108 this.isLoggedIn = false
109 this.user = undefined
110
111 this.computeAdminAccess()
112
113 logger('Logged out.')
114 } else {
115 console.error('Unknown auth status: ' + status)
116 }
117 }
118 )
119
120 this.hotkeysService.cheatSheetToggle
121 .subscribe(isOpen => this.helpVisible = isOpen)
122
123 this.serverService.getVideoLanguages()
124 .subscribe(languages => {
125 this.languages = languages
126
127 this.authService.userInformationLoaded
128 .subscribe(() => this.buildUserLanguages())
129 })
130
131 this.modalService.openQuickSettingsSubject
132 .subscribe(() => this.openQuickSettings())
133 }
134
135 isRegistrationAllowed () {
136 return this.serverConfig.signup.allowed &&
137 this.serverConfig.signup.allowedForCurrentIP
138 }
139
140 getFirstAdminRightAvailable () {
141 const user = this.authService.getUser()
142 if (!user) return undefined
143
144 const adminRights = [
145 UserRight.MANAGE_USERS,
146 UserRight.MANAGE_SERVER_FOLLOW,
147 UserRight.MANAGE_ABUSES,
148 UserRight.MANAGE_VIDEO_BLACKLIST,
149 UserRight.MANAGE_JOBS,
150 UserRight.MANAGE_CONFIGURATION
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]
166 }
167
168 logout (event: Event) {
169 event.preventDefault()
170
171 this.authService.logout()
172 // Redirect to home page
173 this.redirectService.redirectToHomepage()
174 }
175
176 openLanguageChooser () {
177 this.languageChooserModal.show()
178 }
179
180 openHotkeysCheatSheet () {
181 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
182 }
183
184 openQuickSettings () {
185 this.quickSettingsModal.show()
186 }
187
188 toggleUseP2P () {
189 if (!this.user) return
190 this.user.webTorrentEnabled = !this.user.webTorrentEnabled
191
192 this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })
193 .subscribe(() => this.authService.refreshUserInformation())
194 }
195
196 langForLocale (localeId: string) {
197 if (localeId === '_unknown') return $localize`Unknown`
198
199 return this.languages.find(lang => lang.id === localeId).label
200 }
201
202 onActiveLinkScrollToAnchor (link: HTMLAnchorElement) {
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)) {
208 scrollToTop('smooth')
209 }
210
211 // On same url with fragment restore anchor scroll position
212 if (linkHash && this.router.url === linkURL) {
213 this.viewportScroller.scrollToAnchor(linkHash)
214 }
215
216 if (this.screenService.isInSmallView()) {
217 this.menuService.toggleMenu()
218 }
219 }
220
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 = () => {
231 this.dropdown?.close()
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
244 private buildUserLanguages () {
245 if (!this.user) {
246 this.videoLanguages = []
247 return
248 }
249
250 if (!this.user.videoLanguages) {
251 this.videoLanguages = [$localize`any language`]
252 return
253 }
254
255 this.videoLanguages = this.user.videoLanguages
256 .map(locale => this.langForLocale(locale))
257 .map(value => value === undefined ? '?' : value)
258 }
259
260 private computeAdminAccess () {
261 const right = this.getFirstAdminRightAvailable()
262
263 this.userHasAdminAccess = right !== undefined
264 }
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 }
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 }
296 }