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