]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-user-settings/user-video-settings.component.ts
Migrate to $localize
[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
C
97
98 let videoLanguages: string[] = this.form.value['videoLanguages']
52c4976f 99
3caf77d3 100 if (Array.isArray(videoLanguages)) {
52c4976f 101 if (videoLanguages.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
106 if (videoLanguages.length === 0) {
66357162 107 this.notifier.error($localize`You need to enable at least 1 video language.`)
3caf77d3
C
108 return
109 }
52c4976f
C
110
111 if (
112 videoLanguages.length === this.languageItems.length ||
113 (videoLanguages.length === 1 && videoLanguages[0] === this.allLanguagesGroup)
114 ) {
115 videoLanguages = null // null means "All"
116 }
3caf77d3
C
117 }
118
d3217560 119 let details: UserUpdateMe = {
0883b324 120 nsfwPolicy,
ed638e53 121 webTorrentEnabled,
3caf77d3 122 autoPlayVideo,
6aa54148 123 autoPlayNextVideo,
3caf77d3 124 videoLanguages
df98563e 125 }
af5e743b 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
C
143 }
144}