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