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