]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+manage/video-channel-edit/video-channel-update.component.ts
Add channel hooks
[github/Chocobozzz/PeerTube.git] / client / src / app / +manage / video-channel-edit / video-channel-update.component.ts
1 import { Subscription } from 'rxjs'
2 import { HttpErrorResponse } from '@angular/common/http'
3 import { AfterViewInit, Component, OnDestroy, OnInit } from '@angular/core'
4 import { ActivatedRoute } from '@angular/router'
5 import { AuthService, HooksService, Notifier, RedirectService, ServerService } from '@app/core'
6 import { genericUploadErrorHandler } from '@app/helpers'
7 import {
8 VIDEO_CHANNEL_DESCRIPTION_VALIDATOR,
9 VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR,
10 VIDEO_CHANNEL_SUPPORT_VALIDATOR
11 } from '@app/shared/form-validators/video-channel-validators'
12 import { FormValidatorService } from '@app/shared/shared-forms'
13 import { VideoChannel, VideoChannelService } from '@app/shared/shared-main'
14 import { HTMLServerConfig, VideoChannelUpdate } from '@shared/models'
15 import { VideoChannelEdit } from './video-channel-edit'
16
17 @Component({
18 selector: 'my-video-channel-update',
19 templateUrl: './video-channel-edit.component.html',
20 styleUrls: [ './video-channel-edit.component.scss' ]
21 })
22 export class VideoChannelUpdateComponent extends VideoChannelEdit implements OnInit, AfterViewInit, OnDestroy {
23 error: string
24 videoChannel: VideoChannel
25
26 private paramsSub: Subscription
27 private oldSupportField: string
28 private serverConfig: HTMLServerConfig
29
30 constructor (
31 protected formValidatorService: FormValidatorService,
32 private authService: AuthService,
33 private notifier: Notifier,
34 private route: ActivatedRoute,
35 private videoChannelService: VideoChannelService,
36 private serverService: ServerService,
37 private redirectService: RedirectService,
38 private hooks: HooksService
39 ) {
40 super()
41 }
42
43 ngOnInit () {
44 this.serverConfig = this.serverService.getHTMLConfig()
45
46 this.buildForm({
47 'display-name': VIDEO_CHANNEL_DISPLAY_NAME_VALIDATOR,
48 description: VIDEO_CHANNEL_DESCRIPTION_VALIDATOR,
49 support: VIDEO_CHANNEL_SUPPORT_VALIDATOR,
50 bulkVideosSupportUpdate: null
51 })
52
53 this.paramsSub = this.route.params.subscribe(routeParams => {
54 const videoChannelName = routeParams['videoChannelName']
55
56 this.videoChannelService.getVideoChannel(videoChannelName)
57 .subscribe({
58 next: videoChannelToUpdate => {
59 this.videoChannel = videoChannelToUpdate
60
61 this.hooks.runAction('action:video-channel-update.video-channel.loaded', 'video-channel', { videoChannel: this.videoChannel })
62
63 this.oldSupportField = videoChannelToUpdate.support
64
65 this.form.patchValue({
66 'display-name': videoChannelToUpdate.displayName,
67 description: videoChannelToUpdate.description,
68 support: videoChannelToUpdate.support
69 })
70 },
71
72 error: err => {
73 this.error = err.message
74 }
75 })
76 })
77 }
78
79 ngAfterViewInit () {
80 this.hooks.runAction('action:video-channel-update.init', 'video-channel')
81 }
82
83 ngOnDestroy () {
84 if (this.paramsSub) this.paramsSub.unsubscribe()
85 }
86
87 formValidated () {
88 this.error = undefined
89
90 const body = this.form.value
91 const videoChannelUpdate: VideoChannelUpdate = {
92 displayName: body['display-name'],
93 description: body.description || null,
94 support: body.support || null,
95 bulkVideosSupportUpdate: body.bulkVideosSupportUpdate || false
96 }
97
98 this.videoChannelService.updateVideoChannel(this.videoChannel.name, videoChannelUpdate)
99 .subscribe({
100 next: () => {
101 this.authService.refreshUserInformation()
102
103 this.notifier.success($localize`Video channel ${videoChannelUpdate.displayName} updated.`)
104
105 this.redirectService.redirectToPreviousRoute('/c/' + this.videoChannel.name)
106 },
107
108 error: err => {
109 this.error = err.message
110 }
111 })
112 }
113
114 onAvatarChange (formData: FormData) {
115 this.videoChannelService.changeVideoChannelImage(this.videoChannel.name, formData, 'avatar')
116 .subscribe({
117 next: data => {
118 this.notifier.success($localize`Avatar changed.`)
119
120 this.videoChannel.updateAvatar(data.avatars)
121 },
122
123 error: (err: HttpErrorResponse) => genericUploadErrorHandler({
124 err,
125 name: $localize`avatar`,
126 notifier: this.notifier
127 })
128 })
129 }
130
131 onAvatarDelete () {
132 this.videoChannelService.deleteVideoChannelImage(this.videoChannel.name, 'avatar')
133 .subscribe({
134 next: () => {
135 this.notifier.success($localize`Avatar deleted.`)
136
137 this.videoChannel.resetAvatar()
138 },
139
140 error: err => this.notifier.error(err.message)
141 })
142 }
143
144 onBannerChange (formData: FormData) {
145 this.videoChannelService.changeVideoChannelImage(this.videoChannel.name, formData, 'banner')
146 .subscribe({
147 next: data => {
148 this.notifier.success($localize`Banner changed.`)
149
150 this.videoChannel.updateBanner(data.banners)
151 },
152
153 error: (err: HttpErrorResponse) => genericUploadErrorHandler({
154 err,
155 name: $localize`banner`,
156 notifier: this.notifier
157 })
158 })
159 }
160
161 onBannerDelete () {
162 this.videoChannelService.deleteVideoChannelImage(this.videoChannel.name, 'banner')
163 .subscribe({
164 next: () => {
165 this.notifier.success($localize`Banner deleted.`)
166
167 this.videoChannel.resetBanner()
168 },
169
170 error: err => this.notifier.error(err.message)
171 })
172 }
173
174 get maxAvatarSize () {
175 return this.serverConfig.avatar.file.size.max
176 }
177
178 get avatarExtensions () {
179 return this.serverConfig.avatar.file.extensions.join(',')
180 }
181
182 isCreation () {
183 return false
184 }
185
186 getFormButtonTitle () {
187 return $localize`Update`
188 }
189
190 isBulkUpdateVideosDisplayed () {
191 if (this.oldSupportField === undefined) return false
192
193 return this.oldSupportField !== this.form.value['support']
194 }
195 }