]>
Commit | Line | Data |
---|---|---|
1 | import { map, switchMap } from 'rxjs/operators' | |
2 | import { Component, OnInit } from '@angular/core' | |
3 | import { ActivatedRoute, Router } from '@angular/router' | |
4 | import { LoadingBarService } from '@ngx-loading-bar/core' | |
5 | import { NotificationsService } from 'angular2-notifications' | |
6 | import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos' | |
7 | import { ServerService } from '../../core' | |
8 | import { AuthService } from '../../core/auth' | |
9 | import { FormReactive } from '../../shared' | |
10 | import { VideoEdit } from '../../shared/video/video-edit.model' | |
11 | import { VideoService } from '../../shared/video/video.service' | |
12 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' | |
13 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
14 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | |
15 | import { VideoCaptionService } from '@app/shared/video-caption' | |
16 | import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model' | |
17 | ||
18 | @Component({ | |
19 | selector: 'my-videos-update', | |
20 | styleUrls: [ './shared/video-edit.component.scss' ], | |
21 | templateUrl: './video-update.component.html' | |
22 | }) | |
23 | export class VideoUpdateComponent extends FormReactive implements OnInit { | |
24 | video: VideoEdit | |
25 | ||
26 | isUpdatingVideo = false | |
27 | videoPrivacies: VideoConstant<string>[] = [] | |
28 | userVideoChannels: { id: number, label: string, support: string }[] = [] | |
29 | schedulePublicationPossible = false | |
30 | videoCaptions: VideoCaptionEdit[] = [] | |
31 | ||
32 | constructor ( | |
33 | protected formValidatorService: FormValidatorService, | |
34 | private route: ActivatedRoute, | |
35 | private router: Router, | |
36 | private notificationsService: NotificationsService, | |
37 | private serverService: ServerService, | |
38 | private videoService: VideoService, | |
39 | private authService: AuthService, | |
40 | private loadingBar: LoadingBarService, | |
41 | private videoChannelService: VideoChannelService, | |
42 | private videoCaptionService: VideoCaptionService, | |
43 | private i18n: I18n | |
44 | ) { | |
45 | super() | |
46 | } | |
47 | ||
48 | ngOnInit () { | |
49 | this.buildForm({}) | |
50 | ||
51 | this.serverService.videoPrivaciesLoaded | |
52 | .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies()) | |
53 | ||
54 | const uuid: string = this.route.snapshot.params[ 'uuid' ] | |
55 | this.videoService.getVideo(uuid) | |
56 | .pipe( | |
57 | switchMap(video => { | |
58 | return this.videoService | |
59 | .loadCompleteDescription(video.descriptionPath) | |
60 | .pipe(map(description => Object.assign(video, { description }))) | |
61 | }), | |
62 | switchMap(video => { | |
63 | return this.videoChannelService | |
64 | .listAccountVideoChannels(video.account) | |
65 | .pipe( | |
66 | map(result => result.data), | |
67 | map(videoChannels => videoChannels.map(c => ({ id: c.id, label: c.displayName, support: c.support }))), | |
68 | map(videoChannels => ({ video, videoChannels })) | |
69 | ) | |
70 | }), | |
71 | switchMap(({ video, videoChannels }) => { | |
72 | return this.videoCaptionService | |
73 | .listCaptions(video.id) | |
74 | .pipe( | |
75 | map(result => result.data), | |
76 | map(videoCaptions => ({ video, videoChannels, videoCaptions })) | |
77 | ) | |
78 | }) | |
79 | ) | |
80 | .subscribe( | |
81 | ({ video, videoChannels, videoCaptions }) => { | |
82 | this.video = new VideoEdit(video) | |
83 | this.userVideoChannels = videoChannels | |
84 | this.videoCaptions = videoCaptions | |
85 | ||
86 | // We cannot set private a video that was not private | |
87 | if (this.video.privacy !== VideoPrivacy.PRIVATE) { | |
88 | this.videoPrivacies = this.videoPrivacies.filter(p => p.id.toString() !== VideoPrivacy.PRIVATE.toString()) | |
89 | } else { // We can schedule video publication only if it it is private | |
90 | this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE | |
91 | } | |
92 | ||
93 | this.hydrateFormFromVideo() | |
94 | }, | |
95 | ||
96 | err => { | |
97 | console.error(err) | |
98 | this.notificationsService.error(this.i18n('Error'), err.message) | |
99 | } | |
100 | ) | |
101 | } | |
102 | ||
103 | checkForm () { | |
104 | this.forceCheck() | |
105 | ||
106 | return this.form.valid | |
107 | } | |
108 | ||
109 | update () { | |
110 | if (this.checkForm() === false) { | |
111 | return | |
112 | } | |
113 | ||
114 | this.video.patch(this.form.value) | |
115 | ||
116 | this.loadingBar.start() | |
117 | this.isUpdatingVideo = true | |
118 | ||
119 | // Update the video | |
120 | this.videoService.updateVideo(this.video) | |
121 | .pipe( | |
122 | // Then update captions | |
123 | switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions)) | |
124 | ) | |
125 | .subscribe( | |
126 | () => { | |
127 | this.isUpdatingVideo = false | |
128 | this.loadingBar.complete() | |
129 | this.notificationsService.success(this.i18n('Success'), this.i18n('Video updated.')) | |
130 | this.router.navigate([ '/videos/watch', this.video.uuid ]) | |
131 | }, | |
132 | ||
133 | err => { | |
134 | this.isUpdatingVideo = false | |
135 | this.notificationsService.error(this.i18n('Error'), err.message) | |
136 | console.error(err) | |
137 | } | |
138 | ) | |
139 | ||
140 | } | |
141 | ||
142 | private hydrateFormFromVideo () { | |
143 | this.form.patchValue(this.video.toFormPatch()) | |
144 | ||
145 | const objects = [ | |
146 | { | |
147 | url: 'thumbnailUrl', | |
148 | name: 'thumbnailfile' | |
149 | }, | |
150 | { | |
151 | url: 'previewUrl', | |
152 | name: 'previewfile' | |
153 | } | |
154 | ] | |
155 | ||
156 | for (const obj of objects) { | |
157 | fetch(this.video[obj.url]) | |
158 | .then(response => response.blob()) | |
159 | .then(data => { | |
160 | this.form.patchValue({ | |
161 | [ obj.name ]: data | |
162 | }) | |
163 | }) | |
164 | } | |
165 | } | |
166 | } |