]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-user-settings/user-video-settings.component.ts
Redesign channel page
[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
d3217560
RK
41 let oldForm: any
42
d18d6478
C
43 this.buildForm({
44 nsfwPolicy: null,
ed638e53 45 webTorrentEnabled: null,
3caf77d3 46 autoPlayVideo: null,
6aa54148 47 autoPlayNextVideo: null,
3caf77d3 48 videoLanguages: null
df98563e 49 })
af5e743b 50
ccc00cb2 51 forkJoin([
ba430d75 52 this.serverService.getVideoLanguages(),
d3217560 53 this.serverService.getConfig(),
ccc00cb2 54 this.userInformationLoaded.pipe(first())
d3217560 55 ]).subscribe(([ languages, config ]) => {
52c4976f
C
56 const group = this.allLanguagesGroup
57
66357162 58 this.languageItems = [ { label: $localize`Unknown language`, id: '_unknown', group } ]
ccc00cb2 59 this.languageItems = this.languageItems
52c4976f 60 .concat(languages.map(l => ({ label: l.label, id: l.id, group })))
3caf77d3 61
52c4976f
C
62 const videoLanguages: ItemSelectCheckboxValue[] = this.user.videoLanguages
63 ? this.user.videoLanguages.map(l => ({ id: l }))
64 : [ { group } ]
3caf77d3 65
d3217560
RK
66 this.defaultNSFWPolicy = config.instance.defaultNSFWPolicy
67
ccc00cb2 68 this.form.patchValue({
d3217560 69 nsfwPolicy: this.user.nsfwPolicy || this.defaultNSFWPolicy,
ccc00cb2
C
70 webTorrentEnabled: this.user.webTorrentEnabled,
71 autoPlayVideo: this.user.autoPlayVideo === true,
6aa54148 72 autoPlayNextVideo: this.user.autoPlayNextVideo,
ccc00cb2
C
73 videoLanguages
74 })
d3217560
RK
75
76 if (this.reactiveUpdate) {
77 oldForm = { ...this.form.value }
52c4976f 78
d3217560
RK
79 this.formValuesWatcher = this.form.valueChanges.subscribe((formValue: any) => {
80 const updatedKey = Object.keys(formValue).find(k => formValue[k] !== oldForm[k])
81 oldForm = { ...this.form.value }
52c4976f
C
82
83 this.updateDetails([ updatedKey ])
d3217560
RK
84 })
85 }
ccc00cb2 86 })
af5e743b
C
87 }
88
d3217560
RK
89 ngOnDestroy () {
90 this.formValuesWatcher?.unsubscribe()
91 }
92
93 updateDetails (onlyKeys?: string[]) {
ccc00cb2 94 const nsfwPolicy = this.form.value[ 'nsfwPolicy' ]
ed638e53 95 const webTorrentEnabled = this.form.value['webTorrentEnabled']
7efe153b 96 const autoPlayVideo = this.form.value['autoPlayVideo']
6aa54148 97 const autoPlayNextVideo = this.form.value['autoPlayNextVideo']
3caf77d3 98
a9f6802e 99 const videoLanguagesForm = this.form.value['videoLanguages']
52c4976f 100
a9f6802e
C
101 if (Array.isArray(videoLanguagesForm)) {
102 if (videoLanguagesForm.length > 20) {
66357162 103 this.notifier.error($localize`Too many languages are enabled. Please enable them all or stay below 20 enabled languages.`)
3caf77d3 104 return
52c4976f
C
105 }
106
a9f6802e 107 if (videoLanguagesForm.length === 0) {
66357162 108 this.notifier.error($localize`You need to enable at least 1 video language.`)
3caf77d3
C
109 return
110 }
111 }
112
a9f6802e
C
113 const videoLanguages = this.getVideoLanguages(videoLanguagesForm)
114
d3217560 115 let details: UserUpdateMe = {
0883b324 116 nsfwPolicy,
ed638e53 117 webTorrentEnabled,
3caf77d3 118 autoPlayVideo,
6aa54148 119 autoPlayNextVideo,
3caf77d3 120 videoLanguages
df98563e 121 }
af5e743b 122
a9f6802e
C
123 if (videoLanguages) {
124 details = Object.assign(details, videoLanguages)
125 }
126
d3217560 127 if (onlyKeys) details = pick(details, onlyKeys)
af5e743b 128
d3217560
RK
129 if (this.authService.isLoggedIn()) {
130 this.userService.updateMyProfile(details).subscribe(
131 () => {
132 this.authService.refreshUserInformation()
af5e743b 133
66357162 134 if (this.notifyOnUpdate) this.notifier.success($localize`Video settings updated.`)
d3217560
RK
135 },
136
137 err => this.notifier.error(err.message)
138 )
139 } else {
140 this.userService.updateMyAnonymousProfile(details)
66357162 141 if (this.notifyOnUpdate) this.notifier.success($localize`Display/Video settings updated.`)
d3217560 142 }
af5e743b 143 }
a9f6802e
C
144
145 private getVideoLanguages (videoLanguages: ItemSelectCheckboxValue[]) {
146 if (!Array.isArray(videoLanguages)) return undefined
147
148 // null means "All"
149 if (videoLanguages.length === this.languageItems.length) return null
150
151 if (videoLanguages.length === 1) {
152 const videoLanguage = videoLanguages[0]
153
154 if (typeof videoLanguage === 'string') {
155 if (videoLanguage === this.allLanguagesGroup) return null
156 } else {
157 if (videoLanguage.group === this.allLanguagesGroup) return null
158 }
159 }
160
161 return videoLanguages.map(l => {
162 if (typeof l === 'string') return l
163
164 if (l.group) return l.group
165
166 return l.id + ''
167 })
168 }
af5e743b 169}