]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-user-settings/user-video-settings.component.ts
Fix table header overflow
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-user-settings / user-video-settings.component.ts
CommitLineData
67ed6552 1import { pick } from 'lodash-es'
67ed6552 2import { forkJoin, Subject, Subscription } from 'rxjs'
ccc00cb2 3import { first } from 'rxjs/operators'
67ed6552
C
4import { Component, Input, OnDestroy, OnInit } from '@angular/core'
5import { AuthService, Notifier, ServerService, User, UserService } from '@app/core'
52c4976f 6import { FormReactive, FormValidatorService, ItemSelectCheckboxValue, SelectOptionsItem } from '@app/shared/shared-forms'
67ed6552 7import { UserUpdateMe } from '@shared/models'
d3217560 8import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
af5e743b
C
9
10@Component({
67ed6552
C
11 selector: 'my-user-video-settings',
12 templateUrl: './user-video-settings.component.html',
13 styleUrls: [ './user-video-settings.component.scss' ]
af5e743b 14})
67ed6552 15export class UserVideoSettingsComponent extends FormReactive implements OnInit, OnDestroy {
df98563e 16 @Input() user: User = null
d3217560
RK
17 @Input() reactiveUpdate = false
18 @Input() notifyOnUpdate = true
d18d6478 19 @Input() userInformationLoaded: Subject<any>
af5e743b 20
52c4976f 21 languageItems: SelectOptionsItem[] = []
d3217560
RK
22 defaultNSFWPolicy: NSFWPolicyType
23 formValuesWatcher: Subscription
3caf77d3 24
52c4976f
C
25 private allLanguagesGroup: string
26
df98563e 27 constructor (
d18d6478 28 protected formValidatorService: FormValidatorService,
af5e743b 29 private authService: AuthService,
f8b2c1b4 30 private notifier: Notifier,
b1d40cff 31 private userService: UserService,
66357162 32 private serverService: ServerService
af5e743b 33 ) {
df98563e 34 super()
af5e743b
C
35 }
36
d18d6478 37 ngOnInit () {
66357162 38 this.allLanguagesGroup = $localize`All languages`
52c4976f 39
d3217560
RK
40 let oldForm: any
41
d18d6478
C
42 this.buildForm({
43 nsfwPolicy: null,
ed638e53 44 webTorrentEnabled: null,
3caf77d3 45 autoPlayVideo: null,
6aa54148 46 autoPlayNextVideo: null,
3caf77d3 47 videoLanguages: null
df98563e 48 })
af5e743b 49
ccc00cb2 50 forkJoin([
ba430d75 51 this.serverService.getVideoLanguages(),
d3217560 52 this.serverService.getConfig(),
ccc00cb2 53 this.userInformationLoaded.pipe(first())
d3217560 54 ]).subscribe(([ languages, config ]) => {
52c4976f
C
55 const group = this.allLanguagesGroup
56
66357162 57 this.languageItems = [ { label: $localize`Unknown language`, id: '_unknown', group } ]
ccc00cb2 58 this.languageItems = this.languageItems
52c4976f 59 .concat(languages.map(l => ({ label: l.label, id: l.id, group })))
3caf77d3 60
52c4976f
C
61 const videoLanguages: ItemSelectCheckboxValue[] = this.user.videoLanguages
62 ? this.user.videoLanguages.map(l => ({ id: l }))
63 : [ { group } ]
3caf77d3 64
d3217560
RK
65 this.defaultNSFWPolicy = config.instance.defaultNSFWPolicy
66
ccc00cb2 67 this.form.patchValue({
d3217560 68 nsfwPolicy: this.user.nsfwPolicy || this.defaultNSFWPolicy,
ccc00cb2
C
69 webTorrentEnabled: this.user.webTorrentEnabled,
70 autoPlayVideo: this.user.autoPlayVideo === true,
6aa54148 71 autoPlayNextVideo: this.user.autoPlayNextVideo,
ccc00cb2
C
72 videoLanguages
73 })
d3217560
RK
74
75 if (this.reactiveUpdate) {
76 oldForm = { ...this.form.value }
52c4976f 77
d3217560
RK
78 this.formValuesWatcher = this.form.valueChanges.subscribe((formValue: any) => {
79 const updatedKey = Object.keys(formValue).find(k => formValue[k] !== oldForm[k])
80 oldForm = { ...this.form.value }
52c4976f
C
81
82 this.updateDetails([ updatedKey ])
d3217560
RK
83 })
84 }
ccc00cb2 85 })
af5e743b
C
86 }
87
d3217560
RK
88 ngOnDestroy () {
89 this.formValuesWatcher?.unsubscribe()
90 }
91
92 updateDetails (onlyKeys?: string[]) {
ccc00cb2 93 const nsfwPolicy = this.form.value[ 'nsfwPolicy' ]
ed638e53 94 const webTorrentEnabled = this.form.value['webTorrentEnabled']
7efe153b 95 const autoPlayVideo = this.form.value['autoPlayVideo']
6aa54148 96 const autoPlayNextVideo = this.form.value['autoPlayNextVideo']
3caf77d3 97
a9f6802e 98 const videoLanguagesForm = this.form.value['videoLanguages']
52c4976f 99
a9f6802e
C
100 if (Array.isArray(videoLanguagesForm)) {
101 if (videoLanguagesForm.length > 20) {
66357162 102 this.notifier.error($localize`Too many languages are enabled. Please enable them all or stay below 20 enabled languages.`)
3caf77d3 103 return
52c4976f
C
104 }
105
a9f6802e 106 if (videoLanguagesForm.length === 0) {
66357162 107 this.notifier.error($localize`You need to enable at least 1 video language.`)
3caf77d3
C
108 return
109 }
110 }
111
a9f6802e
C
112 const videoLanguages = this.getVideoLanguages(videoLanguagesForm)
113
d3217560 114 let details: UserUpdateMe = {
0883b324 115 nsfwPolicy,
ed638e53 116 webTorrentEnabled,
3caf77d3 117 autoPlayVideo,
6aa54148 118 autoPlayNextVideo,
3caf77d3 119 videoLanguages
df98563e 120 }
af5e743b 121
a9f6802e
C
122 if (videoLanguages) {
123 details = Object.assign(details, videoLanguages)
124 }
125
d3217560 126 if (onlyKeys) details = pick(details, onlyKeys)
af5e743b 127
d3217560
RK
128 if (this.authService.isLoggedIn()) {
129 this.userService.updateMyProfile(details).subscribe(
130 () => {
131 this.authService.refreshUserInformation()
af5e743b 132
66357162 133 if (this.notifyOnUpdate) this.notifier.success($localize`Video settings updated.`)
d3217560
RK
134 },
135
136 err => this.notifier.error(err.message)
137 )
138 } else {
139 this.userService.updateMyAnonymousProfile(details)
66357162 140 if (this.notifyOnUpdate) this.notifier.success($localize`Display/Video settings updated.`)
d3217560 141 }
af5e743b 142 }
a9f6802e
C
143
144 private getVideoLanguages (videoLanguages: ItemSelectCheckboxValue[]) {
145 if (!Array.isArray(videoLanguages)) return undefined
146
147 // null means "All"
148 if (videoLanguages.length === this.languageItems.length) return null
149
150 if (videoLanguages.length === 1) {
151 const videoLanguage = videoLanguages[0]
152
153 if (typeof videoLanguage === 'string') {
154 if (videoLanguage === this.allLanguagesGroup) return null
155 } else {
156 if (videoLanguage.group === this.allLanguagesGroup) return null
157 }
158 }
159
160 return videoLanguages.map(l => {
161 if (typeof l === 'string') return l
162
163 if (l.group) return l.group
164
165 return l.id + ''
166 })
167 }
af5e743b 168}