]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/menu/menu.component.ts
Translated using Weblate (Japanese)
[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 { environment } from 'src/environments/environment'
5 import { ViewportScroller } from '@angular/common'
6 import { Component, OnInit, ViewChild } from '@angular/core'
7 import { Router } from '@angular/router'
8 import {
9 AuthService,
10 AuthStatus,
11 AuthUser,
12 HooksService,
13 MenuSection,
14 MenuService,
15 RedirectService,
16 ScreenService,
17 ServerService,
18 UserService
19 } from '@app/core'
20 import { scrollToTop } from '@app/helpers'
21 import { LanguageChooserComponent } from '@app/menu/language-chooser.component'
22 import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component'
23 import { PeertubeModalService } from '@app/shared/shared-main/peertube-modal/peertube-modal.service'
24 import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
25 import { PluginsManager } from '@root-helpers/plugins-manager'
26 import { HTMLServerConfig, ServerConfig, UserRight, VideoConstant } from '@shared/models'
27
28 const debugLogger = debug('peertube:menu:MenuComponent')
29
30 @Component({
31 selector: 'my-menu',
32 templateUrl: './menu.component.html',
33 styleUrls: [ './menu.component.scss' ]
34 })
35 export class MenuComponent implements OnInit {
36 @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent
37 @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent
38 @ViewChild('dropdown') dropdown: NgbDropdown
39
40 user: AuthUser
41 isLoggedIn: boolean
42
43 userHasAdminAccess = false
44 helpVisible = false
45
46 videoLanguages: string[] = []
47 nsfwPolicy: string
48
49 currentInterfaceLanguage: string
50
51 menuSections: MenuSection[] = []
52
53 private languages: VideoConstant<string>[] = []
54
55 private htmlServerConfig: HTMLServerConfig
56 private serverConfig: ServerConfig
57
58 private routesPerRight: { [role in UserRight]?: string } = {
59 [UserRight.MANAGE_USERS]: '/admin/users',
60 [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends',
61 [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses',
62 [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks',
63 [UserRight.MANAGE_JOBS]: '/admin/jobs',
64 [UserRight.MANAGE_CONFIGURATION]: '/admin/config'
65 }
66
67 constructor (
68 private viewportScroller: ViewportScroller,
69 private authService: AuthService,
70 private userService: UserService,
71 private serverService: ServerService,
72 private redirectService: RedirectService,
73 private hotkeysService: HotkeysService,
74 private screenService: ScreenService,
75 private menuService: MenuService,
76 private modalService: PeertubeModalService,
77 private router: Router,
78 private hooks: HooksService
79 ) { }
80
81 get isInMobileView () {
82 return this.screenService.isInMobileView()
83 }
84
85 get dropdownContainer () {
86 if (this.isInMobileView) return null
87
88 return 'body' as 'body'
89 }
90
91 get language () {
92 return this.languageChooserModal.getCurrentLanguage()
93 }
94
95 get requiresApproval () {
96 return this.serverConfig.signup.requiresApproval
97 }
98
99 ngOnInit () {
100 this.htmlServerConfig = this.serverService.getHTMLConfig()
101 this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage()
102
103 this.isLoggedIn = this.authService.isLoggedIn()
104 this.updateUserState()
105 this.buildMenuSections()
106
107 this.authService.loginChangedSource.subscribe(
108 status => {
109 if (status === AuthStatus.LoggedIn) {
110 this.isLoggedIn = true
111 } else if (status === AuthStatus.LoggedOut) {
112 this.isLoggedIn = false
113 }
114
115 this.updateUserState()
116 this.buildMenuSections()
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.serverService.getConfig()
132 .subscribe(config => this.serverConfig = config)
133
134 this.modalService.openQuickSettingsSubject
135 .subscribe(() => this.openQuickSettings())
136 }
137
138 getExternalLoginHref () {
139 return PluginsManager.getDefaultLoginHref(environment.apiUrl, this.serverConfig)
140 }
141
142 isRegistrationAllowed () {
143 if (!this.serverConfig) return false
144
145 return this.serverConfig.signup.allowed &&
146 this.serverConfig.signup.allowedForCurrentIP
147 }
148
149 getFirstAdminRightAvailable () {
150 const user = this.authService.getUser()
151 if (!user) return undefined
152
153 const adminRights = [
154 UserRight.MANAGE_USERS,
155 UserRight.MANAGE_SERVER_FOLLOW,
156 UserRight.MANAGE_ABUSES,
157 UserRight.MANAGE_VIDEO_BLACKLIST,
158 UserRight.MANAGE_JOBS,
159 UserRight.MANAGE_CONFIGURATION
160 ]
161
162 for (const adminRight of adminRights) {
163 if (user.hasRight(adminRight)) {
164 return adminRight
165 }
166 }
167
168 return undefined
169 }
170
171 getFirstAdminRouteAvailable () {
172 const right = this.getFirstAdminRightAvailable()
173
174 return this.routesPerRight[right]
175 }
176
177 logout (event: Event) {
178 event.preventDefault()
179
180 this.authService.logout()
181 // Redirect to home page
182 this.redirectService.redirectToHomepage()
183 }
184
185 openLanguageChooser () {
186 this.languageChooserModal.show()
187 }
188
189 openHotkeysCheatSheet () {
190 this.hotkeysService.cheatSheetToggle.next(!this.helpVisible)
191 }
192
193 openQuickSettings () {
194 this.quickSettingsModal.show()
195 }
196
197 toggleUseP2P () {
198 if (!this.user) return
199 this.user.p2pEnabled = !this.user.p2pEnabled
200
201 this.userService.updateMyProfile({ p2pEnabled: this.user.p2pEnabled })
202 .subscribe(() => this.authService.refreshUserInformation())
203 }
204
205 langForLocale (localeId: string) {
206 if (localeId === '_unknown') return $localize`Unknown`
207
208 return this.languages.find(lang => lang.id === localeId).label
209 }
210
211 onActiveLinkScrollToAnchor (link: HTMLAnchorElement) {
212 const linkURL = link.getAttribute('href')
213 const linkHash = link.getAttribute('fragment')
214
215 // On same url without fragment restore top scroll position
216 if (!linkHash && this.router.url.includes(linkURL)) {
217 scrollToTop('smooth')
218 }
219
220 // On same url with fragment restore anchor scroll position
221 if (linkHash && this.router.url === linkURL) {
222 this.viewportScroller.scrollToAnchor(linkHash)
223 }
224
225 if (this.screenService.isInSmallView()) {
226 this.menuService.toggleMenu()
227 }
228 }
229
230 // Lock menu scroll when menu scroll to avoid fleeing / detached dropdown
231 onMenuScrollEvent () {
232 document.querySelector('menu').scrollTo(0, 0)
233 }
234
235 onDropdownOpenChange (opened: boolean) {
236 if (this.screenService.isInMobileView()) return
237
238 // Close dropdown when window scroll to avoid dropdown quick jump for re-position
239 const onWindowScroll = () => {
240 this.dropdown?.close()
241 window.removeEventListener('scroll', onWindowScroll)
242 }
243
244 if (opened) {
245 window.addEventListener('scroll', onWindowScroll)
246 document.querySelector('menu').scrollTo(0, 0) // Reset menu scroll to easy lock
247 document.querySelector('menu').addEventListener('scroll', this.onMenuScrollEvent)
248 } else {
249 document.querySelector('menu').removeEventListener('scroll', this.onMenuScrollEvent)
250 }
251 }
252
253 private async buildMenuSections () {
254 const menuSections = []
255
256 if (this.isLoggedIn) {
257 menuSections.push(
258 this.menuService.buildLibraryLinks(this.user?.canSeeVideosLink)
259 )
260 }
261
262 menuSections.push(
263 this.menuService.buildCommonLinks(this.htmlServerConfig)
264 )
265
266 this.menuSections = await this.hooks.wrapObject(menuSections, 'common', 'filter:left-menu.links.create.result')
267 }
268
269 private buildUserLanguages () {
270 if (!this.user) {
271 this.videoLanguages = []
272 return
273 }
274
275 if (!this.user.videoLanguages) {
276 this.videoLanguages = [ $localize`any language` ]
277 return
278 }
279
280 this.videoLanguages = this.user.videoLanguages
281 .map(locale => this.langForLocale(locale))
282 .map(value => value === undefined ? '?' : value)
283 }
284
285 private computeAdminAccess () {
286 const right = this.getFirstAdminRightAvailable()
287
288 this.userHasAdminAccess = right !== undefined
289 }
290
291 private computeVideosLink () {
292 if (!this.isLoggedIn) return
293
294 this.authService.userInformationLoaded
295 .pipe(
296 switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed()))
297 ).subscribe(res => {
298 if (res === true) debugLogger('User can see videos link.')
299 else debugLogger('User cannot see videos link.')
300 })
301 }
302
303 private computeNSFWPolicy () {
304 if (!this.user) {
305 this.nsfwPolicy = null
306 return
307 }
308
309 switch (this.user.nsfwPolicy) {
310 case 'do_not_list':
311 this.nsfwPolicy = $localize`hide`
312 break
313
314 case 'blur':
315 this.nsfwPolicy = $localize`blur`
316 break
317
318 case 'display':
319 this.nsfwPolicy = $localize`display`
320 break
321 }
322 }
323
324 private updateUserState () {
325 this.user = this.isLoggedIn
326 ? this.authService.getUser()
327 : undefined
328
329 this.computeAdminAccess()
330 this.computeNSFWPolicy()
331 this.computeVideosLink()
332 }
333 }