]>
Commit | Line | Data |
---|---|---|
1 | import * as debug from 'debug' | |
2 | import { Subject, Subscription } from 'rxjs' | |
3 | import { debounceTime, filter } from 'rxjs/operators' | |
4 | import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges } from '@angular/core' | |
5 | import { AuthService, DisableForReuseHook, Notifier } from '@app/core' | |
6 | import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' | |
7 | import { Video, VideoExistInPlaylist, VideoPlaylistCreate, VideoPlaylistElementCreate, VideoPlaylistPrivacy } from '@shared/models' | |
8 | import { secondsToTime } from '../../../assets/player/utils' | |
9 | import { VIDEO_PLAYLIST_DISPLAY_NAME_VALIDATOR } from '../form-validators/video-playlist-validators' | |
10 | import { CachedPlaylist, VideoPlaylistService } from './video-playlist.service' | |
11 | ||
12 | const logger = debug('peertube:playlists:VideoAddToPlaylistComponent') | |
13 | ||
14 | type PlaylistSummary = { | |
15 | id: number | |
16 | inPlaylist: boolean | |
17 | displayName: string | |
18 | ||
19 | playlistElementId?: number | |
20 | startTimestamp?: number | |
21 | stopTimestamp?: number | |
22 | } | |
23 | ||
24 | @Component({ | |
25 | selector: 'my-video-add-to-playlist', | |
26 | styleUrls: [ './video-add-to-playlist.component.scss' ], | |
27 | templateUrl: './video-add-to-playlist.component.html', | |
28 | changeDetection: ChangeDetectionStrategy.OnPush | |
29 | }) | |
30 | export class VideoAddToPlaylistComponent extends FormReactive implements OnInit, OnChanges, OnDestroy, DisableForReuseHook { | |
31 | @Input() video: Video | |
32 | @Input() currentVideoTimestamp: number | |
33 | @Input() lazyLoad = false | |
34 | ||
35 | isNewPlaylistBlockOpened = false | |
36 | videoPlaylistSearch: string | |
37 | videoPlaylistSearchChanged = new Subject<string>() | |
38 | videoPlaylists: PlaylistSummary[] = [] | |
39 | timestampOptions: { | |
40 | startTimestampEnabled: boolean | |
41 | startTimestamp: number | |
42 | stopTimestampEnabled: boolean | |
43 | stopTimestamp: number | |
44 | } | |
45 | displayOptions = false | |
46 | ||
47 | private disabled = false | |
48 | ||
49 | private listenToPlaylistChangeSub: Subscription | |
50 | private playlistsData: CachedPlaylist[] = [] | |
51 | ||
52 | constructor ( | |
53 | protected formValidatorService: FormValidatorService, | |
54 | private authService: AuthService, | |
55 | private notifier: Notifier, | |
56 | private videoPlaylistService: VideoPlaylistService, | |
57 | private cd: ChangeDetectorRef | |
58 | ) { | |
59 | super() | |
60 | } | |
61 | ||
62 | get user () { | |
63 | return this.authService.getUser() | |
64 | } | |
65 | ||
66 | ngOnInit () { | |
67 | this.buildForm({ | |
68 | displayName: VIDEO_PLAYLIST_DISPLAY_NAME_VALIDATOR | |
69 | }) | |
70 | ||
71 | this.videoPlaylistService.listenToMyAccountPlaylistsChange() | |
72 | .subscribe(result => { | |
73 | this.playlistsData = result.data | |
74 | ||
75 | this.videoPlaylistService.runPlaylistCheck(this.video.id) | |
76 | }) | |
77 | ||
78 | this.videoPlaylistSearchChanged | |
79 | .pipe(debounceTime(500)) | |
80 | .subscribe(() => this.load()) | |
81 | ||
82 | if (this.lazyLoad === false) this.load() | |
83 | } | |
84 | ||
85 | ngOnChanges (simpleChanges: SimpleChanges) { | |
86 | if (simpleChanges['video']) { | |
87 | this.reload() | |
88 | } | |
89 | } | |
90 | ||
91 | ngOnDestroy () { | |
92 | this.unsubscribePlaylistChanges() | |
93 | } | |
94 | ||
95 | disableForReuse () { | |
96 | this.disabled = true | |
97 | } | |
98 | ||
99 | enabledForReuse () { | |
100 | this.disabled = false | |
101 | } | |
102 | ||
103 | reload () { | |
104 | logger('Reloading component') | |
105 | ||
106 | this.videoPlaylists = [] | |
107 | this.videoPlaylistSearch = undefined | |
108 | ||
109 | this.resetOptions(true) | |
110 | this.load() | |
111 | ||
112 | this.cd.markForCheck() | |
113 | } | |
114 | ||
115 | load () { | |
116 | logger('Loading component') | |
117 | ||
118 | this.listenToPlaylistChanges() | |
119 | ||
120 | this.videoPlaylistService.listMyPlaylistWithCache(this.user, this.videoPlaylistSearch) | |
121 | .subscribe(playlistsResult => { | |
122 | this.playlistsData = playlistsResult.data | |
123 | ||
124 | this.videoPlaylistService.runPlaylistCheck(this.video.id) | |
125 | }) | |
126 | } | |
127 | ||
128 | openChange (opened: boolean) { | |
129 | if (opened === false) { | |
130 | this.isNewPlaylistBlockOpened = false | |
131 | this.displayOptions = false | |
132 | } | |
133 | } | |
134 | ||
135 | openCreateBlock (event: Event) { | |
136 | event.preventDefault() | |
137 | ||
138 | this.isNewPlaylistBlockOpened = true | |
139 | } | |
140 | ||
141 | togglePlaylist (event: Event, playlist: PlaylistSummary) { | |
142 | event.preventDefault() | |
143 | ||
144 | if (playlist.inPlaylist === true) { | |
145 | this.removeVideoFromPlaylist(playlist) | |
146 | } else { | |
147 | this.addVideoInPlaylist(playlist) | |
148 | } | |
149 | ||
150 | playlist.inPlaylist = !playlist.inPlaylist | |
151 | this.resetOptions() | |
152 | ||
153 | this.cd.markForCheck() | |
154 | } | |
155 | ||
156 | createPlaylist () { | |
157 | const displayName = this.form.value[ 'displayName' ] | |
158 | ||
159 | const videoPlaylistCreate: VideoPlaylistCreate = { | |
160 | displayName, | |
161 | privacy: VideoPlaylistPrivacy.PRIVATE | |
162 | } | |
163 | ||
164 | this.videoPlaylistService.createVideoPlaylist(videoPlaylistCreate).subscribe( | |
165 | () => { | |
166 | this.isNewPlaylistBlockOpened = false | |
167 | ||
168 | this.cd.markForCheck() | |
169 | }, | |
170 | ||
171 | err => this.notifier.error(err.message) | |
172 | ) | |
173 | } | |
174 | ||
175 | resetOptions (resetTimestamp = false) { | |
176 | this.displayOptions = false | |
177 | ||
178 | this.timestampOptions = {} as any | |
179 | this.timestampOptions.startTimestampEnabled = false | |
180 | this.timestampOptions.stopTimestampEnabled = false | |
181 | ||
182 | if (resetTimestamp) { | |
183 | this.timestampOptions.startTimestamp = 0 | |
184 | this.timestampOptions.stopTimestamp = this.video.duration | |
185 | } | |
186 | } | |
187 | ||
188 | formatTimestamp (playlist: PlaylistSummary) { | |
189 | const start = playlist.startTimestamp ? secondsToTime(playlist.startTimestamp) : '' | |
190 | const stop = playlist.stopTimestamp ? secondsToTime(playlist.stopTimestamp) : '' | |
191 | ||
192 | return `(${start}-${stop})` | |
193 | } | |
194 | ||
195 | onVideoPlaylistSearchChanged () { | |
196 | this.videoPlaylistSearchChanged.next() | |
197 | } | |
198 | ||
199 | private removeVideoFromPlaylist (playlist: PlaylistSummary) { | |
200 | if (!playlist.playlistElementId) return | |
201 | ||
202 | this.videoPlaylistService.removeVideoFromPlaylist(playlist.id, playlist.playlistElementId, this.video.id) | |
203 | .subscribe( | |
204 | () => { | |
205 | this.notifier.success($localize`Video removed from ${playlist.displayName}`) | |
206 | }, | |
207 | ||
208 | err => { | |
209 | this.notifier.error(err.message) | |
210 | }, | |
211 | ||
212 | () => this.cd.markForCheck() | |
213 | ) | |
214 | } | |
215 | ||
216 | private listenToPlaylistChanges () { | |
217 | this.unsubscribePlaylistChanges() | |
218 | ||
219 | this.listenToPlaylistChangeSub = this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id) | |
220 | .pipe(filter(() => this.disabled === false)) | |
221 | .subscribe(existResult => this.rebuildPlaylists(existResult)) | |
222 | } | |
223 | ||
224 | private unsubscribePlaylistChanges () { | |
225 | if (this.listenToPlaylistChangeSub) { | |
226 | this.listenToPlaylistChangeSub.unsubscribe() | |
227 | this.listenToPlaylistChangeSub = undefined | |
228 | } | |
229 | } | |
230 | ||
231 | private rebuildPlaylists (existResult: VideoExistInPlaylist[]) { | |
232 | logger('Got existing results for %d.', this.video.id, existResult) | |
233 | ||
234 | this.videoPlaylists = [] | |
235 | for (const playlist of this.playlistsData) { | |
236 | const existingPlaylist = existResult.find(p => p.playlistId === playlist.id) | |
237 | ||
238 | this.videoPlaylists.push({ | |
239 | id: playlist.id, | |
240 | displayName: playlist.displayName, | |
241 | inPlaylist: !!existingPlaylist, | |
242 | playlistElementId: existingPlaylist ? existingPlaylist.playlistElementId : undefined, | |
243 | startTimestamp: existingPlaylist ? existingPlaylist.startTimestamp : undefined, | |
244 | stopTimestamp: existingPlaylist ? existingPlaylist.stopTimestamp : undefined | |
245 | }) | |
246 | } | |
247 | ||
248 | logger('Rebuilt playlist state for video %d.', this.video.id, this.videoPlaylists) | |
249 | ||
250 | this.cd.markForCheck() | |
251 | } | |
252 | ||
253 | private addVideoInPlaylist (playlist: PlaylistSummary) { | |
254 | const body: VideoPlaylistElementCreate = { videoId: this.video.id } | |
255 | ||
256 | if (this.timestampOptions.startTimestampEnabled) body.startTimestamp = this.timestampOptions.startTimestamp | |
257 | if (this.timestampOptions.stopTimestampEnabled) body.stopTimestamp = this.timestampOptions.stopTimestamp | |
258 | ||
259 | this.videoPlaylistService.addVideoInPlaylist(playlist.id, body) | |
260 | .subscribe( | |
261 | () => { | |
262 | const message = body.startTimestamp || body.stopTimestamp | |
263 | ? $localize`Video added in ${playlist.displayName} at timestamps ${this.formatTimestamp(playlist)}` | |
264 | : $localize`Video added in ${playlist.displayName}` | |
265 | ||
266 | this.notifier.success(message) | |
267 | }, | |
268 | ||
269 | err => { | |
270 | this.notifier.error(err.message) | |
271 | }, | |
272 | ||
273 | () => this.cd.markForCheck() | |
274 | ) | |
275 | } | |
276 | } |