]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-search/find-in-bulk.service.ts
Increase some timeouts
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-search / find-in-bulk.service.ts
CommitLineData
3da38d6e 1import * as debug from 'debug'
1378c0d3 2import { Observable, Subject } from 'rxjs'
ff4de383 3import { first, map } from 'rxjs/operators'
afb7d2d5 4import { Injectable } from '@angular/core'
3da38d6e
C
5import { buildBulkObservable } from '@app/helpers'
6import { ResultList } from '@shared/models/common'
7import { Video, VideoChannel } from '../shared-main'
8import { VideoPlaylist } from '../shared-video-playlist'
9import { SearchService } from './search.service'
10
11const logger = debug('peertube:search:FindInBulkService')
12
13type BulkObservables <P extends number | string, R> = {
14 notifier: Subject<P>
15 result: Observable<R>
16}
17
18@Injectable()
19export class FindInBulkService {
20
21 private getVideoInBulk: BulkObservables<string, ResultList<Video>>
22 private getChannelInBulk: BulkObservables<string, ResultList<VideoChannel>>
23 private getPlaylistInBulk: BulkObservables<string, ResultList<VideoPlaylist>>
24
25 constructor (
afb7d2d5 26 private searchService: SearchService
3da38d6e
C
27 ) {
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))
31 }
32
33 getVideo (uuid: string): Observable<Video> {
34 logger('Schedule video fetch for uuid %s.', uuid)
35
36 return this.getData({
37 observableObject: this.getVideoInBulk,
38 finder: v => v.uuid === uuid,
39 param: uuid
40 })
41 }
42
43 getChannel (handle: string): Observable<VideoChannel> {
44 logger('Schedule channel fetch for handle %s.', handle)
45
46 return this.getData({
47 observableObject: this.getChannelInBulk,
48 finder: c => c.nameWithHost === handle || c.nameWithHostForced === handle,
49 param: handle
50 })
51 }
52
53 getPlaylist (uuid: string): Observable<VideoPlaylist> {
54 logger('Schedule playlist fetch for uuid %s.', uuid)
55
56 return this.getData({
57 observableObject: this.getPlaylistInBulk,
58 finder: p => p.uuid === uuid,
59 param: uuid
60 })
61 }
62
63 private getData <P extends number | string, R> (options: {
64 observableObject: BulkObservables<P, ResultList<R>>
65 param: P
66 finder: (d: R) => boolean
67 }) {
68 const { observableObject, param, finder } = options
69
70 return new Observable<R>(obs => {
71 observableObject.result
72 .pipe(
ff4de383 73 first(),
3da38d6e
C
74 map(({ data }) => data),
75 map(data => data.find(finder))
76 )
77 .subscribe(result => {
ff4de383
C
78 if (!result) {
79 obs.error(new Error($localize`Element ${param} not found`))
80 } else {
81 obs.next(result)
82 obs.complete()
83 }
3da38d6e
C
84 })
85
86 observableObject.notifier.next(param)
87 })
88 }
89
90 private getVideosInBulk (uuids: string[]) {
91 logger('Fetching videos %s.', uuids.join(', '))
92
93 return this.searchService.searchVideos({ uuids })
94 }
95
96 private getChannelsInBulk (handles: string[]) {
97 logger('Fetching channels %s.', handles.join(', '))
98
99 return this.searchService.searchVideoChannels({ handles })
100 }
101
102 private getPlaylistsInBulk (uuids: string[]) {
103 logger('Fetching playlists %s.', uuids.join(', '))
104
105 return this.searchService.searchVideoPlaylists({ uuids })
106 }
107
108 private buildBulkObservableObject <T extends number | string, R> (bulkGet: (params: T[]) => Observable<R>) {
109 const notifier = new Subject<T>()
110
111 return {
112 notifier,
113
114 result: buildBulkObservable({
115 time: 500,
116 bulkGet,
3da38d6e
C
117 notifierObservable: notifier.asObservable()
118 })
119 }
120 }
121}