aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-video-playlist/video-playlist.service.ts
blob: bc9fb0d7443af034732b7443a405664c6ba5bd86 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import * as debug from 'debug'
import { merge, Observable, of, ReplaySubject, Subject } from 'rxjs'
import { catchError, filter, map, share, switchMap, tap } from 'rxjs/operators'
import { HttpClient, HttpContext, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { AuthService, AuthUser, ComponentPaginationLight, RestExtractor, RestService, ServerService } from '@app/core'
import { buildBulkObservable, objectToFormData } from '@app/helpers'
import { Account, AccountService, VideoChannel, VideoChannelService } from '@app/shared/shared-main'
import { NGX_LOADING_BAR_IGNORED } from '@ngx-loading-bar/http-client'
import {
  CachedVideoExistInPlaylist,
  CachedVideosExistInPlaylists,
  ResultList,
  VideoExistInPlaylist,
  VideoPlaylist as VideoPlaylistServerModel,
  VideoPlaylistCreate,
  VideoPlaylistElement as ServerVideoPlaylistElement,
  VideoPlaylistElementCreate,
  VideoPlaylistElementUpdate,
  VideoPlaylistReorder,
  VideoPlaylistUpdate,
  VideosExistInPlaylists
} from '@shared/models'
import { environment } from '../../../environments/environment'
import { VideoPlaylistElement } from './video-playlist-element.model'
import { VideoPlaylist } from './video-playlist.model'

const debugLogger = debug('peertube:playlists:VideoPlaylistService')

export type CachedPlaylist = VideoPlaylist | { id: number, displayName: string }

@Injectable()
export class VideoPlaylistService {
  static BASE_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/video-playlists/'
  static MY_VIDEO_PLAYLIST_URL = environment.apiUrl + '/api/v1/users/me/video-playlists/'

  // Use a replay subject because we "next" a value before subscribing
  private videoExistsInPlaylistNotifier = new ReplaySubject<number>(1)
  private videoExistsInPlaylistCacheSubject = new Subject<CachedVideosExistInPlaylists>()
  private readonly videoExistsInPlaylistObservable: Observable<CachedVideosExistInPlaylists>

  private videoExistsObservableCache: { [ id: number ]: Observable<CachedVideoExistInPlaylist[]> } = {}
  private videoExistsCache: { [ id: number ]: CachedVideoExistInPlaylist[] } = {}

  private myAccountPlaylistCache: ResultList<CachedPlaylist> = undefined
  private myAccountPlaylistCacheRunning: Observable<ResultList<CachedPlaylist>>
  private myAccountPlaylistCacheSubject = new Subject<ResultList<CachedPlaylist>>()

  constructor (
    private authHttp: HttpClient,
    private auth: AuthService,
    private serverService: ServerService,
    private restExtractor: RestExtractor,
    private restService: RestService
  ) {
    this.videoExistsInPlaylistObservable = merge(
      buildBulkObservable({
        time: 500,
        bulkGet: (videoIds: number[]) => {
          // We added a delay to the request, so ensure the user is still logged in
          if (this.auth.isLoggedIn()) {
            return this.doVideosExistInPlaylist(videoIds)
          }

          return of({})
        },
        notifierObservable: this.videoExistsInPlaylistNotifier
      }).pipe(map(({ response }) => response)),

      this.videoExistsInPlaylistCacheSubject
    )
  }

  listChannelPlaylists (videoChannel: VideoChannel, componentPagination: ComponentPaginationLight): Observable<ResultList<VideoPlaylist>> {
    const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/video-playlists'
    const pagination = this.restService.componentToRestPagination(componentPagination)

    let params = new HttpParams()
    params = this.restService.addRestGetParams(params, pagination)

    return this.authHttp.get<ResultList<VideoPlaylist>>(url, { params })
               .pipe(
                 switchMap(res => this.extractPlaylists(res)),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  listMyPlaylistWithCache (user: AuthUser, search?: string) {
    if (!search) {
      if (this.myAccountPlaylistCacheRunning) return this.myAccountPlaylistCacheRunning
      if (this.myAccountPlaylistCache) return of(this.myAccountPlaylistCache)
    }

    const obs = this.listAccountPlaylists(user.account, undefined, '-updatedAt', search)
               .pipe(
                 tap(result => {
                   if (!search) {
                     this.myAccountPlaylistCacheRunning = undefined
                     this.myAccountPlaylistCache = result
                   }
                 }),
                 share()
               )

    if (!search) this.myAccountPlaylistCacheRunning = obs
    return obs
  }

  listAccountPlaylists (
    account: Account,
    componentPagination: ComponentPaginationLight,
    sort: string,
    search?: string
  ): Observable<ResultList<VideoPlaylist>> {
    const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-playlists'
    const pagination = componentPagination
      ? this.restService.componentToRestPagination(componentPagination)
      : undefined

    let params = new HttpParams()
    params = this.restService.addRestGetParams(params, pagination, sort)
    if (search) params = this.restService.addObjectParams(params, { search })

    return this.authHttp.get<ResultList<VideoPlaylist>>(url, { params })
               .pipe(
                 switchMap(res => this.extractPlaylists(res)),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  getVideoPlaylist (id: string | number) {
    const url = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + id

    return this.authHttp.get<VideoPlaylist>(url)
               .pipe(
                 switchMap(res => this.extractPlaylist(res)),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  createVideoPlaylist (body: VideoPlaylistCreate) {
    const data = objectToFormData(body)

    return this.authHttp.post<{ videoPlaylist: { id: number } }>(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL, data)
               .pipe(
                 tap(res => {
                   if (!this.myAccountPlaylistCache) return

                   this.myAccountPlaylistCache.total++

                   this.myAccountPlaylistCache.data.push({
                     id: res.videoPlaylist.id,
                     displayName: body.displayName
                   })

                   this.myAccountPlaylistCacheSubject.next(this.myAccountPlaylistCache)
                 }),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  updateVideoPlaylist (videoPlaylist: VideoPlaylist, body: VideoPlaylistUpdate) {
    const data = objectToFormData(body)

    return this.authHttp.put(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylist.id, data)
               .pipe(
                 tap(() => {
                   if (!this.myAccountPlaylistCache) return

                   const playlist = this.myAccountPlaylistCache.data.find(p => p.id === videoPlaylist.id)
                   playlist.displayName = body.displayName

                   this.myAccountPlaylistCacheSubject.next(this.myAccountPlaylistCache)
                 }),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  removeVideoPlaylist (videoPlaylist: VideoPlaylist) {
    return this.authHttp.delete(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + videoPlaylist.id)
               .pipe(
                 tap(() => {
                   if (!this.myAccountPlaylistCache) return

                   this.myAccountPlaylistCache.total--
                   this.myAccountPlaylistCache.data = this.myAccountPlaylistCache.data
                                                          .filter(p => p.id !== videoPlaylist.id)

                   this.myAccountPlaylistCacheSubject.next(this.myAccountPlaylistCache)
                 }),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  addVideoInPlaylist (playlistId: number, body: VideoPlaylistElementCreate) {
    const url = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos'

    return this.authHttp.post<{ videoPlaylistElement: { id: number } }>(url, body)
               .pipe(
                 tap(res => {
                   const existsResult = this.videoExistsCache[body.videoId]
                   existsResult.push({
                     playlistId,
                     playlistElementId: res.videoPlaylistElement.id,
                     startTimestamp: body.startTimestamp,
                     stopTimestamp: body.stopTimestamp
                   })

                   this.runVideoExistsInPlaylistCheck(body.videoId)

                   if (this.myAccountPlaylistCache) {
                     const playlist = this.myAccountPlaylistCache.data.find(p => p.id === playlistId)
                     if (!playlist) return

                     const otherPlaylists = this.myAccountPlaylistCache.data.filter(p => p !== playlist)
                     this.myAccountPlaylistCache.data = [ playlist, ...otherPlaylists ]
                   }
                 }),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  updateVideoOfPlaylist (playlistId: number, playlistElementId: number, body: VideoPlaylistElementUpdate, videoId: number) {
    return this.authHttp.put(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/' + playlistElementId, body)
               .pipe(
                 tap(() => {
                   const existsResult = this.videoExistsCache[videoId]

                   if (existsResult) {
                     const elem = existsResult.find(e => e.playlistElementId === playlistElementId)

                     elem.startTimestamp = body.startTimestamp
                     elem.stopTimestamp = body.stopTimestamp
                   }

                   this.runVideoExistsInPlaylistCheck(videoId)
                 }),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  removeVideoFromPlaylist (playlistId: number, playlistElementId: number, videoId?: number) {
    return this.authHttp.delete(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/' + playlistElementId)
               .pipe(
                 tap(() => {
                   if (!videoId) return

                   if (this.videoExistsCache[videoId]) {
                     this.videoExistsCache[videoId] = this.videoExistsCache[videoId]
                       .filter(e => e.playlistElementId !== playlistElementId)
                   }

                   this.runVideoExistsInPlaylistCheck(videoId)
                 }),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  reorderPlaylist (playlistId: number, oldPosition: number, newPosition: number) {
    const body: VideoPlaylistReorder = {
      startPosition: oldPosition,
      insertAfterPosition: newPosition
    }

    return this.authHttp.post(VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + playlistId + '/videos/reorder', body)
               .pipe(catchError(err => this.restExtractor.handleError(err)))
  }

  getPlaylistVideos (options: {
    videoPlaylistId: number | string
    componentPagination: ComponentPaginationLight
  }): Observable<ResultList<VideoPlaylistElement>> {
    const path = VideoPlaylistService.BASE_VIDEO_PLAYLIST_URL + options.videoPlaylistId + '/videos'
    const pagination = this.restService.componentToRestPagination(options.componentPagination)

    let params = new HttpParams()
    params = this.restService.addRestGetParams(params, pagination)

    return this.authHttp
               .get<ResultList<ServerVideoPlaylistElement>>(path, { params })
               .pipe(
                 switchMap(res => this.extractVideoPlaylistElements(res)),
                 catchError(err => this.restExtractor.handleError(err))
               )
  }

  listenToMyAccountPlaylistsChange () {
    return this.myAccountPlaylistCacheSubject.asObservable()
  }

  listenToVideoPlaylistChange (videoId: number) {
    if (this.videoExistsObservableCache[videoId]) {
      return this.videoExistsObservableCache[videoId]
    }

    const obs = this.videoExistsInPlaylistObservable
                    .pipe(
                      map(existsResult => existsResult[videoId]),
                      filter(r => !!r),
                      tap(result => this.videoExistsCache[videoId] = result)
                    )

    this.videoExistsObservableCache[videoId] = obs
    return obs
  }

  runVideoExistsInPlaylistCheck (videoId: number) {
    debugLogger('Running playlist check.')

    if (this.videoExistsCache[videoId]) {
      debugLogger('Found cache for %d.', videoId)

      return this.videoExistsInPlaylistCacheSubject.next({ [videoId]: this.videoExistsCache[videoId] })
    }

    debugLogger('Fetching from network for %d.', videoId)
    return this.videoExistsInPlaylistNotifier.next(videoId)
  }

  extractPlaylists (result: ResultList<VideoPlaylistServerModel>) {
    return this.serverService.getServerLocale()
               .pipe(
                 map(translations => {
                   const playlistsJSON = result.data
                   const total = result.total
                   const playlists: VideoPlaylist[] = []

                   for (const playlistJSON of playlistsJSON) {
                     playlists.push(new VideoPlaylist(playlistJSON, translations))
                   }

                   return { data: playlists, total }
                 })
               )
  }

  extractPlaylist (playlist: VideoPlaylistServerModel) {
    return this.serverService.getServerLocale()
               .pipe(map(translations => new VideoPlaylist(playlist, translations)))
  }

  extractVideoPlaylistElements (result: ResultList<ServerVideoPlaylistElement>) {
    return this.serverService.getServerLocale()
               .pipe(
                 map(translations => {
                   const elementsJson = result.data
                   const total = result.total
                   const elements: VideoPlaylistElement[] = []

                   for (const elementJson of elementsJson) {
                     elements.push(new VideoPlaylistElement(elementJson, translations))
                   }

                   return { total, data: elements }
                 })
               )
  }

  doVideosExistInPlaylist (videoIds: number[]): Observable<VideosExistInPlaylists> {
    const url = VideoPlaylistService.MY_VIDEO_PLAYLIST_URL + 'videos-exist'

    let params = new HttpParams()
    params = this.restService.addObjectParams(params, { videoIds })

    return this.authHttp.get<VideoExistInPlaylist>(url, { params, context: new HttpContext().set(NGX_LOADING_BAR_IGNORED, true) })
               .pipe(catchError(err => this.restExtractor.handleError(err)))
  }
}