1 import * as debug from 'debug'
2 import { Observable, Subject } from 'rxjs'
3 import { filter, first, map } from 'rxjs/operators'
4 import { Injectable } from '@angular/core'
5 import { buildBulkObservable } from '@app/helpers'
6 import { ResultList } from '@shared/models/common'
7 import { Video, VideoChannel } from '../shared-main'
8 import { VideoPlaylist } from '../shared-video-playlist'
9 import { SearchService } from './search.service'
11 const logger = debug('peertube:search:FindInBulkService')
13 type BulkObservables <P extends number | string, R> = {
15 result: Observable<{ params: P[], response: R }>
19 export class FindInBulkService {
21 private getVideoInBulk: BulkObservables<string, ResultList<Video>>
22 private getChannelInBulk: BulkObservables<string, ResultList<VideoChannel>>
23 private getPlaylistInBulk: BulkObservables<string, ResultList<VideoPlaylist>>
26 private searchService: SearchService
28 this.getVideoInBulk = this.buildBulkObservableObject(this.getVideosInBulk.bind(this))
29 this.getChannelInBulk = this.buildBulkObservableObject(this.getChannelsInBulk.bind(this))
30 this.getPlaylistInBulk = this.buildBulkObservableObject(this.getPlaylistsInBulk.bind(this))
33 getVideo (uuid: string): Observable<Video> {
34 logger('Schedule video fetch for uuid %s.', uuid)
37 observableObject: this.getVideoInBulk,
38 finder: v => v.uuid === uuid,
43 getChannel (handle: string): Observable<VideoChannel> {
44 logger('Schedule channel fetch for handle %s.', handle)
47 observableObject: this.getChannelInBulk,
48 finder: c => c.nameWithHost === handle || c.nameWithHostForced === handle,
53 getPlaylist (uuid: string): Observable<VideoPlaylist> {
54 logger('Schedule playlist fetch for uuid %s.', uuid)
57 observableObject: this.getPlaylistInBulk,
58 finder: p => p.uuid === uuid,
63 private getData <P extends number | string, R> (options: {
64 observableObject: BulkObservables<P, ResultList<R>>
66 finder: (d: R) => boolean
68 const { observableObject, param, finder } = options
70 return new Observable<R>(obs => {
71 observableObject.result
73 filter(result => result.params.includes(param)),
75 map(result => result.response.data),
76 map(data => data.find(finder))
78 .subscribe(result => {
80 obs.error(new Error($localize`Element ${param} not found`))
87 observableObject.notifier.next(param)
91 private getVideosInBulk (uuids: string[]) {
92 logger('Fetching videos %s.', uuids.join(', '))
94 return this.searchService.searchVideos({ uuids, componentPagination: { itemsPerPage: uuids.length, currentPage: 1 } })
97 private getChannelsInBulk (handles: string[]) {
98 logger('Fetching channels %s.', handles.join(', '))
100 return this.searchService.searchVideoChannels({ handles, componentPagination: { itemsPerPage: handles.length, currentPage: 1 } })
103 private getPlaylistsInBulk (uuids: string[]) {
104 logger('Fetching playlists %s.', uuids.join(', '))
106 return this.searchService.searchVideoPlaylists({ uuids, componentPagination: { itemsPerPage: uuids.length, currentPage: 1 } })
109 private buildBulkObservableObject <P extends number | string, R> (bulkGet: (params: P[]) => Observable<R>) {
110 const notifier = new Subject<P>()
115 result: buildBulkObservable({
118 notifierObservable: notifier.asObservable()