]>
Commit | Line | Data |
---|---|---|
51b34a11 | 1 | import { bufferTime, catchError, filter, map, share, switchMap, tap } from 'rxjs/operators' |
830b4faf | 2 | import { Injectable } from '@angular/core' |
51b34a11 | 3 | import { merge, Observable, of, ReplaySubject, Subject } from 'rxjs' |
830b4faf | 4 | import { RestExtractor } from '../rest/rest-extractor.service' |
f0a39880 C |
5 | import { HttpClient, HttpParams } from '@angular/common/http' |
6 | import { ResultList, VideoPlaylistElementCreate, VideoPlaylistElementUpdate } from '../../../../../shared' | |
830b4faf C |
7 | import { environment } from '../../../environments/environment' |
8 | import { VideoPlaylist as VideoPlaylistServerModel } from '@shared/models/videos/playlist/video-playlist.model' | |
9 | import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' | |
10 | import { VideoChannel } from '@app/shared/video-channel/video-channel.model' | |
11 | import { VideoPlaylistCreate } from '@shared/models/videos/playlist/video-playlist-create.model' | |
12 | import { VideoPlaylistUpdate } from '@shared/models/videos/playlist/video-playlist-update.model' | |
13 | import { objectToFormData } from '@app/shared/misc/utils' | |
51b34a11 | 14 | import { AuthUser, ServerService } from '@app/core' |
830b4faf C |
15 | import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model' |
16 | import { AccountService } from '@app/shared/account/account.service' | |
17 | import { Account } from '@app/shared/account/account.model' | |
f0a39880 | 18 | import { RestService } from '@app/shared/rest' |
51b34a11 | 19 | import { VideoExistInPlaylist, VideosExistInPlaylists } from '@shared/models/videos/playlist/video-exist-in-playlist.model' |
15e9d5ca | 20 | import { VideoPlaylistReorder } from '@shared/models/videos/playlist/video-playlist-reorder.model' |
440d39c5 | 21 | import { ComponentPaginationLight } from '@app/shared/rest/component-pagination.model' |
bfbd9128 C |
22 | import { VideoPlaylistElement as ServerVideoPlaylistElement } from '@shared/models/videos/playlist/video-playlist-element.model' |
23 | import { VideoPlaylistElement } from '@app/shared/video-playlist/video-playlist-element.model' | |
51b34a11 C |
24 | import { uniq } from 'lodash-es' |
25 | import * as debug from 'debug' | |
26 | ||
27 | const logger = debug('peertube:playlists:VideoPlaylistService') | |
28 | ||
93d54cc7 | 29 | export type CachedPlaylist = VideoPlaylist | { id: number, displayName: string } |
830b4faf C |
30 | |
31 | @Injectable() | |
32 | export class VideoPlaylistService { | |
33 | static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/' | |
f0a39880 C |
34 | static MY_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/users/me/video-playlists/' |
35 | ||
36 | // Use a replay subject because we "next" a value before subscribing | |
51b34a11 C |
37 | private videoExistsInPlaylistNotifier = new ReplaySubject<number>(1) |
38 | private videoExistsInPlaylistCacheSubject = new Subject<VideosExistInPlaylists>() | |
39 | private readonly videoExistsInPlaylistObservable: Observable<VideosExistInPlaylists> | |
40 | ||
41 | private videoExistsObservableCache: { [ id: number ]: Observable<VideoExistInPlaylist[]> } = {} | |
42 | private videoExistsCache: { [ id: number ]: VideoExistInPlaylist[] } = {} | |
43 | ||
44 | private myAccountPlaylistCache: ResultList<CachedPlaylist> = undefined | |
6b0c3c7c | 45 | private myAccountPlaylistCacheRunning = false |
51b34a11 | 46 | private myAccountPlaylistCacheSubject = new Subject<ResultList<CachedPlaylist>>() |
830b4faf C |
47 | |
48 | constructor ( | |
49 | private authHttp: HttpClient, | |
50 | private serverService: ServerService, | |
f0a39880 C |
51 | private restExtractor: RestExtractor, |
52 | private restService: RestService | |
53 | ) { | |
51b34a11 C |
54 | this.videoExistsInPlaylistObservable = merge( |
55 | this.videoExistsInPlaylistNotifier.pipe( | |
56 | bufferTime(500), | |
57 | filter(videoIds => videoIds.length !== 0), | |
58 | map(videoIds => uniq(videoIds)), | |
59 | switchMap(videoIds => this.doVideosExistInPlaylist(videoIds)), | |
60 | share() | |
61 | ), | |
62 | ||
63 | this.videoExistsInPlaylistCacheSubject | |
f0a39880 C |
64 | ) |
65 | } | |
830b4faf | 66 | |
440d39c5 | 67 | listChannelPlaylists (videoChannel: VideoChannel, componentPagination: ComponentPaginationLight): Observable<ResultList<VideoPlaylist>> { |
830b4faf | 68 | const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/video-playlists' |
ad453580 | 69 | const pagination = this.restService.componentPaginationToRestPagination(componentPagination) |
830b4faf | 70 | |
ad453580 C |
71 | let params = new HttpParams() |
72 | params = this.restService.addRestGetParams(params, pagination) | |
73 | ||
74 | return this.authHttp.get<ResultList<VideoPlaylist>>(url, { params }) | |
830b4faf C |
75 | .pipe( |
76 | switchMap(res => this.extractPlaylists(res)), | |
77 | catchError(err => this.restExtractor.handleError(err)) | |
78 | ) | |
79 | } | |
80 | ||
51b34a11 | 81 | listMyPlaylistWithCache (user: AuthUser, search?: string) { |
6b0c3c7c C |
82 | if (!search) { |
83 | if (this.myAccountPlaylistCacheRunning) return | |
84 | if (this.myAccountPlaylistCache) return of(this.myAccountPlaylistCache) | |
85 | } | |
86 | ||
87 | this.myAccountPlaylistCacheRunning = true | |
51b34a11 C |
88 | |
89 | return this.listAccountPlaylists(user.account, undefined, '-updatedAt', search) | |
90 | .pipe( | |
91 | tap(result => { | |
6b0c3c7c C |
92 | if (!search) { |
93 | this.myAccountPlaylistCacheRunning = false | |
94 | this.myAccountPlaylistCache = result | |
95 | } | |
51b34a11 C |
96 | }) |
97 | ) | |
98 | } | |
99 | ||
c06af501 RK |
100 | listAccountPlaylists ( |
101 | account: Account, | |
440d39c5 | 102 | componentPagination: ComponentPaginationLight, |
c06af501 RK |
103 | sort: string, |
104 | search?: string | |
105 | ): Observable<ResultList<VideoPlaylist>> { | |
830b4faf | 106 | const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-playlists' |
ad453580 C |
107 | const pagination = componentPagination |
108 | ? this.restService.componentPaginationToRestPagination(componentPagination) | |
109 | : undefined | |
830b4faf | 110 | |
f0a39880 | 111 | let params = new HttpParams() |
ad453580 | 112 | params = this.restService.addRestGetParams(params, pagination, sort) |
c06af501 | 113 | if (search) params = this.restService.addObjectParams(params, { search }) |
f0a39880 C |
114 | |
115 | return this.authHttp.get<ResultList<VideoPlaylist>>(url, { params }) | |
830b4faf C |
116 | .pipe( |
117 | switchMap(res => this.extractPlaylists(res)), | |
118 | catchError(err => this.restExtractor.handleError(err)) | |
119 | ) | |
120 | } | |
121 | ||
122 | getVideoPlaylist (id: string | number) { | |
123 | const url = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + id | |
124 | ||
125 | return this.authHttp.get<VideoPlaylist>(url) | |
126 | .pipe( | |
127 | switchMap(res => this.extractPlaylist(res)), | |
128 | catchError(err => this.restExtractor.handleError(err)) | |
129 | ) | |
130 | } | |
131 | ||
132 | createVideoPlaylist (body: VideoPlaylistCreate) { | |
133 | const data = objectToFormData(body) | |
134 | ||
f0a39880 | 135 | return this.authHttp.post<{ videoPlaylist: { id: number } }>(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL, data) |
830b4faf | 136 | .pipe( |
51b34a11 C |
137 | tap(res => { |
138 | this.myAccountPlaylistCache.total++ | |
139 | ||
140 | this.myAccountPlaylistCache.data.push({ | |
141 | id: res.videoPlaylist.id, | |
142 | displayName: body.displayName | |
143 | }) | |
144 | ||
145 | this.myAccountPlaylistCacheSubject.next(this.myAccountPlaylistCache) | |
146 | }), | |
830b4faf C |
147 | catchError(err => this.restExtractor.handleError(err)) |
148 | ) | |
149 | } | |
150 | ||
151 | updateVideoPlaylist (videoPlaylist: VideoPlaylist, body: VideoPlaylistUpdate) { | |
152 | const data = objectToFormData(body) | |
153 | ||
154 | return this.authHttp.put(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylist.id, data) | |
155 | .pipe( | |
156 | map(this.restExtractor.extractDataBool), | |
51b34a11 C |
157 | tap(() => { |
158 | const playlist = this.myAccountPlaylistCache.data.find(p => p.id === videoPlaylist.id) | |
159 | playlist.displayName = body.displayName | |
160 | ||
161 | this.myAccountPlaylistCacheSubject.next(this.myAccountPlaylistCache) | |
162 | }), | |
830b4faf C |
163 | catchError(err => this.restExtractor.handleError(err)) |
164 | ) | |
165 | } | |
166 | ||
167 | removeVideoPlaylist (videoPlaylist: VideoPlaylist) { | |
168 | return this.authHttp.delete(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylist.id) | |
169 | .pipe( | |
170 | map(this.restExtractor.extractDataBool), | |
51b34a11 C |
171 | tap(() => { |
172 | this.myAccountPlaylistCache.total-- | |
173 | this.myAccountPlaylistCache.data = this.myAccountPlaylistCache.data | |
174 | .filter(p => p.id !== videoPlaylist.id) | |
175 | ||
176 | this.myAccountPlaylistCacheSubject.next(this.myAccountPlaylistCache) | |
177 | }), | |
830b4faf C |
178 | catchError(err => this.restExtractor.handleError(err)) |
179 | ) | |
180 | } | |
181 | ||
f0a39880 | 182 | addVideoInPlaylist (playlistId: number, body: VideoPlaylistElementCreate) { |
a3671f07 C |
183 | const url = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos' |
184 | ||
185 | return this.authHttp.post<{ videoPlaylistElement: { id: number } }>(url, body) | |
51b34a11 C |
186 | .pipe( |
187 | tap(res => { | |
188 | const existsResult = this.videoExistsCache[body.videoId] | |
189 | existsResult.push({ | |
190 | playlistId, | |
191 | playlistElementId: res.videoPlaylistElement.id, | |
192 | startTimestamp: body.startTimestamp, | |
193 | stopTimestamp: body.stopTimestamp | |
194 | }) | |
195 | ||
196 | this.runPlaylistCheck(body.videoId) | |
197 | }), | |
198 | catchError(err => this.restExtractor.handleError(err)) | |
199 | ) | |
f0a39880 C |
200 | } |
201 | ||
51b34a11 | 202 | updateVideoOfPlaylist (playlistId: number, playlistElementId: number, body: VideoPlaylistElementUpdate, videoId: number) { |
bfbd9128 | 203 | return this.authHttp.put(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/' + playlistElementId, body) |
f0a39880 C |
204 | .pipe( |
205 | map(this.restExtractor.extractDataBool), | |
51b34a11 C |
206 | tap(() => { |
207 | const existsResult = this.videoExistsCache[videoId] | |
208 | const elem = existsResult.find(e => e.playlistElementId === playlistElementId) | |
209 | ||
210 | elem.startTimestamp = body.startTimestamp | |
211 | elem.stopTimestamp = body.stopTimestamp | |
212 | ||
213 | this.runPlaylistCheck(videoId) | |
214 | }), | |
f0a39880 C |
215 | catchError(err => this.restExtractor.handleError(err)) |
216 | ) | |
217 | } | |
218 | ||
51b34a11 | 219 | removeVideoFromPlaylist (playlistId: number, playlistElementId: number, videoId?: number) { |
bfbd9128 | 220 | return this.authHttp.delete(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/' + playlistElementId) |
f0a39880 C |
221 | .pipe( |
222 | map(this.restExtractor.extractDataBool), | |
51b34a11 C |
223 | tap(() => { |
224 | if (!videoId) return | |
225 | ||
226 | this.videoExistsCache[videoId] = this.videoExistsCache[videoId].filter(e => e.playlistElementId !== playlistElementId) | |
227 | this.runPlaylistCheck(videoId) | |
228 | }), | |
f0a39880 C |
229 | catchError(err => this.restExtractor.handleError(err)) |
230 | ) | |
231 | } | |
232 | ||
15e9d5ca C |
233 | reorderPlaylist (playlistId: number, oldPosition: number, newPosition: number) { |
234 | const body: VideoPlaylistReorder = { | |
235 | startPosition: oldPosition, | |
236 | insertAfterPosition: newPosition | |
237 | } | |
238 | ||
239 | return this.authHttp.post(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/reorder', body) | |
240 | .pipe( | |
241 | map(this.restExtractor.extractDataBool), | |
242 | catchError(err => this.restExtractor.handleError(err)) | |
243 | ) | |
244 | } | |
245 | ||
bfbd9128 C |
246 | getPlaylistVideos ( |
247 | videoPlaylistId: number | string, | |
440d39c5 | 248 | componentPagination: ComponentPaginationLight |
bfbd9128 C |
249 | ): Observable<ResultList<VideoPlaylistElement>> { |
250 | const path = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylistId + '/videos' | |
251 | const pagination = this.restService.componentPaginationToRestPagination(componentPagination) | |
252 | ||
253 | let params = new HttpParams() | |
254 | params = this.restService.addRestGetParams(params, pagination) | |
255 | ||
256 | return this.authHttp | |
257 | .get<ResultList<ServerVideoPlaylistElement>>(path, { params }) | |
258 | .pipe( | |
259 | switchMap(res => this.extractVideoPlaylistElements(res)), | |
260 | catchError(err => this.restExtractor.handleError(err)) | |
261 | ) | |
262 | } | |
263 | ||
51b34a11 C |
264 | listenToMyAccountPlaylistsChange () { |
265 | return this.myAccountPlaylistCacheSubject.asObservable() | |
266 | } | |
267 | ||
268 | listenToVideoPlaylistChange (videoId: number) { | |
269 | if (this.videoExistsObservableCache[ videoId ]) { | |
270 | return this.videoExistsObservableCache[ videoId ] | |
271 | } | |
272 | ||
273 | const obs = this.videoExistsInPlaylistObservable | |
274 | .pipe( | |
275 | map(existsResult => existsResult[ videoId ]), | |
276 | filter(r => !!r), | |
277 | tap(result => this.videoExistsCache[ videoId ] = result) | |
278 | ) | |
279 | ||
280 | this.videoExistsObservableCache[ videoId ] = obs | |
281 | return obs | |
282 | } | |
283 | ||
284 | runPlaylistCheck (videoId: number) { | |
285 | logger('Running playlist check.') | |
286 | ||
287 | if (this.videoExistsCache[videoId]) { | |
288 | logger('Found cache for %d.', videoId) | |
289 | ||
290 | return this.videoExistsInPlaylistCacheSubject.next({ [videoId]: this.videoExistsCache[videoId] }) | |
291 | } | |
f0a39880 | 292 | |
51b34a11 C |
293 | logger('Fetching from network for %d.', videoId) |
294 | return this.videoExistsInPlaylistNotifier.next(videoId) | |
f0a39880 C |
295 | } |
296 | ||
830b4faf | 297 | extractPlaylists (result: ResultList<VideoPlaylistServerModel>) { |
ba430d75 | 298 | return this.serverService.getServerLocale() |
830b4faf C |
299 | .pipe( |
300 | map(translations => { | |
301 | const playlistsJSON = result.data | |
302 | const total = result.total | |
303 | const playlists: VideoPlaylist[] = [] | |
304 | ||
305 | for (const playlistJSON of playlistsJSON) { | |
306 | playlists.push(new VideoPlaylist(playlistJSON, translations)) | |
307 | } | |
308 | ||
309 | return { data: playlists, total } | |
310 | }) | |
311 | ) | |
312 | } | |
313 | ||
314 | extractPlaylist (playlist: VideoPlaylistServerModel) { | |
ba430d75 | 315 | return this.serverService.getServerLocale() |
830b4faf C |
316 | .pipe(map(translations => new VideoPlaylist(playlist, translations))) |
317 | } | |
f0a39880 | 318 | |
bfbd9128 | 319 | extractVideoPlaylistElements (result: ResultList<ServerVideoPlaylistElement>) { |
ba430d75 | 320 | return this.serverService.getServerLocale() |
bfbd9128 C |
321 | .pipe( |
322 | map(translations => { | |
323 | const elementsJson = result.data | |
324 | const total = result.total | |
325 | const elements: VideoPlaylistElement[] = [] | |
326 | ||
327 | for (const elementJson of elementsJson) { | |
328 | elements.push(new VideoPlaylistElement(elementJson, translations)) | |
329 | } | |
330 | ||
331 | return { total, data: elements } | |
332 | }) | |
333 | ) | |
334 | } | |
335 | ||
51b34a11 | 336 | private doVideosExistInPlaylist (videoIds: number[]): Observable<VideosExistInPlaylists> { |
f0a39880 | 337 | const url = VideoPlaylistService.MY_VIDEO_PLAYLIST_URL + 'videos-exist' |
f0a39880 | 338 | |
c06af501 | 339 | let params = new HttpParams() |
f0a39880 C |
340 | params = this.restService.addObjectParams(params, { videoIds }) |
341 | ||
b7819090 | 342 | return this.authHttp.get<VideoExistInPlaylist>(url, { params, headers: { ignoreLoadingBar: '' } }) |
f0a39880 C |
343 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
344 | } | |
830b4faf | 345 | } |